Skip to content

Commit 134b524

Browse files
author
petergmurphy
committed
New changes
1 parent bba2ae0 commit 134b524

File tree

11 files changed

+237
-80
lines changed

11 files changed

+237
-80
lines changed

manifests/setup/legacy_compiler_group.pp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010

1111
node_group { 'PE Legacy Compiler':
1212
ensure => 'present',
13-
parent => 'PE Infrastructure',
13+
parent => 'PE Master',
1414
purge_behavior => 'rule',
1515
rule => ['=', ['trusted', 'extensions', 'pp_auth_role'], 'pe_compiler_legacy'],
1616
classes => {
17-
'puppet_enterprise::profile::master' => {
18-
'puppetdb_host' => [$internal_compiler_a_pool_address, $internal_compiler_b_pool_address].filter |$_| { $_ },
19-
'puppetdb_port' => [8081],
20-
'replication_mode' => 'none',
21-
'code_manager_auto_configure' => true,
17+
'puppet_enterprise::profile::master' => {
18+
'puppetdb_host' => [$internal_compiler_a_pool_address, $internal_compiler_b_pool_address].filter |$_| { $_ },
19+
'puppetdb_port' => [8081],
2220
},
2321
},
2422
}

plans/add_compilers.pp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
$compiler_targets = peadm::get_targets($compiler_hosts)
1717
$primary_target = peadm::get_targets($primary_host, 1)
1818

19+
# Check if PE Master rules have been updated to support pe_compiler_legacy
20+
$rules_check = run_task('peadm::check_pe_master_rules', $primary_host).first.value
21+
unless $rules_check['updated'] {
22+
fail_plan('Please run the Convert plan to convert your Puppet infrastructure to be managed by this version of PEADM.')
23+
}
24+
1925
# Get current peadm config to determine where to setup additional rules for
2026
# compiler's secondary PuppetDB instances
2127
$peadm_config = run_task('peadm::get_peadm_config', $primary_target).first.value

plans/convert.pp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@
6060

6161
out::message('# Gathering information')
6262

63+
$cert_extensions_temp = run_task('peadm::cert_data', $all_targets).reduce({}) |$memo,$result| {
64+
$memo + { $result.target.peadm::certname() => $result['extensions'] }
65+
}
66+
67+
# Add legacy compiler role to compilers that are missing it
68+
$compilers_with_legacy_compiler_flag = $cert_extensions_temp.filter |$name,$exts| {
69+
($name in $compiler_targets.map |$t| { $t.name } or $name in $legacy_compiler_targets.map |$t| { $t.name }) and
70+
($exts[peadm::oid('peadm_legacy_compiler')] != undef)
71+
}
72+
73+
if $compilers_with_legacy_compiler_flag.size > 0 {
74+
$legacy_compilers_with_flag = $compilers_with_legacy_compiler_flag.filter |$name,$exts| {
75+
$exts[peadm::oid('peadm_legacy_compiler')] == 'true'
76+
}.keys
77+
78+
$modern_compilers_with_flag = $compilers_with_legacy_compiler_flag.filter |$name,$exts| {
79+
$exts[peadm::oid('peadm_legacy_compiler')] == 'false'
80+
}.keys
81+
82+
if $modern_compilers_with_flag.size > 0 {
83+
run_plan('peadm::modify_certificate', $modern_compilers_with_flag,
84+
primary_host => $primary_target,
85+
remove_extensions => [peadm::oid('peadm_legacy_compiler')],
86+
)
87+
}
88+
89+
if $legacy_compilers_with_flag.size > 0 {
90+
run_plan('peadm::modify_certificate', $legacy_compilers_with_flag,
91+
primary_host => $primary_target,
92+
add_extensions => {
93+
'pp_auth_role' => 'pe_compiler_legacy',
94+
},
95+
remove_extensions => [peadm::oid('peadm_legacy_compiler'), peadm::oid('pp_auth_role')],
96+
)
97+
}
98+
99+
run_task('peadm::puppet_runonce', peadm::flatten_compact([
100+
$compiler_targets,
101+
$legacy_compiler_targets,
102+
]))
103+
}
104+
63105
# Get trusted fact information for all compilers. Use peadm::certname() as
64106
# the hash key because the apply block below will break trying to parse the
65107
# $compiler_extensions variable if it has Target-type hash keys.
@@ -318,6 +360,9 @@
318360
run_command('systemctl restart pe-puppetserver.service pe-puppetdb.service', $compiler_targets)
319361
}
320362
363+
# Update PE Master rules to support legacy compilers
364+
run_task('peadm::update_pe_master_rules', $primary_target)
365+
321366
# Run puppet on all targets again to ensure everything is fully up-to-date
322367
run_task('peadm::puppet_runonce', $all_targets)
323368
}
@@ -333,7 +378,5 @@
333378
# lint:endignore
334379
}
335380
336-
run_task('peadm::update_pe_master_rules', $primary_target)
337-
338381
return("Conversion to peadm Puppet Enterprise ${arch['architecture']} completed.")
339382
}

