Skip to content

Commit

Permalink
remove dependency from PR kubernetes-sigs#416
Browse files Browse the repository at this point in the history
  • Loading branch information
nnmin-aws committed Nov 8, 2022
1 parent cc450b0 commit 97dff6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions pkg/mapper/dynamicfile/dynamicfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"errors"
"fmt"
"os"
"strings"
"sync"
"time"

"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/aws-iam-authenticator/pkg/arn"
"sigs.k8s.io/aws-iam-authenticator/pkg/config"
)

Expand Down Expand Up @@ -147,18 +149,16 @@ func ParseMap(filename string) (userMappings []config.UserMapping, roleMappings
}

for _, userMapping := range dynamicFileData.UserMappings {
err = userMapping.Validate()
if err != nil {
errs = append(errs, err)
if userMapping.UserARN == "" {
errs = append(errs, fmt.Errorf("Value for userarn must be supplied"))
} else {
userMappings = append(userMappings, userMapping)
}
}

for _, roleMapping := range dynamicFileData.RoleMappings {
err = roleMapping.Validate()
if err != nil {
errs = append(errs, err)
if roleMapping.RoleARN == "" {
errs = append(errs, fmt.Errorf("Value for rolearn must be supplied"))
} else {
roleMappings = append(roleMappings, roleMapping)
}
Expand All @@ -184,10 +184,12 @@ func (ms *DynamicFileMapStore) saveMap(
ms.awsAccounts = make(map[string]interface{})

for _, user := range userMappings {
ms.users[user.Key()] = user
canonicalizedARN, _ := arn.Canonicalize(strings.ToLower(user.UserARN))
ms.users[canonicalizedARN] = user
}
for _, role := range roleMappings {
ms.roles[role.Key()] = role
canonicalizedARN, _ := arn.Canonicalize(strings.ToLower(role.RoleARN))
ms.roles[canonicalizedARN] = role
}
for _, awsAccount := range awsAccounts {
ms.awsAccounts[awsAccount] = nil
Expand All @@ -203,23 +205,21 @@ var RoleNotFound = errors.New("Role not found in dynamic file")
func (ms *DynamicFileMapStore) UserMapping(arn string) (config.UserMapping, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
for _, user := range ms.users {
if user.Matches(arn) {
return user, nil
}
if user, ok := ms.users[arn]; !ok {
return config.UserMapping{}, UserNotFound
} else {
return user, nil
}
return config.UserMapping{}, UserNotFound
}

func (ms *DynamicFileMapStore) RoleMapping(arn string) (config.RoleMapping, error) {
ms.mutex.RLock()
defer ms.mutex.RUnlock()
for _, role := range ms.roles {
if role.Matches(arn) {
return role, nil
}
if role, ok := ms.roles[arn]; !ok {
return config.RoleMapping{}, RoleNotFound
} else {
return role, nil
}
return config.RoleMapping{}, RoleNotFound
}

func (ms *DynamicFileMapStore) AWSAccount(id string) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/mapper/dynamicfile/dynamicfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func makeStore() DynamicFileMapStore {
filename: "test.txt",
}
ms.users["arn:aws:iam::012345678912:user/matt"] = testUser
ms.roles["arn:aws:iam::012345678912:role/comp*"] = testRole
ms.roles["arn:aws:iam::012345678912:role/computer"] = testRole
ms.awsAccounts["123"] = nil
return ms
}
Expand Down

0 comments on commit 97dff6b

Please sign in to comment.