dd-vault 0.12.0

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 (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +42 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +26 -0
  5. data/CHANGELOG.md +228 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE +362 -0
  8. data/README.md +214 -0
  9. data/Rakefile +6 -0
  10. data/lib/vault/api/approle.rb +218 -0
  11. data/lib/vault/api/auth.rb +316 -0
  12. data/lib/vault/api/auth_tls.rb +92 -0
  13. data/lib/vault/api/auth_token.rb +242 -0
  14. data/lib/vault/api/help.rb +33 -0
  15. data/lib/vault/api/logical.rb +150 -0
  16. data/lib/vault/api/secret.rb +156 -0
  17. data/lib/vault/api/sys/audit.rb +91 -0
  18. data/lib/vault/api/sys/auth.rb +116 -0
  19. data/lib/vault/api/sys/health.rb +63 -0
  20. data/lib/vault/api/sys/init.rb +83 -0
  21. data/lib/vault/api/sys/leader.rb +48 -0
  22. data/lib/vault/api/sys/lease.rb +49 -0
  23. data/lib/vault/api/sys/mount.rb +103 -0
  24. data/lib/vault/api/sys/policy.rb +92 -0
  25. data/lib/vault/api/sys/seal.rb +81 -0
  26. data/lib/vault/api/sys.rb +25 -0
  27. data/lib/vault/api.rb +12 -0
  28. data/lib/vault/client.rb +447 -0
  29. data/lib/vault/configurable.rb +48 -0
  30. data/lib/vault/defaults.rb +197 -0
  31. data/lib/vault/encode.rb +19 -0
  32. data/lib/vault/errors.rb +72 -0
  33. data/lib/vault/persistent/connection.rb +42 -0
  34. data/lib/vault/persistent/pool.rb +48 -0
  35. data/lib/vault/persistent/timed_stack_multi.rb +70 -0
  36. data/lib/vault/persistent.rb +1158 -0
  37. data/lib/vault/request.rb +43 -0
  38. data/lib/vault/response.rb +89 -0
  39. data/lib/vault/vendor/connection_pool/timed_stack.rb +178 -0
  40. data/lib/vault/vendor/connection_pool/version.rb +5 -0
  41. data/lib/vault/vendor/connection_pool.rb +150 -0
  42. data/lib/vault/version.rb +3 -0
  43. data/lib/vault.rb +49 -0
  44. data/vault.gemspec +30 -0
  45. metadata +185 -0
@@ -0,0 +1,49 @@
1
+ module Vault
2
+ class Sys
3
+ # Renew a lease with the given ID.
4
+ #
5
+ # @example
6
+ # Vault.sys.renew("aws/username") #=> #<Vault::Secret ...>
7
+ #
8
+ # @param [String] id
9
+ # the lease ID
10
+ # @param [Fixnum] increment
11
+ #
12
+ # @return [Secret]
13
+ def renew(id, increment = 0)
14
+ json = client.put("/v1/sys/renew/#{id}", JSON.fast_generate(
15
+ increment: increment,
16
+ ))
17
+ return Secret.decode(json)
18
+ end
19
+
20
+ # Revoke the secret at the given id. If the secret does not exist, an error
21
+ # will be raised.
22
+ #
23
+ # @example
24
+ # Vault.sys.revoke("aws/username") #=> true
25
+ #
26
+ # @param [String] id
27
+ # the lease ID
28
+ #
29
+ # @return [true]
30
+ def revoke(id)
31
+ client.put("/v1/sys/revoke/#{id}", nil)
32
+ return true
33
+ end
34
+
35
+ # Revoke all secrets under the given prefix.
36
+ #
37
+ # @example
38
+ # Vault.sys.revoke_prefix("aws") #=> true
39
+ #
40
+ # @param [String] id
41
+ # the lease ID
42
+ #
43
+ # @return [true]
44
+ def revoke_prefix(id)
45
+ client.put("/v1/sys/revoke-prefix/#{id}", nil)
46
+ return true
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,103 @@
1
+ require "json"
2
+
3
+ module Vault
4
+ class Mount < Response
5
+ # @!attribute [r] config
6
+ # Arbitrary configuration for the backend.
7
+ # @return [Hash<Symbol, Object>]
8
+ field :config
9
+
10
+ # @!attribute [r] description
11
+ # Description of the mount.
12
+ # @return [String]
13
+ field :description
14
+
15
+ # @!attribute [r] type
16
+ # Type of the mount.
17
+ # @return [String]
18
+ field :type
19
+ end
20
+
21
+ class Sys < Request
22
+ # List all mounts in the vault.
23
+ #
24
+ # @example
25
+ # Vault.sys.mounts #=> { :secret => #<struct Vault::Mount type="generic", description="generic secret storage"> }
26
+ #
27
+ # @return [Hash<Symbol, Mount>]
28
+ def mounts
29
+ json = client.get("/v1/sys/mounts")
30
+ json = json[:data] if json[:data]
31
+ return Hash[*json.map do |k,v|
32
+ [k.to_s.chomp("/").to_sym, Mount.decode(v)]
33
+ end.flatten]
34
+ end
35
+
36
+ # Create a mount at the given path.
37
+ #
38
+ # @example
39
+ # Vault.sys.mount("pg", "postgresql", "Postgres user management") #=> true
40
+ #
41
+ # @param [String] path
42
+ # the path to mount at
43
+ # @param [String] type
44
+ # the type of mount
45
+ # @param [String] description
46
+ # a human-friendly description (optional)
47
+ def mount(path, type, description = nil)
48
+ payload = { type: type }
49
+ payload[:description] = description if !description.nil?
50
+
51
+ client.post("/v1/sys/mounts/#{encode_path(path)}", JSON.fast_generate(payload))
52
+ return true
53
+ end
54
+
55
+ # Tune a mount at the given path.
56
+ #
57
+ # @example
58
+ # Vault.sys.mount_tune("pki", max_lease_ttl: '87600h') #=> true
59
+ #
60
+ # @param [String] path
61
+ # the path to write
62
+ # @param [Hash] data
63
+ # the data to write
64
+ def mount_tune(path, data = {})
65
+ json = client.post("/v1/sys/mounts/#{encode_path(path)}/tune", JSON.fast_generate(data))
66
+ return true
67
+ end
68
+
69
+ # Unmount the thing at the given path. If the mount does not exist, an error
70
+ # will be raised.
71
+ #
72
+ # @example
73
+ # Vault.sys.unmount("pg") #=> true
74
+ #
75
+ # @param [String] path
76
+ # the path to unmount
77
+ #
78
+ # @return [true]
79
+ def unmount(path)
80
+ client.delete("/v1/sys/mounts/#{encode_path(path)}")
81
+ return true
82
+ end
83
+
84
+ # Change the name of the mount
85
+ #
86
+ # @example
87
+ # Vault.sys.remount("pg", "postgres") #=> true
88
+ #
89
+ # @param [String] from
90
+ # the origin mount path
91
+ # @param [String] to
92
+ # the new mount path
93
+ #
94
+ # @return [true]
95
+ def remount(from, to)
96
+ client.post("/v1/sys/remount", JSON.fast_generate(
97
+ from: from,
98
+ to: to,
99
+ ))
100
+ return true
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,92 @@
1
+ require "json"
2
+
3
+ module Vault
4
+ class Policy < Response
5
+ # @!attribute [r] name
6
+ # Name of the policy.
7
+ #
8
+ # @example Get the name of the policy
9
+ # policy.name #=> "default"
10
+ #
11
+ # @return [String]
12
+ field :name
13
+
14
+ # @!attribute [r] rules
15
+ # Raw HCL policy.
16
+ #
17
+ # @example Display the list of rules
18
+ # policy.rules #=> "path \"secret/foo\" {}"
19
+ #
20
+ # @return [String]
21
+ field :rules
22
+ end
23
+
24
+ class Sys
25
+ # The list of policies in vault.
26
+ #
27
+ # @example
28
+ # Vault.sys.policies #=> ["root"]
29
+ #
30
+ # @return [Array<String>]
31
+ def policies
32
+ client.get("/v1/sys/policy")[:policies]
33
+ end
34
+
35
+ # Get the policy by the given name. If a policy does not exist by that name,
36
+ # +nil+ is returned.
37
+ #
38
+ # @example
39
+ # Vault.sys.policy("root") #=> #<Vault::Policy rules="">
40
+ #
41
+ # @return [Policy, nil]
42
+ def policy(name)
43
+ json = client.get("/v1/sys/policy/#{encode_path(name)}")
44
+ return Policy.decode(json)
45
+ rescue HTTPError => e
46
+ return nil if e.code == 404
47
+ raise
48
+ end
49
+
50
+ # Create a new policy with the given name and rules.
51
+ #
52
+ # @example
53
+ # policy = <<-EOH
54
+ # path "sys" {
55
+ # policy = "deny"
56
+ # }
57
+ # EOH
58
+ # Vault.sys.put_policy("dev", policy) #=> true
59
+ #
60
+ # It is recommend that you load policy rules from a file:
61
+ #
62
+ # @example
63
+ # policy = File.read("/path/to/my/policy.hcl")
64
+ # Vault.sys.put_policy("dev", policy)
65
+ #
66
+ # @param [String] name
67
+ # the name of the policy
68
+ # @param [String] rules
69
+ # the policy rules
70
+ #
71
+ # @return [true]
72
+ def put_policy(name, rules)
73
+ client.put("/v1/sys/policy/#{encode_path(name)}", JSON.fast_generate(
74
+ rules: rules,
75
+ ))
76
+ return true
77
+ end
78
+
79
+ # Delete the policy with the given name. If a policy does not exist, vault
80
+ # will not return an error.
81
+ #
82
+ # @example
83
+ # Vault.sys.delete_policy("dev") #=> true
84
+ #
85
+ # @param [String] name
86
+ # the name of the policy
87
+ def delete_policy(name)
88
+ client.delete("/v1/sys/policy/#{encode_path(name)}")
89
+ return true
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,81 @@
1
+ require "json"
2
+
3
+ module Vault
4
+ class SealStatus < Response
5
+ # @!method sealed?
6
+ # Returns if the Vault is sealed.
7
+ #
8
+ # @example Check if the Vault is sealed
9
+ # status.sealed? #=> true
10
+ #
11
+ # @return [Boolean]
12
+ field :sealed, as: :sealed?
13
+
14
+ # @!attribute t
15
+ # Threshold of keys required to unseal the Vault.
16
+ #
17
+ # @example Get the threshold of keys
18
+ # status.t #=> 3
19
+ #
20
+ # @return [Fixnum]
21
+ field :t
22
+
23
+ # @!attribute n
24
+ # Total number of unseal keys.
25
+ #
26
+ # @example Get the total number of keys
27
+ # status.n #=> 5
28
+ #
29
+ # @return [Fixnum]
30
+ field :n
31
+
32
+ # @!attribute progress
33
+ # Number of keys that have been entered.
34
+ #
35
+ # @example Get the current unseal progress
36
+ # status.progress #=> 2
37
+ #
38
+ # @return [Fixnum]
39
+ field :progress
40
+ end
41
+
42
+ class Sys
43
+ # Get the current seal status.
44
+ #
45
+ # @example
46
+ # Vault.sys.seal_status #=> #<Vault::SealStatus sealed=false, t=1, n=1, progress=0>
47
+ #
48
+ # @return [SealStatus]
49
+ def seal_status
50
+ json = client.get("/v1/sys/seal-status")
51
+ return SealStatus.decode(json)
52
+ end
53
+
54
+ # Seal the vault. Warning: this will seal the vault!
55
+ #
56
+ # @example
57
+ # Vault.sys.seal #=> true
58
+ #
59
+ # @return [true]
60
+ def seal
61
+ client.put("/v1/sys/seal", nil)
62
+ return true
63
+ end
64
+
65
+ # Unseal the vault with the given shard.
66
+ #
67
+ # @example
68
+ # Vault.sys.unseal("abcd-1234") #=> #<Vault::SealStatus sealed=true, t=3, n=5, progress=1>
69
+ #
70
+ # @param [String] shard
71
+ # the key to use
72
+ #
73
+ # @return [SealStatus]
74
+ def unseal(shard)
75
+ json = client.put("/v1/sys/unseal", JSON.fast_generate(
76
+ key: shard,
77
+ ))
78
+ return SealStatus.decode(json)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,25 @@
1
+ require_relative "../client"
2
+ require_relative "../request"
3
+ require_relative "../response"
4
+
5
+ module Vault
6
+ class Client
7
+ # A proxy to the {Sys} methods.
8
+ # @return [Sys]
9
+ def sys
10
+ @sys ||= Sys.new(self)
11
+ end
12
+ end
13
+
14
+ class Sys < Request; end
15
+ end
16
+
17
+ require_relative "sys/audit"
18
+ require_relative "sys/auth"
19
+ require_relative "sys/health"
20
+ require_relative "sys/init"
21
+ require_relative "sys/leader"
22
+ require_relative "sys/lease"
23
+ require_relative "sys/mount"
24
+ require_relative "sys/policy"
25
+ require_relative "sys/seal"
data/lib/vault/api.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Vault
2
+ module API
3
+ require_relative "api/approle"
4
+ require_relative "api/auth_token"
5
+ require_relative "api/auth_tls"
6
+ require_relative "api/auth"
7
+ require_relative "api/help"
8
+ require_relative "api/logical"
9
+ require_relative "api/secret"
10
+ require_relative "api/sys"
11
+ end
12
+ end