Skip to content

Commit 0cec740

Browse files
author
petergmurphy
committed
Add task to update PE Master group rules
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.
1 parent a0db439 commit 0cec740

File tree

6 files changed

+120
-7
lines changed

6 files changed

+120
-7
lines changed

manifests/setup/legacy_compiler_group.pp

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
node_group { 'PE Legacy Compiler':
1212
ensure => 'present',
13-
parent => 'PE Master',
14-
purge_behavior => 'classes',
13+
parent => 'PE Infrastructure',
1514
rule => ['=', ['trusted', 'extensions', 'pp_auth_role'], 'legacy_compiler'],
1615
classes => {
1716
'puppet_enterprise::profile::master' => {
@@ -26,7 +25,6 @@
2625
node_group { 'PE Legacy Compiler Group A':
2726
ensure => 'present',
2827
parent => 'PE Legacy Compiler',
29-
purge_behavior => 'classes',
3028
rule => ['and',
3129
['=', ['trusted', 'extensions', 'pp_auth_role'], 'legacy_compiler'],
3230
['=', ['trusted', 'extensions', peadm::oid('peadm_availability_group')], 'A'],

manifests/setup/node_manager.pp

-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@
7777
parent => 'PE Infrastructure',
7878
data => $compiler_pool_address_data,
7979
variables => { 'pe_master' => true },
80-
rule => ['or',
81-
['=', ['trusted', 'extensions', 'pp_auth_role'], 'legacy_compiler'],
82-
['=', ['trusted', 'extensions', 'pp_auth_role'], 'pe_compiler'],
83-
],
8480
}
8581

8682
# PE Compiler group comes from default PE and already has the pe compiler role

plans/convert.pp

+2
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,7 @@
333333
# lint:endignore
334334
}
335335
336+
run_task('peadm::update_pe_master_rules', $primary_target)
337+
336338
return("Conversion to peadm Puppet Enterprise ${arch['architecture']} completed.")
337339
}

plans/install.pp

+2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@
143143
final_agent_state => $final_agent_state,
144144
)
145145

146+
run_task('peadm::update_pe_master_rules', $primary_host)
147+
146148
# Return a string banner reporting on what was done
147149
return([$install_result, $configure_result])
148150
}

tasks/update_pe_master_rules.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"description": "Updates the PE Master group rules to replace pe_compiler with a regex match for any *_compiler role",
3+
"input_method": "stdin",
4+
"private": true,
5+
"implementations": [
6+
{"name": "update_pe_master_rules.rb"}
7+
]
8+
}

tasks/update_pe_master_rules.rb

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
# frozen_string_literal: true
3+
4+
require 'json'
5+
require 'net/https'
6+
require 'puppet'
7+
8+
# UpdatePeMasterRules task class
9+
class UpdatePeMasterRules
10+
def initialize(params)
11+
@params = params
12+
end
13+
14+
def https_client
15+
client = Net::HTTP.new(Puppet.settings[:certname], 4433)
16+
client.use_ssl = true
17+
client.cert = @cert ||= OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert]))
18+
client.key = @key ||= OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey]))
19+
client.verify_mode = OpenSSL::SSL::VERIFY_PEER
20+
client.ca_file = Puppet.settings[:localcacert]
21+
client
22+
end
23+
24+
def get_pe_master_group_id
25+
net = https_client
26+
res = net.get('/classifier-api/v1/groups')
27+
28+
unless res.code == '200'
29+
raise "Failed to fetch groups: HTTP #{res.code} - #{res.body}"
30+
end
31+
32+
groups = JSON.parse(res.body)
33+
pe_master_group = groups.find { |group| group['name'] == 'PE Master' }
34+
35+
raise "Could not find PE Master group" unless pe_master_group
36+
pe_master_group['id']
37+
rescue JSON::ParserError => e
38+
raise "Invalid JSON response from server: #{e.message}"
39+
rescue StandardError => e
40+
raise "Error fetching PE Master group ID: #{e.message}"
41+
end
42+
43+
def get_current_rules(group_id)
44+
net = https_client
45+
url = "/classifier-api/v1/groups/#{group_id}/rules"
46+
req = Net::HTTP::Get.new(url)
47+
res = net.request(req)
48+
49+
unless res.code == '200'
50+
raise "Failed to fetch rules: HTTP #{res.code} - #{res.body}"
51+
end
52+
53+
JSON.parse(res.body)['rule']
54+
rescue JSON::ParserError => e
55+
raise "Invalid JSON response from server: #{e.message}"
56+
rescue StandardError => e
57+
raise "Error fetching rules: #{e.message}"
58+
end
59+
60+
def update_rules(group_id)
61+
net = https_client
62+
begin
63+
current_rules = get_current_rules(group_id)
64+
65+
# Find the specific "and" rule for pe_compiler and transform it to match any *_compiler role
66+
old_rule = ['and', ['=', ['trusted', 'extensions', 'pp_auth_role'], 'pe_compiler']]
67+
new_rule = ['and', ['~', ['trusted', 'extensions', 'pp_auth_role'], '.*_compiler$']]
68+
69+
# Replace the old rule with the new rule if it exists
70+
new_rules = current_rules.map { |rule| rule == old_rule ? new_rule : rule }
71+
72+
# Update the group with the modified rules
73+
url = "/classifier-api/v1/groups/#{group_id}"
74+
req = Net::HTTP::Post.new(url)
75+
req['Content-Type'] = 'application/json'
76+
req.body = { rule: new_rules }.to_json
77+
78+
res = net.request(req)
79+
80+
case res.code
81+
when '200', '201', '204'
82+
puts "Successfully transformed pe_compiler rule to match any *_compiler role in group #{group_id}"
83+
else
84+
begin
85+
error_body = JSON.parse(res.body.to_s)
86+
raise "Failed to update rules: #{error_body['kind'] || error_body}"
87+
rescue JSON::ParserError
88+
raise "Invalid response from server (status #{res.code}): #{res.body}"
89+
end
90+
end
91+
rescue StandardError => e
92+
raise "Error during rules update: #{e.message}"
93+
end
94+
end
95+
96+
def execute!
97+
group_id = get_pe_master_group_id
98+
update_rules(group_id)
99+
end
100+
end
101+
102+
# Run the task unless an environment flag has been set
103+
unless ENV['RSPEC_UNIT_TEST_MODE']
104+
Puppet.initialize_settings
105+
task = UpdatePeMasterRules.new(JSON.parse(STDIN.read))
106+
task.execute!
107+
end

0 commit comments

Comments
 (0)