Skip to content

Commit

Permalink
Add task to update PE Master group rules
Browse files Browse the repository at this point in the history
This commit introduces a new private task to update
the AND conditional for the pe_compiler auth role in
the PE Master node group, changing it to regex match
for any *_compiler role. The task ensures that the
group rules are simplified and display more correctly
on the PE console.
  • Loading branch information
petergmurphy committed Feb 7, 2025
1 parent a0db439 commit 0cec740
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 7 deletions.
4 changes: 1 addition & 3 deletions manifests/setup/legacy_compiler_group.pp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

node_group { 'PE Legacy Compiler':
ensure => 'present',

Check warning on line 12 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)

Check warning on line 12 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)
parent => 'PE Master',
purge_behavior => 'classes',
parent => 'PE Infrastructure',

Check warning on line 13 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)

Check warning on line 13 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)
rule => ['=', ['trusted', 'extensions', 'pp_auth_role'], 'legacy_compiler'],

Check warning on line 14 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)

Check warning on line 14 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)
classes => {

Check warning on line 15 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)

Check warning on line 15 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)
'puppet_enterprise::profile::master' => {
Expand All @@ -26,7 +25,6 @@
node_group { 'PE Legacy Compiler Group A':
ensure => 'present',

Check warning on line 26 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)

Check warning on line 26 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)
parent => 'PE Legacy Compiler',

Check warning on line 27 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)

Check warning on line 27 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)
purge_behavior => 'classes',
rule => ['and',

Check warning on line 28 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)

Check warning on line 28 in manifests/setup/legacy_compiler_group.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

indentation of => is not properly aligned (expected in column 13, but found it in column 20) (check: arrow_alignment)
['=', ['trusted', 'extensions', 'pp_auth_role'], 'legacy_compiler'],
['=', ['trusted', 'extensions', peadm::oid('peadm_availability_group')], 'A'],
Expand Down
4 changes: 0 additions & 4 deletions manifests/setup/node_manager.pp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@
parent => 'PE Infrastructure',
data => $compiler_pool_address_data,
variables => { 'pe_master' => true },
rule => ['or',
['=', ['trusted', 'extensions', 'pp_auth_role'], 'legacy_compiler'],
['=', ['trusted', 'extensions', 'pp_auth_role'], 'pe_compiler'],
],
}

# PE Compiler group comes from default PE and already has the pe compiler role
Expand Down
2 changes: 2 additions & 0 deletions plans/convert.pp
Original file line number Diff line number Diff line change
Expand Up @@ -333,5 +333,7 @@
# lint:endignore
}
run_task('peadm::update_pe_master_rules', $primary_target)
return("Conversion to peadm Puppet Enterprise ${arch['architecture']} completed.")
}
2 changes: 2 additions & 0 deletions plans/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
final_agent_state => $final_agent_state,
)

run_task('peadm::update_pe_master_rules', $primary_host)

# Return a string banner reporting on what was done
return([$install_result, $configure_result])
}
8 changes: 8 additions & 0 deletions tasks/update_pe_master_rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"description": "Updates the PE Master group rules to replace pe_compiler with a regex match for any *_compiler role",
"input_method": "stdin",
"private": true,
"implementations": [
{"name": "update_pe_master_rules.rb"}
]
}
107 changes: 107 additions & 0 deletions tasks/update_pe_master_rules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

require 'json'
require 'net/https'
require 'puppet'

# UpdatePeMasterRules task class
class UpdatePeMasterRules
def initialize(params)
@params = params
end

def https_client
client = Net::HTTP.new(Puppet.settings[:certname], 4433)
client.use_ssl = true
client.cert = @cert ||= OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert]))
client.key = @key ||= OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey]))
client.verify_mode = OpenSSL::SSL::VERIFY_PEER
client.ca_file = Puppet.settings[:localcacert]
client
end

def get_pe_master_group_id
net = https_client
res = net.get('/classifier-api/v1/groups')

unless res.code == '200'
raise "Failed to fetch groups: HTTP #{res.code} - #{res.body}"
end

groups = JSON.parse(res.body)
pe_master_group = groups.find { |group| group['name'] == 'PE Master' }

raise "Could not find PE Master group" unless pe_master_group
pe_master_group['id']
rescue JSON::ParserError => e
raise "Invalid JSON response from server: #{e.message}"
rescue StandardError => e
raise "Error fetching PE Master group ID: #{e.message}"
end

def get_current_rules(group_id)
net = https_client
url = "/classifier-api/v1/groups/#{group_id}/rules"
req = Net::HTTP::Get.new(url)
res = net.request(req)

unless res.code == '200'
raise "Failed to fetch rules: HTTP #{res.code} - #{res.body}"
end

JSON.parse(res.body)['rule']
rescue JSON::ParserError => e
raise "Invalid JSON response from server: #{e.message}"
rescue StandardError => e
raise "Error fetching rules: #{e.message}"
end

def update_rules(group_id)
net = https_client
begin
current_rules = get_current_rules(group_id)

# Find the specific "and" rule for pe_compiler and transform it to match any *_compiler role
old_rule = ['and', ['=', ['trusted', 'extensions', 'pp_auth_role'], 'pe_compiler']]
new_rule = ['and', ['~', ['trusted', 'extensions', 'pp_auth_role'], '.*_compiler$']]

# Replace the old rule with the new rule if it exists
new_rules = current_rules.map { |rule| rule == old_rule ? new_rule : rule }

# Update the group with the modified rules
url = "/classifier-api/v1/groups/#{group_id}"
req = Net::HTTP::Post.new(url)
req['Content-Type'] = 'application/json'
req.body = { rule: new_rules }.to_json

res = net.request(req)

case res.code
when '200', '201', '204'
puts "Successfully transformed pe_compiler rule to match any *_compiler role in group #{group_id}"
else
begin
error_body = JSON.parse(res.body.to_s)
raise "Failed to update rules: #{error_body['kind'] || error_body}"
rescue JSON::ParserError
raise "Invalid response from server (status #{res.code}): #{res.body}"
end
end
rescue StandardError => e
raise "Error during rules update: #{e.message}"
end
end

def execute!
group_id = get_pe_master_group_id
update_rules(group_id)
end
end

# Run the task unless an environment flag has been set
unless ENV['RSPEC_UNIT_TEST_MODE']
Puppet.initialize_settings
task = UpdatePeMasterRules.new(JSON.parse(STDIN.read))
task.execute!
end

0 comments on commit 0cec740

Please sign in to comment.