-
Notifications
You must be signed in to change notification settings - Fork 114
/
credentials.go
240 lines (216 loc) · 6.21 KB
/
credentials.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
package credentials
import (
"context"
"strings"
"github.com/argoproj/argo-cd/v2/applicationset/utils"
"github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/util/git"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const authorizedProjectsAnnotationKey = "kargo.akuity.io/authorized-projects"
// Type is a string type used to represent a type of Credentials.
type Type string
const (
// TypeGit represents credentials for a Git repository.
TypeGit Type = "git"
// TypeHelm represents credentials for a Helm chart repository.
TypeHelm Type = "helm"
// TypeImage represents credentials for an image repository.
TypeImage Type = "image"
kargoSecretTypeLabel = "kargo.akuity.io/secret-type" // nolint: gosec
)
// Credentials generically represents any type of repository credential.
type Credentials struct {
// Username identifies a principal, which combined with the value of the
// Password field, can be used for access to some repository.
Username string
// Password, when combined with the principal identified by the Username
// field, can be used for access to some repository.
Password string
// SSHPrivateKey is a private key that can be used for access to some remote
// repository. This is primarily applicable for Git repositories.
SSHPrivateKey string
}
// Database is an interface for a Credentials store.
type Database interface {
Get(
ctx context.Context,
namespace string,
credType Type,
repo string,
) (Credentials, bool, error)
}
// kubernetesDatabase is an implementation of the Database interface that
// utilizes a Kubernetes controller runtime client to index and retrieve
// credentials stored in Kubernetes Secrets.
type kubernetesDatabase struct {
argoCDNamespace string
kargoClient client.Client
argoClient client.Client
}
// NewKubernetesDatabase initializes and returns an implementation of the
// Database interface that utilizes a Kubernetes controller runtime client to
// index and retrieve Credentials stored in Kubernetes Secrets. This function
// carries out the important task of indexing Credentials stored in Kubernetes
// Secrets by repository type + URL.
func NewKubernetesDatabase(
argoCDNamespace string,
kargoClient client.Client,
argoClient client.Client,
) Database {
return &kubernetesDatabase{
argoCDNamespace: argoCDNamespace,
kargoClient: kargoClient,
argoClient: argoClient,
}
}
func (k *kubernetesDatabase) Get(
ctx context.Context,
namespace string,
credType Type,
repoURL string,
) (Credentials, bool, error) {
creds := Credentials{}
var secret *corev1.Secret
var err error
// Check namespace for credentials
if secret, err = getCredentialsSecret(
ctx,
k.kargoClient,
namespace,
labels.Set(map[string]string{
kargoSecretTypeLabel: common.LabelValueSecretTypeRepository,
}).AsSelector(),
credType,
repoURL,
false, // repoURL is not a prefix
); err != nil {
return creds, false, err
}
if secret == nil {
// Check namespace for credentials template
if secret, err = getCredentialsSecret(
ctx,
k.kargoClient,
namespace,
labels.Set(map[string]string{
kargoSecretTypeLabel: common.LabelValueSecretTypeRepoCreds,
}).AsSelector(),
credType,
repoURL,
true, // repoURL is a prefix
); err != nil {
return creds, false, err
}
}
if secret != nil {
return secretToCreds(secret), true, nil
}
if k.argoClient == nil {
// We cannot borrow creds from from Argo CD
return creds, false, nil
}
// Check Argo CD's namespace for credentials
if secret, err = getCredentialsSecret(
ctx,
k.argoClient,
k.argoCDNamespace,
labels.Set(map[string]string{
utils.ArgoCDSecretTypeLabel: common.LabelValueSecretTypeRepository,
}).AsSelector(),
credType,
repoURL,
false, // repoURL is not a prefix
); err != nil {
return creds, false, err
}
if secret == nil {
// Check Argo CD's namespace for credentials template
if secret, err = getCredentialsSecret(
ctx,
k.argoClient,
k.argoCDNamespace,
labels.Set(map[string]string{
utils.ArgoCDSecretTypeLabel: common.LabelValueSecretTypeRepoCreds,
}).AsSelector(),
credType,
repoURL,
true, // repoURL is a prefix
); err != nil || secret == nil {
return creds, false, err
}
}
if secret == nil {
return creds, false, nil
}
// This Secret represents credentials borrowed from Argo CD. We need to look
// at its annotations to see if this is authorized by the Secret's owner.
// If it's not annotated properly, we'll treat it as we didn't find it.
allowedProjectsStr, ok := secret.Annotations[authorizedProjectsAnnotationKey]
if !ok {
return creds, false, nil
}
allowedProjects := strings.Split(allowedProjectsStr, ",")
for _, allowedProject := range allowedProjects {
if strings.TrimSpace(allowedProject) == namespace {
return secretToCreds(secret), true, nil
}
}
return creds, false, nil
}
func getCredentialsSecret(
ctx context.Context,
kubeClient client.Client,
namespace string,
labelSelector labels.Selector,
credType Type,
repoURL string,
acceptPrefixMatch bool,
) (*corev1.Secret, error) {
if credType == TypeGit {
// This is important. We don't want the presence or absence of ".git" at the
// end of the URL to affect credential lookups.
repoURL = git.NormalizeGitURL(repoURL)
}
secrets := corev1.SecretList{}
if err := kubeClient.List(
ctx,
&secrets,
&client.ListOptions{
Namespace: namespace,
LabelSelector: labelSelector,
},
); err != nil {
return nil, err
}
// Scan for the credentials we're looking for
for _, secret := range secrets.Items {
if secret.Data == nil {
continue
}
if typeBytes, ok := secret.Data["type"]; !ok || Type(typeBytes) != credType {
continue
}
urlBytes, ok := secret.Data["url"]
if !ok {
continue
}
url := string(urlBytes)
if acceptPrefixMatch && strings.HasPrefix(repoURL, url) {
return &secret, nil
}
if !acceptPrefixMatch && git.NormalizeGitURL(url) == repoURL {
return &secret, nil
}
}
return nil, nil
}
func secretToCreds(secret *corev1.Secret) Credentials {
return Credentials{
Username: string(secret.Data["username"]),
Password: string(secret.Data["password"]),
SSHPrivateKey: string(secret.Data["sshPrivateKey"]),
}
}