puppetdb-ruby 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0efa1ff7283edd4cd51d062cd7f0b3a146a011dd
4
- data.tar.gz: 92542f3c6fa618fad69c67755a567d0d1f04a4eb
2
+ SHA256:
3
+ metadata.gz: 352d2c2bcc6b198f6c37d529472db8a5272eef594d82612880110424dbb2d341
4
+ data.tar.gz: c36524430b66a3e1ffdd74006ecdd195ba187ee9dd8deea356037c4900a9cc43
5
5
  SHA512:
6
- metadata.gz: 6d72e69d42b39d1e9bd1c2d773e4c8358e7d430b3f7461420e484c90bbe5987e1343e02e7ec1d163820eea76dd40dec6c4df7faaacbe01e0cbf3fce80041df27
7
- data.tar.gz: 674afb129c0e9890da67bcf9b6ea831bfb749c88b438805f8f45dbd2037f77df13a44f86942bf922da407e238b0fc727c5a51fab4ac42110aa7aa0e8c3cf8bb3
6
+ metadata.gz: 1bc2a9a1f385d1eb4eb203f422ca8d97a70440eb48f1b06baaab698b9b16984db818e507a50d2c6ea9ab8f25f764c15eb36b9d28d868dad4cfa6fd4b617ea745
7
+ data.tar.gz: c0af4327590e03ef0cf73c1abfb0c577d8d108bcd25aebed201fb68dcc5fe65764b3295dcf3ae954d5471512578f59982dd7ea8918e90f1fe0511799acd483a8
@@ -4,8 +4,21 @@ All notable changes to this project will be documented in this file.
4
4
  Each new release typically also includes the latest modulesync defaults.
5
5
  These should not impact the functionality of the module.
6
6
 
