Class: KubeKrypt::Decryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/kubekrypt/decryptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_name) ⇒ Decryptor

Returns a new instance of Decryptor.



5
6
7
8
# File 'lib/kubekrypt/decryptor.rb', line 5

def initialize(key_name)
  @client = Google::Cloud::Kms.key_management_service
  @key_name = key_name
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/kubekrypt/decryptor.rb', line 3

def client
  @client
end

#key_nameObject (readonly)

Returns the value of attribute key_name.



3
4
5
# File 'lib/kubekrypt/decryptor.rb', line 3

def key_name
  @key_name
end

Class Method Details

.call(content:, base64:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kubekrypt/decryptor.rb', line 22

def self.call(content:, base64:)
  unless content["data"]
    raise InvalidSecretError, "no data key found — nothing to decrypt"
  end

  key_name = content.fetch(METADATA_KEY).fetch(KMS_KEY.to_s)
  decryptor = new(key_name)
  content["data"].transform_values! { |encodedtext| decryptor.call(encodedtext, base64:) }
  content.delete(METADATA_KEY)
  content.to_yaml
end

Instance Method Details

#call(encodedtext, base64: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kubekrypt/decryptor.rb', line 10

def call(encodedtext, base64: false)
  ciphertext = Base64.strict_decode64(encodedtext.sub("#{ENC_PREFIX}:", ""))

  result = client.decrypt(name: key_name, ciphertext:).plaintext

  if base64
    Base64.strict_encode64(result)
  else
    result
  end
end