plans/convert_compiler_to_legacy.pp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
$primary_target = peadm::get_targets($primary_host, 1)
88
$convert_legacy_compiler_targets = peadm::get_targets($legacy_hosts)
99

10+
# Check if PE Master rules have been updated to support pe_compiler_legacy
11+
$rules_check = run_task('peadm::check_pe_master_rules', $primary_target).first.value
12+
unless $rules_check['updated'] {
13+
fail_plan('Please run the Convert plan to convert your Puppet infrastructure to be managed by this version of PEADM.')
14+
}
15+
1016
$cluster = run_task('peadm::get_peadm_config', $primary_host).first.value
1117
$error = getvar('cluster.error')
1218
if $error {

plans/install.pp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@
143143
final_agent_state => $final_agent_state,
144144
)
145145

146-
run_task('peadm::update_pe_master_rules', $primary_host)
147-
148146
# Return a string banner reporting on what was done
149147
return([$install_result, $configure_result])
150148
}

plans/subplans/configure.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,9 @@
174174
$legacy_compiler_targets,
175175
]))
176176

177+
# Update PE Master rules to support legacy compilers
178+
run_task('peadm::update_pe_master_rules', $primary_host)
179+
run_task('peadm::puppet_runonce', $legacy_compiler_targets)
180+
177181
return("Configuration of Puppet Enterprise ${arch['architecture']} succeeded.")
178182
}

plans/update_compiler_extensions.pp

Lines changed: 0 additions & 20 deletions
This file was deleted.

plans/upgrade.pp

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -146,41 +146,12 @@
146146
($exts[peadm::oid('peadm_legacy_compiler')] != undef)
147147
}
148148

149-
run_task('peadm::update_pe_master_rules', $primary_target)
150-
151149
if $compilers_with_legacy_compiler_flag.size > 0 {
152-
$legacy_compilers = $compilers_with_legacy_compiler_flag.filter |$name,$exts| {
153-
$exts[peadm::oid('peadm_legacy_compiler')] == 'true'
154-
}.keys
155-
156-
$modern_compilers = $compilers_with_legacy_compiler_flag.filter |$name,$exts| {
157-
$exts[peadm::oid('peadm_legacy_compiler')] == 'false'
158-
}.keys
159-
160-
if $modern_compilers.size > 0 {
161-
out::message('MODERN COMPILERS: Beginning removal of legacy compiler flag')
162-
out::message($modern_compilers)
163-
run_plan('peadm::modify_certificate', $modern_compilers,
164-
primary_host => $primary_target,
165-
remove_extensions => [peadm::oid('peadm_legacy_compiler')],
166-
)
167-
out::message('MODERN COMPILERS: Removed legacy compiler flag')
168-
}
169-
170-
if $legacy_compilers.size > 0 {
171-
out::message('LEGACY COMPILERS: Beginning addition of legacy compiler role and removal of legacy compiler flag')
172-
out::message($legacy_compilers)
173-
run_plan('peadm::modify_certificate', $legacy_compilers,
174-
primary_host => $primary_target,
175-
add_extensions => {
176-
'pp_auth_role' => 'pe_compiler_legacy',
177-
},
178-
remove_extensions => [peadm::oid('peadm_legacy_compiler'), peadm::oid('pp_auth_role')],
179-
)
180-
out::message('LEGACY COMPILERS: Added legacy compiler role and removed legacy compiler flag')
181-
}
150+
fail_plan('Please run the Convert plan to convert your Puppet infrastructure to be managed by this version of PEADM.')
182151
}
183152

