-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New flag --namespace-iam-role-key and the ability to request a role at the namespace level #250
Open
vladiacob
wants to merge
4
commits into
jtblin:master
Choose a base branch
from
vladiacob:role-on-namespace-level
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,4 @@ _testmain.go | |
/build/ | ||
coverage.out | ||
/vendor/ | ||
.idea | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ type RoleMapper struct { | |
iam *iam.Client | ||
store store | ||
namespaceRestrictionFormat string | ||
namespaceIamRoleKey string | ||
} | ||
|
||
type store interface { | ||
|
@@ -47,14 +48,24 @@ func (r *RoleMapper) GetRoleMapping(IP string) (*RoleMappingResult, error) { | |
return nil, err | ||
} | ||
|
||
namespace := pod.GetNamespace() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to make this have 0 overhead for everyone else, how about it only fetches the namespace when the flag is enabled ? |
||
|
||
role, err := r.extractRoleARN(pod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
namespaceRole, err := r.extractNamespaceRoleARN(namespace) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(namespaceRole) > 0 { | ||
role = namespaceRole | ||
} | ||
|
||
// Determine if normalized role is allowed to be used in pod's namespace | ||
if r.checkRoleForNamespace(role, pod.GetNamespace()) { | ||
return &RoleMappingResult{Role: role, Namespace: pod.GetNamespace(), IP: IP}, nil | ||
if r.checkRoleForNamespace(role, namespace) { | ||
return &RoleMappingResult{Role: role, Namespace: namespace, IP: IP}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("role requested %s not valid for namespace of pod at %s with namespace %s", role, IP, pod.GetNamespace()) | ||
|
@@ -73,12 +84,11 @@ func (r *RoleMapper) GetExternalIDMapping(IP string) (string, error) { | |
return externalID, nil | ||
} | ||
|
||
// extractQualifiedRoleName extracts a fully qualified ARN for a given pod, | ||
// extractRoleARN extracts a fully qualified ARN for a given pod, | ||
// taking into consideration the appropriate fallback logic and defaulting | ||
// logic along with the namespace role restrictions | ||
func (r *RoleMapper) extractRoleARN(pod *v1.Pod) (string, error) { | ||
rawRoleName, annotationPresent := pod.GetAnnotations()[r.iamRoleKey] | ||
|
||
if !annotationPresent && r.defaultRoleARN == "" { | ||
return "", fmt.Errorf("unable to find role for IP %s", pod.Status.PodIP) | ||
} | ||
|
@@ -91,6 +101,23 @@ func (r *RoleMapper) extractRoleARN(pod *v1.Pod) (string, error) { | |
return r.iam.RoleARN(rawRoleName), nil | ||
} | ||
|
||
// extractNamespaceRoleARN extracts a fully qualified ARN for a given namespace, | ||
// if the role is not set on the namespace level we will return empty role arn without error | ||
func (r *RoleMapper) extractNamespaceRoleARN(namespace string) (string, error) { | ||
ns, err := r.store.NamespaceByName(namespace) | ||
if err != nil { | ||
log.Debugf("Unable to find an indexed namespace of %s in order to check if the role iam annotation is present", namespace) | ||
return "", fmt.Errorf("unable to find an indexed namespace of %s", namespace) | ||
} | ||
|
||
rawRoleName, annotationPresent := ns.GetAnnotations()[r.iamRoleKey] | ||
if !annotationPresent { | ||
return "", nil | ||
} | ||
|
||
return r.iam.RoleARN(rawRoleName), nil | ||
} | ||
|
||
// checkRoleForNamespace checks the 'database' for a role allowed in a namespace, | ||
// returns true if the role is found, otheriwse false | ||
func (r *RoleMapper) checkRoleForNamespace(roleArn string, namespace string) bool { | ||
|
@@ -100,7 +127,7 @@ func (r *RoleMapper) checkRoleForNamespace(roleArn string, namespace string) boo | |
|
||
ns, err := r.store.NamespaceByName(namespace) | ||
if err != nil { | ||
log.Debug("Unable to find an indexed namespace of %s", namespace) | ||
log.Debugf("Unable to find an indexed namespace of %s", namespace) | ||
return false | ||
} | ||
|
||
|
@@ -134,7 +161,8 @@ func (r *RoleMapper) DumpDebugInfo() map[string]interface{} { | |
output := make(map[string]interface{}) | ||
rolesByIP := make(map[string]string) | ||
namespacesByIP := make(map[string]string) | ||
rolesByNamespace := make(map[string][]string) | ||
rolesRestrictionsByNamespace := make(map[string][]string) | ||
rolesByNamespace := make(map[string]string) | ||
|
||
for _, ip := range r.store.ListPodIPs() { | ||
// When pods have `hostNetwork: true` they share an IP and we receive an error | ||
|
@@ -150,18 +178,25 @@ func (r *RoleMapper) DumpDebugInfo() map[string]interface{} { | |
|
||
for _, namespaceName := range r.store.ListNamespaces() { | ||
if namespace, err := r.store.NamespaceByName(namespaceName); err == nil { | ||
rolesByNamespace[namespace.GetName()] = kube2iam.GetNamespaceRoleAnnotation(namespace, r.namespaceKey) | ||
rolesRestrictionsByNamespace[namespace.GetName()] = kube2iam.GetNamespaceRoleAnnotation(namespace, r.namespaceKey) | ||
|
||
rawRoleName, annotationPresent := namespace.GetAnnotations()[r.namespaceIamRoleKey] | ||
rolesByNamespace[namespace.GetName()] = "" | ||
if annotationPresent { | ||
rolesByNamespace[namespace.GetName()] = rawRoleName | ||
} | ||
} | ||
} | ||
|
||
output["rolesByIP"] = rolesByIP | ||
output["namespaceByIP"] = namespacesByIP | ||
output["rolesByNamespace"] = rolesByNamespace | ||
output["rolesRestrictionsByNamespace"] = rolesRestrictionsByNamespace | ||
return output | ||
} | ||
|
||
// NewRoleMapper returns a new RoleMapper for use. | ||
func NewRoleMapper(roleKey string, externalIDKey string, defaultRole string, namespaceRestriction bool, namespaceKey string, iamInstance *iam.Client, kubeStore store, namespaceRestrictionFormat string) *RoleMapper { | ||
func NewRoleMapper(roleKey string, externalIDKey string, defaultRole string, namespaceRestriction bool, namespaceKey string, iamInstance *iam.Client, kubeStore store, namespaceRestrictionFormat string, namespaceRoleKey string) *RoleMapper { | ||
return &RoleMapper{ | ||
defaultRoleARN: iamInstance.RoleARN(defaultRole), | ||
iamRoleKey: roleKey, | ||
|
@@ -171,5 +206,6 @@ func NewRoleMapper(roleKey string, externalIDKey string, defaultRole string, nam | |
iam: iamInstance, | ||
store: kubeStore, | ||
namespaceRestrictionFormat: namespaceRestrictionFormat, | ||
namespaceIamRoleKey: namespaceRoleKey, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
best use a global .gitignore for that