-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #23799 - Refactor: Make PuppetCa pluggable
- Loading branch information
Julian Todt
committed
Jun 21, 2018
1 parent
14a7505
commit 641f2e4
Showing
18 changed files
with
258 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
# | ||
# Configuration of the PuppetCA hostname_whitelisting provider | ||
# | ||
|
||
#:autosignfile: /etc/puppet/autosign.conf |
33 changes: 33 additions & 0 deletions
33
extra/migrations/2018062000000_migrate_puppetca_settings.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'yaml' | ||
|
||
class MigratePuppetCaSettings < ::Proxy::Migration | ||
def migrate | ||
copy_original_configuration_except(path('settings.d', 'puppetca.yml'), | ||
path('settings.d', 'puppetca_hostname_whitelisting.yml.example')) | ||
|
||
module_settings = YAML.load_file(path(src_dir, 'settings.d', 'puppetca.yml')) | ||
provider_settings = YAML.load_file(path(src_dir, 'settings.d', 'puppetca_hostname_whitelisting.yml')) | ||
|
||
write_yaml(path(dst_dir, 'settings.d', 'puppetca_hostname_whitelisting.yml'), | ||
transform_provider_yaml(module_settings, provider_settings)) | ||
write_yaml(path(dst_dir, 'settings.d', 'puppetca.yml'), transform_puppetca_yaml(module_settings)) | ||
end | ||
|
||
def transform_puppetca_yaml(input) | ||
input.delete(:autosignfile) | ||
input[:use_provider] = 'puppetca_hostname_whitelisting' | ||
input | ||
end | ||
|
||
def transform_provider_yaml(module_settings, provider_settings) | ||
provider_settings = {} unless provider_settings.is_a? Hash | ||
provider_settings[:autosignfile] = module_settings[:autosignfile] | ||
provider_settings | ||
end | ||
|
||
def write_yaml(filepath, yaml) | ||
File.open(filepath, 'w') do |f| | ||
f.write(yaml.to_yaml) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Proxy::PuppetCa | ||
module DependencyInjection | ||
include Proxy::DependencyInjection::Accessors | ||
def container_instance | ||
@container_instance ||= ::Proxy::Plugins.instance.find {|p| p[:name] == :puppetca }[:di_container] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module ::Proxy::PuppetCa | ||
class PluginConfiguration | ||
def load_classes | ||
require 'puppetca/puppetca_puppet_cert' | ||
require 'puppetca/dependency_injection' | ||
require 'puppetca/puppetca_api' | ||
end | ||
|
||
def load_dependency_injection_wirings(container_instance, settings) | ||
container_instance.dependency :puppet_cert, lambda { ::Proxy::PuppetCa::PuppetCert.new } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
require 'puppetca/plugin_configuration' | ||
require 'puppetca/puppetca_plugin' | ||
|
||
module Proxy::PuppetCa; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
modules/puppetca_hostname_whitelisting/plugin_configuration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module ::Proxy::PuppetCa::HostnameWhitelisting | ||
class PluginConfiguration | ||
def load_classes | ||
require 'puppetca_hostname_whitelisting/puppetca_hostname_whitelisting_autosigner' | ||
end | ||
|
||
def load_dependency_injection_wirings(container_instance, settings) | ||
container_instance.dependency :autosigner, lambda { ::Proxy::PuppetCa::HostnameWhitelisting::Autosigner.new } | ||
end | ||
end | ||
end | ||
|
2 changes: 2 additions & 0 deletions
2
modules/puppetca_hostname_whitelisting/puppetca_hostname_whitelisting.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'puppetca_hostname_whitelisting/plugin_configuration' | ||
require 'puppetca_hostname_whitelisting/puppetca_hostname_whitelisting_plugin' |
57 changes: 57 additions & 0 deletions
57
modules/puppetca_hostname_whitelisting/puppetca_hostname_whitelisting_autosigner.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module ::Proxy::PuppetCa::HostnameWhitelisting | ||
class Autosigner | ||
include ::Proxy::Log | ||
include ::Proxy::Util | ||
|
||
def autosign_file | ||
Proxy::PuppetCa::HostnameWhitelisting::Plugin.settings.autosignfile | ||
end | ||
|
||
#remove certname from autosign if exists | ||
def disable certname | ||
raise "No such file #{autosign_file}" unless File.exist?(autosign_file) | ||
|
||
found = false | ||
entries = File.readlines(autosign_file).collect do |l| | ||
if l.chomp != certname | ||
l | ||
else | ||
found = true | ||
nil | ||
end | ||
end.uniq.compact | ||
if found | ||
open(autosign_file, File::TRUNC|File::RDWR) do |autosign| | ||
autosign.write entries.join | ||
end | ||
logger.debug "Removed #{certname} from autosign" | ||
else | ||
logger.debug "Attempt to remove nonexistent client autosign for #{certname}" | ||
raise ::Proxy::PuppetCa::NotPresent, "Attempt to remove nonexistent client autosign for #{certname}" | ||
end | ||
end | ||
|
||
# add certname to puppet autosign file | ||
# parameter is certname to use | ||
def autosign certname | ||
FileUtils.touch(autosign_file) unless File.exist?(autosign_file) | ||
|
||
open(autosign_file, File::RDWR) do |autosign| | ||
# Check that we don't have that host already | ||
found = autosign.readlines.find { |line| line.chomp == certname } | ||
autosign.puts certname unless found | ||
end | ||
logger.debug "Added #{certname} to autosign" | ||
end | ||
|
||
# list of hosts which are now allowed to be installed via autosign | ||
def autosign_list | ||
return [] unless File.exist?(autosign_file) | ||
File.read(autosign_file).split("\n").reject do |v| | ||
v =~ /^\s*#.*|^$/ ## Remove comments and empty lines | ||
end.map do |v| | ||
v.chomp ## Strip trailing spaces | ||
end | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
modules/puppetca_hostname_whitelisting/puppetca_hostname_whitelisting_plugin.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module ::Proxy::PuppetCa::HostnameWhitelisting | ||
class Plugin < ::Proxy::Provider | ||
plugin :puppetca_hostname_whitelisting, ::Proxy::VERSION | ||
|
||
requires :puppetca, ::Proxy::VERSION | ||
default_settings :autosignfile => '/etc/puppet/autosign.conf' | ||
|
||
load_classes ::Proxy::PuppetCa::HostnameWhitelisting::PluginConfiguration | ||
load_dependency_injection_wirings ::Proxy::PuppetCa::HostnameWhitelisting::PluginConfiguration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
require 'test_helper' | ||
require 'puppetca/puppetca_plugin' | ||
require 'puppetca/puppetca' | ||
|
||
class PuppetCAConfigTest < Test::Unit::TestCase | ||
def test_omitted_settings_have_default_values | ||
Proxy::PuppetCa::Plugin.load_test_settings({}) | ||
assert_equal '/var/lib/puppet/ssl', Proxy::PuppetCa::Plugin.settings.ssldir | ||
assert_equal '/etc/puppet/autosign.conf', Proxy::PuppetCa::Plugin.settings.autosignfile | ||
assert_equal 'puppetca_hostname_whitelisting', Proxy::PuppetCa::Plugin.settings.use_provider | ||
end | ||
end |
Oops, something went wrong.