puppetclassify 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +28 -1
  3. data/lib/puppet_https.rb +85 -25
  4. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 523f6019b4d5b7deb67a65980e47f0be7f049b29
4
- data.tar.gz: fc27b32eafa867ffabc950a03ec04ec42d2e5878
3
+ metadata.gz: 7a263281371dfe1516c40c60aa682e5de078a4b7
4
+ data.tar.gz: ff35572dea53621a79790ed5e6bf91b65a8ba257
5
5
  SHA512:
6
- metadata.gz: 7a9c9cc102ffe563c6237adbdd7b9e389c96103147f2b8126761acf73e6c871cbf906499c57541909c24e412b7c5a240dc8ea815116b8fb3b4572e138e2e212a
7
- data.tar.gz: f6667db9fd1432e0852c0624f7d1b2899639501228eb4e5da68af248098d507ff640f67b7d22d5ad4a186c8bf8e846cfcdb170e38ef29a5e2b3213d29af44612
6
+ metadata.gz: c6ef8817dcd5d0c829573a52db46db2d6e5a4645289dae740544604d46b9a433bc817e664a9e98550fc0e30da35985fcb48f0b20983fa1a604e0fbaac6b1f76a
7
+ data.tar.gz: 044d06e59b36d1d86ade763648a8aff7784f5ee645e8cd3ddb26b8dcc07a540bdd2a79c0b0024734c30795d49a9efe547ecc3b7c49219db28fb521fb1b0532cf
data/README.md CHANGED
@@ -25,7 +25,7 @@ Tickets: Open an issue or pull request directly on this repository
25
25
 
26
26
  ## How to use
27
27
 
28
- Here is the basic configuration you'll need to use the puppetclassify class:
28
+ Here is the basic configuration you'll need to use the puppetclassify class with certificate auth:
29
29
 
30
30
  ```ruby
31
31
  require 'puppetclassify'
@@ -41,6 +41,33 @@ classifier_url = 'https://puppetmaster.local:4433/classifier-api'
41
41
  puppetclassify = PuppetClassify.new(classifier_url, auth_info)
42
42
  ```
43
43
 
44
+ You can also use token auth by either supplying a path to a token file:
45
+
46
+ ```ruby
47
+ auth_info = {
48
+ "ca_certificate_path" => "/etc/puppetlabs/puppet/ssl/certs/ca.pem",
49
+ "token_path" => "/home/sam/.puppetlabs/token",
50
+ }
51
+ ```
52
+
53
+ Or by specifying a token string directly:
54
+
55
+ ```ruby
56
+ token = 'eyJhbGciOiJSUzUxM....'
57
+ auth_info = {
58
+ "ca_certificate_path" => "/etc/puppetlabs/puppet/ssl/certs/ca.pem",
59
+ "token" => token,
60
+ }
61
+ ```
62
+
63
+ If you have a token file at `~/.puppetlabs/token` then you can make use of it by not specifying any authentication info, ie:
64
+
65
+ ```ruby
66
+ auth_info = {
67
+ "ca_certificate_path" => "/etc/puppetlabs/puppet/ssl/certs/ca.pem",
68
+ }
69
+ ```
70
+
44
71
  ### Basic case
45
72
 
46
73
  If you are wanting to get all of the groups the classifier knows about:
data/lib/puppet_https.rb CHANGED
@@ -2,38 +2,82 @@ require 'uri'
2
2
  require 'net/https'
3
3
 
4
4
  class PuppetHttps
5
+ attr_reader :auth_method, :token_path
6
+
5
7
  def initialize(settings)
6
8
  # Settings hash:
7
9
  # - ca_certificate_path
8
- # - certificate_path
9
- # - private_key_path
10
- # - read_timeout
10
+ # - certificate_path (optional)
11
+ # - private_key_path (optional)
12
+ # - read_timeout (optional)
13
+ # - token_path (default: $HOME/.puppetlabs/token)
14
+ # - token (optional, takes precedence over token_path)
15
+ #
16
+ # token auth takes precedence over cert auth (in the case that both methods are provided)
17
+
18
+ default_token_path = File.join(ENV['HOME'], '.puppetlabs', 'token')
19
+
20
+ ca_cert_path = settings['ca_certificate_path']
21
+ cert_path = settings['certificate_path']
22
+ pkey_path = settings['private_key_path']
23
+
24
+ @ca_file = settings['ca_certificate_path'] if ca_cert_path and File.exists?(ca_cert_path)
25
+ @read_timeout = settings['read_timeout'] || 90 # A default timeout value in seconds
26
+
27
+ @auth_method = case
28
+ when (settings['token'] or settings['token_path'])
29
+ 'token'
30
+ when (cert_path and pkey_path)
31
+ 'cert'
32
+ when File.exists?(default_token_path)
33
+ 'token'
34
+ else
35
+ nil
36
+ end
37
+
38
+ unless @auth_method
39
+ raise RuntimeError, "No authentication methods available."
40
+ end
41
+
42
+ case @auth_method
43
+ when 'token'
44
+ @token = settings['token']
45
+ @token_path = (settings['token_path'] || default_token_path) unless @token
46
+ # Make sure we have a token and it's not empty
47
+ case
48
+ when (@token and @token.empty?)
49
+ raise RuntimeError, "Received an empty string for token"
50
+ when (not @token and not File.exists?(@token_path))
51
+ raise RuntimeError, "Token file not found at [#{@token_path}]"
52
+ when (not @token and File.zero?(@token_path))
53
+ raise RuntimeError, "Token file at [#{@token_path}] is empty"
54
+ end
55
+ when 'cert'
56
+ if File.exists?(cert_path) and File.exists?(pkey_path)
57
+ @cert = OpenSSL::X509::Certificate.new(File.read(cert_path))
58
+ @key = OpenSSL::PKey::RSA.new(File.read(pkey_path))
59
+ else
60
+ raise RuntimeError, "Certificate auth requested but certificate or private key cannot be found."
61
+ end
62
+ end
63
+
11
64
 
