| 1 |
package oauth2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
|
| 8 |
"cel.dev/cel-go/cel" |
| 9 |
celtypes "cel.dev/cel-go/common/types" |
| 10 |
"cel.dev/cel-go/ext" |
| 11 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken" |
| 12 |
celcontext "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/openvpn_auth_oauth2" |
| 13 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" |
| 14 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" |
| 15 |
) |
| 16 |
|
| 17 |
type CELAuthMode = types.AuthMode |
| 18 |
|
| 19 |
const ( |
| 20 |
CELAuthModeInteractive = types.AuthModeInteractive |
| 21 |
CELAuthModeNonInteractive = types.AuthModeNonInteractive |
| 22 |
|
| 23 |
celAuthContextTypeName = "openvpn_auth_oauth2.AuthContext" |
| 24 |
celOpenVPNContextTypeName = "openvpn_auth_oauth2.OpenVPNContext" |
| 25 |
celTokenContextTypeName = "openvpn_auth_oauth2.TokenContext" |
| 26 |
celUserContextTypeName = "openvpn_auth_oauth2.UserContext" |
| 27 |
) |
| 28 |
|
| 29 |
// celActivation keeps the fixed CEL variables inline and resolves pointers to them, |
| 30 |
// avoiding the map allocation and interface boxing required by map-based activations. |
| 31 |
type celActivation struct { |
| 32 |
token celcontext.TokenContext |
| 33 |
openvpn celcontext.OpenVPNContext |
| 34 |
auth celcontext.AuthContext |
| 35 |
claims map[string]any |
| 36 |
userSubject string |
| 37 |
openVPNSession string |
| 38 |
userUsername string |
| 39 |
userEmail string |
| 40 |
tokenIP string |
| 41 |
authMode string |
| 42 |
openVPNIP string |
| 43 |
openVPNCommonName string |
| 44 |
user celcontext.UserContext |
| 45 |
} |
| 46 |
|
| 47 |
var _ cel.Activation = (*celActivation)(nil) |
| 48 |
|
| 49 |
func (a *celActivation) ResolveName(name string) (any, bool) { |
| 50 |
switch name { |
| 51 |
case "auth": |
| 52 |
return &a.auth, true |
| 53 |
case "openvpn": |
| 54 |
return &a.openvpn, true |
| 55 |
case "token": |
| 56 |
if a.token.Claims.Mapper == nil { |
| 57 |
a.token.Claims = celcontext.NewClaims(a.claims) |
| 58 |
} |
| 59 |
|
| 60 |
return &a.token, true |
| 61 |
case "user": |
| 62 |
return &a.user, true |
| 63 |
default: |
| 64 |
return nil, false |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func (a *celActivation) Parent() cel.Activation { |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
// newCELEnvironment returns the common CEL environment used by all configurable expressions. |
| 73 |
func newCELEnvironment() (*cel.Env, error) { |
| 74 |
nativeTypes, err := newCELNativeTypes() |
| 75 |
if err != nil { |
| 76 |
return nil, fmt.Errorf("create CEL native context types: %w", err) |
| 77 |
} |
| 78 |
|
| 79 |
env, err := cel.NewEnv( |
| 80 |
nativeTypes, |
| 81 |
cel.VariableWithDoc("auth", cel.ObjectType(celAuthContextTypeName), "Authentication context"), |
| 82 |
cel.VariableWithDoc("openvpn", cel.ObjectType(celOpenVPNContextTypeName), "OpenVPN session context"), |
| 83 |
cel.VariableWithDoc("token", cel.ObjectType(celTokenContextTypeName), "OAuth2 token context"), |
| 84 |
cel.VariableWithDoc("user", cel.ObjectType(celUserContextTypeName), "Normalized user context"), |
| 85 |
ext.Strings(ext.StringsVersion(5)), |
| 86 |
ext.Lists(ext.ListsVersion(4)), |
| 87 |
) |
| 88 |
if err != nil { |
| 89 |
return nil, fmt.Errorf("create CEL environment: %w", err) |
| 90 |
} |
| 91 |
|
| 92 |
return env, nil |
| 93 |
} |
| 94 |
|
| 95 |
func newCELNativeTypes() (cel.EnvOption, error) { |
| 96 |
contextTypes := []reflect.Type{ |
| 97 |
reflect.TypeFor[celcontext.AuthContext](), |
| 98 |
reflect.TypeFor[celcontext.OpenVPNContext](), |
| 99 |
reflect.TypeFor[celcontext.TokenContext](), |
| 100 |
reflect.TypeFor[celcontext.UserContext](), |
| 101 |
} |
| 102 |
descriptors := make([]any, 0, len(contextTypes)) |
| 103 |
|
| 104 |
for _, contextType := range contextTypes { |
| 105 |
descriptor, err := celtypes.NewNativeType(contextType, celtypes.ParseStructTag("cel")) |
| 106 |
if err != nil { |
| 107 |
return nil, fmt.Errorf("create descriptor for %s: %w", contextType, err) |
| 108 |
} |
| 109 |
|
| 110 |
descriptors = append(descriptors, descriptor) |
| 111 |
} |
| 112 |
|
| 113 |
return cel.Types(descriptors...), nil |
| 114 |
} |
| 115 |
|
| 116 |
func (c *Client) initializeCELPrograms() error { |
| 117 |
if err := c.initializeUsernameResolver(); err != nil { |
| 118 |
return err |
| 119 |
} |
| 120 |
|
| 121 |
if err := c.initializeCELValidation(); err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
|
| 125 |
return c.initializeClientConfigResolver() |
| 126 |
} |
| 127 |
|
| 128 |
// initializeUsernameResolver compiles the optional CEL expression used to derive the OpenVPN username. |
| 129 |
func (c *Client) initializeUsernameResolver() error { |
| 130 |
if c.conf.OAuth2.OpenVPNUsername == "" { |
| 131 |
return nil |
| 132 |
} |
| 133 |
|
| 134 |
program, err := compileCELProgram(c.conf.OAuth2.OpenVPNUsername, cel.StringType) |
| 135 |
if err != nil { |
| 136 |
return fmt.Errorf("failed to initialize username CEL expression: %w", err) |
| 137 |
} |
| 138 |
|
| 139 |
c.usernameCELPrg = program |
| 140 |
|
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
// initializeCELValidation compiles the configured CEL expression used for user validation. |
| 145 |
func (c *Client) initializeCELValidation() error { |
| 146 |
if c.conf.OAuth2.Validate.Expression == "" { |
| 147 |
return nil |
| 148 |
} |
| 149 |
|
| 150 |
program, err := compileCELProgram(c.conf.OAuth2.Validate.Expression, cel.BoolType) |
| 151 |
if err != nil { |
| 152 |
return fmt.Errorf("failed to initialize validation CEL expression: %w", err) |
| 153 |
} |
| 154 |
|
| 155 |
c.validationCELPrg = program |
| 156 |
|
| 157 |
return nil |
| 158 |
} |
| 159 |
|
| 160 |
func compileCELProgram(expression string, expectedType *cel.Type) (cel.Program, error) { |
| 161 |
env, err := newCELEnvironment() |
| 162 |
if err != nil { |
| 163 |
return nil, fmt.Errorf("failed to create CEL environment: %w", err) |
| 164 |
} |
| 165 |
|
| 166 |
ast, issues := env.Compile(expression) |
| 167 |
if issues.Err() != nil { |
| 168 |
return nil, fmt.Errorf("failed to compile CEL expression: %w", issues.Err()) |
| 169 |
} |
| 170 |
|
| 171 |
if expectedType != nil && !ast.OutputType().IsAssignableType(expectedType) { |
| 172 |
return nil, fmt.Errorf("cel expression must evaluate to %s, got %s", expectedType, ast.OutputType()) |
| 173 |
} |
| 174 |
|
| 175 |
program, err := env.Program(ast) |
| 176 |
if err != nil { |
| 177 |
return nil, fmt.Errorf("failed to create CEL program: %w", err) |
| 178 |
} |
| 179 |
|
| 180 |
return program, nil |
| 181 |
} |
| 182 |
|
| 183 |
// resolveUsername evaluates the configured username expression against the normalized identity. |
| 184 |
func (c *Client) resolveUsername( |
| 185 |
authMode CELAuthMode, |
| 186 |
session state.State, |
| 187 |
tokens *idtoken.IDToken, |
| 188 |
user types.UserInfo, |
| 189 |
) (string, error) { |
| 190 |
if c.conf.OAuth2.OpenVPNUsername == "" { |
| 191 |
return session.Client.CommonName, nil |
| 192 |
} |
| 193 |
|
| 194 |
if c.usernameCELPrg == nil { |
| 195 |
return "", errors.New("username CEL expression is not initialized") |
| 196 |
} |
| 197 |
|
| 198 |
out, _, err := c.usernameCELPrg.Eval(newCELActivation(authMode, session, tokens, user)) |
| 199 |
if err != nil { |
| 200 |
return "", fmt.Errorf("failed to evaluate CEL expression for username: %w", err) |
| 201 |
} |
| 202 |
|
| 203 |
username, ok := out.Value().(string) |
| 204 |
if !ok { |
| 205 |
return "", fmt.Errorf( |
| 206 |
"%w: CEL expression for username did not evaluate to a string: %T", |
| 207 |
types.ErrInvalidClaimType, |
| 208 |
out.Value(), |
| 209 |
) |
| 210 |
} |
| 211 |
|
| 212 |
if username == "" { |
| 213 |
return session.Client.CommonName, nil |
| 214 |
} |
| 215 |
|
| 216 |
return username, nil |
| 217 |
} |
| 218 |
|
| 219 |
// CheckIdentityCEL checks the normalized identity against the configured CEL expression. |
| 220 |
func (c *Client) CheckIdentityCEL( |
| 221 |
authMode CELAuthMode, |
| 222 |
session state.State, |
| 223 |
tokens *idtoken.IDToken, |
| 224 |
user types.UserInfo, |
| 225 |
) error { |
| 226 |
if c.validationCELPrg == nil { |
| 227 |
return nil |
| 228 |
} |
| 229 |
|
| 230 |
result, _, err := c.validationCELPrg.Eval(newCELActivation(authMode, session, tokens, user)) |
| 231 |
if err != nil { |
| 232 |
return fmt.Errorf("failed to evaluate CEL expression: %w", err) |
| 233 |
} |
| 234 |
|
| 235 |
resultValue, ok := result.Value().(bool) |
| 236 |
if !ok { |
| 237 |
return ErrCELNoBooleanResult |
| 238 |
} |
| 239 |
|
| 240 |
if !resultValue { |
| 241 |
return ErrCELValidationFailed |
| 242 |
} |
| 243 |
|
| 244 |
return nil |
| 245 |
} |
| 246 |
|
| 247 |
func newCELActivation( |
| 248 |
authMode CELAuthMode, |
| 249 |
session state.State, |
| 250 |
tokens *idtoken.IDToken, |
| 251 |
userInfo types.UserInfo, |
| 252 |
) *celActivation { |
| 253 |
var claims map[string]any |
| 254 |
|
| 255 |
tokenIP := "" |
| 256 |
|
| 257 |
if tokens != nil && tokens.IDTokenClaims != nil { |
| 258 |
tokenIP = tokens.IDTokenClaims.IPAddr |
| 259 |
|
| 260 |
if tokens.IDTokenClaims.Claims != nil { |
| 261 |
claims = tokens.IDTokenClaims.Claims |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
groups := userInfo.Groups |
| 266 |
if groups == nil { |
| 267 |
groups = make([]string, 0) |
| 268 |
} |
| 269 |
|
| 270 |
roles := userInfo.Roles |
| 271 |
if roles == nil { |
| 272 |
roles = make([]string, 0) |
| 273 |
} |
| 274 |
|
| 275 |
activation := &celActivation{ |
| 276 |
authMode: string(authMode), |
| 277 |
openVPNCommonName: session.Client.CommonName, |
| 278 |
openVPNIP: session.IPAddr, |
| 279 |
openVPNSession: session.SessionState, |
| 280 |
tokenIP: tokenIP, |
| 281 |
userEmail: userInfo.Email, |
| 282 |
userSubject: userInfo.Subject, |
| 283 |
userUsername: userInfo.Username, |
| 284 |
claims: claims, |
| 285 |
} |
| 286 |
activation.auth = celcontext.AuthContext{ |
| 287 |
Mode: &activation.authMode, |
| 288 |
} |
| 289 |
activation.openvpn = celcontext.OpenVPNContext{ |
| 290 |
CommonName: &activation.openVPNCommonName, |
| 291 |
IP: &activation.openVPNIP, |
| 292 |
SessionState: &activation.openVPNSession, |
| 293 |
} |
| 294 |
activation.token = celcontext.TokenContext{ |
| 295 |
IP: &activation.tokenIP, |
| 296 |
} |
| 297 |
activation.user = celcontext.UserContext{ |
| 298 |
Email: &activation.userEmail, |
| 299 |
Groups: groups, |
| 300 |
Roles: roles, |
| 301 |
Subject: &activation.userSubject, |
| 302 |
Username: &activation.userUsername, |
| 303 |
} |
| 304 |
|
| 305 |
return activation |
| 306 |
} |