forked from kubernetes-sigs/aws-load-balancer-controller
/
auth_config_builder.go
144 lines (127 loc) · 5.3 KB
/
auth_config_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package ingress
import (
"context"
"github.com/pkg/errors"
"github.com/sonal-chauhan/aws-load-balancer-controller/pkg/annotations"
)
const (
defaultAuthType = AuthTypeNone
defaultAuthScope = "openid"
defaultAuthSessionCookieName = "AWSELBAuthSessionCookie"
defaultAuthSessionTimeout = 604800
defaultAuthOnUnauthenticatedRequest = "authenticate"
)
// Auth config for Service / Ingresses
type AuthConfig struct {
Type AuthType
IDPConfigCognito *AuthIDPConfigCognito
IDPConfigOIDC *AuthIDPConfigOIDC
OnUnauthenticatedRequest string
Scope string
SessionCookieName string
SessionTimeout int64
}
// AuthConfig builder can build auth configuration for service or ingresses.
type AuthConfigBuilder interface {
Build(ctx context.Context, svcAndIngAnnotations map[string]string) (AuthConfig, error)
}
// NewDefaultAuthConfigBuilder constructs new defaultAuthConfigBuilder.
func NewDefaultAuthConfigBuilder(annotationParser annotations.Parser) *defaultAuthConfigBuilder {
return &defaultAuthConfigBuilder{
annotationParser: annotationParser,
}
}
var _ AuthConfigBuilder = &defaultAuthConfigBuilder{}
// default implementation for AuthConfigBuilder
type defaultAuthConfigBuilder struct {
annotationParser annotations.Parser
}
func (b *defaultAuthConfigBuilder) Build(ctx context.Context, svcAndIngAnnotations map[string]string) (AuthConfig, error) {
authType, err := b.buildAuthType(ctx, svcAndIngAnnotations)
if err != nil {
return AuthConfig{}, err
}
authOnUnauthenticatedRequest := b.buildAuthOnUnauthenticatedRequest(ctx, svcAndIngAnnotations)
authScope := b.buildAuthScope(ctx, svcAndIngAnnotations)
authSessionCookieName := b.buildAuthSessionCookieName(ctx, svcAndIngAnnotations)
authSessionTimeout, err := b.buildAuthSessionTimeout(ctx, svcAndIngAnnotations)
if err != nil {
return AuthConfig{}, err
}
authIDPCognito, err := b.buildAuthIDPConfigCognito(ctx, svcAndIngAnnotations)
if err != nil {
return AuthConfig{}, err
}
authIDPOIDC, err := b.buildAuthIDPConfigOIDC(ctx, svcAndIngAnnotations)
if err != nil {
return AuthConfig{}, err
}
authConfig := AuthConfig{
Type: authType,
OnUnauthenticatedRequest: authOnUnauthenticatedRequest,
Scope: authScope,
SessionCookieName: authSessionCookieName,
SessionTimeout: authSessionTimeout,
IDPConfigOIDC: authIDPOIDC,
IDPConfigCognito: authIDPCognito,
}
return authConfig, nil
}
func (b *defaultAuthConfigBuilder) buildAuthType(_ context.Context, svcAndIngAnnotations map[string]string) (AuthType, error) {
rawAuthType := string(defaultAuthType)
_ = b.annotationParser.ParseStringAnnotation(annotations.IngressSuffixAuthType, &rawAuthType, svcAndIngAnnotations)
switch rawAuthType {
case string(AuthTypeCognito):
return AuthTypeCognito, nil
case string(AuthTypeOIDC):
return AuthTypeOIDC, nil
case string(AuthTypeNone):
return AuthTypeNone, nil
default:
return "", errors.Errorf("unknown authType: %v", rawAuthType)
}
}
func (b *defaultAuthConfigBuilder) buildAuthIDPConfigCognito(_ context.Context, svcAndIngAnnotations map[string]string) (*AuthIDPConfigCognito, error) {
authIDP := AuthIDPConfigCognito{}
exists, err := b.annotationParser.ParseJSONAnnotation(annotations.IngressSuffixAuthIDPCognito, &authIDP, svcAndIngAnnotations)
if err != nil {
return nil, err
}
if !exists {
return nil, nil
}
return &authIDP, nil
}
func (b *defaultAuthConfigBuilder) buildAuthIDPConfigOIDC(_ context.Context, svcAndIngAnnotations map[string]string) (*AuthIDPConfigOIDC, error) {
authIDP := AuthIDPConfigOIDC{}
exists, err := b.annotationParser.ParseJSONAnnotation(annotations.IngressSuffixAuthIDPOIDC, &authIDP, svcAndIngAnnotations)
if err != nil {
return nil, err
}
if !exists {
return nil, nil
}
return &authIDP, nil
}
func (b *defaultAuthConfigBuilder) buildAuthOnUnauthenticatedRequest(_ context.Context, svcAndIngAnnotations map[string]string) string {
rawOnUnauthenticatedRequest := defaultAuthOnUnauthenticatedRequest
_ = b.annotationParser.ParseStringAnnotation(annotations.IngressSuffixAuthOnUnauthenticatedRequest, &rawOnUnauthenticatedRequest, svcAndIngAnnotations)
return rawOnUnauthenticatedRequest
}
func (b *defaultAuthConfigBuilder) buildAuthScope(_ context.Context, svcAndIngAnnotations map[string]string) string {
rawAuthScope := defaultAuthScope
_ = b.annotationParser.ParseStringAnnotation(annotations.IngressSuffixAuthScope, &rawAuthScope, svcAndIngAnnotations)
return rawAuthScope
}
func (b *defaultAuthConfigBuilder) buildAuthSessionCookieName(_ context.Context, svcAndIngAnnotations map[string]string) string {
rawAuthSessionCookieName := defaultAuthSessionCookieName
_ = b.annotationParser.ParseStringAnnotation(annotations.IngressSuffixAuthSessionCookie, &rawAuthSessionCookieName, svcAndIngAnnotations)
return rawAuthSessionCookieName
}
func (b *defaultAuthConfigBuilder) buildAuthSessionTimeout(_ context.Context, svcAndIngAnnotations map[string]string) (int64, error) {
rawAuthSessionTimeout := int64(defaultAuthSessionTimeout)
if _, err := b.annotationParser.ParseInt64Annotation(annotations.IngressSuffixAuthSessionTimeout, &rawAuthSessionTimeout, svcAndIngAnnotations); err != nil {
return 0, err
}
return rawAuthSessionTimeout, nil
}