153+
run_task('peadm::update_pe_master_rules', $primary_target)
154+
184155
# Gather certificate extension information from all systems
185156
$cert_extensions = run_task('peadm::cert_data', $all_targets).reduce({}) |$memo,$result| {
186157
$memo + { $result.target.peadm::certname => $result['extensions'] }

tasks/check_pe_master_rules.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"description": "Checks if the PE Master group rules have already been updated to support 'pe_compiler_legacy' as a pp_auth_role",
3+
"input_method": "stdin",
4+
"private": true,
5+
"implementations": [
6+
{"name": "check_pe_master_rules.rb"}
7+
],
8+
"parameters": {},
9+
"supports_noop": false,
10+
"output": {
11+
"updated": {
12+
"description": "Whether the PE Master rules have already been updated",
13+
"type": "Boolean"
14+
},
15+
"message": {
16+
"description": "A message describing the current state of the PE Master rules",
17+
"type": "String"
18+
},
19+
"error": {
20+
"description": "Error message if the task failed",
21+
"type": "Optional[String]"
22+
}
23+
}
24+
}

tasks/check_pe_master_rules.rb

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
# frozen_string_literal: true
3+
4+
require 'json'
5+
require 'net/https'
6+
require 'puppet'
7+
8+
# CheckPeMasterRules task class
9+
class CheckPeMasterRules
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 check_rules_updated(rules)
61+
# If not an array, return false
62+
return false unless rules.is_a?(Array)
63+
64+
# Check if this is an 'and' rule with at least 2 elements
65+
if rules[0] == 'and' && rules.length > 1
66+
# Check if the first element is an 'or' rule for pe_compiler and pe_compiler_legacy
67+
if rules[1].is_a?(Array) && rules[1][0] == 'or'
68+
# Look for the pe_compiler and pe_compiler_legacy rules
69+
pe_compiler_found = false
70+
pe_compiler_legacy_found = false
71+
72+
rules[1][1..-1].each do |rule|
73+
if rule.is_a?(Array) &&
74+
rule[0] == '=' &&
75+
rule[1].is_a?(Array) &&
76+
rule[1] == ['trusted', 'extensions', 'pp_auth_role']
77+
78+
pe_compiler_found = true if rule[2] == 'pe_compiler'
79+
pe_compiler_legacy_found = true if rule[2] == 'pe_compiler_legacy'
80+
end
81+
end
82+
83+
return pe_compiler_found && pe_compiler_legacy_found
84+
end
85+
end
86+
87+
# Check if the rule is already using a regex match
88+
if rules[0] == '~' &&
89+
rules[1].is_a?(Array) &&
90+
rules[1] == ['trusted', 'extensions', 'pp_auth_role'] &&
91+
rules[2] == '^pe_compiler.*$'
92+
return true
93+
end
94+
95+
false
96+
end
97+
98+
def execute!
99+
begin
100+
group_id = get_pe_master_group_id
101+
current_rules = get_current_rules(group_id)
102+
103+
is_updated = check_rules_updated(current_rules)
104+
105+
result = {
106+
'updated' => is_updated,
107+
'message' => is_updated ?
108+
'PE Master rules have already been updated with pe_compiler_legacy support' :
109+
'PE Master rules need to be updated to support pe_compiler_legacy'
110+
}
111+
112+
puts result.to_json
113+
rescue StandardError => e
114+
puts({ 'error' => e.message, 'updated' => false }.to_json)
115+
exit 1
116+
end
117+
end
118+
end
119+
120+
# Run the task unless an environment flag has been set
121+
unless ENV['RSPEC_UNIT_TEST_MODE']
122+
Puppet.initialize_settings
123+
task = CheckPeMasterRules.new(JSON.parse(STDIN.read))
124+
task.execute!
125+
end

0 commit comments

Comments
 (0)