puppet-rest 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +7 -0
  3. data/.pryrc +58 -0
  4. data/Gemfile +10 -0
  5. data/Gemfile.lock +73 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.md +81 -0
  8. data/Rakefile +3 -0
  9. data/lib/puppet-rest.rb +43 -0
  10. data/lib/puppet-rest/db/client.rb +42 -0
  11. data/lib/puppet-rest/db/config.rb +83 -0
  12. data/lib/puppet-rest/db/connection.rb +26 -0
  13. data/lib/puppet-rest/db/connection/fact-names.rb +9 -0
  14. data/lib/puppet-rest/db/connection/facts.rb +11 -0
  15. data/lib/puppet-rest/db/connection/nodes.rb +32 -0
  16. data/lib/puppet-rest/db/connection/resources.rb +11 -0
  17. data/lib/puppet-rest/db/entities/base.rb +100 -0
  18. data/lib/puppet-rest/db/entities/fact.rb +9 -0
  19. data/lib/puppet-rest/db/entities/node.rb +22 -0
  20. data/lib/puppet-rest/db/entities/resource.rb +12 -0
  21. data/lib/puppet-rest/db/request.rb +74 -0
  22. data/lib/puppet-rest/error.rb +42 -0
  23. data/lib/puppet-rest/identity_map.rb +4 -0
  24. data/lib/puppet-rest/monkey_patches/array.rb +7 -0
  25. data/lib/puppet-rest/monkey_patches/enumerable.rb +11 -0
  26. data/lib/puppet-rest/monkey_patches/hash.rb +49 -0
  27. data/lib/puppet-rest/monkey_patches/mash.rb +219 -0
  28. data/lib/puppet-rest/pe/client.rb +42 -0
  29. data/lib/puppet-rest/pe/config.rb +93 -0
  30. data/lib/puppet-rest/pe/connection.rb +25 -0
  31. data/lib/puppet-rest/pe/connection/ca_cert.rb +10 -0
  32. data/lib/puppet-rest/pe/connection/catalog.rb +10 -0
  33. data/lib/puppet-rest/pe/connection/node.rb +10 -0
  34. data/lib/puppet-rest/pe/entities/base.rb +100 -0
  35. data/lib/puppet-rest/pe/entities/catalog.rb +16 -0
  36. data/lib/puppet-rest/pe/entities/node.rb +9 -0
  37. data/lib/puppet-rest/pe/request.rb +77 -0
  38. data/lib/puppet-rest/response/client_error.rb +33 -0
  39. data/lib/puppet-rest/response/parse_json.rb +26 -0
  40. data/lib/puppet-rest/version.rb +3 -0
  41. data/puppet-rest.gemspec +28 -0
  42. data/spec/certificates/ca.pem +35 -0
  43. data/spec/certificates/cert.pem +35 -0
  44. data/spec/certificates/pk.pem +51 -0
  45. data/spec/spec_helper.rb +9 -0
  46. metadata +143 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTFlMDU2MmFhMzE0ZmQxMjk4Y2YyNGMxNjExZmIyNmNlMzhkNWYxMw==