12
- @settings = settings
13
65
  end
14
66
 
15
67
  def make_ssl_request(url, req)
16
68
  connection = Net::HTTP.new(url.host, url.port)
17
- # connection.set_debug_output $stderr
18
- connection.use_ssl = true
19
- connection.ssl_version = :TLSv1
20
- connection.read_timeout = @settings['read_timeout'] || 90 #A default timeout value in seconds
21
69
 
22
- connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
23
- ca_file = @settings['ca_certificate_path']
24
- certpath = @settings['certificate_path']
25
- pkey_path = @settings['private_key_path']
26
-
27
- if File.exists?(ca_file)
28
- connection.ca_file = ca_file
29
- end
70
+ # connection.set_debug_output $stderr
30
71
 
31
- if File.exists?(certpath)
32
- connection.cert = OpenSSL::X509::Certificate.new(File.read(certpath))
33
- end
72
+ connection.use_ssl = true
73
+ connection.ssl_version = :TLSv1
74
+ connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
75
+ connection.ca_file = @ca_file if @ca_file
76
+ connection.read_timeout = @read_timeout
34
77
 
35
- if File.exists?(pkey_path)
36
- connection.key = OpenSSL::PKey::RSA.new(File.read(pkey_path))
78
+ if @auth_method == 'cert'
79
+ connection.cert = @cert
80
+ connection.key = @key
37
81
  end
38
82
 
39
83
  connection.start { |http| http.request(req) }
@@ -41,7 +85,7 @@ class PuppetHttps
41
85
 
42
86
  def put(url, request_body=nil)
43
87
  url = URI.parse(url)
44
- req = Net::HTTP::Put.new(url.path)
88
+ req = Net::HTTP::Put.new(url.path, self.auth_header)
45
89
  req.content_type = 'application/json'
46
90
 
47
91
  unless request_body.nil?
@@ -54,7 +98,7 @@ class PuppetHttps
54
98
  def get(url)
55
99
  url = URI.parse(url)
56
100
  accept = 'application/json'
57
- req = Net::HTTP::Get.new("#{url.path}?#{url.query}", "Accept" => accept)
101
+ req = Net::HTTP::Get.new("#{url.path}?#{url.query}", {"Accept" => accept}.merge(self.auth_header))
58
102
  res = make_ssl_request(url, req)
59
103
  res
60
104
  end
@@ -62,7 +106,7 @@ class PuppetHttps
62
106
  def post(url, request_body=nil)
63
107
  url = URI.parse(url)
64
108
 
65
- request = Net::HTTP::Post.new(url.request_uri)
109
+ request = Net::HTTP::Post.new(url.request_uri, self.auth_header)
66
110
  request.content_type = 'application/json'
67
111
 
68
112
  unless request_body.nil?
@@ -76,10 +120,26 @@ class PuppetHttps
76
120
  def delete(url)
77
121
  url = URI.parse(url)
78
122
 
79
- request = Net::HTTP::Delete.new(url.request_uri)
123
+ request = Net::HTTP::Delete.new(url.request_uri, self.auth_header)
80
124
  request.content_type = 'application/json'
81
125
 
82
126
  res = make_ssl_request(url, request)
83
127
  res
84
128
  end
129
+
130
+ #private
131
+
132
+ def token
133
+ return @token if @token
134
+ if @token_path and File.exists?(@token_path)
135
+ @token = File.read(@token_path)
136
+ return @token
137
+ end
138
+ return nil
139
+ end
140
+
141
+ def auth_header
142
+ token = self.token
143
+ header = token ? {"X-Authentication" => token} : {}
144
+ end
85
145
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppetclassify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
- - Brian Cain
7
+ - Puppet Labs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-30 00:00:00.000000000 Z
11
+ date: 2017-05-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A ruby library to interface with the classifier service
14
- email: brian.cain@puppetlabs.com
14
+ email: info@puppet.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  version: '0'
51
51
  requirements: []
52
52
  rubyforge_project:
53
- rubygems_version: 2.2.2
53
+ rubygems_version: 2.2.5
54
54
  signing_key:
55
55
  specification_version: 4
56
56
  summary: Puppet Classify!