| 1 |
package oauth2 //nolint:testpackage // The CEL environment and activation are internal implementation details. |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"cel.dev/cel-go/cel" |
| 7 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken" |
| 8 |
oauth2types "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" |
| 9 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" |
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
func TestCELContextSchema(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
|
| 17 |
for _, tc := range []struct { |
| 18 |
name string |
| 19 |
expression string |
| 20 |
expectedType *cel.Type |
| 21 |
err string |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "authentication field", |
| 25 |
expression: "auth.mode", |
| 26 |
expectedType: cel.StringType, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "OpenVPN field", |
| 30 |
expression: "openvpn.commonName", |
| 31 |
expectedType: cel.StringType, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "OpenVPN IP field", |
| 35 |
expression: "openvpn.ip", |
| 36 |
expectedType: cel.StringType, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "OpenVPN session state field", |
| 40 |
expression: "openvpn.sessionState", |
| 41 |
expectedType: cel.StringType, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "token claims field", |
| 45 |
expression: "token.claims", |
| 46 |
expectedType: cel.MapType(cel.StringType, cel.DynType), |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "token field", |
| 50 |
expression: "token.ip", |
| 51 |
expectedType: cel.StringType, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "dynamic token claim", |
| 55 |
expression: "token.claims.department", |
| 56 |
expectedType: cel.DynType, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "normalized user email field", |
| 60 |
expression: "user.email", |
| 61 |
expectedType: cel.StringType, |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "normalized user groups field", |
| 65 |
expression: "user.groups", |
| 66 |
expectedType: cel.ListType(cel.StringType), |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "normalized user roles field", |
| 70 |
expression: "user.roles", |
| 71 |
expectedType: cel.ListType(cel.StringType), |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "normalized user subject field", |
| 75 |
expression: "user.subject", |
| 76 |
expectedType: cel.StringType, |
| 77 |
}, |
| 78 |
{ |
| 79 |
name: "normalized user username field", |
| 80 |
expression: "user.username", |
| 81 |
expectedType: cel.StringType, |
| 82 |
}, |
| 83 |
{ |
| 84 |
name: "unknown authentication field", |
| 85 |
expression: "auth.unknown", |
| 86 |
err: "undefined field 'unknown'", |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "unknown OpenVPN field", |
| 90 |
expression: "openvpn.unknown", |
| 91 |
err: "undefined field 'unknown'", |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "unknown token field", |
| 95 |
expression: "token.unknown", |
| 96 |
err: "undefined field 'unknown'", |
| 97 |
}, |
| 98 |
{ |
| 99 |
name: "unknown user field", |
| 100 |
expression: "user.unknown", |
| 101 |
err: "undefined field 'unknown'", |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "incompatible normalized user operation", |
| 105 |
expression: `user.groups == "vpn-users"`, |
| 106 |
err: "found no matching overload for '_==_'", |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: "map-style user access", |
| 110 |
expression: `user["username"]`, |
| 111 |
err: "found no matching overload for '_[_]'", |
| 112 |
}, |
| 113 |
} { |
| 114 |
t.Run(tc.name, func(t *testing.T) { |
| 115 |
t.Parallel() |
| 116 |
|
| 117 |
env, err := newCELEnvironment() |
| 118 |
require.NoError(t, err) |
| 119 |
|
| 120 |
ast, issues := env.Compile(tc.expression) |
| 121 |
if tc.err != "" { |
| 122 |
require.ErrorContains(t, issues.Err(), tc.err) |
| 123 |
|
| 124 |
return |
| 125 |
} |
| 126 |
|
| 127 |
require.NoError(t, issues.Err()) |
| 128 |
assert.True(t, ast.OutputType().IsExactType(tc.expectedType), "expected %s, got %s", tc.expectedType, ast.OutputType()) |
| 129 |
}) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestCELContextEvaluation(t *testing.T) { |
| 134 |
t.Parallel() |
| 135 |
|
| 136 |
program, err := compileCELProgram(` |
| 137 |
has(auth.mode) && |
| 138 |
has(openvpn.commonName) && |
| 139 |
has(openvpn.ip) && |
| 140 |
has(openvpn.sessionState) && |
| 141 |
has(token.claims) && |
| 142 |
has(token.ip) && |
| 143 |
has(user.email) && |
| 144 |
has(user.groups) && |
| 145 |
has(user.roles) && |
| 146 |
has(user.subject) && |
| 147 |
has(user.username) && |
| 148 |
!has(token.claims.missing) && |
| 149 |
auth == auth && |
| 150 |
auth == openvpn_auth_oauth2.AuthContext{mode: "interactive"} && |
| 151 |
openvpn == openvpn && |
| 152 |
token == token && |
| 153 |
user == user && |
| 154 |
type(auth) == openvpn_auth_oauth2.AuthContext && |
| 155 |
type(openvpn) == openvpn_auth_oauth2.OpenVPNContext && |
| 156 |
type(token) == openvpn_auth_oauth2.TokenContext && |
| 157 |
type(user) == openvpn_auth_oauth2.UserContext && |
| 158 |
[auth][0].mode == "interactive" && |
| 159 |
[openvpn][0].commonName == "alice" && |
| 160 |
[token][0].claims.department == "engineering" && |
| 161 |
[user].map(context, context.username) == ["alice"] && |
| 162 |
dyn(user).username == "alice" && |
| 163 |
auth.mode == "interactive" && |
| 164 |
openvpn.commonName == user.username && |
| 165 |
openvpn.ip == "192.0.2.10" && |
| 166 |
openvpn.sessionState == "authenticated" && |
| 167 |
token.claims.department == "engineering" && |
| 168 |
token.ip == "192.0.2.20" && |
| 169 |
user.email == "[email protected]" && |
| 170 |
user.groups == ["vpn-users"] && |
| 171 |
user.roles == ["vpn-admin"] && |
| 172 |
user.subject == "user-123" |
| 173 |
`, cel.BoolType) |
| 174 |
require.NoError(t, err) |
| 175 |
|
| 176 |
out, _, err := program.Eval(newCELActivation( |
| 177 |
CELAuthModeInteractive, |
| 178 |
state.State{ |
| 179 |
Client: state.ClientIdentifier{CommonName: "alice"}, |
| 180 |
IPAddr: "192.0.2.10", |
| 181 |
SessionState: "authenticated", |
| 182 |
}, |
| 183 |
&idtoken.IDToken{IDTokenClaims: &idtoken.Claims{ |
| 184 |
Claims: map[string]any{"department": "engineering"}, |
| 185 |
IPAddr: "192.0.2.20", |
| 186 |
}}, |
| 187 |
oauth2types.UserInfo{ |
| 188 |
Email: "[email protected]", |
| 189 |
Groups: []string{"vpn-users"}, |
| 190 |
Roles: []string{"vpn-admin"}, |
| 191 |
Subject: "user-123", |
| 192 |
Username: "alice", |
| 193 |
}, |
| 194 |
)) |
| 195 |
require.NoError(t, err) |
| 196 |
assert.Equal(t, true, out.Value()) |
| 197 |
} |
| 198 |
|
| 199 |
func TestCELContextConstruction(t *testing.T) { |
| 200 |
t.Parallel() |
| 201 |
|
| 202 |
program, err := compileCELProgram(` |
| 203 |
openvpn_auth_oauth2.AuthContext{mode: "interactive"}.mode == "interactive" && |
| 204 |
openvpn_auth_oauth2.OpenVPNContext{ |
| 205 |
commonName: "alice", |
| 206 |
ip: "192.0.2.10", |
| 207 |
sessionState: "authenticated" |
| 208 |
}.commonName == "alice" && |
| 209 |
openvpn_auth_oauth2.UserContext{ |
| 210 |
email: "[email protected]", |
| 211 |
groups: ["vpn-users"], |
| 212 |
roles: ["vpn-admin"], |
| 213 |
subject: "user-123", |
| 214 |
username: "alice" |
| 215 |
}.username == "alice" |
| 216 |
`, cel.BoolType) |
| 217 |
require.NoError(t, err) |
| 218 |
|
| 219 |
out, _, err := program.Eval(map[string]any{}) |
| 220 |
require.NoError(t, err) |
| 221 |
assert.Equal(t, true, out.Value()) |
| 222 |
} |
| 223 |
|
| 224 |
func TestCELContextEmptyValues(t *testing.T) { |
| 225 |
t.Parallel() |
| 226 |
|
| 227 |
program, err := compileCELProgram(` |
| 228 |
has(auth.mode) && |
| 229 |
has(openvpn.commonName) && |
| 230 |
has(openvpn.ip) && |
| 231 |
has(openvpn.sessionState) && |
| 232 |
has(token.claims) && |
| 233 |
has(token.ip) && |
| 234 |
has(user.email) && |
| 235 |
has(user.groups) && |
| 236 |
has(user.roles) && |
| 237 |
has(user.subject) && |
| 238 |
has(user.username) && |
| 239 |
size(token.claims) == 0 && |
| 240 |
auth.mode == "" && |
| 241 |
openvpn.commonName == "" && |
| 242 |
openvpn.ip == "" && |
| 243 |
openvpn.sessionState == "" && |
| 244 |
token.ip == "" && |
| 245 |
user.email == "" && |
| 246 |
user.groups == [] && |
| 247 |
user.roles == [] && |
| 248 |
user.subject == "" && |
| 249 |
user.username == "" |
| 250 |
`, cel.BoolType) |
| 251 |
require.NoError(t, err) |
| 252 |
|
| 253 |
out, _, err := program.Eval(newCELActivation( |
| 254 |
CELAuthMode(""), |
| 255 |
state.State{}, |
| 256 |
nil, |
| 257 |
oauth2types.UserInfo{}, |
| 258 |
)) |
| 259 |
require.NoError(t, err) |
| 260 |
assert.Equal(t, true, out.Value()) |
| 261 |
} |