|
| 1 | +require 'fileutils' |
| 2 | +require 'English' |
| 3 | + |
| 4 | +if module_enabled?('katello') |
| 5 | + SSL_BUILD_DIR = param('certs', 'ssl_build_dir').value |
| 6 | + CHECK_SCRIPT = `which katello-certs-check`.strip |
| 7 | + |
| 8 | + def mark_for_update(cert_name, hostname = nil) |
| 9 | + path = File.join(*[SSL_BUILD_DIR, hostname, cert_name].compact) |
| 10 | + if app_value(:noop) |
| 11 | + puts "Marking certificate #{path} for update (noop)" |
| 12 | + else |
| 13 | + puts "Marking certificate #{path} for update" |
| 14 | + FileUtils.touch("#{path}.update") |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + ca_file = param('certs', 'server_ca_cert').value |
| 19 | + cert_file = param('certs', 'server_cert').value |
| 20 | + key_file = param('certs', 'server_key').value |
| 21 | + |
| 22 | + if app_value('certs_update_server_ca') && !module_enabled?('katello') |
| 23 | + fail_and_exit("--certs-update-server-ca needs to be used with katello", 101) |
| 24 | + end |
| 25 | + |
| 26 | + if param('foreman_proxy_certs', 'foreman_proxy_fqdn') |
| 27 | + hostname = param('foreman_proxy_certs', 'foreman_proxy_fqdn').value |
| 28 | + else |
| 29 | + hostname = param('certs', 'node_fqdn').value |
| 30 | + end |
| 31 | + |
| 32 | + if app_value('certs_update_server') |
| 33 | + mark_for_update("#{hostname}-apache", hostname) |
| 34 | + mark_for_update("#{hostname}-foreman-proxy", hostname) |
| 35 | + end |
| 36 | + |
| 37 | + if app_value('certs_update_all') || app_value('certs_update_default_ca') || app_value('certs_reset') |
| 38 | + all_cert_names = Dir.glob(File.join(SSL_BUILD_DIR, hostname, '*.noarch.rpm')).map do |rpm| |
| 39 | + File.basename(rpm).sub(/-1\.0-\d+\.noarch\.rpm/, '') |
| 40 | + end.uniq |
| 41 | + |
| 42 | + all_cert_names.each do |cert_name| |
| 43 | + mark_for_update(cert_name, hostname) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + if app_value('certs_update_server_ca') || app_value('certs_reset') |
| 48 | + mark_for_update('katello-server-ca') |
| 49 | + end |
| 50 | + |
| 51 | + if !app_value('certs_skip_check') && |
| 52 | + cert_file.to_s != "" && |
| 53 | + (app_value('certs_update_server_ca') || app_value('certs_update_server')) |
| 54 | + check_cmd = %(#{CHECK_SCRIPT} -c "#{cert_file}" -k "#{key_file}" -b "#{ca_file}") |
| 55 | + output = `#{check_cmd} 2>&1` |
| 56 | + unless $CHILD_STATUS.success? |
| 57 | + fail_and_exit("Command '#{check_cmd}' exited with #{$CHILD_STATUS.exitstatus}:\n #{output}", 101) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + if app_value('certs_reset') && !app_value(:noop) |
| 62 | + param('certs', 'server_cert').unset_value |
| 63 | + param('certs', 'server_key').unset_value |
| 64 | + param('certs', 'server_ca_cert').unset_value |
| 65 | + end |
| 66 | +end |
0 commit comments