7
- ## [v1.1.1](https://github.com/voxpupuli/puppetdb-ruby/tree/v1.1.1) (2017-08-17)
8
- [Full Changelog](https://github.com/voxpupuli/puppetdb-ruby/compare/1.1.0...v1.1.1)
7
+ ## [1.2.0](https://github.com/voxpupuli/puppetdb-ruby/tree/1.2.0) (2019-08-06)
8
+ [Full Changelog](https://github.com/voxpupuli/puppetdb-ruby/compare/1.1.1...1.2.0)
9
+
10
+ **Implemented enhancements:**
11
+
12
+ - Add PE RBAC token support [\#34](https://github.com/voxpupuli/puppetdb-ruby/pull/34) ([seanmil](https://github.com/seanmil))
13
+
14
+ **Closed issues:**
15
+
16
+ - cannot load such file -- puppetdb [\#33](https://github.com/voxpupuli/puppetdb-ruby/issues/33)
17
+ - Attempting to use puppetdb-ruby in a custom function [\#11](https://github.com/voxpupuli/puppetdb-ruby/issues/11)
18
+ - No way to retrieve all facts/nodes [\#8](https://github.com/voxpupuli/puppetdb-ruby/issues/8)
19
+
20
+ ## [1.1.1](https://github.com/voxpupuli/puppetdb-ruby/tree/1.1.1) (2017-08-17)
21
+ [Full Changelog](https://github.com/voxpupuli/puppetdb-ruby/compare/1.1.0...1.1.1)
9
22
 
10
23
  **Fixed bugs:**
11
24
 
data/README.md CHANGED
@@ -40,7 +40,7 @@ Non-SSL:
40
40
  client = PuppetDB::Client.new({:server => 'http://localhost:8080'})
41
41
  ```
42
42
 
43
- SSL:
43
+ SSL with cert-based authentication:
44
44
  ``` ruby
45
45
  client = PuppetDB::Client.new({
46
46
  :server => 'https://localhost:8081',
@@ -51,6 +51,24 @@ client = PuppetDB::Client.new({
51
51
  }})
52
52
  ```
53
53
 
54
+ SSL with PE RBAC token based authentication:
55
+ ``` ruby
56
+ client = PuppetDB::Client.new({
57
+ :server => "https://localhost:8081",
58
+ :token => "my_pe_rbac_token",
59
+ :cacert => "/path/to/cacert.pem",
60
+ })
61
+ ```
62
+
63
+ SSL with PE RBAC token based authentication, using all settings from PE Client Tools configurations:
64
+ ``` ruby
65
+ client = PuppetDB::Client.new()
66
+ ```
67
+
68
+ Note: When using cert-based authentication you must specify the full pem structure. When using token based authentication
69
+ you must NOT provide the pem structure and instead pass ':token' and ':cacert' (or allow them to be read from the
70
+ PE Client Tools configuration).
71
+
54
72
  #### Query API usage
55
73
 
56
74
  The Query Feature allows the user to request data from PuppetDB using the Query endpoints. It defaults to the latest version of the Query Endpoint.
@@ -2,5 +2,7 @@ require 'puppetdb/version'
2
2
  require 'puppetdb/client'
3
3
  require 'puppetdb/query'
4
4
  require 'puppetdb/response'
5
+ require 'puppetdb/error'
6
+ require 'puppetdb/config'
5
7
 
6
8
  module PuppetDB; end
@@ -1,19 +1,18 @@
1
1
  require 'httparty'
2
2
  require 'logger'
3
3
 
4
- module PuppetDB
5
- class APIError < RuntimeError
6
- attr_reader :code, :response
7
- def initialize(response)
8
- @response = response
9
- end
10
- end
4
+ require 'puppetdb/error'
11
5
 
6
+ module PuppetDB
12
7
  class FixSSLConnectionAdapter < HTTParty::ConnectionAdapter
13
8
  def attach_ssl_certificates(http, options)
14
- http.cert = OpenSSL::X509::Certificate.new(File.read(options[:pem]['cert']))
15
- http.key = OpenSSL::PKey::RSA.new(File.read(options[:pem]['key']))
16
- http.ca_file = options[:pem]['ca_file']
9
+ if options[:pem].empty?
10
+ http.ca_file = options[:cacert]
11
+ else
12
+ http.cert = OpenSSL::X509::Certificate.new(File.read(options[:pem]['cert']))
13
+ http.key = OpenSSL::PKey::RSA.new(File.read(options[:pem]['key']))
14
+ http.ca_file = options[:pem]['ca_file']
15
+ end
17
16
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
18
17
  end
19
18
  end
@@ -23,19 +22,6 @@ module PuppetDB
23
22
  attr_reader :use_ssl
24
23
  attr_writer :logger
25
24
 
26
- def hash_get(hash, key)
27
- untouched = hash[key]
28
- return untouched if untouched
29
-
30
- sym = hash[key.to_sym]
31
- return sym if sym
32
-
33
- str = hash[key.to_s]
34
- return str if str
35
-
36
- nil
37
- end
38
-
39
25
  def hash_includes?(hash, *sought_keys)
40
26
  sought_keys.each { |x| return false unless hash.include?(x) }
41
27
  true
@@ -45,12 +31,14 @@ module PuppetDB
45
31
  @logger.debug(msg) if @logger
46
32
  end
47
33
 
48
- def initialize(settings, query_api_version = 4, command_api_version = 1)
34
+ def initialize(settings = {}, query_api_version = 4, command_api_version = 1)
35
+ config = Config.new(settings, load_files: true)
49
36
  @query_api_version = query_api_version
50
37
  @command_api_version = command_api_version
51
38
 
52
- server = hash_get(settings, 'server')
53
- pem = hash_get(settings, 'pem')
39
+ server = config.server
40
+ pem = config['pem'] || {}
41
+ token = config.token
54
42
 
55
43
  scheme = URI.parse(server).scheme
56
44
 
@@ -60,13 +48,14 @@ module PuppetDB
60
48
  end
61
49
 
62
50
  @use_ssl = scheme == 'https'
63
- if @use_ssl && pem
64
- unless hash_includes?(pem, 'key', 'cert', 'ca_file')
65
- error_msg = 'Configuration error: https:// specified but pem is missing or incomplete. It requires cert, key, and ca_file.'
51
+ if @use_ssl
52
+ unless pem.empty? || hash_includes?(pem, 'key', 'cert', 'ca_file')
53
+ error_msg = 'Configuration error: https:// specified with pem, but pem is incomplete. It requires cert, key, and ca_file.'
66
54
  raise error_msg
67
55
  end
68
56
 
69
- self.class.default_options = { pem: pem }
57
+ self.class.default_options = { pem: pem, cacert: config['cacert'] }
58
+ self.class.headers('X-Authentication' => token) if token
70
59
  self.class.connection_adapter(FixSSLConnectionAdapter)
71
60
  end
72
61
 
@@ -74,6 +63,8 @@ module PuppetDB
74
63
  end
75
64
 
76
65
  def raise_if_error(response)
66
+ raise UnauthorizedError, response if response.code == 401
67
+ raise ForbiddenError, response if response.code == 403
77
68
  raise APIError, response if response.code.to_s =~ %r{^[4|5]}
78
69
  end
79
70
 
@@ -0,0 +1,87 @@
1
+ require 'json'
2
+
3
+ class PuppetDB::Config
4
+ def initialize(overrides = nil, load_files = false)
5
+ @overrides = {}
6
+ overrides.each { |k, v| @overrides[k.to_s] = v } unless overrides.nil?
7
+
8
+ @load_files = load_files
9
+ end
10
+
11
+ def load_file(path)
12
+ File.open(path) { |f| JSON.parse(f.read)['puppetdb'] }
13
+ end
14
+
15
+ def puppetlabs_root
16
+ '/etc/puppetlabs'
17
+ end
18
+
19
+ def global_conf
20
+ File.join(puppetlabs_root, 'client-tools', 'puppetdb.conf')
21
+ end
22
+
23
+ def user_root
24
+ File.join(Dir.home, '.puppetlabs')
25
+ end
26
+
27
+ def user_conf
28
+ File.join(user_root, 'client-tools', 'puppetdb.conf')
29
+ end
30
+
31
+ def default_cacert
32
+ "#{puppetlabs_root}/puppet/ssl/certs/ca.pem"
33
+ end
34
+
35
+ def defaults
36
+ {
37
+ 'cacert' => default_cacert,
38
+ 'token-file' => File.join(user_root, 'token')
39
+ }
40
+ end
41
+
42
+ def load_config
43
+ config = defaults
44
+ if @load_files
45
+ if File.exist?(global_conf) && File.readable?(global_conf)
46
+ config = config.merge(load_file(global_conf))
47
+ end
48
+
49
+ if @overrides['config-file']
50
+ config = config.merge(load_file(@overrides['config-file']))
51
+ elsif File.exist?(user_conf) && File.readable?(user_conf)
52
+ config = config.merge(load_file(user_conf))
53
+ end
54
+ end
55
+
56
+ config.merge(@overrides)
57
+ end
58
+
59
+ def config
60
+ @config ||= load_config
61
+ end
62
+
63
+ def load_token
64
+ if @config.include?('token')
65
+ @config['token']
66
+ elsif File.readable?(config['token-file'])
67
+ File.read(config['token-file']).strip
68
+ end
69
+ end
70
+
71
+ def token
72
+ @token ||= load_token
73
+ end
74
+
75
+ def server_urls
76
+ return [config['server']] unless config['server'].nil?
77
+ config['server_urls'] || []
78
+ end
79
+
80
+ def server
81
+ server_urls.first || {}
82
+ end
83
+
84
+ def [](key)
85
+ @config[key]
86
+ end
87
+ end
@@ -0,0 +1,17 @@
1
+ module PuppetDB
2
+ class APIError < RuntimeError
3
+ attr_reader :code, :response
4
+ def initialize(response)
5
+ @response = response
6
+ end
7
+ end
8
+
9
+ class AccessDeniedError < APIError
10
+ end
11
+
12
+ class ForbiddenError < AccessDeniedError
13
+ end
14
+
15
+ class UnauthorizedError < AccessDeniedError
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module PuppetDB
2
- VERSION = '1.1.1'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppetdb-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vox Pupuli
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-08-17 00:00:00.000000000 Z
13
+ date: 2019-08-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: httparty
@@ -121,12 +121,14 @@ files:
121
121
  - README.md
122
122
  - lib/puppetdb.rb
123
123
  - lib/puppetdb/client.rb
124
+ - lib/puppetdb/config.rb
125
+ - lib/puppetdb/error.rb
124
126
  - lib/puppetdb/query.rb
125
127
  - lib/puppetdb/response.rb
126
128
  - lib/puppetdb/version.rb
127
129
  homepage: https://github.com/voxpupuli/puppetdb-ruby
128
130
  licenses:
129
- - apache
131
+ - Apache-2.0
130
132
  metadata: {}
131
133
  post_install_message:
132
134
  rdoc_options: []
@@ -144,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
146
  version: '0'
145
147
  requirements: []
146
148
  rubyforge_project:
147
- rubygems_version: 2.4.5
149
+ rubygems_version: 2.7.7
148
150
  signing_key:
149
151
  specification_version: 4
150
152
  summary: Simple Ruby client library for PuppetDB API