|
| 1 | +package translate |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + gatewayv1beta1 "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1" |
| 7 | + annotations "sigs.k8s.io/aws-load-balancer-controller/pkg/annotations" |
| 8 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/ingress" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + authTypeNone = "none" |
| 13 | + authTypeCognito = "cognito" |
| 14 | + authTypeOIDC = "oidc" |
| 15 | +) |
| 16 | + |
| 17 | +// buildAuthAction reads auth-type and related annotations and returns a |
| 18 | +// ListenerRuleConfiguration Action for authenticate-cognito or authenticate-oidc. |
| 19 | +// Returns (nil, nil) when auth-type is "none" or absent. |
| 20 | +func buildAuthAction(annos map[string]string) (*gatewayv1beta1.Action, error) { |
| 21 | + authType := getString(annos, annotations.IngressSuffixAuthType) |
| 22 | + if authType == "" || authType == authTypeNone { |
| 23 | + return nil, nil |
| 24 | + } |
| 25 | + |
| 26 | + switch authType { |
| 27 | + case authTypeCognito: |
| 28 | + return buildAuthCognitoAction(annos) |
| 29 | + case authTypeOIDC: |
| 30 | + return buildOIDCAction(annos) |
| 31 | + default: |
| 32 | + return nil, fmt.Errorf("unsupported auth-type %q", authType) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// buildAuthCognitoAction parses auth-idp-cognito JSON and shared auth annotations |
| 37 | +// into an authenticate-cognito Action. |
| 38 | +func buildAuthCognitoAction(annos map[string]string) (*gatewayv1beta1.Action, error) { |
| 39 | + var idp ingress.AuthIDPConfigCognito |
| 40 | + exists, err := ingressAnnotationParser.ParseJSONAnnotation(annotations.IngressSuffixAuthIDPCognito, &idp, annos) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("failed to parse %s annotation: %w", annotations.IngressSuffixAuthIDPCognito, err) |
| 43 | + } |
| 44 | + if !exists { |
| 45 | + return nil, fmt.Errorf("auth-type is %q but %s annotation is missing", authTypeCognito, annotations.IngressSuffixAuthIDPCognito) |
| 46 | + } |
| 47 | + |
| 48 | + scope := getOptionalAuthScope(annos) |
| 49 | + onUnauth := getOptionalAuthOnUnauthenticatedRequest(annos) |
| 50 | + sessionCookie := getOptionalAuthSessionCookieName(annos) |
| 51 | + sessionTimeout := getOptionalAuthSessionTimeout(annos) |
| 52 | + |
| 53 | + cfg := &gatewayv1beta1.AuthenticateCognitoActionConfig{ |
| 54 | + UserPoolArn: idp.UserPoolARN, |
| 55 | + UserPoolClientID: idp.UserPoolClientID, |
| 56 | + UserPoolDomain: idp.UserPoolDomain, |
| 57 | + } |
| 58 | + |
| 59 | + if scope != nil { |
| 60 | + cfg.Scope = scope |
| 61 | + } |
| 62 | + if onUnauth != nil { |
| 63 | + cognitoEnum := gatewayv1beta1.AuthenticateCognitoActionConditionalBehaviorEnum(*onUnauth) |
| 64 | + cfg.OnUnauthenticatedRequest = &cognitoEnum |
| 65 | + } |
| 66 | + if sessionCookie != nil { |
| 67 | + cfg.SessionCookieName = sessionCookie |
| 68 | + } |
| 69 | + if sessionTimeout != nil { |
| 70 | + cfg.SessionTimeout = sessionTimeout |
| 71 | + } |
| 72 | + |
| 73 | + if len(idp.AuthenticationRequestExtraParams) > 0 { |
| 74 | + params := make(map[string]string, len(idp.AuthenticationRequestExtraParams)) |
| 75 | + for k, v := range idp.AuthenticationRequestExtraParams { |
| 76 | + params[k] = v |
| 77 | + } |
| 78 | + cfg.AuthenticationRequestExtraParams = ¶ms |
| 79 | + } |
| 80 | + |
| 81 | + return &gatewayv1beta1.Action{ |
| 82 | + Type: gatewayv1beta1.ActionTypeAuthenticateCognito, |
| 83 | + AuthenticateCognitoConfig: cfg, |
| 84 | + }, nil |
| 85 | +} |
| 86 | + |
| 87 | +// buildOIDCAction parses auth-idp-oidc JSON and shared auth annotations |
| 88 | +// into an authenticate-oidc Action. The Secret reference (secretName) from |
| 89 | +// the annotation JSON is preserved as Secret.Name on the CRD. |
| 90 | +func buildOIDCAction(annos map[string]string) (*gatewayv1beta1.Action, error) { |
| 91 | + var idp ingress.AuthIDPConfigOIDC |
| 92 | + exists, err := ingressAnnotationParser.ParseJSONAnnotation(annotations.IngressSuffixAuthIDPOIDC, &idp, annos) |
| 93 | + if err != nil { |
| 94 | + return nil, fmt.Errorf("failed to parse %s annotation: %w", annotations.IngressSuffixAuthIDPOIDC, err) |
| 95 | + } |
| 96 | + if !exists { |
| 97 | + return nil, fmt.Errorf("auth-type is %q but %s annotation is missing", authTypeOIDC, annotations.IngressSuffixAuthIDPOIDC) |
| 98 | + } |
| 99 | + |
| 100 | + scope := getOptionalAuthScope(annos) |
| 101 | + onUnauth := getOptionalAuthOnUnauthenticatedRequest(annos) |
| 102 | + sessionCookie := getOptionalAuthSessionCookieName(annos) |
| 103 | + sessionTimeout := getOptionalAuthSessionTimeout(annos) |
| 104 | + |
| 105 | + cfg := &gatewayv1beta1.AuthenticateOidcActionConfig{ |
| 106 | + Issuer: idp.Issuer, |
| 107 | + AuthorizationEndpoint: idp.AuthorizationEndpoint, |
| 108 | + TokenEndpoint: idp.TokenEndpoint, |
| 109 | + UserInfoEndpoint: idp.UserInfoEndpoint, |
| 110 | + Secret: &gatewayv1beta1.Secret{Name: idp.SecretName}, |
| 111 | + } |
| 112 | + |
| 113 | + if scope != nil { |
| 114 | + cfg.Scope = scope |
| 115 | + } |
| 116 | + if onUnauth != nil { |
| 117 | + oidcEnum := gatewayv1beta1.AuthenticateOidcActionConditionalBehaviorEnum(*onUnauth) |
| 118 | + cfg.OnUnauthenticatedRequest = &oidcEnum |
| 119 | + } |
| 120 | + if sessionCookie != nil { |
| 121 | + cfg.SessionCookieName = sessionCookie |
| 122 | + } |
| 123 | + if sessionTimeout != nil { |
| 124 | + cfg.SessionTimeout = sessionTimeout |
| 125 | + } |
| 126 | + |
| 127 | + if len(idp.AuthenticationRequestExtraParams) > 0 { |
| 128 | + params := make(map[string]string, len(idp.AuthenticationRequestExtraParams)) |
| 129 | + for k, v := range idp.AuthenticationRequestExtraParams { |
| 130 | + params[k] = v |
| 131 | + } |
| 132 | + cfg.AuthenticationRequestExtraParams = ¶ms |
| 133 | + } |
| 134 | + |
| 135 | + return &gatewayv1beta1.Action{ |
| 136 | + Type: gatewayv1beta1.ActionTypeAuthenticateOIDC, |
| 137 | + AuthenticateOIDCConfig: cfg, |
| 138 | + }, nil |
| 139 | +} |
| 140 | + |
| 141 | +// getOptionalAuthScope returns the auth-scope annotation value, or nil if not set. |
| 142 | +// The CRD has kubebuilder:default="openid" so omitting it lets the webhook fill the default. |
| 143 | +func getOptionalAuthScope(annos map[string]string) *string { |
| 144 | + v := getString(annos, annotations.IngressSuffixAuthScope) |
| 145 | + if v == "" { |
| 146 | + return nil |
| 147 | + } |
| 148 | + return &v |
| 149 | +} |
| 150 | + |
| 151 | +// getOptionalAuthOnUnauthenticatedRequest returns the annotation value, or nil if not set. |
| 152 | +// The CRD has kubebuilder:default="authenticate". |
| 153 | +func getOptionalAuthOnUnauthenticatedRequest(annos map[string]string) *string { |
| 154 | + v := getString(annos, annotations.IngressSuffixAuthOnUnauthenticatedRequest) |
| 155 | + if v == "" { |
| 156 | + return nil |
| 157 | + } |
| 158 | + return &v |
| 159 | +} |
| 160 | + |
| 161 | +// getOptionalAuthSessionCookieName returns the annotation value, or nil if not set. |
| 162 | +// The CRD has kubebuilder:default="AWSELBAuthSessionCookie". |
| 163 | +func getOptionalAuthSessionCookieName(annos map[string]string) *string { |
| 164 | + v := getString(annos, annotations.IngressSuffixAuthSessionCookie) |
| 165 | + if v == "" { |
| 166 | + return nil |
| 167 | + } |
| 168 | + return &v |
| 169 | +} |
| 170 | + |
| 171 | +// getOptionalAuthSessionTimeout returns the annotation value, or nil if not set. |
| 172 | +// The CRD has kubebuilder:default=604800. |
| 173 | +func getOptionalAuthSessionTimeout(annos map[string]string) *int64 { |
| 174 | + return getInt64(annos, annotations.IngressSuffixAuthSessionTimeout) |
| 175 | +} |
0 commit comments