-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
repository.go
373 lines (314 loc) · 11.7 KB
/
repository.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package db
import (
"context"
"fmt"
"hash/fnv"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
apiv1 "k8s.io/api/core/v1"
appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/util/settings"
)
const (
// Prefix to use for naming repository secrets
repoSecretPrefix = "repo"
// Prefix to use for naming credential template secrets
credSecretPrefix = "creds"
// The name of the key storing the username in the secret
username = "username"
// The name of the key storing the password in the secret
password = "password"
// The name of the key storing the SSH private in the secret
sshPrivateKey = "sshPrivateKey"
// The name of the key storing the TLS client cert data in the secret
tlsClientCertData = "tlsClientCertData"
// The name of the key storing the TLS client cert key in the secret
tlsClientCertKey = "tlsClientCertKey"
// The name of the key storing the GitHub App private key in the secret
githubAppPrivateKey = "githubAppPrivateKey"
// The name of the key storing the service account to access Google Cloud Source repositories
gcpServiceAccountKey = "gcpServiceAccountKey"
)
// repositoryBackend defines the API for types that wish to provide interaction with repository storage
type repositoryBackend interface {
CreateRepository(ctx context.Context, r *appsv1.Repository) (*appsv1.Repository, error)
GetRepository(ctx context.Context, repoURL string) (*appsv1.Repository, error)
ListRepositories(ctx context.Context, repoType *string) ([]*appsv1.Repository, error)
UpdateRepository(ctx context.Context, r *appsv1.Repository) (*appsv1.Repository, error)
DeleteRepository(ctx context.Context, repoURL string) error
RepositoryExists(ctx context.Context, repoURL string) (bool, error)
CreateRepoCreds(ctx context.Context, r *appsv1.RepoCreds) (*appsv1.RepoCreds, error)
GetRepoCreds(ctx context.Context, repoURL string) (*appsv1.RepoCreds, error)
ListRepoCreds(ctx context.Context) ([]string, error)
UpdateRepoCreds(ctx context.Context, r *appsv1.RepoCreds) (*appsv1.RepoCreds, error)
DeleteRepoCreds(ctx context.Context, name string) error
RepoCredsExists(ctx context.Context, repoURL string) (bool, error)
GetAllHelmRepoCreds(ctx context.Context) ([]*appsv1.RepoCreds, error)
}
func (db *db) CreateRepository(ctx context.Context, r *appsv1.Repository) (*appsv1.Repository, error) {
secretBackend := db.repoBackend()
legacyBackend := db.legacyRepoBackend()
secretExists, err := secretBackend.RepositoryExists(ctx, r.Repo)
if err != nil {
return nil, err
}
legacyExists, err := legacyBackend.RepositoryExists(ctx, r.Repo)
if err != nil {
return nil, err
}
if secretExists || legacyExists {
return nil, status.Errorf(codes.AlreadyExists, "repository %q already exists", r.Repo)
}
return secretBackend.CreateRepository(ctx, r)
}
func (db *db) GetRepository(ctx context.Context, repoURL string) (*appsv1.Repository, error) {
repository, err := db.getRepository(ctx, repoURL)
if err != nil {
return repository, err
}
if err := db.enrichCredsToRepo(ctx, repository); err != nil {
return repository, err
}
return repository, err
}
func (db *db) GetProjectRepositories(ctx context.Context, project string) ([]*appsv1.Repository, error) {
informer, err := db.settingsMgr.GetSecretsInformer()
if err != nil {
return nil, err
}
secrets, err := informer.GetIndexer().ByIndex(settings.ByProjectRepoIndexer, project)
if err != nil {
return nil, err
}
var res []*appv1.Repository
for i := range secrets {
repo, err := secretToRepository(secrets[i].(*apiv1.Secret))
if err != nil {
return nil, err
}
res = append(res, repo)
}
return res, nil
}
func (db *db) RepositoryExists(ctx context.Context, repoURL string) (bool, error) {
secretsBackend := db.repoBackend()
exists, err := secretsBackend.RepositoryExists(ctx, repoURL)
if exists || err != nil {
return exists, err
}
legacyBackend := db.legacyRepoBackend()
return legacyBackend.RepositoryExists(ctx, repoURL)
}
func (db *db) getRepository(ctx context.Context, repoURL string) (*appsv1.Repository, error) {
secretsBackend := db.repoBackend()
exists, err := secretsBackend.RepositoryExists(ctx, repoURL)
if err != nil {
return nil, err
} else if exists {
return secretsBackend.GetRepository(ctx, repoURL)
}
legacyBackend := db.legacyRepoBackend()
exists, err = legacyBackend.RepositoryExists(ctx, repoURL)
if err != nil {
return nil, err
} else if exists {
return legacyBackend.GetRepository(ctx, repoURL)
}
return &appsv1.Repository{Repo: repoURL}, nil
}
func (db *db) ListRepositories(ctx context.Context) ([]*appsv1.Repository, error) {
return db.listRepositories(ctx, nil)
}
func (db *db) listRepositories(ctx context.Context, repoType *string) ([]*appsv1.Repository, error) {
// TODO It would be nice to check for duplicates between secret and legacy repositories and make it so that
// repositories from secrets overlay repositories from legacys.
secretRepositories, err := db.repoBackend().ListRepositories(ctx, repoType)
if err != nil {
return nil, err
}
legacyRepositories, err := db.legacyRepoBackend().ListRepositories(ctx, repoType)
if err != nil {
return nil, err
}
repositories := append(secretRepositories, legacyRepositories...)
if err := db.enrichCredsToRepos(ctx, repositories); err != nil {
return nil, err
}
return repositories, nil
}
// UpdateRepository updates a repository
func (db *db) UpdateRepository(ctx context.Context, r *appsv1.Repository) (*appsv1.Repository, error) {
secretsBackend := db.repoBackend()
exists, err := secretsBackend.RepositoryExists(ctx, r.Repo)
if err != nil {
return nil, err
} else if exists {
return secretsBackend.UpdateRepository(ctx, r)
}
legacyBackend := db.legacyRepoBackend()
exists, err = legacyBackend.RepositoryExists(ctx, r.Repo)
if err != nil {
return nil, err
} else if exists {
return legacyBackend.UpdateRepository(ctx, r)
}
return nil, status.Errorf(codes.NotFound, "repo '%s' not found", r.Repo)
}
func (db *db) DeleteRepository(ctx context.Context, repoURL string) error {
secretsBackend := db.repoBackend()
exists, err := secretsBackend.RepositoryExists(ctx, repoURL)
if err != nil {
return err
} else if exists {
return secretsBackend.DeleteRepository(ctx, repoURL)
}
legacyBackend := db.legacyRepoBackend()
exists, err = legacyBackend.RepositoryExists(ctx, repoURL)
if err != nil {
return err
} else if exists {
return legacyBackend.DeleteRepository(ctx, repoURL)
}
return status.Errorf(codes.NotFound, "repo '%s' not found", repoURL)
}
// ListRepositoryCredentials returns a list of URLs that contain repo credential sets
func (db *db) ListRepositoryCredentials(ctx context.Context) ([]string, error) {
// TODO It would be nice to check for duplicates between secret and legacy repositories and make it so that
// repositories from secrets overlay repositories from legacys.
secretRepoCreds, err := db.repoBackend().ListRepoCreds(ctx)
if err != nil {
return nil, err
}
legacyRepoCreds, err := db.legacyRepoBackend().ListRepoCreds(ctx)
if err != nil {
return nil, err
}
return append(secretRepoCreds, legacyRepoCreds...), nil
}
// GetRepositoryCredentials retrieves a repository credential set
func (db *db) GetRepositoryCredentials(ctx context.Context, repoURL string) (*appsv1.RepoCreds, error) {
secretsBackend := db.repoBackend()
exists, err := secretsBackend.RepoCredsExists(ctx, repoURL)
if err != nil {
return nil, err
} else if exists {
return secretsBackend.GetRepoCreds(ctx, repoURL)
}
legacyBackend := db.legacyRepoBackend()
exists, err = legacyBackend.RepoCredsExists(ctx, repoURL)
if err != nil {
return nil, err
} else if exists {
return legacyBackend.GetRepoCreds(ctx, repoURL)
}
return nil, nil
}
// GetAllHelmRepositoryCredentials retrieves all repository credentials
func (db *db) GetAllHelmRepositoryCredentials(ctx context.Context) ([]*appsv1.RepoCreds, error) {
// TODO It would be nice to check for duplicates between secret and legacy repositories and make it so that
// repositories from secrets overlay repositories from legacys.
secretRepoCreds, err := db.repoBackend().GetAllHelmRepoCreds(ctx)
if err != nil {
return nil, err
}
legacyRepoCreds, err := db.legacyRepoBackend().GetAllHelmRepoCreds(ctx)
if err != nil {
return nil, err
}
return append(secretRepoCreds, legacyRepoCreds...), nil
}
// CreateRepositoryCredentials creates a repository credential set
func (db *db) CreateRepositoryCredentials(ctx context.Context, r *appsv1.RepoCreds) (*appsv1.RepoCreds, error) {
legacyBackend := db.legacyRepoBackend()
secretBackend := db.repoBackend()
secretExists, err := secretBackend.RepositoryExists(ctx, r.URL)
if err != nil {
return nil, err
}
legacyExists, err := legacyBackend.RepositoryExists(ctx, r.URL)
if err != nil {
return nil, err
}
if secretExists || legacyExists {
return nil, status.Errorf(codes.AlreadyExists, "repository credentials %q already exists", r.URL)
}
return secretBackend.CreateRepoCreds(ctx, r)
}
// UpdateRepositoryCredentials updates a repository credential set
func (db *db) UpdateRepositoryCredentials(ctx context.Context, r *appsv1.RepoCreds) (*appsv1.RepoCreds, error) {
secretsBackend := db.repoBackend()
exists, err := secretsBackend.RepoCredsExists(ctx, r.URL)
if err != nil {
return nil, err
} else if exists {
return secretsBackend.UpdateRepoCreds(ctx, r)
}
legacyBackend := db.legacyRepoBackend()
exists, err = legacyBackend.RepoCredsExists(ctx, r.URL)
if err != nil {
return nil, err
} else if exists {
return legacyBackend.UpdateRepoCreds(ctx, r)
}
return nil, status.Errorf(codes.NotFound, "repository credentials '%s' not found", r.URL)
}
// DeleteRepositoryCredentials deletes a repository credential set from config, and
// also all the secrets which actually contained the credentials.
func (db *db) DeleteRepositoryCredentials(ctx context.Context, name string) error {
secretsBackend := db.repoBackend()
exists, err := secretsBackend.RepoCredsExists(ctx, name)
if err != nil {
return err
} else if exists {
return secretsBackend.DeleteRepoCreds(ctx, name)
}
legacyBackend := db.legacyRepoBackend()
exists, err = legacyBackend.RepoCredsExists(ctx, name)
if err != nil {
return err
} else if exists {
return legacyBackend.DeleteRepoCreds(ctx, name)
}
return status.Errorf(codes.NotFound, "repository credentials '%s' not found", name)
}
func (db *db) enrichCredsToRepos(ctx context.Context, repositories []*appsv1.Repository) error {
for _, repository := range repositories {
if err := db.enrichCredsToRepo(ctx, repository); err != nil {
return err
}
}
return nil
}
func (db *db) repoBackend() repositoryBackend {
return &secretsRepositoryBackend{db: db}
}
func (db *db) legacyRepoBackend() repositoryBackend {
return &legacyRepositoryBackend{db: db}
}
func (db *db) enrichCredsToRepo(ctx context.Context, repository *appsv1.Repository) error {
if !repository.HasCredentials() {
creds, err := db.GetRepositoryCredentials(ctx, repository.Repo)
if err == nil {
if creds != nil {
repository.CopyCredentialsFrom(creds)
repository.InheritedCreds = true
}
} else {
return err
}
} else {
log.Debugf("%s has credentials", repository.Repo)
}
return nil
}
// RepoURLToSecretName hashes repo URL to a secret name using a formula. This is used when
// repositories are _imperatively_ created and need its credentials to be stored in a secret.
// NOTE: this formula should not be considered stable and may change in future releases.
// Do NOT rely on this formula as a means of secret lookup, only secret creation.
func RepoURLToSecretName(prefix string, repo string) string {
h := fnv.New32a()
_, _ = h.Write([]byte(repo))
return fmt.Sprintf("%s-%v", prefix, h.Sum32())
}