Class: CredStash::Secret

Inherits:
Object
  • Object
show all
Defined in:
lib/cred_stash/secret.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, value: nil, key: nil, encrypted_value: nil, hmac: nil, context: {}) ⇒ Secret

Returns a new instance of Secret.



4
5
6
7
8
9
10
11
# File 'lib/cred_stash/secret.rb', line 4

def initialize(name:, value: nil, key: nil, encrypted_value: nil, hmac: nil, context: {})
  @name = name
  @value = value
  @key = key
  @encrypted_value = encrypted_value
  @hmac = hmac
  @context = context
end

Instance Attribute Details

#encrypted_valueObject (readonly)

Returns the value of attribute encrypted_value.



2
3
4
# File 'lib/cred_stash/secret.rb', line 2

def encrypted_value
  @encrypted_value
end

#hmacObject (readonly)

Returns the value of attribute hmac.



2
3
4
# File 'lib/cred_stash/secret.rb', line 2

def hmac
  @hmac
end

#keyObject (readonly)

Returns the value of attribute key.



2
3
4
# File 'lib/cred_stash/secret.rb', line 2

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/cred_stash/secret.rb', line 2

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



2
3
4
# File 'lib/cred_stash/secret.rb', line 2

def value
  @value
end

Class Method Details

.find(name, context: {}) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/cred_stash/secret.rb', line 32

def find(name, context: {})
  item = repository.get(name)
  new(
    name: name,
    key: CredStash::CipherKey.decrypt(Base64.decode64(item.key), context: context),
    encrypted_value: Base64.decode64(item.contents),
    hmac: item.hmac
  )
end

.repositoryObject



42
43
44
# File 'lib/cred_stash/secret.rb', line 42

def repository
  CredStash::Repository.instance
end

Instance Method Details

#decrypted_valueObject



27
28
29
# File 'lib/cred_stash/secret.rb', line 27

def decrypted_value
  @key.decrypt(@encrypted_value)
end

#encrypt!(kms_key_id: nil) ⇒ Object



13
14
15
16
17
# File 'lib/cred_stash/secret.rb', line 13

def encrypt!(kms_key_id: nil)
  @key = CredStash::CipherKey.generate(kms_key_id: kms_key_id, context: @context)
  @encrypted_value = @key.encrypt(@value)
  @hmac = @key.hmac(@encrypted_value)
end

#falsified?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/cred_stash/secret.rb', line 23

def falsified?
  @key.hmac(@encrypted_value) == @hmac
end

#saveObject



19
20
21
# File 'lib/cred_stash/secret.rb', line 19

def save
  self.class.repository.put(to_item)
end