Class: SymmetricEncryption::Keystore::Gcp

Inherits:
Object
  • Object
show all
Includes:
Utils::Files
Defined in:
lib/symmetric_encryption/keystore/gcp.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_file:, app_name: nil, environment: nil, key_encrypting_key: nil, crypto_key: nil, project_id: nil, credentials: nil, location_id: nil) ⇒ Gcp

Returns a new instance of Gcp.



30
31
32
33
34
35
36
37
38
# File 'lib/symmetric_encryption/keystore/gcp.rb', line 30

def initialize(key_file:, app_name: nil, environment: nil, key_encrypting_key: nil, crypto_key: nil, project_id: nil, credentials: nil, location_id: nil)
  @crypto_key  = crypto_key
  @app_name    = app_name
  @environment = environment
  @file_name   = key_file
  @project_id  = project_id
  @credentials = credentials
  @location_id = location_id
end

Class Method Details

.generate_data_key(cipher_name:, app_name:, environment:, key_path:, version: 0) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/symmetric_encryption/keystore/gcp.rb', line 8

def self.generate_data_key(cipher_name:, app_name:, environment:, key_path:, version: 0)
  version >= 255 ? (version = 1) : (version += 1)

  dek       = SymmetricEncryption::Key.new(cipher_name: cipher_name)
  file_name = "#{key_path}/#{app_name}_#{environment}_v#{version}.encrypted_key"
  keystore  = new(
    key_file:    file_name,
    app_name:    app_name,
    environment: environment
  )
  keystore.write(dek.key)

  {
    keystore:    :gcp,
    cipher_name: dek.cipher_name,
    version:     version,
    key_file:    file_name,
    iv:          dek.iv,
    crypto_key:  keystore.crypto_key
  }
end

Instance Method Details

#crypto_keyObject



48
49
50
51
# File 'lib/symmetric_encryption/keystore/gcp.rb', line 48

def crypto_key
  @crypto_key ||= self.class::KMS::KeyManagementServiceClient.crypto_key_path(project_id, location_id, app_name,
                                                                              environment.to_s)
end

#readObject



40
41
42
# File 'lib/symmetric_encryption/keystore/gcp.rb', line 40

def read
  decrypt(read_file_and_decode(file_name))
end

#write(data_key) ⇒ Object



44
45
46
# File 'lib/symmetric_encryption/keystore/gcp.rb', line 44

def write(data_key)
  write_encoded_to_file(file_name, encrypt(data_key))
end