/
auth.go
188 lines (156 loc) · 5.52 KB
/
auth.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
// Copyright © 2019 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"context"
"encoding/base64"
"net/url"
"emperror.dev/errors"
dex "github.com/dexidp/dex/api/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
k8sClient "k8s.io/client-go/tools/clientcmd"
k8sClientApi "k8s.io/client-go/tools/clientcmd/api"
"github.com/banzaicloud/pipeline/internal/cluster/clustersecret"
"github.com/banzaicloud/pipeline/internal/global"
"github.com/banzaicloud/pipeline/internal/secret/secrettype"
"github.com/banzaicloud/pipeline/src/secret"
)
const (
authSecretName = "dex-client"
configSecretName = "config"
clientIDKey = "clientID"
clientSecretKey = "clientSecret"
)
type ClusterClientSecret struct {
ClientID string
ClientSecret string
}
type dexClient struct {
dex.DexClient
grpcConn *grpc.ClientConn
}
func (d *dexClient) Close() error {
return d.grpcConn.Close()
}
func newDexClient(hostAndPort, caPath string) (*dexClient, error) {
dialOption := grpc.WithInsecure()
if caPath != "" {
creds, err := credentials.NewClientTLSFromFile(caPath, "")
if err != nil {
return nil, errors.WrapIff(err, "loading dex CA cert failed")
}
dialOption = grpc.WithTransportCredentials(creds)
}
conn, err := grpc.Dial(hostAndPort, dialOption)
if err != nil {
return nil, errors.WrapIff(err, "grpc dial failed")
}
return &dexClient{DexClient: dex.NewDexClient(conn), grpcConn: conn}, nil
}
type ClusterAuthService interface {
RegisterCluster(context.Context, string, uint, string) error
UnRegisterCluster(context.Context, string) error
GetClusterConfig(context.Context, uint) (*k8sClientApi.Config, error)
ClusterClientSecretGetter
}
type ClusterClientSecretGetter interface {
GetClusterClientSecret(context.Context, uint) (ClusterClientSecret, error)
}
type dexClusterAuthService struct {
dexClient *dexClient
secretStore *clustersecret.Store
pipelineRedirectURI string
}
func NewDexClusterAuthService(secretStore *clustersecret.Store) (ClusterAuthService, error) {
client, err := newDexClient(global.Config.Dex.APIAddr, global.Config.Dex.APICa)
if err != nil {
return nil, errors.WrapIff(err, "failed to create dex auth service")
}
pipelineExternalURL, err := url.Parse(global.Config.Pipeline.External.URL)
if err != nil {
return nil, errors.WrapIff(err, "failed to parse pipeline externalURL")
}
pipelineExternalURL.Path = "/auth/dex/cluster/callback"
return &dexClusterAuthService{
dexClient: client,
secretStore: secretStore,
pipelineRedirectURI: pipelineExternalURL.String(),
}, nil
}
func (a *dexClusterAuthService) RegisterCluster(ctx context.Context, clusterName string, clusterID uint, clusterUID string) error {
clientID := clusterUID
clientSecret, _ := secret.RandomString("randAlphaNum", 32)
cliRedirectURI := "http://localhost:5555/callback"
pipelineRedirectURI := a.pipelineRedirectURI
req := &dex.CreateClientReq{
Client: &dex.Client{
Id: clientID,
Name: clusterName,
Secret: clientSecret,
RedirectUris: []string{
cliRedirectURI,
pipelineRedirectURI,
},
},
}
if _, err := a.dexClient.CreateClient(ctx, req); err != nil {
return errors.WrapIff(err, "failed to create dex client for cluster: %s", clusterUID)
}
// save the secret to the secret store
secretRequest := clustersecret.SecretCreateRequest{
Type: secrettype.GenericSecret,
Name: authSecretName,
Values: map[string]string{
clientIDKey: clientID,
clientSecretKey: clientSecret,
},
}
_, err := a.secretStore.EnsureSecretExists(ctx, clusterID, secretRequest)
if err != nil {
return errors.WrapIff(err, "failed to create secret for dex clientID/clientSecret for cluster: %s", clusterUID)
}
return nil
}
func (a *dexClusterAuthService) UnRegisterCluster(ctx context.Context, clusterUID string) error {
clientID := clusterUID
req := &dex.DeleteClientReq{
Id: clientID,
}
if _, err := a.dexClient.DeleteClient(ctx, req); err != nil {
return errors.WrapIff(err, "failed to delete dex client for cluster: %s", clusterUID)
}
return nil
}
func (a *dexClusterAuthService) GetClusterClientSecret(ctx context.Context, clusterID uint) (ClusterClientSecret, error) {
secret, err := a.secretStore.GetSecret(ctx, clusterID, authSecretName)
if err != nil {
return ClusterClientSecret{}, errors.WrapIff(err, "failed to get dex client for cluster: %d", clusterID)
}
return ClusterClientSecret{
ClientID: secret.Values[clientIDKey],
ClientSecret: secret.Values[clientSecretKey],
}, nil
}
func (a *dexClusterAuthService) GetClusterConfig(ctx context.Context, clusterID uint) (*k8sClientApi.Config, error) {
secret, err := a.secretStore.GetSecret(ctx, clusterID, configSecretName)
if err != nil {
return nil, errors.WrapIff(err, "failed to get dex client for cluster: %d", clusterID)
}
configData, err := base64.StdEncoding.DecodeString(secret.Values[secrettype.K8SConfig])
if err != nil {
return nil, errors.WrapIff(err, "failed to base64 decode kubeconfig: %d", clusterID)
}
return k8sClient.Load(configData)
}