blob: 24101dbe7ac1d604a861b82d961bd0fd8162733a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#
# Author:: Daniel DeLeo (<dan@getchef.com>)
# Copyright:: Copyright (c) 2014 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'spec_helper'
require 'chef/knife/ssl_fetch'
describe Chef::Knife::SslFetch do
let(:name_args) { [] }
let(:stdout_io) { StringIO.new }
let(:stderr_io) { StringIO.new }
def stderr
stderr_io.string
end
def stdout
stdout_io.string
end
subject(:ssl_fetch) do
s = Chef::Knife::SslFetch.new
s.name_args = name_args
allow(s.ui).to receive(:stdout).and_return(stdout_io)
allow(s.ui).to receive(:stderr).and_return(stderr_io)
s
end
context "when no arguments are given" do
before do
Chef::Config.chef_server_url = "https://example.com:8443/chef-server"
end
it "uses the chef_server_url as the host to fetch" do
expect(ssl_fetch.host).to eq("example.com")
expect(ssl_fetch.port).to eq(8443)
end
end
context "when a specific URI is given" do
let(:name_args) { %w{https://example.test:10443/foo} }
it "fetchs the SSL configuration against the given host" do
expect(ssl_fetch.host).to eq("example.test")
expect(ssl_fetch.port).to eq(10443)
end
end
context "when an invalid URI is given" do
let(:name_args) { %w{foo.test} }
it "prints an error and exits" do
expect { ssl_fetch.run }.to raise_error(SystemExit)
expected_stdout=<<-E
USAGE: knife ssl fetch [URL] (options)
E
expected_stderr=<<-E
ERROR: Given URI: `foo.test' is invalid
E
expect(stdout_io.string).to eq(expected_stdout)
expect(stderr_io.string).to eq(expected_stderr)
end
context "and its malformed enough to make URI.parse barf" do
let(:name_args) { %w{ftp://lkj\\blah:example.com/blah} }
it "prints an error and exits" do
expect { ssl_fetch.run }.to raise_error(SystemExit)
expected_stdout=<<-E
USAGE: knife ssl fetch [URL] (options)
E
expected_stderr=<<-E
ERROR: Given URI: `#{name_args[0]}' is invalid
E
expect(stdout_io.string).to eq(expected_stdout)
expect(stderr_io.string).to eq(expected_stderr)
end
end
end
describe "normalizing CNs for use as paths" do
it "normalizes '*' to 'wildcard'" do
expect(ssl_fetch.normalize_cn("*.example.com")).to eq("wildcard_example_com")
end
it "normalizes non-alnum and hyphen characters to underscores" do
expect(ssl_fetch.normalize_cn("Billy-Bob's Super Awesome CA!")).to eq("Billy-Bob_s_Super_Awesome_CA_")
end
end
describe "fetching the remote cert chain" do
let(:name_args) { %w{https://foo.example.com:8443} }
let(:tcp_socket) { double(TCPSocket) }
let(:ssl_socket) { double(OpenSSL::SSL::SSLSocket) }
let(:self_signed_crt_path) { File.join(CHEF_SPEC_DATA, "trusted_certs", "example.crt") }
let(:self_signed_crt) { OpenSSL::X509::Certificate.new(File.read(self_signed_crt_path)) }
let(:trusted_certs_dir) { Dir.mktmpdir }
def run
ssl_fetch.run
rescue Exception
puts "OUT: #{stdout_io.string}"
puts "ERR: #{stderr_io.string}"
raise
end
before do
Chef::Config.trusted_certs_dir = trusted_certs_dir
expect(TCPSocket).to receive(:new).with("foo.example.com", 8443).and_return(tcp_socket)
expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(tcp_socket, ssl_fetch.noverify_peer_ssl_context).and_return(ssl_socket)
expect(ssl_socket).to receive(:connect)
expect(ssl_socket).to receive(:peer_cert_chain).and_return([self_signed_crt])
end
after do
FileUtils.rm_rf(trusted_certs_dir)
end
it "fetches the cert chain and writes the certs to the trusted_certs_dir" do
run
stored_cert_path = File.join(trusted_certs_dir, "example_local.crt")
expect(File).to exist(stored_cert_path)
expect(File.read(stored_cert_path)).to eq(File.read(self_signed_crt_path))
end
end
end
|