|
| 1 | +require 'spec_helper' |
| 2 | +require 'conjur/config' |
| 3 | +require 'conjur/command/rspec/output_matchers' |
| 4 | + |
| 5 | +describe Conjur::Config do |
| 6 | + include_context "fresh config" |
| 7 | + |
| 8 | + describe ".default_config_files" do |
| 9 | + subject { Conjur::Config.default_config_files } |
| 10 | + let(:homedir) { '/src/conjur-api' } |
| 11 | + around do |example| |
| 12 | + realhome = ENV.delete 'HOME' |
| 13 | + ENV['HOME'] = homedir |
| 14 | + example.run |
| 15 | + ENV['HOME'] = realhome |
| 16 | + end |
| 17 | + |
| 18 | + let(:deprecation_warning) { /\.conjurrc/ } |
| 19 | + |
| 20 | + shared_examples "no deprecation warning" do |
| 21 | + it "does not issue a deprecation warning" do |
| 22 | + expect { subject }.to_not write(deprecation_warning).to(:stderr) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + context "when CONJURRC is not set" do |
| 27 | + around do |example| |
| 28 | + oldrc = ENV.delete 'CONJURRC' |
| 29 | + |
| 30 | + example.run |
| 31 | + |
| 32 | + ENV['CONJURRC'] = oldrc |
| 33 | + end |
| 34 | + |
| 35 | + it { is_expected.to include('/etc/conjur.conf') } |
| 36 | + it { is_expected.to include("#{homedir}/.conjurrc") } |
| 37 | + |
| 38 | + before do |
| 39 | + allow(File).to receive(:expand_path).and_call_original |
| 40 | + allow(File).to receive(:expand_path).with('.conjurrc').and_return '.conjurrc' |
| 41 | + end |
| 42 | + |
| 43 | + context "When .conjurrc is present" do |
| 44 | + before { allow(File).to receive(:file?).with('.conjurrc').and_return true } |
| 45 | + it "Issues a deprecation warning" do |
| 46 | + expect { subject }.to write(deprecation_warning).to(:stderr) |
| 47 | + end |
| 48 | + |
| 49 | + it "doesn't use the file" do |
| 50 | + expect(subject).to_not include '.conjurrc' |
| 51 | + end |
| 52 | + |
| 53 | + context "but the current directory is home" do |
| 54 | + before do |
| 55 | + allow(File).to receive(:expand_path).and_call_original |
| 56 | + allow(File).to receive(:expand_path).and_call_original |
| 57 | + allow(File).to receive(:expand_path).with('.conjurrc').and_return("#{homedir}/.conjurrc") |
| 58 | + end |
| 59 | + |
| 60 | + include_examples "no deprecation warning" |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + context "When .conjurrc is missing" do |
| 65 | + before { allow(File).to receive(:file?).with('.conjurrc').and_return false } |
| 66 | + include_examples "no deprecation warning" |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context "when CONJURRC is set but not available" do |
| 71 | + oldrc = ENV['CONJURRC'] |
| 72 | + |
| 73 | + before { ENV['CONJURRC']='stub_conjurrc' } |
| 74 | + |
| 75 | + it { is_expected.to include('/etc/conjur.conf') } |
| 76 | + it { is_expected.not_to include("#{homedir}/.conjurrc") } |
| 77 | + it { is_expected.to include('stub_conjurrc') } |
| 78 | + |
| 79 | + include_examples "no deprecation warning" |
| 80 | + ENV['CONJURRC'] = oldrc |
| 81 | + end |
| 82 | + |
| 83 | + context "when CONJURRC is set to conjurrc" do |
| 84 | + oldrc = ENV['CONJURRC'] |
| 85 | + |
| 86 | + before { ENV['CONJURRC']='conjurrc' } |
| 87 | + |
| 88 | + before { allow(File).to receive(:file?).with('conjurrc').and_return true } |
| 89 | + it { is_expected.to include('/etc/conjur.conf') } |
| 90 | + it { is_expected.to include('conjurrc') } |
| 91 | + it { is_expected.not_to include("#{homedir}/conjurrc") } |
| 92 | + |
| 93 | + include_examples "no deprecation warning" |
| 94 | + ENV['CONJURRC'] = oldrc |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + let(:load!) { Conjur::Config.load([ File.expand_path('conjurrc', File.dirname(__FILE__)) ]) } |
| 99 | + let(:cert_path) { File.expand_path('conjur-ci.pem', File.dirname(__FILE__)) } |
| 100 | + |
| 101 | + describe "#load" do |
| 102 | + it "resolves the cert_file" do |
| 103 | + load! |
| 104 | + |
| 105 | + expect(Conjur::Config[:cert_file]).to eq(cert_path) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + describe "#apply" do |
| 110 | + before { |
| 111 | + allow(OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE).to receive(:add_file) |
| 112 | + allow(File).to receive(:open) |
| 113 | + } |
| 114 | + |
| 115 | + context "ssl_certificate string" do |
| 116 | + let(:ssl_certificate) do |
| 117 | + """-----BEGIN CERTIFICATE----- |
| 118 | +MIIDPjCCAiagAwIBAgIVAKW1gdmOFrXt6xB0iQmYQ4z8Pf+kMA0GCSqGSIb3DQEB |
| 119 | +CwUAMD0xETAPBgNVBAoTCGN1Y3VtYmVyMRIwEAYDVQQLEwlDb25qdXIgQ0ExFDAS |
| 120 | +BgNVBAMTC2N1a2UtbWFzdGVyMB4XDTE1MTAwNzE2MzAwNloXDTI1MTAwNDE2MzAw |
| 121 | +NlowFjEUMBIGA1UEAwwLY3VrZS1tYXN0ZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IB |
| 122 | +DwAwggEKAoIBAQC9e8bGIHOLOypKA4lsLcAOcDLAq+ICuVxn9Vg0No0m32Ok/K7G |
| 123 | +uEGtlC8RidObntblUwqdX2uP7mqAQm19j78UTl1KT97vMmmFrpVZ7oQvEm1FUq3t |
| 124 | +FBmJglthJrSbpdZjLf7a7eL1NnunkfBdI1DK9QL9ndMjNwZNFbXhld4fC5zuSr/L |
| 125 | +PxawSzTEsoTaB0Nw0DdRowaZgrPxc0hQsrj9OF20gTIJIYO7ctZzE/JJchmBzgI4 |
| 126 | +CdfAYg7zNS+0oc0ylV0CWMerQtLICI6BtiQ482bCuGYJ00NlDcdjd3w+A2cj7PrH |
| 127 | +wH5UhtORL5Q6i9EfGGUCDbmfpiVD9Bd3ukbXAgMBAAGjXDBaMA4GA1UdDwEB/wQE |
| 128 | +AwIFoDAdBgNVHQ4EFgQU2jmj7l5rSw0yVb/vlWAYkK/YBwkwKQYDVR0RBCIwIIIL |
| 129 | +Y3VrZS1tYXN0ZXKCCWxvY2FsaG9zdIIGY29uanVyMA0GCSqGSIb3DQEBCwUAA4IB |
| 130 | +AQBCepy6If67+sjuVnT9NGBmjnVaLa11kgGNEB1BZQnvCy0IN7gpLpshoZevxYDR |
| 131 | +3DnPAetQiZ70CSmCwjL4x6AVxQy59rRj0Awl9E1dgFTYI3JxxgLsI9ePdIRVEPnH |
| 132 | +dhXqPY5ZIZhvdHlLStjsXX7laaclEtMeWfSzxe4AmP/Sm/er4ks0gvLQU6/XJNIu |
| 133 | +RnRH59ZB1mZMsIv9Ii790nnioYFR54JmQu1JsIib77ZdSXIJmxAtraJSTLcZbU1E |
| 134 | ++SM3XCE423Xols7onyluMYDy3MCUTFwoVMRBcRWCAk5gcv6XvZDfLi6Zwdne6x3Y |
| 135 | +bGenr4vsPuSFsycM03/EcQDT |
| 136 | +-----END CERTIFICATE----- |
| 137 | +""" |
| 138 | + end |
| 139 | + let(:certificate){ double('Certificate') } |
| 140 | + before{ |
| 141 | + Conjur::Config.class_variable_set('@@attributes', {'ssl_certificate' => ssl_certificate}) |
| 142 | + } |
| 143 | + |
| 144 | + it 'trusts the certificate string' do |
| 145 | + expect(OpenSSL::X509::Certificate).to receive(:new).with(ssl_certificate).once.and_return certificate |
| 146 | + expect(OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE).to receive(:add_cert).with(certificate).once |
| 147 | + Conjur::Config.apply |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + context "cert_file" do |
| 152 | + let(:cert_file) { "/path/to/cert.pem" } |
| 153 | + before { |
| 154 | + Conjur::Config.class_variable_set("@@attributes", { 'cert_file' => cert_file }) |
| 155 | + } |
| 156 | + |
| 157 | + it "trusts the cert_file" do |
| 158 | + expect(OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE).to receive(:add_file).with cert_file |
| 159 | + Conjur::Config.apply |
| 160 | + end |
| 161 | + |
| 162 | + it "propagates the cert_file to Configuration.cert_file" do |
| 163 | + Conjur::Config.apply |
| 164 | + expect(Conjur.configuration.cert_file).to eq(cert_file) |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + it "shadows rc with envars" do |
| 169 | + url = 'https://other-conjur.example.com' |
| 170 | + ENV['CONJUR_APPLIANCE_URL'] = url |
| 171 | + load! |
| 172 | + Conjur::Config.apply |
| 173 | + expect(Conjur.configuration.appliance_url).to eq(url) |
| 174 | + end |
| 175 | + end |
| 176 | +end |
0 commit comments