File: proxy_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (347 lines) | stat: -rw-r--r-- 11,677 bytes parent folder | download | duplicates (2)
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
require 'uri'
require 'spec_helper'
require 'puppet/http'

describe Puppet::HTTP::Proxy do
  before(:all) do
    ENV['http_proxy'] = nil
    ENV['HTTP_PROXY'] = nil
  end

  host, port, user, password = 'some.host', 1234, 'user1', 'pAssw0rd'

  def expects_direct_connection_to(http, www)
    expect(http.address).to eq(www.host)
    expect(http.port).to eq(www.port)

    expect(http.proxy_address).to be_nil
    expect(http.proxy_port).to be_nil
    expect(http.proxy_user).to be_nil
    expect(http.proxy_pass).to be_nil
  end

  def expects_proxy_connection_via(http, www, host, port, user, password)
    expect(http.address).to eq(www.host)
    expect(http.port).to eq(www.port)

    expect(http.proxy_address).to eq(host)
    expect(http.proxy_port).to eq(port)
    expect(http.proxy_user).to eq(user)
    expect(http.proxy_pass).to eq(password)
  end

  describe '.proxy' do
    let(:www) { URI::HTTP.build(host: 'www.example.com', port: 80) }

    it 'uses a proxy' do
      Puppet[:http_proxy_host] = host
      Puppet[:http_proxy_port] = port
      Puppet[:http_proxy_user] = user
      Puppet[:http_proxy_password] = password

      http = subject.proxy(www)
      expects_proxy_connection_via(http, www, host, port, user, password)
    end

    it 'connects directly to the server' do
      http = subject.proxy(www)
      expects_direct_connection_to(http, www)
    end

    it 'connects directly to the server when HTTP_PROXY environment variable is set, but server matches no_proxy setting' do
      Puppet[:http_proxy_host] = host
      Puppet[:http_proxy_port] = port
      Puppet[:no_proxy] = www.host

      Puppet::Util.withenv('HTTP_PROXY' => "http://#{host}:#{port}") do
        http = subject.proxy(www)
        expects_direct_connection_to(http, www)
      end
    end

    context 'when setting no_proxy' do
      before :each do
        Puppet[:http_proxy_host] = host
        Puppet[:http_proxy_port] = port
      end

      it 'connects directly to the server when HTTP_PROXY environment variable is set, but server matches no_proxy setting' do
        Puppet[:no_proxy] = www.host

        Puppet::Util.withenv('HTTP_PROXY' => "http://#{host}:#{port}") do
          http = subject.proxy(www)
          expects_direct_connection_to(http, www)
        end
      end

      it 'connects directly to the server when no_proxy matches wildcard domain' do
        Puppet[:no_proxy] = '*.example.com'

        http = subject.proxy(www)
        expects_direct_connection_to(http, www)
      end

      it 'connects directly to the server when no_proxy matches dotted domain' do
        Puppet[:no_proxy] = '.example.com'

        http = subject.proxy(www)
        expects_direct_connection_to(http, www)
      end

      it 'connects directly to the server when no_proxy matches a domain suffix like ruby does' do
        Puppet[:no_proxy] = 'example.com'

        http = subject.proxy(www)
        expects_direct_connection_to(http, www)
      end

      it 'connects directly to the server when no_proxy matches a partial suffix like ruby does' do
        Puppet[:no_proxy] = 'ample.com'

        http = subject.proxy(www)
        expects_direct_connection_to(http, www)
      end

      it 'connects directly to the server when it is a subdomain of no_proxy' do
        Puppet[:no_proxy] = '*.com'

        http = subject.proxy(www)
        expects_direct_connection_to(http, www)
      end

      it 'connects directly to the server when no_proxy is *' do
        Puppet[:no_proxy] = '*'

        http = subject.proxy(www)
        expects_direct_connection_to(http, www)
      end
    end
  end

  describe ".http_proxy_env" do
    it "should return nil if no environment variables" do
      expect(subject.http_proxy_env).to eq(nil)
    end

    it "should return a URI::HTTP object if http_proxy env variable is set" do
      Puppet::Util.withenv('HTTP_PROXY' => host) do
        expect(subject.http_proxy_env).to eq(URI.parse(host))
      end
    end

    it "should return a URI::HTTP object if HTTP_PROXY env variable is set" do
      Puppet::Util.withenv('HTTP_PROXY' => host) do
        expect(subject.http_proxy_env).to eq(URI.parse(host))
      end
    end

    it "should return a URI::HTTP object with .host and .port if URI is given" do
      Puppet::Util.withenv('HTTP_PROXY' => "http://#{host}:#{port}") do
        expect(subject.http_proxy_env).to eq(URI.parse("http://#{host}:#{port}"))
      end
    end

    it "should return nil if proxy variable is malformed" do
      Puppet::Util.withenv('HTTP_PROXY' => 'this is not a valid URI') do
        expect(subject.http_proxy_env).to eq(nil)
      end
    end
  end

  describe ".http_proxy_host" do
    it "should return nil if no proxy host in config or env" do
      expect(subject.http_proxy_host).to eq(nil)
    end

    it "should return a proxy host if set in config" do
      Puppet.settings[:http_proxy_host] = host
      expect(subject.http_proxy_host).to eq(host)
    end

    it "should return nil if set to `none` in config" do
      Puppet.settings[:http_proxy_host] = 'none'
      expect(subject.http_proxy_host).to eq(nil)
    end

    it "uses environment variable before puppet settings" do
      Puppet::Util.withenv('HTTP_PROXY' => "http://#{host}:#{port}") do
        Puppet.settings[:http_proxy_host] = 'not.correct'
        expect(subject.http_proxy_host).to eq(host)
      end
    end
  end

  describe ".http_proxy_port" do
    it "should return a proxy port if set in environment" do
      Puppet::Util.withenv('HTTP_PROXY' => "http://#{host}:#{port}") do
        expect(subject.http_proxy_port).to eq(port)
      end
    end

    it "should return a proxy port if set in config" do
      Puppet.settings[:http_proxy_port] = port
      expect(subject.http_proxy_port).to eq(port)
    end

    it "uses environment variable before puppet settings" do
      Puppet::Util.withenv('HTTP_PROXY' => "http://#{host}:#{port}") do
        Puppet.settings[:http_proxy_port] = 7456
        expect(subject.http_proxy_port).to eq(port)
      end
    end

  end

  describe ".http_proxy_user" do
    it "should return a proxy user if set in environment" do
      Puppet::Util.withenv('HTTP_PROXY' => "http://#{user}:#{password}@#{host}:#{port}") do
        expect(subject.http_proxy_user).to eq(user)
      end
    end

    it "should return a proxy user if set in config" do
      Puppet.settings[:http_proxy_user] = user
      expect(subject.http_proxy_user).to eq(user)
    end

    it "should use environment variable before puppet settings" do
      Puppet::Util.withenv('HTTP_PROXY' => "http://#{user}:#{password}@#{host}:#{port}") do
        Puppet.settings[:http_proxy_user] = 'clownpants'
        expect(subject.http_proxy_user).to eq(user)
      end
    end

  end

  describe ".http_proxy_password" do
    it "should return a proxy password if set in environment" do
      Puppet::Util.withenv('HTTP_PROXY' => "http://#{user}:#{password}@#{host}:#{port}") do
        expect(subject.http_proxy_password).to eq(password)
      end
    end

    it "should return a proxy password if set in config" do
      Puppet.settings[:http_proxy_user] = user
      Puppet.settings[:http_proxy_password] = password
      expect(subject.http_proxy_password).to eq(password)
    end

    it "should use environment variable before puppet settings" do
      Puppet::Util.withenv('HTTP_PROXY' => "http://#{user}:#{password}@#{host}:#{port}") do
        Puppet.settings[:http_proxy_password] = 'clownpants'
        expect(subject.http_proxy_password).to eq(password)
      end
    end

  end

  describe ".no_proxy" do
    no_proxy = '127.0.0.1, localhost'
    it "should use a no_proxy list if set in environment" do
      Puppet::Util.withenv('NO_PROXY' => no_proxy) do
        expect(subject.no_proxy).to eq(no_proxy)
      end
    end

    it "should use a no_proxy list if set in config" do
      Puppet.settings[:no_proxy] = no_proxy
      expect(subject.no_proxy).to eq(no_proxy)
    end

    it "should use environment variable before puppet settings" do
      no_proxy_puppet_setting = '10.0.0.1, localhost'
      Puppet::Util.withenv('NO_PROXY' => no_proxy) do
        Puppet.settings[:no_proxy] = no_proxy_puppet_setting
        expect(subject.no_proxy).to eq(no_proxy)
      end
    end
  end

  describe ".no_proxy?" do
    no_proxy = '127.0.0.1, localhost, mydomain.com, *.otherdomain.com, oddport.com:8080, *.otheroddport.com:8080, .anotherdomain.com, .anotheroddport.com:8080'

    it "should return false if no_proxy does not exist in environment or puppet settings" do
      Puppet::Util.withenv('no_proxy' => nil) do
        dest = 'https://puppetlabs.com'
        expect(subject.no_proxy?(dest)).to be false
      end
    end

    it "should return false if the dest does not match any element in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'https://puppetlabs.com'
        expect(subject.no_proxy?(dest)).to be false
      end
    end

    it "should return true if the dest as an IP does match any element in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://127.0.0.1'
        expect(subject.no_proxy?(dest)).to be true
      end
    end

    it "should return true if the dest as single word does match any element in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://localhost'
        expect(subject.no_proxy?(dest)).to be true
      end
    end

    it "should return true if the dest as standard domain word does match any element in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://mydomain.com'
        expect(subject.no_proxy?(dest)).to be true
      end
    end

    it "should return true if the dest as standard domain with port does match any element in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://oddport.com:8080'
        expect(subject.no_proxy?(dest)).to be true
      end
    end

    it "should return false if the dest is standard domain not matching port" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://oddport.com'
        expect(subject.no_proxy?(dest)).to be false
      end
    end

    it "should return true if the dest does match any wildcarded element in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://sub.otherdomain.com'
        expect(subject.no_proxy?(dest)).to be true
      end
    end

    it "should return true if the dest does match any wildcarded element with port in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://sub.otheroddport.com:8080'
        expect(subject.no_proxy?(dest)).to be true
      end
    end

    it "should return true if the dest does match any domain level (no wildcard) element in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://sub.anotherdomain.com'
        expect(subject.no_proxy?(dest)).to be true
      end
    end

    it "should return true if the dest does match any domain level (no wildcard) element with port in the no_proxy list" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = 'http://sub.anotheroddport.com:8080'
        expect(subject.no_proxy?(dest)).to be true
      end
    end

    it "should work if passed a URI object" do
      Puppet::Util.withenv('no_proxy' => no_proxy) do
        dest = URI.parse('http://sub.otheroddport.com:8080')
        expect(subject.no_proxy?(dest)).to be true
      end
    end
  end
end