-
Notifications
You must be signed in to change notification settings - Fork 18
/
secrets_controller.go
286 lines (249 loc) · 7.72 KB
/
secrets_controller.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/heptiolabs/healthcheck"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)
const (
heritageLabelKey = "heritage"
awsIAMControllerLabelValue = "kube-aws-iam-controller"
secretPrefix = "aws-iam-"
roleARNKey = "role-arn"
expireKey = "expire"
credentialsFileKey = "credentials"
credentialsFileTemplate = `[default]
aws_access_key_id = %s
aws_secret_access_key = %s
aws_session_token = %s
aws_expiration = %s
`
credentialsProcessFileKey = "credentials.process"
credentialsProcessFileContent = `[default]
credential_process = cat /meta/aws-iam/credentials.json
`
credentialsJSONFileKey = "credentials.json"
healthEndpointAddress = ":8080"
)
var (
ownerLabels = map[string]string{heritageLabelKey: awsIAMControllerLabelValue}
)
// SecretsController is a controller which listens for pod events and updates
// secrets with AWS IAM roles as requested by pods.
type SecretsController struct {
client kubernetes.Interface
interval time.Duration
refreshLimit time.Duration
creds CredentialsGetter
roleStore *RoleStore
namespace string
healthReporter healthcheck.Handler
}
// ProcessCredentials defines the format expected from process credentials.
// https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#sourcing-credentials-from-external-processes
type ProcessCredentials struct {
Version int `json:"Version"`
AccessKeyID string `json:"AccessKeyId"`
SecretAccessKey string `json:"SecretAccessKey"`
SessionToken string `json:"SessionToken"`
Expiration time.Time `json:"Expiration"`
}
// NewSecretsController initializes a new SecretsController.
func NewSecretsController(client kubernetes.Interface, namespace string, interval, refreshLimit time.Duration, creds CredentialsGetter) *SecretsController {
return &SecretsController{
client: client,
interval: interval,
refreshLimit: refreshLimit,
creds: creds,
roleStore: NewRoleStore(),
namespace: namespace,
healthReporter: healthcheck.NewHandler(),
}
}
// getCreds gets new credentials from the CredentialsGetter and converts them
// to a secret data map.
func (c *SecretsController) getCreds(ctx context.Context, role string) (map[string][]byte, error) {
creds, err := c.creds.Get(ctx, role, 3600*time.Second)
if err != nil {
return nil, err
}
credsFile := fmt.Sprintf(
credentialsFileTemplate,
creds.AccessKeyID,
creds.SecretAccessKey,
creds.SessionToken,
creds.Expiration.Format(time.RFC3339),
)
processCreds := ProcessCredentials{
Version: 1,
AccessKeyID: creds.AccessKeyID,
SecretAccessKey: creds.SecretAccessKey,
SessionToken: creds.SessionToken,
Expiration: creds.Expiration,
}
processCredsData, err := json.Marshal(&processCreds)
if err != nil {
return nil, err
}
return map[string][]byte{
expireKey: []byte(creds.Expiration.Format(time.RFC3339)),
credentialsFileKey: []byte(credsFile),
credentialsProcessFileKey: []byte(credentialsProcessFileContent),
credentialsJSONFileKey: processCredsData,
}, nil
}
// Run runs the secret controller loop. This will refresh secrets with AWS IAM
// roles.
func (c *SecretsController) Run(ctx context.Context) {
// Defining the liveness check
var nextRefresh time.Time
// If the controller hasn't refreshed credentials in a while, fail liveness
c.healthReporter.AddLivenessCheck("nextRefresh", func() error {
if time.Since(nextRefresh) > 5*c.interval {
return fmt.Errorf("nextRefresh too old")
}
return nil
})
nextRefresh = time.Now().Add(-c.interval)
// Add the liveness endpoint at /healthz
http.HandleFunc("/healthz", c.healthReporter.LiveEndpoint)
// Start the HTTP server
http.ListenAndServe(healthEndpointAddress, nil)
for {
select {
case <-time.After(time.Until(nextRefresh)):
nextRefresh = time.Now().Add(c.interval)
err := c.refresh(ctx)
if err != nil {
log.Error(err)
}
case <-ctx.Done():
log.Info("Terminating main controller loop.")
return
}
}
}
// refresh checks for soon to expire secrets and requests new credentials. It
// also looks for roles where secrets are missing and creates the secrets for
// the designated namespace.
func (c *SecretsController) refresh(ctx context.Context) error {
opts := metav1.ListOptions{
LabelSelector: labels.Set(ownerLabels).AsSelector().String(),
}
secrets, err := c.client.CoreV1().Secrets(c.namespace).List(ctx, opts)
if err != nil {
return err
}
credsCache := map[string]map[string][]byte{}
tmpSecretStore := NewRoleStore()
for _, secret := range secrets.Items {
// skip secrets owned by someone
if len(secret.OwnerReferences) > 0 {
continue
}
role := strings.TrimPrefix(secret.Name, secretPrefix)
// store found secrets in a tmp store so we can lookup missing
// role -> secret mappings later
tmpSecretStore.Add(role, secret.Namespace, "")
if !c.roleStore.Exists(role, secret.Namespace) {
// TODO: mark for deletion first
err := c.client.CoreV1().Secrets(secret.Namespace).Delete(ctx, secret.Name, metav1.DeleteOptions{})
if err != nil {
log.Errorf("Failed to delete secret %s/%s: %v", secret.Namespace, secret.Name, err)
continue
}
log.WithFields(log.Fields{
"action": "delete",
"role": role,
"secret": secret.Name,
"namespace": secret.Namespace,
}).Info("Removing unused credentials")
continue
}
// TODO: move to function
refreshCreds := false
expirary, ok := secret.Data[expireKey]
if !ok {
refreshCreds = true
} else {
expire, err := time.Parse(time.RFC3339, string(expirary))
if err != nil {
log.Debugf("Failed to parse expirary time %s: %v", expirary, err)
refreshCreds = true
} else if time.Now().UTC().Add(c.refreshLimit).After(expire) {
refreshCreds = true
}
}
if refreshCreds {
secret.Data, err = c.getCreds(ctx, role)
if err != nil {
log.Errorf("Failed to get credentials for role %s: %v", role, err)
continue
}
// update secret with refreshed credentials
_, err := c.client.CoreV1().Secrets(secret.Namespace).Update(ctx, &secret, metav1.UpdateOptions{})
if err != nil {
log.Errorf("Failed to update secret %s/%s: %v", secret.Namespace, secret.Name, err)
continue
}
log.WithFields(log.Fields{
"action": "update",
"role": role,
"secret": secret.Name,
"namespace": secret.Namespace,
"expire": string(secret.Data[expireKey]),
}).Info()
}
if secret.Data != nil {
credsCache[role] = secret.Data
}
}
// create missing secrets
c.roleStore.RLock()
for role, namespaces := range c.roleStore.Store {
creds, ok := credsCache[role]
if !ok {
creds, err = c.getCreds(ctx, role)
if err != nil {
log.Errorf("Failed to get credentials for role %s: %v", role, err)
continue
}
}
for ns := range namespaces {
if !tmpSecretStore.Exists(role, ns) {
// create secret
name := secretPrefix + role
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
Labels: ownerLabels,
},
Data: creds,
}
_, err := c.client.CoreV1().Secrets(ns).Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
log.Errorf("Failed to create secret %s/%s: %v", ns, name, err)
continue
}
log.WithFields(log.Fields{
"action": "create",
"role": role,
"secret": name,
"namespace": ns,
"expire": string(creds[expireKey]),
}).Info()
}
}
}
c.roleStore.RUnlock()
return nil
}