Skip to content
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 schema_version key to groups for calculated yaml definitions #52

Merged
merged 11 commits into from
Feb 21, 2025
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ GEM
crack (1.0.0)
bigdecimal
rexml
date (3.4.1)
debug (1.8.0)
irb (>= 1.5.0)
reline (>= 0.3.1)
Expand Down Expand Up @@ -67,7 +68,8 @@ GEM
parser (3.3.1.0)
ast (~> 2.4.1)
racc
psych (5.1.2)
psych (5.2.3)
date
stringio
public_suffix (5.0.5)
racc (1.8.0)
Expand Down Expand Up @@ -131,7 +133,7 @@ GEM
simplecov (< 1.0)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
stringio (3.1.0)
stringio (3.1.4)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand Down
22 changes: 22 additions & 0 deletions lib/entitlements/data/groups/calculated/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ def description
parsed_data.fetch("description", "")
end

# Standard interface: Get the schema version of this group.
#
# Takes no arguments.
# Examples: "entitlements/v1", "entitlements/v1.2.3", "entitlements/1.2.3"
# Format: namespace/major.minor.patch
#
# Returns a String with the schema version (k8s-style), or "entitlements/v1" if undefined.
Contract C::None => String
def schema_version
schema_version = parsed_data.fetch("schema_version", "entitlements/v1").to_s

namespace, version = schema_version.split("/")

unless namespace.match?(/\A[a-zA-Z0-9]+\z/) && version.match?(/\A(v?\d+(\.\d+){0,2})\z/)
raise "Invalid schema version format: #{schema_version} - Expected format is '<namespace>/<version>' " \
"- Examples: entitlements/v1, entitlements/v1.2.3, entitlements/1.2.3"
end

# rebuild the schema version string to ensure it's in the correct format and return it
return "#{namespace}/#{version}"
end

# Files can support modifiers that act independently of rules.
# This returns the modifiers from the file as a hash.
#
Expand Down
38 changes: 38 additions & 0 deletions spec/unit/entitlements/data/groups/calculated/yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@
end
end

describe "#schema_version" do
it "returns the version string when one is set" do
filename = fixture("ldap-config/filters/no-filters-with-schema-version.yaml")
subject = described_class.new(filename: filename)
expect(subject.schema_version).to eq("entitlements/1.2.3")
end

it "returns the version string when one is set without the patch" do
filename = fixture("ldap-config/filters/no-filters-with-schema-version-no-patch.yaml")
subject = described_class.new(filename: filename)
expect(subject.schema_version).to eq("entitlements/1.2")
end

it "returns the version string when one is set (with v prefix)" do
filename = fixture("ldap-config/filters/no-filters-with-schema-version-with-v.yaml")
subject = described_class.new(filename: filename)
expect(subject.schema_version).to eq("entitlements/v1.2.3")
end

it "returns the default version when schema_version is undefined" do
filename = fixture("ldap-config/filters/no-filters-description.yaml")
subject = described_class.new(filename: filename)
expect(subject.schema_version).to eq("entitlements/v1")
end

it "throws an error when an invalid schema_version string is provided" do
filename = fixture("ldap-config/filters/no-filters-with-bad-schema-version.yaml")
subject = described_class.new(filename: filename)
expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/)
end

it "throws an error when the version string is missing the namespace" do
filename = fixture("ldap-config/filters/no-filters-with-missing-version-namespace.yaml")
subject = described_class.new(filename: filename)
expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/)
end
end

describe "#initialize_filters" do
it "returns the default filter hash when no filters are defined" do
filename = fixture("ldap-config/filters/no-filters.yaml")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description: Yo kittens
schema_version: entitlements/1.abc2.3
rules:
or:
- username: russianblue
- username: BlackManx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description: Yo kittens
schema_version: 1.2.3
rules:
or:
- username: russianblue
- username: BlackManx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description: Yo kittens
schema_version: entitlements/1.2
rules:
or:
- username: russianblue
- username: BlackManx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description: Yo kittens
schema_version: entitlements/v1.2.3
rules:
or:
- username: russianblue
- username: BlackManx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description: Yo kittens
schema_version: entitlements/1.2.3
rules:
or:
- username: russianblue
- username: BlackManx
Binary file added vendor/cache/date-3.4.1.gem
Binary file not shown.
Binary file added vendor/cache/prism-1.3.0.gem
Binary file not shown.
Binary file removed vendor/cache/psych-5.1.2.gem
Binary file not shown.
Binary file added vendor/cache/psych-5.2.3.gem
Binary file not shown.
Binary file added vendor/cache/rbs-3.8.1.gem
Binary file not shown.
Binary file added vendor/cache/ruby-lsp-0.23.11.gem
Binary file not shown.
Binary file added vendor/cache/sorbet-runtime-0.5.11839.gem
Binary file not shown.
Binary file removed vendor/cache/stringio-3.1.0.gem
Binary file not shown.
Binary file added vendor/cache/stringio-3.1.4.gem
Binary file not shown.
Loading