| 1 |
//nolint:testpackage |
| 2 |
package openvpn |
| 3 |
|
| 4 |
import ( |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"io/fs" |
| 8 |
"log/slog" |
| 9 |
"testing" |
| 10 |
"testing/fstest" |
| 11 |
|
| 12 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 13 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types" |
| 14 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
) |
| 17 |
|
| 18 |
func TestLoadClientConfigMerge(t *testing.T) { |
| 19 |
t.Parallel() |
| 20 |
|
| 21 |
conf := config.Defaults |
| 22 |
conf.OpenVPN.OverrideUsername = true |
| 23 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 24 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyMerge |
| 25 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 26 |
FS: fstest.MapFS{ |
| 27 |
"base.conf": { |
| 28 |
Data: []byte("push \"route 10.0.0.0 255.0.0.0\"\npush \"route 10.1.0.0 255.255.0.0\"\n"), |
| 29 |
}, |
| 30 |
"admin.conf": { |
| 31 |
Data: []byte("push \"route 10.1.0.0 255.255.0.0\"\npush \"route 10.2.0.0 255.255.0.0\"\n"), |
| 32 |
}, |
| 33 |
"alice.conf": { |
| 34 |
Data: []byte("push \"route 10.3.0.0 255.255.0.0\"\n"), |
| 35 |
}, |
| 36 |
}, |
| 37 |
} |
| 38 |
|
| 39 |
client := Client{conf: &conf} |
| 40 |
clientConfig, err := client.loadClientConfig( |
| 41 |
context.Background(), |
| 42 |
slog.New(slog.DiscardHandler), |
| 43 |
state.ClientIdentifier{}, |
| 44 |
[]string{"base", "admin", "base", "missing", "alice"}, |
| 45 |
"alice", |
| 46 |
) |
| 47 |
|
| 48 |
require.NoError(t, err) |
| 49 |
require.Equal(t, []string{ |
| 50 |
`push "route 10.0.0.0 255.0.0.0"`, |
| 51 |
`push "route 10.1.0.0 255.255.0.0"`, |
| 52 |
`push "route 10.2.0.0 255.255.0.0"`, |
| 53 |
`push "route 10.3.0.0 255.255.0.0"`, |
| 54 |
`override-username "alice"`, |
| 55 |
}, clientConfig) |
| 56 |
} |
| 57 |
|
| 58 |
func TestLoadClientConfigDisabledDoesNotReadDefault(t *testing.T) { |
| 59 |
t.Parallel() |
| 60 |
|
| 61 |
conf := config.Defaults |
| 62 |
conf.OAuth2.Refresh.ValidateUser = false |
| 63 |
conf.OpenVPN.ClientConfig.Enabled = false |
| 64 |
conf.OpenVPN.ClientConfig.Path = types.FS{FS: failOpenFS{}} |
| 65 |
|
| 66 |
client := Client{conf: &conf} |
| 67 |
clientConfig, err := client.loadClientConfig( |
| 68 |
context.Background(), |
| 69 |
slog.New(slog.DiscardHandler), |
| 70 |
state.ClientIdentifier{}, |
| 71 |
nil, |
| 72 |
"alice", |
| 73 |
) |
| 74 |
|
| 75 |
require.NoError(t, err) |
| 76 |
require.Empty(t, clientConfig) |
| 77 |
} |
| 78 |
|
| 79 |
func TestLoadClientConfigEmptyListFallsBackToDefault(t *testing.T) { |
| 80 |
t.Parallel() |
| 81 |
|
| 82 |
conf := config.Defaults |
| 83 |
conf.OAuth2.Refresh.ValidateUser = false |
| 84 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 85 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyMerge |
| 86 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 87 |
FS: fstest.MapFS{ |
| 88 |
"DEFAULT.conf": { |
| 89 |
Data: []byte("push \"route 10.0.0.0 255.0.0.0\"\n"), |
| 90 |
}, |
| 91 |
}, |
| 92 |
} |
| 93 |
|
| 94 |
client := Client{conf: &conf} |
| 95 |
clientConfig, err := client.loadClientConfig( |
| 96 |
context.Background(), |
| 97 |
slog.New(slog.DiscardHandler), |
| 98 |
state.ClientIdentifier{}, |
| 99 |
nil, |
| 100 |
"alice", |
| 101 |
) |
| 102 |
|
| 103 |
require.NoError(t, err) |
| 104 |
require.Equal(t, []string{`push "route 10.0.0.0 255.0.0.0"`}, clientConfig) |
| 105 |
} |
| 106 |
|
| 107 |
func TestLoadClientConfigMissingDoesNotFallBackToDefault(t *testing.T) { |
| 108 |
t.Parallel() |
| 109 |
|
| 110 |
conf := config.Defaults |
| 111 |
conf.OAuth2.Refresh.ValidateUser = false |
| 112 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 113 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyMerge |
| 114 |
conf.OpenVPN.ClientConfig.Path = types.FS{ |
| 115 |
FS: fstest.MapFS{ |
| 116 |
"DEFAULT.conf": { |
| 117 |
Data: []byte("push \"route 10.0.0.0 255.0.0.0\"\n"), |
| 118 |
}, |
| 119 |
}, |
| 120 |
} |
| 121 |
|
| 122 |
client := Client{conf: &conf} |
| 123 |
clientConfig, err := client.loadClientConfig( |
| 124 |
context.Background(), |
| 125 |
slog.New(slog.DiscardHandler), |
| 126 |
state.ClientIdentifier{}, |
| 127 |
[]string{"missing"}, |
| 128 |
"alice", |
| 129 |
) |
| 130 |
|
| 131 |
require.NoError(t, err) |
| 132 |
require.Empty(t, clientConfig) |
| 133 |
} |
| 134 |
|
| 135 |
func TestLoadClientConfigMissingStrict(t *testing.T) { |
| 136 |
t.Parallel() |
| 137 |
|
| 138 |
conf := config.Defaults |
| 139 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 140 |
conf.OpenVPN.ClientConfig.IgnoreNotFound = false |
| 141 |
conf.OpenVPN.ClientConfig.Strategy = config.OpenVPNConfigStrategyMerge |
| 142 |
conf.OpenVPN.ClientConfig.Path = types.FS{FS: fstest.MapFS{}} |
| 143 |
|
| 144 |
client := Client{conf: &conf} |
| 145 |
clientConfig, err := client.loadClientConfig( |
| 146 |
context.Background(), |
| 147 |
slog.New(slog.DiscardHandler), |
| 148 |
state.ClientIdentifier{}, |
| 149 |
[]string{"missing"}, |
| 150 |
"alice", |
| 151 |
) |
| 152 |
|
| 153 |
require.Error(t, err) |
| 154 |
require.Nil(t, clientConfig) |
| 155 |
} |
| 156 |
|
| 157 |
func TestLoadClientConfigFailsClosedOnIOError(t *testing.T) { |
| 158 |
t.Parallel() |
| 159 |
|
| 160 |
errOpen := errors.New("open failure") |
| 161 |
errRead := errors.New("read failure") |
| 162 |
|
| 163 |
strategies := []struct { |
| 164 |
name string |
| 165 |
strategy config.OpenVPNConfigStrategy |
| 166 |
}{ |
| 167 |
{ |
| 168 |
name: "merge", |
| 169 |
strategy: config.OpenVPNConfigStrategyMerge, |
| 170 |
}, |
| 171 |
{ |
| 172 |
name: "user selector", |
| 173 |
strategy: config.OpenVPNConfigStrategyUserSelector, |
| 174 |
}, |
| 175 |
} |
| 176 |
|
| 177 |
failures := []struct { |
| 178 |
name string |
| 179 |
fs fs.FS |
| 180 |
wantErr error |
| 181 |
}{ |
| 182 |
{ |
| 183 |
name: "permission denied", |
| 184 |
fs: failOpenFS{err: &fs.PathError{Op: "open", Path: "alice.conf", Err: fs.ErrPermission}}, |
| 185 |
wantErr: fs.ErrPermission, |
| 186 |
}, |
| 187 |
{ |
| 188 |
name: "open failure", |
| 189 |
fs: failOpenFS{err: errOpen}, |
| 190 |
wantErr: errOpen, |
| 191 |
}, |
| 192 |
{ |
| 193 |
name: "read failure", |
| 194 |
fs: readErrorFS{ |
| 195 |
FS: fstest.MapFS{ |
| 196 |
"alice.conf": {Data: []byte("client config")}, |
| 197 |
}, |
| 198 |
err: errRead, |
| 199 |
}, |
| 200 |
wantErr: errRead, |
| 201 |
}, |
| 202 |
} |
| 203 |
|
| 204 |
for _, strategy := range strategies { |
| 205 |
t.Run(strategy.name, func(t *testing.T) { |
| 206 |
t.Parallel() |
| 207 |
|
| 208 |
for _, failure := range failures { |
| 209 |
t.Run(failure.name, func(t *testing.T) { |
| 210 |
t.Parallel() |
| 211 |
|
| 212 |
conf := config.Defaults |
| 213 |
conf.OpenVPN.ClientConfig.Enabled = true |
| 214 |
conf.OpenVPN.ClientConfig.Strategy = strategy.strategy |
| 215 |
conf.OpenVPN.ClientConfig.Path = types.FS{FS: failure.fs} |
| 216 |
|
| 217 |
client := Client{conf: &conf} |
| 218 |
clientConfig, err := client.loadClientConfig( |
| 219 |
t.Context(), |
| 220 |
slog.New(slog.DiscardHandler), |
| 221 |
state.ClientIdentifier{}, |
| 222 |
[]string{"alice"}, |
| 223 |
"alice", |
| 224 |
) |
| 225 |
|
| 226 |
require.ErrorIs(t, err, failure.wantErr) |
| 227 |
require.Nil(t, clientConfig) |
| 228 |
}) |
| 229 |
} |
| 230 |
}) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
type failOpenFS struct { |
| 235 |
err error |
| 236 |
} |
| 237 |
|
| 238 |
func (f failOpenFS) Open(string) (fs.File, error) { |
| 239 |
if f.err != nil { |
| 240 |
return nil, f.err |
| 241 |
} |
| 242 |
|
| 243 |
return nil, errors.New("unexpected client config read") |
| 244 |
} |
| 245 |
|
| 246 |
type readErrorFS struct { |
| 247 |
fs.FS |
| 248 |
|
| 249 |
err error |
| 250 |
} |
| 251 |
|
| 252 |
func (f readErrorFS) Open(name string) (fs.File, error) { |
| 253 |
file, err := f.FS.Open(name) |
| 254 |
if err != nil { |
| 255 |
return nil, err |
| 256 |
} |
| 257 |
|
| 258 |
return readErrorFile{File: file, err: f.err}, nil |
| 259 |
} |
| 260 |
|
| 261 |
type readErrorFile struct { |
| 262 |
fs.File |
| 263 |
|
| 264 |
err error |
| 265 |
} |
| 266 |
|
| 267 |
func (f readErrorFile) Read([]byte) (int, error) { |
| 268 |
return 0, f.err |
| 269 |
} |