Skip to content

Commit 2b2457c

Browse files
committed
feat: preserve secrets referenced by application replicasets
1 parent c9fa919 commit 2b2457c

File tree

6 files changed

+32
-29
lines changed

6 files changed

+32
-29
lines changed

charts/Chart.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ apiVersion: v2
22
name: azurerator
33
description: Operator that reconciles Azure AD applications.
44
type: application
5-
version: 0.6.0
5+
version: 0.7.0
66
sources:
77
- https://github.com/nais/azurerator/tree/master/charts

charts/templates/rbac.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ rules:
3939
- list
4040
- get
4141
- watch
42+
- apiGroups:
43+
- apps
44+
resources:
45+
- replicasets
46+
verbs:
47+
- list
48+
- get
49+
- watch
4250

4351
---
4452
# permissions to do leader election.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/go-jose/go-jose/v4 v4.0.4
1212
github.com/go-logr/zapr v1.3.0
1313
github.com/google/uuid v1.6.0
14-
github.com/nais/liberator v0.0.0-20241216095017-87471bb214d0
14+
github.com/nais/liberator v0.0.0-20241219121707-182675e6f1ad
1515
github.com/nais/msgraph.go v0.1.5
1616
github.com/prometheus/client_golang v1.20.5
1717
github.com/sethvargo/go-retry v0.3.0

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
4242
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
4343
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
4444
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
45-
github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8P3k=
46-
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
45+
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
46+
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
4747
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
4848
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
4949
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
@@ -163,8 +163,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
163163
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
164164
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
165165
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
166-
github.com/nais/liberator v0.0.0-20241216095017-87471bb214d0 h1:N2yzxyyI5h8w4NtcYWeGaDIZhiluf1vN1/nGbeKkNSs=
167-
github.com/nais/liberator v0.0.0-20241216095017-87471bb214d0/go.mod h1:gRUXR0S/Il3JnHlfc6ESLAih27Su+WFPm5aaXp/tHpE=
166+
github.com/nais/liberator v0.0.0-20241219121707-182675e6f1ad h1:Z/HPNfZgmNsqlBwfwUE9DMyhsQThpQnudadqncwGdik=
167+
github.com/nais/liberator v0.0.0-20241219121707-182675e6f1ad/go.mod h1:DtQgc26XvoZFe8jy0++wbuFUu5yCFTQ3vGV+y7TA7Uw=
168168
github.com/nais/msgraph.go v0.1.5 h1:Sf9/DUZ8mMipgG2bDE249mHTo8E9qV8tBJpAbuuQsJw=
169169
github.com/nais/msgraph.go v0.1.5/go.mod h1:WhEs+KY7Nrt3rpUZwCpSKtwy2CiL+YG+OJ+vhh2/ZMU=
170170
github.com/onsi/ginkgo/v2 v2.21.0 h1:7rg/4f3rB88pb5obDgNZrNHrQ4e6WpjonchcpuBRnZM=

hack/resources/02-rbac.yaml

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ rules:
1818
- create
1919
- update
2020
- apiGroups:
21-
- '*'
21+
- ""
2222
resources:
2323
- secrets
2424
- events
@@ -31,14 +31,22 @@ rules:
3131
- update
3232
- patch
3333
- apiGroups:
34-
- '*'
34+
- ""
3535
resources:
3636
- pods
3737
- namespaces
3838
verbs:
3939
- list
4040
- get
4141
- watch
42+
- apiGroups:
43+
- apps
44+
resources:
45+
- replicasets
46+
verbs:
47+
- list
48+
- get
49+
- watch
4250
- apiGroups:
4351
- coordination.k8s.io
4452
resources:

pkg/reconciler/secrets/secrets.go

+8-21
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (s secretsReconciler) Prepare(ctx context.Context, instance *v1.AzureAdAppl
5858
return nil, fmt.Errorf("getting managed secrets: %w", err)
5959
}
6060

61-
secretsExtractor := secrets.NewExtractor(*managedSecrets, dataKeys)
61+
secretsExtractor := secrets.NewExtractor(managedSecrets, dataKeys)
6262

6363
keyIDs := func() credentials.KeyIDs {
6464
keyIDs := secretsExtractor.GetKeyIDs()
@@ -79,7 +79,7 @@ func (s secretsReconciler) Prepare(ctx context.Context, instance *v1.AzureAdAppl
7979
},
8080
DataKeys: dataKeys,
8181
KeyIDs: keyIDs,
82-
ManagedSecrets: *managedSecrets,
82+
ManagedSecrets: managedSecrets,
8383
}, nil
8484
}
8585

@@ -156,26 +156,13 @@ func (s secretsReconciler) createOrUpdate(tx transaction.Transaction, result res
156156
return nil
157157
}
158158

159-
func (s secretsReconciler) getManaged(ctx context.Context, instance *v1.AzureAdApplication) (*kubernetes.SecretLists, error) {
160-
// fetch all application pods for this app
161-
podList, err := kubernetes.ListPodsForApplication(ctx, s.reader, instance.GetName(), instance.GetNamespace())
162-
if err != nil {
163-
return nil, err
164-
}
165-
166-
// fetch all managed secrets
167-
var allSecrets corev1.SecretList
168-
opts := []client.ListOption{
169-
client.InNamespace(instance.GetNamespace()),
170-
client.MatchingLabels(labels.Labels(instance)),
159+
func (s secretsReconciler) getManaged(ctx context.Context, instance *v1.AzureAdApplication) (kubernetes.SecretLists, error) {
160+
objectKey := client.ObjectKey{
161+
Name: instance.GetName(),
162+
Namespace: instance.GetNamespace(),
171163
}
172-
if err := s.reader.List(ctx, &allSecrets, opts...); err != nil {
173-
return nil, err
174-
}
175-
176-
// find intersect between secrets in use by application pods and all managed secrets
177-
podSecrets := kubernetes.ListUsedAndUnusedSecretsForPods(allSecrets, podList)
178-
return &podSecrets, nil
164+
secretLabels := labels.Labels(instance)
165+
return kubernetes.ListSecretsForApplication(ctx, s.reader, objectKey, secretLabels)
179166
}
180167

181168
func (s secretsReconciler) DeleteUnused(tx transaction.Transaction) error {

0 commit comments

Comments
 (0)