Class: Veil::CredentialCollection::Base
- Inherits:
-
Object
- Object
- Veil::CredentialCollection::Base
- Extended by:
- Forwardable
- Defined in:
- lib/veil/credential_collection/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#hasher ⇒ Object
readonly
Returns the value of attribute hasher.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
-
#add(*args) ⇒ Object
(also: #<<)
Add a new credential to the credentials.
-
#add_from_file(filepath, *args) ⇒ Object
Add the contents of a file as a credential after verifying that the file can be read.
-
#exist?(*args) ⇒ Boolean
Check to see if a given credential has been added.
-
#get(*args) ⇒ Object
(also: #get_credential)
Retrieves a credential from the credential store:.
-
#initialize(opts = {}) ⇒ Base
constructor
A new instance of Base.
- #remove(group_or_cred, cred = nil) ⇒ Object (also: #delete)
- #rotate(group_or_cred, cred = nil) ⇒ Object
- #rotate_credentials ⇒ Object
- #rotate_hasher ⇒ Object
- #save ⇒ Object
- #to_hash ⇒ Object (also: #to_h)
Constructor Details
#initialize(opts = {}) ⇒ Base
Returns a new instance of Base.
20 21 22 23 24 |
# File 'lib/veil/credential_collection/base.rb', line 20 def initialize(opts = {}) @hasher = Veil::Hasher.create(opts[:hasher] || {}) @credentials = (opts[:credentials] || {}) @version = opts[:version] || 1 end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
16 17 18 |
# File 'lib/veil/credential_collection/base.rb', line 16 def credentials @credentials end |
#hasher ⇒ Object (readonly)
Returns the value of attribute hasher.
16 17 18 |
# File 'lib/veil/credential_collection/base.rb', line 16 def hasher @hasher end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
16 17 18 |
# File 'lib/veil/credential_collection/base.rb', line 16 def version @version end |
Class Method Details
.create(hash = {}) ⇒ Object
9 10 11 |
# File 'lib/veil/credential_collection/base.rb', line 9 def create(hash = {}) new(hash) end |
Instance Method Details
#add(*args) ⇒ Object Also known as: <<
Add a new credential to the credentials
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 |
# File 'lib/veil/credential_collection/base.rb', line 106 def add(*args) params = { name: nil, group: nil, length: 128, value: nil, force: false } case args.length when 1 # add('foo') params[:name] = args.first when 2 if args.all? { |a| a.is_a?(String) } # add('my_app', 'foo') params[:group], params[:name] = args elsif args[1].is_a?(Hash) # add('my_app', value: 'something') # add('foo', length: 50) params[:name] = args.first params.merge!(args[1]) end when 3 # add('my_app', 'foo', value: 'something') # add('my_app', 'foo', length: 50) params[:group], params[:name] = args[0], args[1] params.merge!(args[2]) else raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1-3)" end add_from_params(params) end |
#add_from_file(filepath, *args) ⇒ Object
Add the contents of a file as a credential after verifying that the file can be read. Usage:
add_from_file(filename, "secretname")
add_from_file(filename, "groupname", "secretname")
Anything added from file will automatically be frozen.
add‘s options are not supported.
144 145 146 147 148 149 150 |
# File 'lib/veil/credential_collection/base.rb', line 144 def add_from_file(filepath, *args) unless File.readable?(filepath) raise Veil::FileNotReadable.new("Cannot read #{filepath}") end add(*args, value: File.read(filepath), frozen: true) end |
#exist?(*args) ⇒ Boolean
Check to see if a given credential has been added.
94 95 96 97 98 99 |
# File 'lib/veil/credential_collection/base.rb', line 94 def exist?(*args) get(*args) true rescue Veil::GroupNotFound, Veil::CredentialNotFound false end |
#get(*args) ⇒ Object Also known as: get_credential
Retrieves a credential from the credential store:
get(name)
get(group, name)
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 |
# File 'lib/veil/credential_collection/base.rb', line 58 def get(*args) case args.length when 1 cred_name = args[0] c = credentials[cred_name] if c.nil? raise Veil::CredentialNotFound, "Credential '#{cred_name}' not found." else c.value end when 2 group_name = args[0] cred_name = args[1] g = credentials[group_name] if g.nil? raise Veil::GroupNotFound, "Credential group '#{group_name}' not found." else c = g[cred_name] if c.nil? raise Veil::CredentialNotFound, "Credential '#{cred_name}' not found in group '#{group_name}'." else c.value end end else raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1 or 2)" end end |
#remove(group_or_cred, cred = nil) ⇒ Object Also known as: delete
152 153 154 155 156 157 158 |
# File 'lib/veil/credential_collection/base.rb', line 152 def remove(group_or_cred, cred = nil) if group_or_cred && cred && credentials.key?(group_or_cred) credentials[group_or_cred].delete(cred) else credentials.delete(group_or_cred) end end |
#rotate(group_or_cred, cred = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/veil/credential_collection/base.rb', line 40 def rotate(group_or_cred, cred = nil) if cred && credentials.key?(group_or_cred) && credentials[group_or_cred].key?(cred) credentials[group_or_cred][cred].rotate(hasher) elsif credentials.key?(group_or_cred) if credentials[group_or_cred].is_a?(Hash) credentials[group_or_cred].each { |_s, c| c.rotate(hasher) } else credentials[group_or_cred].rotate(hasher) end end end |
#rotate_credentials ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/veil/credential_collection/base.rb', line 166 def rotate_credentials credentials.each do |cred_or_group_name, cred_or_group| if cred_or_group.is_a?(Veil::Credential) cred_or_group.rotate(hasher) else cred_or_group.each { |_group, cred| cred.rotate(hasher) } end end end |
#rotate_hasher ⇒ Object
161 162 163 164 |
# File 'lib/veil/credential_collection/base.rb', line 161 def rotate_hasher @hasher = Veil::Hasher.create rotate_credentials end |
#save ⇒ Object
36 37 38 |
# File 'lib/veil/credential_collection/base.rb', line 36 def save raise "Save has not been implemented for this class" end |
#to_hash ⇒ Object Also known as: to_h
26 27 28 29 30 31 32 33 |
# File 'lib/veil/credential_collection/base.rb', line 26 def to_hash { type: self.class.name, version: version, hasher: hasher.to_h, credentials: credentials_as_hash } end |