-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathurl_test.rb
363 lines (318 loc) · 12.7 KB
/
url_test.rb
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
require "helper"
require "inspec/fetcher" # TODO: require fetcher/url directly
describe Inspec::Fetcher::Url do
it "registers with the fetchers registry" do
reg = Inspec::Fetcher::Registry.registry
_(reg["url"]).must_equal Inspec::Fetcher::Url
end
describe "testing different urls" do
# We don't use the MockLoader here because it produces tarballs
# with different sha's on each run
let(:expected_shasum) { "98b1ae45059b004178a8eee0c1f6179dcea139c0fd8a69ee47a6f02d97af1f17" }
let(:mock_open) do
m = Minitest::Mock.new
m.expect :meta, { "content-type" => "application/gzip" }
m.expect :read, "fake content"
m
end
let(:git_remote_head_main) do
out = mock
out.stubs(:stdout).returns("main\tHEAD\n")
out.stubs(:exitstatus).returns(0)
out.stubs(:stderr).returns("")
out.stubs(:error!).returns(false)
out.stubs(:run_command).returns(true)
out
end
def expect_git_remote_head_main(remote_url)
Mixlib::ShellOut.expects(:new).returns(git_remote_head_main)
end
it "handles a http url" do
url = "http://chef.io/some.tar.gz"
res = Inspec::Fetcher::Url.resolve(url)
res.expects(:open).returns(mock_open)
_(res).must_be_kind_of Inspec::Fetcher::Url
_(res.resolved_source).must_equal({ url: "http://chef.io/some.tar.gz", sha256: expected_shasum })
end
it "handles a https url" do
url = "https://chef.io/some.tar.gz"
res = Inspec::Fetcher::Url.resolve(url)
res.expects(:open).returns(mock_open)
_(res).must_be_kind_of Inspec::Fetcher::Url
_(res.resolved_source).must_equal({ url: "https://chef.io/some.tar.gz", sha256: expected_shasum })
end
it "handles an https URI" do
uri = URI.parse("https://chef.io/some.tar.gz")
res = Inspec::Fetcher::Url.resolve(uri)
res.expects(:open).returns(mock_open)
_(res).must_be_kind_of Inspec::Fetcher::Url
_(res.resolved_source).must_equal({ url: "https://chef.io/some.tar.gz", sha256: expected_shasum })
end
it "doesnt handle other schemas" do
_(Inspec::Fetcher::Url.resolve("gopher://chef.io/some.tar.gz")).must_be_nil
end
it "only handles URLs" do
_(Inspec::Fetcher::Url.resolve(__FILE__)).must_be_nil
end
%w{https://github.com/chef/inspec
https://github.com/chef/inspec.git
https://github.com/chef/inspec.git
http://github.com/chef/inspec
http://github.com/chef/inspec.git
http://github.com/chef/inspec.git}.each do |github|
it "resolves a github url #{github}" do
expect_git_remote_head_main(github)
res = Inspec::Fetcher::Url.resolve(github)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({ url: "https://github.com/chef/inspec/archive/main.tar.gz", sha256: expected_shasum })
end
end
it "resolves a github url with dot" do
github = "https://github.com/mitre/aws-rds-oracle-mysql-ee-5.7-cis-baseline"
expect_git_remote_head_main(github)
res = Inspec::Fetcher::Url.resolve(github)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({ url: "https://github.com/mitre/aws-rds-oracle-mysql-ee-5.7-cis-baseline/archive/main.tar.gz", sha256: expected_shasum })
end
it "resolves a github branch url" do
github = "https://github.com/hardening-io/tests-os-hardening/tree/2.0"
res = Inspec::Fetcher::Url.resolve(github)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({ url: "https://github.com/hardening-io/tests-os-hardening/archive/2.0.tar.gz", sha256: expected_shasum })
end
it "resolves a github commit url" do
github = "https://github.com/hardening-io/tests-os-hardening/tree/48bd4388ddffde68badd83aefa654e7af3231876"
res = Inspec::Fetcher::Url.resolve(github)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({ url: "https://github.com/hardening-io/tests-os-hardening/archive/48bd4388ddffde68badd83aefa654e7af3231876.tar.gz",
sha256: expected_shasum })
end
%w{https://bitbucket.org/chef/inspec
https://bitbucket.org/chef/inspec.git
https://www.bitbucket.org/chef/inspec.git
http://bitbucket.org/chef/inspec
http://bitbucket.org/chef/inspec.git
http://www.bitbucket.org/chef/inspec.git}.each do |bitbucket|
it "resolves a bitbucket url #{bitbucket}" do
expect_git_remote_head_main(bitbucket)
res = Inspec::Fetcher::Url.resolve(bitbucket)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({ url: "https://bitbucket.org/chef/inspec/get/main.tar.gz", sha256: expected_shasum })
end
end
it "resolves a bitbucket branch url" do
bitbucket = "https://bitbucket.org/chef/inspec/branch/newbranch"
res = Inspec::Fetcher::Url.resolve(bitbucket)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({ url: "https://bitbucket.org/chef/inspec/get/newbranch.tar.gz", sha256: expected_shasum })
end
it "resolves a bitbucket commit url" do
bitbucket = "https://bitbucket.org/chef/inspec/commits/48bd4388ddffde68badd83aefa654e7af3231876"
res = Inspec::Fetcher::Url.resolve(bitbucket)
res.expects(:open).returns(mock_open)
_(res).wont_be_nil
_(res.resolved_source).must_equal({ url: "https://bitbucket.org/chef/inspec/get/48bd4388ddffde68badd83aefa654e7af3231876.tar.gz", sha256: expected_shasum })
end
end
describe "download_automate2_archive_to_temp" do
let(:target) { "https://myurl/file.tar.gz" }
let(:options) do
{
"automate" => {
"ent" => "automate",
"token_type" => "dctoken",
},
"token" => "1234abcd",
"server_type" => "automate2",
"profile" => ["admin", "ssh-baseline", "2.0"],
}
end
let(:subject) { Inspec::Fetcher::Url.resolve(target, options) }
it "downloads tar to tmp file" do
mock = Object.new
mock.stubs(:body).returns("this is the body")
Net::HTTP.expects(:start)
.returns(mock)
path = subject.send(:download_automate2_archive_to_temp)
_(File.read(path)).must_equal "this is the body"
end
it "sets default http options" do
mock = Object.new
mock.stubs(:body).returns("this is the body")
opts = { "chef-delivery-enterprise" => "automate", "x-data-collector-token" => "1234abcd", :use_ssl => true, :verify_mode => 1 }
Net::HTTP.expects(:start)
.with("myurl", 443, opts)
.returns(mock)
subject.send(:download_automate2_archive_to_temp)
end
it "sets insecure http options" do
options["insecure"] = true
mock = Object.new
mock.stubs(:body).returns("this is the body")
opts = { :ssl_verify_mode => 0, "chef-delivery-enterprise" => "automate", "x-data-collector-token" => "1234abcd", :use_ssl => true, :verify_mode => 0 }
Net::HTTP.expects(:start)
.with("myurl", 443, opts)
.returns(mock)
subject.send(:download_automate2_archive_to_temp)
end
end
describe "applied to a valid url (mocked tar.gz)" do
let(:mock_file) { MockLoader.profile_tgz("complete-profile") }
let(:target) { "http://myurl/file.tar.gz" }
let(:subject) { Inspec::Fetcher::Url.resolve(target) }
let(:mock_open) do
m = Minitest::Mock.new
m.expect :meta, { "content-type" => "application/gzip" }
m.expect :read, File.open(mock_file, "rb").read
m
end
let(:mock_dest) do
f = Tempfile.new("url-fetch-test")
f.path
end
it "tries to fetch the file" do
subject.expects(:open).returns(mock_open)
subject.fetch(mock_dest)
end
it "returns the resolved_source hash" do
subject.expects(:open).returns(mock_open)
_(subject.resolved_source[:url]).must_equal(target)
end
end
describe "#http_opts" do
let(:subject) { Inspec::Fetcher::Url.new("fake_url", config) }
describe "when username and password is specified" do
let(:config) { { username: "dummy", password: "dummy" } }
it "returns a hash containing http_basic_authentication setting" do
_(subject.send(:http_opts)[:http_basic_authentication]).must_equal %w{dummy dummy}
end
end
describe "when only password is specified" do
let(:config) { { password: "dummy" } }
it "returns a hash containing http_basic_authentication setting as nil" do
_(subject.send(:http_opts)[:http_basic_authentication]).must_be_nil
end
end
describe "when insecure is specified" do
let(:config) { { "insecure" => true } }
it "returns a hash containing an ssl_verify_mode setting" do
_(subject.send(:http_opts)[:ssl_verify_mode]).must_equal OpenSSL::SSL::VERIFY_NONE
end
end
describe "when insecure is not specific" do
let(:config) { {} }
it "returns a hash that does not contain an ssl_verify_mode setting" do
_(subject.send(:http_opts).key?(:ssl_verify_mode)).must_equal false
end
end
describe "when the server is an automate server using dctoken" do
describe "when the config is properly populated" do
let(:config) do
{
"server_type" => "automate",
"automate" => {
"ent" => "my_ent",
"token_type" => "dctoken",
},
"token" => "my_token",
}
end
it "returns a properly formatted headers hash" do
headers = subject.send(:http_opts)
_(headers["chef-delivery-enterprise"]).must_equal "my_ent"
_(headers["x-data-collector-token"]).must_equal "my_token"
end
end
# rubocop:disable Style/BlockDelimiters
describe "when the enterprise is not supplied" do
it "raises an exception" do
_ {
config = {
"server_type" => "automate",
"automate" => { "token_type" => "dctoken" },
"token" => "my_token",
}
Inspec::Fetcher::Url.new("fake_url", config).send(:http_opts)
}.must_raise Inspec::FetcherFailure
end
end
describe "when the token is not supplied" do
it "raises an exception" do
_ {
config = {
"server_type" => "automate",
"automate" => {
"ent" => "my_ent",
"token_type" => "dctoken",
},
}
Inspec::Fetcher::Url.new("fake_url", config).send(:http_opts)
}.must_raise Inspec::FetcherFailure
end
end
end
describe "when the server is an automate server not using dctoken" do
describe "when the config is properly populated" do
let(:config) do
{
"server_type" => "automate",
"automate" => {
"ent" => "my_ent",
"token_type" => "usertoken",
},
"user" => "my_user",
"token" => "my_token",
}
end
it "returns a properly formatted headers hash" do
headers = subject.send(:http_opts)
_(headers["chef-delivery-enterprise"]).must_equal "my_ent"
_(headers["chef-delivery-user"]).must_equal "my_user"
_(headers["chef-delivery-token"]).must_equal "my_token"
end
end
describe "when the user is not supplied" do
it "raises an exception" do
_ {
config = {
"server_type" => "automate",
"automate" => {
"ent" => "my_ent",
"token_type" => "usertoken",
},
"token" => "my_token",
}
Inspec::Fetcher::Url.new("fake_url", config).send(:http_opts)
}.must_raise Inspec::FetcherFailure
end
end
describe "when the token is not supplied" do
it "raises an exception" do
_ {
config = {
"server_type" => "automate",
"automate" => {
"ent" => "my_ent",
"token_type" => "usertoken",
},
"user" => "my_user",
}
Inspec::Fetcher::Url.new("fake_url", config).send(:http_opts)
}.must_raise Inspec::FetcherFailure
end
end
end
describe "when only a token is supplied" do
let(:config) { { "token" => "my_token" } }
it "returns a hash containing an Authorization header" do
_(subject.send(:http_opts)["Authorization"]).must_equal "Bearer my_token"
end
end
end
end