-
Notifications
You must be signed in to change notification settings - Fork 83
/
oauth_proxy.go
293 lines (275 loc) · 8.44 KB
/
oauth_proxy.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// Copyright (c) 2019-2023 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package gateway
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/util/intstr"
identityprovider "github.com/eclipse-che/che-operator/pkg/deploy/identity-provider"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/resource"
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
chev2 "github.com/eclipse-che/che-operator/api/v2"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
"github.com/eclipse-che/che-operator/pkg/common/constants"
defaults "github.com/eclipse-che/che-operator/pkg/common/operator-defaults"
"github.com/eclipse-che/che-operator/pkg/common/utils"
"github.com/eclipse-che/che-operator/pkg/deploy"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func getGatewayOauthProxyConfigSpec(ctx *chetypes.DeployContext, cookieSecret string) corev1.ConfigMap {
instance := ctx.CheCluster
var config string
if infrastructure.IsOpenShift() {
config = openshiftOauthProxyConfig(ctx, cookieSecret)
} else {
config = kubernetesOauthProxyConfig(ctx, cookieSecret)
}
return corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "che-gateway-config-oauth-proxy",
Namespace: instance.Namespace,
Labels: deploy.GetLabels(GatewayServiceName),
},
Data: map[string]string{
"oauth-proxy.cfg": config,
},
}
}
func openshiftOauthProxyConfig(ctx *chetypes.DeployContext, cookieSecret string) string {
oauthSecret := ""
oauthClientName := ""
oauthClient, _ := identityprovider.GetOAuthClient(ctx)
if oauthClient == nil {
logrus.Error("oauth client not found")
} else {
oauthSecret = oauthClient.Secret
oauthClientName = oauthClient.Name
}
return fmt.Sprintf(`
http_address = ":%d"
https_address = ""
provider = "openshift"
redirect_url = "https://%s/oauth/callback"
upstreams = [
"http://127.0.0.1:8081/"
]
client_id = "%s"
client_secret = "%s"
scope = "%s"
openshift_service_account = "%s"
cookie_secret = "%s"
cookie_expire = "%s"
email_domains = "*"
cookie_httponly = false
pass_access_token = true
skip_provider_button = false
%s
`, GatewayServicePort,
ctx.CheHost,
oauthClientName,
oauthSecret,
utils.GetValue(ctx.CheCluster.Spec.Networking.Auth.OAuthScope, constants.OpenShiftOAuthScope),
GatewayServiceName,
cookieSecret,
cookieExpireAsString(ctx.CheCluster),
skipAuthConfig(ctx.CheCluster))
}
func kubernetesOauthProxyConfig(ctx *chetypes.DeployContext, cookieSecret string) string {
return fmt.Sprintf(`
proxy_prefix = "/oauth"
http_address = ":%d"
https_address = ""
provider = "oidc"
redirect_url = "https://%s/oauth/callback"
oidc_issuer_url = "%s"
insecure_oidc_skip_issuer_verification = true
ssl_insecure_skip_verify = true
upstreams = [
"http://127.0.0.1:8081/"
]
client_id = "%s"
client_secret = "%s"
cookie_secret = "%s"
cookie_expire = "%s"
email_domains = "*"
cookie_httponly = false
skip_provider_button = true
whitelist_domains = "%s"
cookie_domains = "%s"
%s
%s
%s
`, GatewayServicePort,
ctx.CheHost,
ctx.CheCluster.Spec.Networking.Auth.IdentityProviderURL,
ctx.CheCluster.Spec.Networking.Auth.OAuthClientName,
ctx.CheCluster.Spec.Networking.Auth.OAuthSecret,
cookieSecret,
cookieExpireAsString(ctx.CheCluster),
utils.Whitelist(ctx.CheHost),
utils.Whitelist(ctx.CheHost),
skipAuthConfig(ctx.CheCluster),
identityTokenConfig(ctx.CheCluster),
oauthScopeConfig(ctx.CheCluster))
}
func skipAuthConfig(instance *chev2.CheCluster) string {
var skipAuthPaths []string
if !instance.Spec.Components.PluginRegistry.DisableInternalRegistry {
skipAuthPaths = append(skipAuthPaths, "^/"+constants.PluginRegistryName)
}
if !instance.Spec.Components.DevfileRegistry.DisableInternalRegistry {
skipAuthPaths = append(skipAuthPaths, "^/"+constants.DevfileRegistryName)
}
skipAuthPaths = append(skipAuthPaths, "^/$")
skipAuthPaths = append(skipAuthPaths, "/healthz$")
skipAuthPaths = append(skipAuthPaths, "^/dashboard/static/preload")
skipAuthPaths = append(skipAuthPaths, fmt.Sprintf("^%s$", defaults.GetConsoleLinkImage()))
if len(skipAuthPaths) > 0 {
propName := "skip_auth_routes"
if infrastructure.IsOpenShift() {
propName = "skip_auth_regex"
}
return fmt.Sprintf("%s = \"%s\"", propName, strings.Join(skipAuthPaths, "|"))
}
return ""
}
func identityTokenConfig(instance *chev2.CheCluster) string {
if instance.IsAccessTokenConfigured() {
// pass OAuth access_token to upstream via X-Forwarded-Access-Token header
return "pass_access_token = true"
}
// pass OIDC IDToken to upstream via Authorization Bearer header
return "pass_authorization_header = true"
}
func oauthScopeConfig(instance *chev2.CheCluster) string {
scope := instance.Spec.Networking.Auth.OAuthScope
if len(scope) > 1 {
return fmt.Sprintf("scope = \"%s\"", scope)
}
return ""
}
func getOauthProxyContainerSpec(ctx *chetypes.DeployContext) corev1.Container {
// append env var with ConfigMap revision to restore pod automatically when config has been changed
cm := &corev1.ConfigMap{}
exists, _ := deploy.GetNamespacedObject(ctx, "che-gateway-config-oauth-proxy", cm)
configMapRevision := map[bool]string{true: cm.GetResourceVersion(), false: ""}[exists]
return corev1.Container{
Name: "oauth-proxy",
Image: defaults.GetGatewayAuthenticationSidecarImage(ctx.CheCluster),
ImagePullPolicy: corev1.PullIfNotPresent,
Args: map[bool][]string{
true: {
"--config=/etc/oauth-proxy/oauth-proxy.cfg",
},
false: {
"--config=/etc/oauth-proxy/oauth-proxy.cfg",
"--ping-path=/ping",
"--exclude-logging-path=/ping"},
}[infrastructure.IsOpenShift()],
VolumeMounts: []corev1.VolumeMount{
{
Name: "oauth-proxy-config",
MountPath: "/etc/oauth-proxy",
},
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("512Mi"),
corev1.ResourceCPU: resource.MustParse("0.5"),
},
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("64Mi"),
corev1.ResourceCPU: resource.MustParse("0.1"),
},
},
Ports: []corev1.ContainerPort{
{ContainerPort: GatewayServicePort, Protocol: "TCP"},
},
Env: []corev1.EnvVar{
{
Name: "http_proxy",
Value: ctx.Proxy.HttpProxy,
},
{
Name: "https_proxy",
Value: ctx.Proxy.HttpsProxy,
},
{
Name: "no_proxy",
Value: ctx.Proxy.NoProxy,
},
{
Name: "CM_REVISION",
Value: configMapRevision,
},
},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: map[bool]string{true: "/oauth/healthz", false: "/ping"}[infrastructure.IsOpenShift()],
Port: intstr.IntOrString{
Type: intstr.Int,
IntVal: int32(8080),
},
Scheme: corev1.URISchemeHTTP,
},
},
InitialDelaySeconds: 5,
TimeoutSeconds: 5,
PeriodSeconds: 5,
SuccessThreshold: 1,
FailureThreshold: 5,
},
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: map[bool]string{true: "/oauth/healthz", false: "/ping"}[infrastructure.IsOpenShift()],
Port: intstr.IntOrString{
Type: intstr.Int,
IntVal: int32(8080),
},
Scheme: corev1.URISchemeHTTP,
},
},
InitialDelaySeconds: 15,
TimeoutSeconds: 5,
PeriodSeconds: 5,
SuccessThreshold: 1,
FailureThreshold: 5,
},
}
}
func getOauthProxyConfigVolume() corev1.Volume {
return corev1.Volume{
Name: "oauth-proxy-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "che-gateway-config-oauth-proxy",
},
},
},
}
}
func cookieExpireAsString(cheCluster *chev2.CheCluster) string {
cookieExpire := constants.DefaultOAuthProxyCookieExpireSeconds
if cheCluster.Spec.Networking.Auth.Gateway.OAuthProxy != nil && cheCluster.Spec.Networking.Auth.Gateway.OAuthProxy.CookieExpireSeconds != nil {
cookieExpire = *cheCluster.Spec.Networking.Auth.Gateway.OAuthProxy.CookieExpireSeconds
}
return fmt.Sprintf("%dh%dm%ds", cookieExpire/3600, cookieExpire%3600/60, cookieExpire%60)
}