/
oauth.go
186 lines (167 loc) · 6.43 KB
/
oauth.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package operator2
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
osinv1 "github.com/openshift/api/osin/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/openshift/library-go/pkg/crypto"
"github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
"github.com/openshift/library-go/pkg/operator/v1helpers"
)
func (c *authOperator) handleOAuthConfig(
operatorConfig *operatorv1.Authentication,
route *routev1.Route,
routerSecret *corev1.Secret,
service *corev1.Service,
consoleConfig *configv1.Console,
infrastructureConfig *configv1.Infrastructure,
) (
*configv1.OAuth,
*corev1.ConfigMap,
*configSyncData,
error,
) {
oauthConfigNoDefaults, err := c.oauth.Get(globalConfigName, metav1.GetOptions{})
if errors.IsNotFound(err) {
oauthConfigNoDefaults, err = c.oauth.Create(&configv1.OAuth{
ObjectMeta: defaultGlobalConfigMeta(),
})
}
if err != nil {
return nil, nil, nil, err
}
oauthConfig := defaultOAuthConfig(oauthConfigNoDefaults)
var accessTokenInactivityTimeoutSeconds *int32
timeout := oauthConfig.Spec.TokenConfig.AccessTokenInactivityTimeoutSeconds
switch {
case timeout < 0:
zero := int32(0)
accessTokenInactivityTimeoutSeconds = &zero
case timeout == 0:
accessTokenInactivityTimeoutSeconds = nil
case timeout > 0:
accessTokenInactivityTimeoutSeconds = &timeout
}
syncData := newConfigSyncData()
templates, err := c.handleBrandingTemplates(oauthConfig.Spec.Templates, syncData)
if err != nil {
return nil, nil, nil, err
}
var errsIDP []error
identityProviders := make([]osinv1.IdentityProvider, 0, len(oauthConfig.Spec.IdentityProviders))
for i, idp := range oauthConfig.Spec.IdentityProviders {
data, err := c.convertProviderConfigToIDPData(&idp.IdentityProviderConfig, &syncData, i)
if err != nil {
klog.Errorf("failed to honor IDP %#v: %v", idp, err)
errsIDP = append(errsIDP, fmt.Errorf("failed to apply IDP %s config: %v", idp.Name, err))
continue
}
identityProviders = append(identityProviders,
osinv1.IdentityProvider{
Name: idp.Name,
UseAsChallenger: data.challenge,
UseAsLogin: data.login,
MappingMethod: string(idp.MappingMethod),
Provider: runtime.RawExtension{
Raw: encodeOrDie(data.provider),
},
},
)
}
handleDegraded(operatorConfig, "IdentityProviderConfig", v1helpers.NewMultiLineAggregate(errsIDP))
assetPublicURL, corsAllowedOrigins := consoleToDeploymentData(consoleConfig)
cliConfig := &osinv1.OsinServerConfig{
GenericAPIServerConfig: configv1.GenericAPIServerConfig{
ServingInfo: configv1.HTTPServingInfo{
ServingInfo: configv1.ServingInfo{
BindAddress: fmt.Sprintf("0.0.0.0:%d", containerPort),
BindNetwork: "tcp4",
// we have valid serving certs provided by service-ca
// this is our main server cert which is used if SNI does not match
CertInfo: configv1.CertInfo{
CertFile: servingCertPathCert,
KeyFile: servingCertPathKey,
},
ClientCA: "", // I think this can be left unset
NamedCertificates: routerSecretToSNI(routerSecret),
MinTLSVersion: crypto.TLSVersionToNameOrDie(crypto.DefaultTLSVersion()),
CipherSuites: crypto.CipherSuitesToNamesOrDie(crypto.DefaultCiphers()),
},
MaxRequestsInFlight: 1000, // TODO this is a made up number
RequestTimeoutSeconds: 5 * 60, // 5 minutes
},
CORSAllowedOrigins: corsAllowedOrigins, // set console route as valid CORS (so JS can logout)
AuditConfig: configv1.AuditConfig{}, // TODO probably need this
KubeClientConfig: configv1.KubeClientConfig{
KubeConfig: "", // this should use in cluster config
ConnectionOverrides: configv1.ClientConnectionOverrides{
QPS: 400, // TODO figure out values
Burst: 400,
},
},
},
OAuthConfig: osinv1.OAuthConfig{
MasterCA: getMasterCA(), // we have valid serving certs provided by service-ca so we can use the service for loopback
MasterURL: fmt.Sprintf("https://%s.%s.svc", service.Name, service.Namespace),
MasterPublicURL: fmt.Sprintf("https://%s", route.Spec.Host),
LoginURL: infrastructureConfig.Status.APIServerURL,
AssetPublicURL: assetPublicURL, // set console route as valid 302 redirect for logout
AlwaysShowProviderSelection: false,
IdentityProviders: identityProviders,
GrantConfig: osinv1.GrantConfig{
Method: osinv1.GrantHandlerDeny, // force denial as this field must be set per OAuth client
ServiceAccountMethod: osinv1.GrantHandlerPrompt,
},
SessionConfig: &osinv1.SessionConfig{
SessionSecretsFile: sessionPath,
SessionMaxAgeSeconds: 5 * 60, // 5 minutes
SessionName: "ssn",
},
TokenConfig: osinv1.TokenConfig{
AuthorizeTokenMaxAgeSeconds: 5 * 60, // 5 minutes
AccessTokenMaxAgeSeconds: oauthConfig.Spec.TokenConfig.AccessTokenMaxAgeSeconds,
AccessTokenInactivityTimeoutSeconds: accessTokenInactivityTimeoutSeconds,
},
Templates: templates,
},
}
cliConfigBytes := encodeOrDie(cliConfig)
completeConfigBytes, err := resourcemerge.MergeProcessConfig(nil, cliConfigBytes, operatorConfig.Spec.UnsupportedConfigOverrides.Raw)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to merge config with unsupportedConfigOverrides: %v", err)
}
// TODO update OAuth status
return oauthConfig, getCliConfigMap(completeConfigBytes), &syncData, nil
}
func getCliConfigMap(completeConfigBytes []byte) *corev1.ConfigMap {
meta := defaultMeta()
meta.Name = cliConfigNameAndKey
return &corev1.ConfigMap{
ObjectMeta: meta,
Data: map[string]string{
cliConfigNameAndKey: string(completeConfigBytes),
},
}
}
func getMasterCA() *string {
ca := serviceCAPath // need local var to be able to take address of it
return &ca
}
func defaultOAuthConfig(oauthConfig *configv1.OAuth) *configv1.OAuth {
out := oauthConfig.DeepCopy() // do not mutate informer cache
for i := range out.Spec.IdentityProviders {
if out.Spec.IdentityProviders[i].MappingMethod == "" {
out.Spec.IdentityProviders[i].MappingMethod = configv1.MappingMethodClaim
}
}
if out.Spec.TokenConfig.AccessTokenMaxAgeSeconds == 0 {
out.Spec.TokenConfig.AccessTokenMaxAgeSeconds = 24 * 60 * 60 // 1 day
}
return out
}