/
secret.go
293 lines (249 loc) · 7.73 KB
/
secret.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 2022 The kpt and Nephio Authors
//
// 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 porch
import (
"context"
"fmt"
"time"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/nephio-project/porch/pkg/registry/porch/wi"
"github.com/nephio-project/porch/pkg/repository"
"golang.org/x/oauth2"
stsv1 "google.golang.org/api/sts/v1"
core "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// Values for scret types supported by porch.
BasicAuthType = core.SecretTypeBasicAuth
WorkloadIdentityAuthType = "kpt.dev/workload-identity-auth"
// Annotation used to specify the gsa for a ksa.
WIGCPSAAnnotation = "iam.gke.io/gcp-service-account"
)
func NewCredentialResolver(coreClient client.Reader, resolverChain []Resolver) repository.CredentialResolver {
return &secretResolver{
coreClient: coreClient,
resolverChain: resolverChain,
}
}
type secretResolver struct {
resolverChain []Resolver
coreClient client.Reader
}
type Resolver interface {
Resolve(ctx context.Context, secret core.Secret) (repository.Credential, bool, error)
}
var _ repository.CredentialResolver = &secretResolver{}
func (r *secretResolver) ResolveCredential(ctx context.Context, namespace, name string) (repository.Credential, error) {
var secret core.Secret
if err := r.coreClient.Get(ctx, client.ObjectKey{
Namespace: namespace,
Name: name,
}, &secret); err != nil {
return nil, fmt.Errorf("cannot resolve credentials in a secret %s/%s: %w", namespace, name, err)
}
for _, resolver := range r.resolverChain {
cred, found, err := resolver.Resolve(ctx, secret)
if err != nil {
return nil, fmt.Errorf("error resolving credential: %w", err)
}
if found {
return cred, nil
}
}
return nil, &NoMatchingResolverError{
Type: string(secret.Type),
}
}
type NoMatchingResolverError struct {
Type string
}
func (e *NoMatchingResolverError) Error() string {
return fmt.Sprintf("no resolver for secret with type %s", e.Type)
}
func (e *NoMatchingResolverError) Is(err error) bool {
nmre, ok := err.(*NoMatchingResolverError)
if !ok {
return false
}
return nmre.Type == e.Type
}
func NewBasicAuthResolver() Resolver {
return &BasicAuthResolver{}
}
var _ Resolver = &BasicAuthResolver{}
type BasicAuthResolver struct{}
func (b *BasicAuthResolver) Resolve(_ context.Context, secret core.Secret) (repository.Credential, bool, error) {
if secret.Type != BasicAuthType {
return nil, false, nil
}
return &BasicAuthCredential{
Username: string(secret.Data["username"]),
Password: string(secret.Data["password"]),
}, true, nil
}
type BasicAuthCredential struct {
Username string
Password string
}
var _ repository.Credential = &BasicAuthCredential{}
func (b *BasicAuthCredential) Valid() bool {
return true
}
func (b *BasicAuthCredential) ToAuthMethod() transport.AuthMethod {
return &http.BasicAuth{
Username: string(b.Username),
Password: string(b.Password),
}
}
func NewGcloudWIResolver(corev1Client *corev1client.CoreV1Client, stsClient *stsv1.Service) Resolver {
return &GcloudWIResolver{
coreV1Client: corev1Client,
exchanger: wi.NewWITokenExchanger(corev1Client, stsClient),
circuitBreaker: &circuitBreaker{
duration: 5 * time.Second, // We always wait at least 5 seconds before trying again.
factor: 2, // We double the wait time for every consecutive failure.
maxDuration: 5 * time.Minute, // Max wait time is 5 minutes.
},
}
}
var _ Resolver = &GcloudWIResolver{}
type GcloudWIResolver struct {
coreV1Client *corev1client.CoreV1Client
exchanger *wi.WITokenExchanger
circuitBreaker *circuitBreaker
}
var porchKSA = types.NamespacedName{
Name: "porch-server",
Namespace: "porch-system",
}
func (g *GcloudWIResolver) Resolve(ctx context.Context, secret core.Secret) (repository.Credential, bool, error) {
if secret.Type != WorkloadIdentityAuthType {
return nil, false, nil
}
var token *oauth2.Token
err := g.circuitBreaker.do(func() error {
var tokenErr error
token, tokenErr = g.getToken(ctx)
return tokenErr
})
if err != nil {
return nil, true, err
}
return &GcloudWICredential{
token: token,
}, true, nil
}
func (g *GcloudWIResolver) getToken(ctx context.Context) (*oauth2.Token, error) {
gsa, err := g.lookupGSAEmail(ctx, porchKSA.Name, porchKSA.Namespace)
if err != nil {
return nil, err
}
token, err := g.exchanger.Exchange(ctx, porchKSA, gsa)
if err != nil {
return nil, err
}
return token, nil
}
func (g *GcloudWIResolver) lookupGSAEmail(ctx context.Context, name, namespace string) (string, error) {
sa, err := g.coreV1Client.ServiceAccounts(namespace).Get(ctx, name, v1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return "", fmt.Errorf("porch service account %s/%s not found", namespace, name)
}
return "", fmt.Errorf("error looking up porch service account %s/%s: %w", namespace, name, err)
}
gsa, found := sa.Annotations[WIGCPSAAnnotation]
if !found {
return "", fmt.Errorf("%s annotation not found on porch sa", WIGCPSAAnnotation)
}
return gsa, nil
}
type GcloudWICredential struct {
token *oauth2.Token
}
var _ repository.Credential = &GcloudWICredential{}
func (b *GcloudWICredential) Valid() bool {
timeLeft := time.Until(b.token.Expiry)
return timeLeft > 5*time.Minute
}
func (b *GcloudWICredential) ToAuthMethod() transport.AuthMethod {
return &http.BasicAuth{
Username: "token", // username doesn't matter here.
Password: string(b.token.AccessToken),
}
}
// circuitBreaker makes sure that failing operations are retried
// using exponential backoff.
type circuitBreaker struct {
// duration defines how long to wait after the first failure.
duration time.Duration
// factor is multiplied with the previous wait time after a failure.
factor float64
// maxDuration is the maximum wait time we require.
maxDuration time.Duration
open bool
delay time.Duration
expiration time.Time
lastErr error
}
func (cb *circuitBreaker) do(action func() error) error {
if cb.open {
// If the circuit breaker is open and the required amount of
// time hasn't passed yet, we don't retry but instead return
// and error wrapping the error from the last attempt.
if time.Now().Before(cb.expiration) {
return &CircuitBreakerError{
Err: cb.lastErr,
}
}
}
err := action()
if err != nil {
// After an error, we determine delay before we allow
// another attempt.
if !cb.open {
cb.delay = cb.duration
} else {
cb.delay = cb.delay * time.Duration(cb.factor)
}
if cb.delay > cb.maxDuration {
cb.delay = cb.maxDuration
}
// Set the new expiration in the future.
cb.expiration = time.Now().Add(cb.delay)
cb.open = true
cb.lastErr = err
return err
}
// If the opration succeeded, reset the circuit breaker.
cb.open = false
cb.delay = 0
cb.lastErr = nil
return nil
}
type CircuitBreakerError struct {
Err error
}
func (cbe *CircuitBreakerError) Error() string {
return fmt.Sprintf("circuit breaker is open. Last error: %s", cbe.Err.Error())
}
func (cbe *CircuitBreakerError) Unwrap() error {
return cbe.Err
}