Skip to content

Commit

Permalink
Add special rule format for mesh and peering type policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Cornel Bratu committed Jan 25, 2024
1 parent 76a6042 commit 99e0ef8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/puppet/provider/consul_policy/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def self.encode_rules(rules)
encoded = []

rules.each do |rule|
if %w[acl operator keyring].include?(rule['resource'])
if %w[acl operator keyring mesh peering].include?(rule['resource'])
encoded.push("#{rule['resource']} = \"#{rule['disposition']}\"")
else
encoded.push("#{rule['resource']} \"#{rule['segment']}\" {\n policy = \"#{rule['disposition']}\"\n}")
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/type/consul_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
raise ArgumentError, 'Policy rule must be a hash' unless value.is_a?(Hash)

raise ArgumentError, 'Policy rule needs to specify a resource' unless value.key?('resource')
raise ArgumentError, 'Policy rule needs to specify a segment' unless value.key?('segment') || %w[acl operator keyring].include?(value['resource'])
raise ArgumentError, 'Policy rule needs to specify a segment' unless value.key?('segment') || %w[acl operator keyring mesh peering].include?(value['resource'])
raise ArgumentError, 'Policy rule needs to specify a disposition' unless value.key?('disposition')

raise ArgumentError, 'Policy rule resource must be a string' unless value['resource'].is_a?(String)
raise ArgumentError, 'Policy rule segment must be a string' unless value['segment'].is_a?(String) || %w[acl operator keyring].include?(value['resource'])
raise ArgumentError, 'Policy rule segment must be a string' unless value['segment'].is_a?(String) || %w[acl operator keyring mesh peering].include?(value['resource'])
raise ArgumentError, 'Policy rule disposition must be a string' unless value['disposition'].is_a?(String)
end

Expand Down
26 changes: 24 additions & 2 deletions spec/unit/puppet/type/consul_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
end.to raise_error(Puppet::Error, %r{Policy rule disposition must be a string})
end

context 'resource is acl, operator or keyring' do
context 'resource is acl, operator, keyring, mesh or peering' do
it 'passes if rule segment is missing' do
expect do
Puppet::Type.type(:consul_policy).new(
Expand Down Expand Up @@ -138,6 +138,28 @@
},
]
)
Puppet::Type.type(:consul_policy).new(
name: 'testing',
id: '39c75e12-7f43-0a40-dfba-9aa3fcda08d4',
description: 'test description',
rules: [
{
'resource' => 'mesh',
'disposition' => 'read'
},
]
)
Puppet::Type.type(:consul_policy).new(
name: 'testing',
id: '39c75e12-7f43-0a40-dfba-9aa3fcda08d4',
description: 'test description',
rules: [
{
'resource' => 'peering',
'disposition' => 'read'
},
]
)
end.not_to raise_error
end

Expand All @@ -159,7 +181,7 @@
end
end

context 'resource is neither acl nor operator nor keyring' do
context 'resource is neither acl nor operator nor keyring nor peering nor mesh' do
it 'fails if rule segment is missing' do
expect do
Puppet::Type.type(:consul_policy).new(
Expand Down

0 comments on commit 99e0ef8

Please sign in to comment.