summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMatt Bostock <matt@mattbostock.com>2015-11-23 23:45:23 +0000
committerMatt Bostock <matt@mattbostock.com>2016-01-08 11:09:45 +0000
commit97320ab42121a10b76c642b8378c82a888148e4b (patch)
treebf92502d1d5399d8e086be6b39d05552d0911168 /spec
parentef0c13b1afdd5fd339083015d387d669acd67066 (diff)
downloadpuppet-stdlib-97320ab42121a10b76c642b8378c82a888148e4b.tar.gz
puppet-stdlib-97320ab42121a10b76c642b8378c82a888148e4b.tar.bz2
Add a function to validate an x509 RSA key pair
Add a function to validate an x509 RSA certificate and key pair, as commonly used for TLS certificates. The rationale behind this is that we store our TLS certificates and private keys in Hiera YAML files, and poor indentation or formatting in the YAML file could cause a valid certificate to be considered invalid. Will cause the Puppet run to fail if: - an invalid certificate is detected - an invalid RSA key is detected - the certificate does not match the key, i.e. the certificate has not been signed by the supplied key The test certificates I've used in the spec tests were generated using the Go standard library: $ go run $GOROOT/src/crypto/tls/generate_cert.go -host localhost Example output: ==> cache-1.router: Error: Not a valid RSA key: Neither PUB key nor PRIV key:: nested asn1 error at /var/govuk/puppet/modules/nginx/manifests/config/ssl.pp:30 on node cache-1.router.dev.gov.uk
Diffstat (limited to 'spec')
-rwxr-xr-xspec/functions/validate_x509_rsa_key_pair_spec.rb154
1 files changed, 154 insertions, 0 deletions
diff --git a/spec/functions/validate_x509_rsa_key_pair_spec.rb b/spec/functions/validate_x509_rsa_key_pair_spec.rb
new file mode 100755
index 0000000..048d65e
--- /dev/null
+++ b/spec/functions/validate_x509_rsa_key_pair_spec.rb
@@ -0,0 +1,154 @@
+require 'spec_helper'
+
+describe 'validate_x509_rsa_key_pair' do
+
+ let(:valid_cert) do
+ <<EOS
+-----BEGIN CERTIFICATE-----
+MIIC9jCCAeCgAwIBAgIRAK11n3X7aypJ7FPM8UFyAeowCwYJKoZIhvcNAQELMBIx
+EDAOBgNVBAoTB0FjbWUgQ28wHhcNMTUxMTIzMjIzOT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1RrWRNSw9TdvrK6qaDKgTtCTtfRVXAYQXUgq7lSFOTtL3A==
+-----END RSA PRIVATE KEY-----
+EOS
+ end
+
+ let(:valid_cert_but_indented) do
+ valid_cert.gsub(/^/, ' ')
+ end
+
+ let(:valid_key_but_indented) do
+ valid_key.gsub(/^/, ' ')
+ end
+
+ let(:bad_cert) do
+ 'foo'
+ end
+
+ let(:bad_key) do
+ 'bar'
+ end
+
+ context 'function signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(0, 1, 2, 3).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
+
+ context 'valid input' do
+ describe 'valid certificate and key' do
+ it { is_expected.to run.with_params(valid_cert, valid_key) }
+ end
+ end
+
+ context 'bad input' do
+ describe 'valid but indented certificate, valid key' do
+ it { is_expected.to run.with_params(valid_cert_but_indented, valid_key).and_raise_error(Puppet::ParseError, /Not a valid x509 certificate/) }
+ end
+
+ describe 'valid certificate, valid but indented key' do
+ it { is_expected.to run.with_params(valid_cert, valid_key_but_indented).and_raise_error(Puppet::ParseError, /Not a valid RSA key/) }
+ end
+
+ describe 'valid certificate, bad key' do
+ it { is_expected.to run.with_params(valid_cert, bad_key).and_raise_error(Puppet::ParseError, /Not a valid RSA key/) }
+ end
+
+ describe 'bad certificate, valid key' do
+ it { is_expected.to run.with_params(bad_cert, valid_key).and_raise_error(Puppet::ParseError, /Not a valid x509 certificate/) }
+ end
+
+ describe 'validate certificate and key; certficate not signed by key' do
+ it { is_expected.to run.with_params(valid_cert, another_valid_key).and_raise_error(Puppet::ParseError, /Certificate signature does not match supplied key/) }
+ end
+
+ describe 'valid cert and key but arguments in wrong order' do
+ it { is_expected.to run.with_params(valid_key, valid_cert).and_raise_error(Puppet::ParseError, /Not a valid x509 certificate/) }
+ end
+
+ describe 'non-string arguments' do
+ it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(true, true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params("foo", {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(1, "bar").and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params("baz", true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ end
+ end
+end