/
oauthprox.go
186 lines (163 loc) · 4.9 KB
/
oauthprox.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 auth
import (
"context"
"fmt"
"strings"
"github.com/Nerzal/gocloak/v8"
"github.com/opentracing/opentracing-go"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
proxPort = 4180
httpPort = 80
)
// OIDCClient represents the critical data to be able to connect with an openid
// connect client that supports the discovery endpoints.
type OIDCClient struct {
IssuerURL string
// Redirects []string
ClientID string
ClientSecret string
}
// An OIDCCreator can takes a context and client spec, and returns a clientid and
// clientsecret or error.
type OIDCCreator interface {
CreateOIDCClient(ctx context.Context, c *gocloak.Client) (*OIDCClient, error)
}
// Returns a gocloak spec for a given kubernetes ingress spec
func getGocloakSpecForIngress(ctx context.Context, ing *networkv1.Ingress) (*gocloak.Client, error) {
var uris []string
for _, rule := range ing.Spec.Rules {
uris = append(uris, fmt.Sprintf("https://%s/*", rule.Host))
}
if len(uris) == 0 {
return nil, fmt.Errorf("no URIs present in ingress")
}
return &gocloak.Client{
Name: gocloak.StringP(fmt.Sprintf("oauth2-" + ing.Name)),
RedirectURIs: &uris,
BaseURL: &uris[0],
ConsentRequired: gocloak.BoolP(false),
AdminURL: &uris[0],
Enabled: gocloak.BoolP(true),
PublicClient: gocloak.BoolP(false),
ClientID: gocloak.StringP(fmt.Sprintf("%s-oauth2-proxy", ing.Name)),
ClientAuthenticatorType: gocloak.StringP("client-secret"),
}, nil
}
// ReplaceWithOauth2Proxy replaces an ingress with the oauth2 proxy.
func ReplaceWithOauth2Proxy(ctx context.Context, cs SecureStackCreator, ing *networkv1.Ingress, oid OIDCCreator, opts ProxyOpts) error {
sp, ctx := opentracing.StartSpanFromContext(ctx, "ReplaceWithOauth2Proxy")
defer sp.Finish()
c, err := getGocloakSpecForIngress(ctx, ing)
if err != nil {
return fmt.Errorf("could't build client spec: %w", err)
}
oClient, err := oid.CreateOIDCClient(ctx, c)
if err != nil {
return fmt.Errorf("error creating client: %w", err)
}
svc, err := CreateSvc(ctx, cs, oClient, opts)
updatedIng := ing.DeepCopy()
for i := range updatedIng.Spec.Rules {
for j := range updatedIng.Spec.Rules[i].HTTP.Paths {
updatedIng.Spec.Rules[i].HTTP.Paths[j].Backend = networkv1.IngressBackend{
Service: &networkv1.IngressServiceBackend{
Name: svc.Name,
Port: networkv1.ServiceBackendPort{
Number: httpPort,
},
},
}
}
}
_, err = cs.CreateIngress(ctx, ing)
if err != nil && !strings.Contains(err.Error(), "exists") {
return fmt.Errorf("error updating ingress: %w", err)
}
return nil
}
func CreateSvc(ctx context.Context, cs SecureStackCreator, oClient *OIDCClient, opts ProxyOpts) (*corev1.Service, error) {
sp, ctx := opentracing.StartSpanFromContext(ctx, "createSvc")
defer sp.Finish()
ns, n := opts.targetSvcNamespaceAndName()
proxN := n + "-oauth2-proxy"
om := metav1.ObjectMeta{
Name: proxN,
Namespace: ns,
Labels: map[string]string{
"app": proxN,
},
Annotations: opts.Annotations,
}
probe := corev1.Probe{
InitialDelaySeconds: 0,
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/ping",
Port: intstr.FromInt(proxPort),
},
},
}
env, err := opts.SetupEnv(ctx, cs, oClient)
if err != nil {
return nil, fmt.Errorf("error speccing out proxy env vars: %w", err)
}
args, err := opts.Args(ctx)
if err != nil {
return nil, fmt.Errorf("error getting args from proxyopts: %w", err)
}
_, err = cs.CreateDeployment(ctx, &appsv1.Deployment{
ObjectMeta: om,
Spec: appsv1.DeploymentSpec{
Replicas: &one32,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": proxN,
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: om,
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: n,
Image: "quay.io/oauth2-proxy/oauth2-proxy:latest",
Args: args,
Env: env,
Ports: []corev1.ContainerPort{{
ContainerPort: proxPort,
Protocol: corev1.ProtocolTCP,
}},
LivenessProbe: &probe,
ReadinessProbe: &probe,
}},
},
},
},
})
if err != nil && !strings.Contains(err.Error(), "exists") {
return nil, fmt.Errorf("error creating deployment: %w", err)
}
svc, err := cs.CreateService(ctx, &corev1.Service{
ObjectMeta: om,
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Selector: om.Labels,
Ports: []corev1.ServicePort{{
Protocol: corev1.ProtocolTCP,
Name: ns,
Port: httpPort,
TargetPort: intstr.FromInt(proxPort),
}},
},
})
if err != nil && !strings.Contains(err.Error(), "exists") {
return nil, fmt.Errorf("error creating oidc service: %w", err)
}
return svc, nil
}
var one32 = int32(1)