-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix-comparison
- Loading branch information
Showing
18 changed files
with
540 additions
and
145 deletions.
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
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
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
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
19 changes: 19 additions & 0 deletions
19
app/domain/authentication/constraints/required_exclusive_constraint.rb
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Authentication | ||
module Constraints | ||
|
||
# This constraint is initialized with an array of strings. | ||
# They represent resource restrictions where exactly one is required. | ||
class RequiredExclusiveConstraint | ||
|
||
def initialize(required_exclusive:) | ||
@required_exclusive = required_exclusive | ||
end | ||
|
||
def validate(resource_restrictions:) | ||
restrictions_found = resource_restrictions & @required_exclusive | ||
raise Errors::Authentication::Constraints::IllegalRequiredExclusiveCombination.new(@required_exclusive, restrictions_found) unless restrictions_found.length == 1 | ||
end | ||
|
||
end | ||
end | ||
end |
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
|
||
IMAGE="registry.tld/conjur-appliance:eval-authn-k8s-label-selector" | ||
|
||
echo "Building on top of stable appliance image and pushing to ${IMAGE}" | ||
|
||
echo " | ||
# --- | ||
FROM registry.tld/conjur-appliance:5.0-stable | ||
# Copy new source files | ||
$( | ||
echo " | ||
app/domain/authentication/authn_k8s/authentication_request.rb | ||
app/domain/authentication/authn_k8s/consts.rb | ||
app/domain/authentication/authn_k8s/k8s_object_lookup.rb | ||
app/domain/authentication/authn_k8s/k8s_resource_validator.rb | ||
app/domain/authentication/constraints/required_exclusive_constraint.rb | ||
app/domain/errors.rb | ||
" | docker run --rm -i --entrypoint="" ruby:2-alpine ruby -e ' | ||
files = STDIN.read.split("\n").reject(&:empty?) | ||
puts files.map {|file| "COPY #{file} /opt/conjur/possum/#{file}"}.join("\n") | ||
' | ||
) | ||
RUN chown -R conjur:conjur /opt/conjur/possum/app | ||
# --- | ||
" | \ | ||
tee /dev/stderr | \ | ||
docker build -f - -t "${IMAGE}" . | ||
|
||
docker push "${IMAGE}" |
114 changes: 114 additions & 0 deletions
114
spec/app/domain/authentication/authn_k8s/k8s_resource_validator_spec.rb
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe(Authentication::AuthnK8s::K8sResourceValidator) do | ||
let(:log_output) { StringIO.new } | ||
let(:logger) { | ||
Logger.new( | ||
log_output, | ||
formatter: proc do | severity, time, progname, msg | | ||
"#{severity},#{msg}\n" | ||
end) | ||
} | ||
|
||
subject { | ||
described_class.new(k8s_object_lookup: k8s_object_lookup, pod: pod, logger: logger) | ||
} | ||
|
||
let(:k8s_object_lookup) { | ||
double("k8s_object_lookup").tap { |d| | ||
allow(d).to receive(:namespace_labels_hash) | ||
.with("namespace_name") | ||
.and_return({ :key1 => "value1", :key2 => "value2" }) | ||
} | ||
} | ||
|
||
let(:pod) { | ||
double("pod").tap { |d| | ||
d.stub_chain("metadata.namespace").and_return("namespace_name") | ||
} | ||
} | ||
|
||
context "#valid_namespace?" do | ||
it 'raises error on empty label selector' do | ||
expect { subject.valid_namespace?(label_selector: "") }.to( | ||
raise_error( | ||
::Errors::Authentication::AuthnK8s::InvalidLabelSelector | ||
) | ||
) | ||
end | ||
|
||
it 'raises error on invalid label selector' do | ||
# No key-value pair | ||
expect { subject.valid_namespace?(label_selector: "key,") }.to( | ||
raise_error( | ||
::Errors::Authentication::AuthnK8s::InvalidLabelSelector | ||
) | ||
) | ||
|
||
# Unsupported operator | ||
expect { subject.valid_namespace?(label_selector: "key!=value") }.to( | ||
raise_error( | ||
::Errors::Authentication::AuthnK8s::InvalidLabelSelector | ||
) | ||
) | ||
end | ||
|
||
it 'returns true for labels matching label-selector' do | ||
# Single key, single equals format | ||
expect( | ||
subject.valid_namespace?(label_selector: "key1=value1") | ||
).to be true | ||
# Single key, double equals format | ||
expect( | ||
subject.valid_namespace?(label_selector: "key2==value2") | ||
).to be true | ||
# Multiple keys | ||
expect( | ||
subject.valid_namespace?(label_selector: "key1=value1,key2=value2") | ||
).to be true | ||
end | ||
|
||
it 'throws an error for labels not matching label-selector' do | ||
# Value mismatch | ||
expect { subject.valid_namespace?(label_selector: "key1=notvalue") }.to( | ||
raise_error( | ||
::Errors::Authentication::AuthnK8s::LabelSelectorMismatch | ||
) | ||
) | ||
# Key not found | ||
expect { subject.valid_namespace?(label_selector: "notfoundkey=value") }.to( | ||
raise_error( | ||
::Errors::Authentication::AuthnK8s::LabelSelectorMismatch | ||
) | ||
) | ||
# One of multiple keys does not match | ||
expect { subject.valid_namespace?(label_selector: "key1=value1,notfoundkey=value") }.to( | ||
raise_error( | ||
::Errors::Authentication::AuthnK8s::LabelSelectorMismatch | ||
) | ||
) | ||
end | ||
|
||
it 'logs before label-selector validation begins, and after success' do | ||
subject.valid_namespace?(label_selector: "key1=value1") | ||
|
||
expect(log_output.string.split("\n")).to include( | ||
"DEBUG,CONJ00145D Validating K8s resource using label selector. Type:'namespace', Name:'namespace_name', Label:'key1=value1'", | ||
"DEBUG,CONJ00146D Validated K8s resource using label selector. Type:'namespace', Name:'namespace_name', Label:'key1=value1'" | ||
) | ||
end | ||
|
||
it 'logs before label-selector validation begins, but not after failure' do | ||
expect { subject.valid_namespace?(label_selector: "key1=notvalue") }.to raise_error | ||
|
||
expect(log_output.string.split("\n")).to include( | ||
"DEBUG,CONJ00145D Validating K8s resource using label selector. Type:'namespace', Name:'namespace_name', Label:'key1=notvalue'", | ||
) | ||
expect(log_output.string.split("\n")).not_to include( | ||
"DEBUG,CONJ00146D Validated K8s resource using label selector. Type:'namespace', Name:'namespace_name', Label:'key1=notvalue'" | ||
) | ||
end | ||
end | ||
end |
Oops, something went wrong.