5
+ data.tar.gz: !binary |-
6
+ YzNmOTVkZTVjY2I1MTM2MzdjNGE4ZmY4MGNlNjk5ODM0MTAyYWQzYQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MmI1ZDkwNzNhOTMwZTc1NjVkMTgwMzE2N2IxNDJmZjdhZmRjNGFkNzlmZmU1
10
+ NjQ4MjUzMzVjNzg5NjUxMjk1MWEyYTJlYzQzYmY0NTE2MDc2OThmODU0MzAy
11
+ ZWNmMzY0Yjc5MmYxMDIwMGE2MzZmNDA4YTVlN2JmZjkxMjFmMTA=
12
+ data.tar.gz: !binary |-
13
+ OGUwYzc5OWYxMzhkYzg1MmU5MDYwZjkzZDU3YzQxZGZiNWUzZDQ3NDgxMmY2
14
+ Y2UzMjhlZWI0OGM4NjE3NGNmYjEwYWNiYTFjNTUxMzBmZWM1NTJmYjM1MmQ3
15
+ NzE0MGNkOWY4ZTk4ZGE0OGY3ZjE1ZDgxMDVhNGRjNjNlM2M4YjU=
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ .bundle
2
+ vendor/
3
+ bin/
4
+ *.log
5
+ spec/internal/log
6
+ .ruby*
7
+ .rspec
data/.pryrc ADDED
@@ -0,0 +1,58 @@
1
+ require 'listen'
2
+
3
+ listener = Listen.to('./lib', only: /\.rb$/) do |modified, added, removed|
4
+ modified.each do |m|
5
+ puts "[modified] [code] Reloading: #{m} - #{load(m)}"
6
+ end
7
+ added.each do |a|
8
+ puts "[added] [code] Loading: #{a} - #{load(a)}"
9
+ end
10
+ puts "[removed] #{removed}" unless removed.empty?
11
+ end
12
+
13
+ listener.start # not blocking
14
+ #listener.only /\.(rb|yml)$/
15
+
16
+ require 'rake'
17
+
18
+ load 'Rakefile'
19
+
20
+ cert_folder = File.dirname(__FILE__) + '/spec/certificates/'
21
+ $key = OpenSSL::PKey::RSA.new(IO.binread(cert_folder + 'pk.pem'))
22
+ $cert = OpenSSL::X509::Certificate.new(IO.binread(cert_folder + 'cert.pem'))
23
+ $ca_cert = cert_folder + 'ca.pem'
24
+
25
+ def get_pe_api
26
+ PuppetRestClient::PE::Connection.new(
27
+ :server_url => 'https://learning.puppetlabs.vm:8140',
28
+ :connection_options => {
29
+ :request => {
30
+ :open_timeout => 30
31
+ },
32
+ :ssl => {
33
+ :client_key => $key,
34
+ :client_cert => $cert,
35
+ :ca_file => $ca_cert,
36
+ :verify => true,
37
+ #Additional options: vendor/jruby/1.9/gems/faraday-0.8.8/lib/faraday/adapter/net_http.rb
38
+ }
39
+ }
40
+ )
41
+ end
42
+
43
+ def get_db_api
44
+ PuppetRestClient::DB::Connection.new(
45
+ :server_url => 'https://learning.puppetlabs.vm:8081',
46
+ :connection_options => {
47
+ :request => {
48
+ :open_timeout => 30
49
+ },
50
+ :ssl => {
51
+ :client_key => $key,
52
+ :client_cert => $cert,
53
+ :ca_file => $ca_cert,
54
+ :verify => true
55
+ }
56
+ }
57
+ )
58
+ end
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test, :development do
6
+ gem 'pry'
7
+ gem 'listen'
8
+ gem 'shoulda'
9
+ gem 'shoulda-matchers'
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ puppet-rest (0.0.3)
5
+ faraday (~> 0.8.8)
6
+ multi_json
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (4.2.4)
12
+ i18n (~> 0.7)
13
+ json (~> 1.7, >= 1.7.7)
14
+ minitest (~> 5.1)
15
+ thread_safe (~> 0.3, >= 0.3.4)
16
+ tzinfo (~> 1.1)
17
+ coderay (1.1.0)
18
+ diff-lcs (1.2.5)
19
+ faraday (0.8.11)
20
+ multipart-post (~> 1.2.0)
21
+ ffi (1.9.10)
22
+ i18n (0.7.0)
23
+ json (1.8.3)
24
+ listen (3.0.3)
25
+ rb-fsevent (>= 0.9.3)
26
+ rb-inotify (>= 0.9)
27
+ method_source (0.8.2)
28
+ minitest (5.8.1)
29
+ multi_json (1.11.2)
30
+ multipart-post (1.2.0)
31
+ pry (0.10.3)
32
+ coderay (~> 1.1.0)
33
+ method_source (~> 0.8.1)
34
+ slop (~> 3.4)
35
+ rake (10.4.2)
36
+ rb-fsevent (0.9.6)
37
+ rb-inotify (0.9.5)
38
+ ffi (>= 0.5.0)
39
+ rspec (3.3.0)
40
+ rspec-core (~> 3.3.0)
41
+ rspec-expectations (~> 3.3.0)
42
+ rspec-mocks (~> 3.3.0)
43
+ rspec-core (3.3.2)
44
+ rspec-support (~> 3.3.0)
45
+ rspec-expectations (3.3.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.3.0)
48
+ rspec-mocks (3.3.2)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.3.0)
51
+ rspec-support (3.3.0)
52
+ shoulda (3.5.0)
53
+ shoulda-context (~> 1.0, >= 1.0.1)
54
+ shoulda-matchers (>= 1.4.1, < 3.0)
55
+ shoulda-context (1.2.1)
56
+ shoulda-matchers (2.8.0)
57
+ activesupport (>= 3.0.0)
58
+ slop (3.6.0)
59
+ thread_safe (0.3.5)
60
+ tzinfo (1.2.2)
61
+ thread_safe (~> 0.1)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ listen
68
+ pry
69
+ puppet-rest!
70
+ rake
71
+ rspec
72
+ shoulda
73
+ shoulda-matchers
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 CloudHealth Tech
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ Puppet Ruby REST Client
2
+ ========
3
+
4
+ Interact with Puppet and PuppetDB through their REST Apis
5
+
6
+ Certificates
7
+ ---------
8
+
9
+ * On Puppet Master: `sudo puppet cert --generate learning.puppetlabs.vm`
10
+ * Copy the 3 files to a remote computer:
11
+ * /etc/puppetlabs/puppet/ssl/private_keys/learning.puppetlabs.vm.pem
12
+ * /etc/puppetlabs/puppet/ssl/certs/learning.puppetlabs.vm.pem
13
+ * /etc/puppetlabs/puppet/ssl/certs/ca.pem
14
+ * Test 2015.02+
15
+ * Puppet Enterprise
16
+ * `curl -X GET --cacert ca.pem --cert learning.puppetlabs.vm.cert.pem --key learning.puppetlabs.vm.priv-key.pem -H 'Accept: pson' 'https://learning.puppetlabs.vm:8140/puppet/v3/node/learning.puppetlabs.vm?environment=production'`
17
+ * PuppetDB
18
+ * `curl -X GET -H 'Accept: application/json' --cacert ca.pem --cert learning.puppetlabs.vm.cert.pem --key learning.puppetlabs.vm.priv-key.pem 'https://learning.puppetlabs.vm:8081/pdb/query/v4/facts'`
19
+ * Test Legacy
20
+ * Puppet Enterprise
21
+ * `curl -X GET --cacert ca.pem --cert learning.puppetlabs.vm.cert.pem --key learning.puppetlabs.vm.priv-key.pem 'https://learning.puppetlabs.vm:8140/v2/catalogs/learning.puppetlabs.vm'`
22
+ * PuppetDB
23
+ * `curl -X GET --cacert ca.pem --cert learning.puppetlabs.vm.cert.pem --key learning.puppetlabs.vm.priv-key.pem 'https://learning.puppetlabs.vm:8081/v2/facts'`
24
+
25
+ Configure PuppetDB
26
+ --------
27
+
28
+ * Change PuppetDB Listen Directive
29
+ * If puppet is not managing puppetdb:
30
+ * Reference: http://docs.puppetlabs.com/puppetdb/1.3/configure.html#host
31
+ * Edit /etc/puppetlabs/puppetdb/conf.d/jetty.ini to listen on 0.0.0.0
32
+ * /etc/init.d/pe-puppetdb restart
33
+ * Otherwise, change through the puppet portal
34
+ * Add `listen_host` to `pe_puppetdb` class with value of `0.0.0.0`
35
+ * Force a puppet run to change in /etc/puppetlabs/puppetdb/conf.d/jetty.ini
36
+ * If PuppetDB is using its own certs, and not the Puppet Master's:
37
+ * sudo /opt/puppet/sbin/puppetdb-ssl-setup -f
38
+ * sudo /etc/init.d/pe-puppetdb restart
39
+ * (Wait a few min for it to actually restart)
40
+ * Make sure your ec2 security groups have 8081 open between your internal instances
41
+
42
+ Configure Puppet Master
43
+ --------
44
+
45
+ * Edit /etc/puppetlabs/puppet/auth.conf, and change:
46
+
47
+ ```
48
+ path ~ ^/catalog/([^/]+)$
49
+ method find
50
+ auth yes
51
+ allow $1
52
+ ```
53
+
54
+ to
55
+
56
+ <pre>
57
+ path ~ ^/catalog/([^/]+)$
58
+ method find
59
+ auth yes
60
+ allow $1<b>, aggregator</b>
61
+ </pre>
62
+
63
+ Useful Links
64
+ ---------
65
+
66
+ * http://docs.puppetlabs.com/puppetdb/latest/install_from_source.html
67
+ * http://docs.puppetlabs.com/puppetdb/latest/configure.html#certificate-whitelist
68
+ * http://docs.puppetlabs.com/puppetdb/1.5/api/query/curl.html
69
+ * http://docs.puppetlabs.com/guides/inventory_service.html
70
+
71
+ About
72
+ ---------
73
+
74
+ puppet-rest was inspired by [spice](https://github.com/danryan/spice) and follows its framework.
75
+
76
+ Copyright
77
+ ---------
78
+
79
+ Copyright (c) 2013 CloudHealth Tech. See LICENSE.txt for further details.
80
+
81
+ Puppet, PuppetDB and related trademarks are Copyright (c) 2005-2013 Puppet Labs Inc. Puppet and PuppetDB are released under the Apache 2.0 license.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require_relative 'lib/puppet-rest'
@@ -0,0 +1,43 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ module PuppetRestClient
5
+ module PE; end
6
+ module DB; end
7
+ end
8
+
9
+ require_relative 'puppet-rest/monkey_patches/mash'
10
+ require_relative 'puppet-rest/monkey_patches/hash'
11
+ require_relative 'puppet-rest/monkey_patches/array'
12
+ require_relative 'puppet-rest/monkey_patches/enumerable'
13
+
14
+ require_relative 'puppet-rest/version'
15
+ require_relative 'puppet-rest/error'
16
+
17
+ require_relative 'puppet-rest/identity_map'
18
+ require_relative 'puppet-rest/response/client_error'
19
+ require_relative 'puppet-rest/response/parse_json'
20
+
21
+ require_relative 'puppet-rest/pe/config'
22
+ require_relative 'puppet-rest/pe/request'
23
+ require_relative 'puppet-rest/pe/entities/base'
24
+ require_relative 'puppet-rest/pe/entities/node'
25
+ require_relative 'puppet-rest/pe/entities/catalog'
26
+ require_relative 'puppet-rest/pe/connection/node'
27
+ require_relative 'puppet-rest/pe/connection/catalog'
28
+ require_relative 'puppet-rest/pe/connection/ca_cert'
29
+ require_relative 'puppet-rest/pe/connection'
30
+ require_relative 'puppet-rest/pe/client'
31
+
32
+ require_relative 'puppet-rest/db/config'
33
+ require_relative 'puppet-rest/db/entities/base'
34
+ require_relative 'puppet-rest/db/entities/node'
35
+ require_relative 'puppet-rest/db/entities/fact'
36
+ require_relative 'puppet-rest/db/entities/resource'
37
+ require_relative 'puppet-rest/db/request'
38
+ require_relative 'puppet-rest/db/connection/nodes'
39
+ require_relative 'puppet-rest/db/connection/fact-names'
40
+ require_relative 'puppet-rest/db/connection/facts'
41
+ require_relative 'puppet-rest/db/connection/resources'
42
+ require_relative 'puppet-rest/db/connection'
43
+ require_relative 'puppet-rest/db/client'
@@ -0,0 +1,42 @@
1
+ module PuppetRestClient::DB
2
+ extend PuppetRestClient::DB::Config
3
+
4
+ class << self
5
+ # Convenience alias for PuppetRestClient::DB::Connection.new
6
+ #
7
+ # return [PuppetRestClient::DB::Connection]
8
+ def new(options=Mash.new)
9
+ PuppetRestClient::DB::Connection.new(options)
10
+ end
11
+
12
+ # Delegate methods to PuppetRestClient::Connection
13
+ def method_missing(method, *args, &block)
14
+ return super unless new.respond_to?(method)
15
+ new.send(method, *args, &block)
16
+ end
17
+
18
+ def respond_to?(method, include_private=false)
19
+ new.respond_to?(method, include_private) || super(method, include_private)
20
+ end
21
+
22
+ def read_key_file(path)
23
+ key_file_path = File.expand_path(path)
24
+
25
+ begin
26
+ raw_key = File.read(key_file_path).strip
27
+ rescue SystemCallError, IOError => e
28
+ raise IOError, "Unable to read #{key_file_path}"
29
+ end
30
+
31
+ begin_rsa = '-----BEGIN RSA PRIVATE KEY-----'
32
+ end_rsa = '-----END RSA PRIVATE KEY-----'
33
+
34
+ unless (raw_key =~ /\A#{begin_rsa}$/) && (raw_key =~ /^#{end_rsa}\Z/)
35
+ msg = "The file #{key_file_path} is not a properly formatted private key.\n"
36
+ msg << "It must contain '#{begin_rsa}' and '#{end_rsa}'"
37
+ raise ArgumentError, msg
38
+ end
39
+ return OpenSSL::PKey::RSA.new(raw_key)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,83 @@
1
+ module PuppetRestClient::DB
2
+ module Config
3
+
4
+ # The default Chef server URL
5
+ DEFAULT_SERVER_URL = 'http://localhost:8080'
6
+
7
+ # The default Spice User-Agent header
8
+ DEFAULT_USER_AGENT = "PuppetRestClient #{PuppetRestClient::VERSION}"
9
+
10
+ # Default connection options
11
+ DEFAULT_CONNECTION_OPTIONS = {}
12
+
13
+ # Default client name
14
+ DEFAULT_CLIENT_NAME = ''
15
+
16
+ # Default key file
17
+ DEFAULT_CLIENT_KEY = ''
18
+
19
+ # Default puppetdb rest api version (e.g. v1, v2, v3, pdb/query/v4)
20
+ DEFAULT_API_VERSION = 'v2'
21
+
22
+ # An array of valid config options
23
+ VALID_OPTIONS_KEYS = [
24
+ :server_url,
25
+ :api_version,
26
+ :client_name,
27
+ :client_key,
28
+ :user_agent,
29
+ :connection_options,
30
+ :middleware
31
+ ]
32
+
33
+ # Default middleware stack
34
+ DEFAULT_MIDDLEWARE = Proc.new do |builder|
35
+ builder.use PuppetRestClient::Response::ParseJSON
36
+ builder.use PuppetRestClient::Response::ClientError
37
+ builder.adapter Faraday.default_adapter
38
+ end
39
+
40
+ VALID_OPTIONS_KEYS.each do |key|
41
+ attr_accessor key
42
+ end
43
+
44
+ # Reset all config options to default when the module is extended
45
+ def self.extended(base)
46
+ base.reset
47
+ end # def self.extended
48
+
49
+ # Convenience method to configure PuppetRestClient in a block
50
+ # @example Configuring PuppetRestClient
51
+ # PuppetRestClient.setup do |s|
52
+ # s.server_url = "http://puppetdb.example.com:8081"
53
+ # s.client_name = "admin"
54
+ # s.client_key = PuppetRestClient.read_key_file("/path/to/key_file.pem")
55
+ # end
56
+ # @yieldparam PuppetRestClient
57
+ # @yieldreturn PuppetRestClient
58
+ def setup
59
+ yield self
60
+ self
61
+ end
62
+
63
+ # Create an options hash from valid options keys
64
+ def options
65
+ options = {}
66
+ VALID_OPTIONS_KEYS.each{|k| options[k] = send(k)}
67
+ options
68
+ end
69
+
70
+ # Reset all config options to their defaults
71
+ def reset
72
+ self.user_agent = DEFAULT_USER_AGENT
73
+ self.api_version = DEFAULT_API_VERSION
74
+ self.server_url = DEFAULT_SERVER_URL
75
+ self.client_name = DEFAULT_CLIENT_NAME
76
+ self.client_key = DEFAULT_CLIENT_KEY
77
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
78
+ self.middleware = DEFAULT_MIDDLEWARE
79
+ self
80
+ end
81
+
82
+ end
83
+ end