-
Notifications
You must be signed in to change notification settings - Fork 124
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
feat: add support for namespace label selector to authn-k8s #2613
Conversation
4d2ba6f
to
74ec584
Compare
Revert Jenkinsfile, clean up Gemfile, build-and-publish-internal-appliance.sh and authn-k8s test server
db53815
to
1a7d45b
Compare
@@ -24,6 +24,41 @@ def valid_resource?(type:, name:) | |||
@logger.debug(LogMessages::Authentication::AuthnK8s::ValidatedK8sResource.new(type, name)) | |||
end | |||
|
|||
def valid_namespace?(label_selector:) |
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.
Method valid_namespace?
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
def valid_namespace?(label_selector:) | ||
# Validates label selector and creates a hash | ||
# In the spirit of https://github.com/kubernetes/apimachinery/blob/master/pkg/labels/selector.go | ||
if label_selector.length == 0 |
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.
Use empty?
instead of length == 0
.
@@ -24,6 +24,41 @@ def valid_resource?(type:, name:) | |||
@logger.debug(LogMessages::Authentication::AuthnK8s::ValidatedK8sResource.new(type, name)) | |||
end | |||
|
|||
def valid_namespace?(label_selector:) |
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.
Complex method Authentication::AuthnK8s::K8sResourceValidator#valid_namespace? (33.5)
def namespace_labels_hash(namespace) | ||
namespace_object = k8s_client_for_method("get_namespace").get_namespace(namespace) | ||
|
||
return namespace_object.metadata.labels.to_h unless namespace_object.nil? |
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.
Authentication::AuthnK8s::K8sObjectLookup#namespace_labels_hash refers to 'namespace_object' more than self (maybe move it to another class?)
def namespace_labels_hash(namespace) | ||
namespace_object = k8s_client_for_method("get_namespace").get_namespace(namespace) | ||
|
||
return namespace_object.metadata.labels.to_h unless namespace_object.nil? |
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.
Authentication::AuthnK8s::K8sObjectLookup#namespace_labels_hash performs a nil-check
- revert to namespace-label-selector - break out resource validator logs test case - remove unneeded namespaces list response JSON
Refactor and test namespace label authentication
Add Changelog entry for Authn-K8s label-based authn scope
end | ||
|
||
@logger.debug(LogMessages::Authentication::AuthnK8s::ValidatedK8sResourceLabel.new('namespace', namespace, label_selector)) | ||
return true |
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.
Redundant return
detected.
invalid ||= kv_pair.length != 2 | ||
invalid ||= kv_pair[0].include?("!") | ||
|
||
if (invalid) |
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.
Don't use parentheses around a variable.
@@ -24,6 +24,44 @@ def valid_resource?(type:, name:) | |||
@logger.debug(LogMessages::Authentication::AuthnK8s::ValidatedK8sResource.new(type, name)) | |||
end | |||
|
|||
# Validates label selector and creates a hash | |||
# In the spirit of https://github.com/kubernetes/apimachinery/blob/master/pkg/labels/selector.go | |||
def valid_namespace?(label_selector:) |
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.
Complex method Authentication::AuthnK8s::K8sResourceValidator#valid_namespace? (40.4)
kv_pair = kv_pair.split(/={1,2}/, 2) | ||
|
||
invalid ||= kv_pair.length != 2 | ||
invalid ||= kv_pair[0].include?("!") |
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.
Authentication::AuthnK8s::K8sResourceValidator#valid_namespace? calls 'kv_pair[0]' 2 times
@logger.debug(LogMessages::Authentication::AuthnK8s::ValidatingK8sResourceLabel.new('namespace', namespace, label_selector)) | ||
|
||
if label_selector.length == 0 | ||
raise Errors::Authentication::AuthnK8s::InvalidLabelSelector.new(label_selector) |
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.
Authentication::AuthnK8s::K8sResourceValidator#valid_namespace? calls 'raise Errors::Authentication::AuthnK8s::InvalidLabelSelector.new(label_selector)' 2 times
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.
The change entries in the CHANGELOG
should live under 1.18.0
, please fix TY!
d1b1378
to
e7d1727
Compare
invalid ||= kv_pair[0].include?("!") | ||
|
||
if (invalid) | ||
raise Errors::Authentication::AuthnK8s::InvalidLabelSelector.new(label_selector) |
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.
Provide an exception class and message as arguments to raise
.
@logger.debug(LogMessages::Authentication::AuthnK8s::ValidatingK8sResourceLabel.new('namespace', namespace, label_selector)) | ||
|
||
if label_selector.length == 0 | ||
raise Errors::Authentication::AuthnK8s::InvalidLabelSelector.new(label_selector) |
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.
Provide an exception class and message as arguments to raise
.
@@ -24,6 +24,44 @@ def valid_resource?(type:, name:) | |||
@logger.debug(LogMessages::Authentication::AuthnK8s::ValidatedK8sResource.new(type, name)) | |||
end | |||
|
|||
# Validates label selector and creates a hash | |||
# In the spirit of https://github.com/kubernetes/apimachinery/blob/master/pkg/labels/selector.go | |||
def valid_namespace?(label_selector:) |
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.
Authentication::AuthnK8s::K8sResourceValidator#valid_namespace? has approx 14 statements
|
||
if label_selector.length == 0 | ||
raise Errors::Authentication::AuthnK8s::InvalidLabelSelector.new(label_selector) | ||
end |
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.
Add empty line after guard clause.
invalid ||= kv_pair.length != 2 | ||
invalid ||= kv_pair[0].include?("!") | ||
|
||
if (invalid) |
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.
Don't use parentheses around the condition of an if
.
Code Climate has analyzed commit e7d1727 and detected 19 issues on this pull request. Here's the issue category breakdown:
The test coverage on the diff in this pull request is 97.3% (50% is the threshold). This pull request will bring the total coverage in the repository to 89.9% (-1.5% change). View more on Code Climate. |
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.
LGTM, approved 👍
Not sure if we should remove the appliance build script.
@@ -0,0 +1,37 @@ | |||
#!/usr/bin/env bash |
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.
Do we want to merge this script?
Maybe this could be codified as a way of building appliance images for a target working/POC branches, but it seems like once this PR merges, we won't need it in its current form.
Desired Outcome
Adds support for scoping Authn-K8s workload identity based on namespace labels.
See related solution design for more context.
Implemented Changes
authn-k8s/namespace-label-selector
Connected Issue/Story
CyberArk internal issue link:
ONYX-23180
ONYX-23186
ONYX-23189
ONYX-23191
ONYX-23193
Definition of Done
At least 1 todo must be completed in the sections below for the PR to be
merged.
Changelog
CHANGELOG update
Test coverage
changes, or
Documentation
README
s) were updated in this PRBehavior
Security