Skip to content

Commit

Permalink
Merge pull request #251 from olemarkus/fix-wildcard-namespace
Browse files Browse the repository at this point in the history
Support ConfigMap cache entries with wildcard namespace
  • Loading branch information
kmala authored Jan 8, 2025
2 parents e3a5463 + 2182866 commit 9f13e26
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func (c *serviceAccountCache) Get(req Request) Response {
}
{
entry := c.getCM(req.Name, req.Namespace)
if entry == nil {
entry = c.getCM(req.Name, "*")
}
if entry != nil {
result.FoundInCache = true
result.RoleARN = entry.RoleARN
Expand Down
60 changes: 60 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,66 @@ func TestPopulateCacheFromCM(t *testing.T) {

}

func TestPopulateCacheFromCMWithWildcard(t *testing.T) {
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-identity-webhook",
},
Data: map[string]string{
"config": "{\"*/mysa\":{\"RoleARN\":\"arn:aws:iam::111122223333:role/s3-reader\"},\"*/mysa2\": {\"RoleARN\":\"arn:aws:iam::111122223333:role/s3-reader2\"}}",
},
}
cm2 := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-identity-webhook",
},
Data: map[string]string{
"config": "{\"*/mysa\":{\"RoleARN\":\"arn:aws:iam::111122223333:role/s3-reader\"}}",
},
}

c := serviceAccountCache{
cmCache: make(map[string]*Entry),
}

{
err := c.populateCacheFromCM(nil, cm)
if err != nil {
t.Errorf("failed to build cache: %v", err)
}

resp := c.Get(Request{Name: "mysa2", Namespace: "myns2"})
if resp.RoleARN == "" {
t.Errorf("cloud not find entry that should have been added")
}
}

{
err := c.populateCacheFromCM(cm, cm)
if err != nil {
t.Errorf("failed to build cache: %v", err)
}

resp := c.Get(Request{Name: "mysa2", Namespace: "myns2"})
if resp.RoleARN == "" {
t.Errorf("cloud not find entry that should have been added")
}
}

{
err := c.populateCacheFromCM(cm, cm2)
if err != nil {
t.Errorf("failed to build cache: %v", err)
}

resp := c.Get(Request{Name: "mysa2", Namespace: "myns2"})
if resp.RoleARN != "" {
t.Errorf("found entry that should have been removed")
}
}

}

func TestSAAnnotationRemoval(t *testing.T) {
roleArn := "arn:aws:iam::111122223333:role/s3-reader"
oldSA := &v1.ServiceAccount{
Expand Down

0 comments on commit 9f13e26

Please sign in to comment.