Class: TerraspacePluginGoogle::Interfaces::Helper::Secret

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Clients, Logging
Defined in:
lib/terraspace_plugin_google/interfaces/helper/secret.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Methods included from Clients

#resource_manager, #secret_manager_service, #storage

Constructor Details

#initialize(mod, options = {}) ⇒ Secret

Returns a new instance of Secret.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/terraspace_plugin_google/interfaces/helper/secret.rb', line 9

def initialize(mod, options={})
  @mod, @options = mod, options
  @base64 = options[:base64]
  @project_id = options[:google_project] || ENV['GOOGLE_CLOUD_PROJECT'] || ENV['GOOGLE_PROJECT'] || raise("GOOGLE_PROJECT env variable is not set. It's required.")
  # So google sdk newer versions use GOOGLE_CLOUD_PROJECT instead of GOOGLE_PROJECT
  # Found out between google-cloud-storage-1.35.0 and google-cloud-storage-1.28.0
  # Though it seems like an library underneath that with the change.
  # Keeping backwards compatibility to not create breakage users who already have GOOGLE_PROJECT
  # But then setting GOOGLE_CLOUD_PROJECT so it works with the SDK.
  # For users, who set GOOGLE_CLOUD_PROJECT that will work also.
  ENV['GOOGLE_CLOUD_PROJECT'] ||= @project_id
end

Instance Method Details

#fetch(short_name, version: "latest") ⇒ Object



22
23
24
25
26
# File 'lib/terraspace_plugin_google/interfaces/helper/secret.rb', line 22

def fetch(short_name, version: "latest")
  value = fetch_value(short_name, version)
  value = Base64.strict_encode64(value).strip if @base64
  value
end

#fetch_value(short_name, version = "latest") ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/terraspace_plugin_google/interfaces/helper/secret.rb', line 28

def fetch_value(short_name, version="latest")
  short_name = expansion(short_name) if expand?
  name = "projects/#{project_number}/secrets/#{short_name}/versions/#{version}"
  version = secret_manager_service.access_secret_version(name: name)
  version.payload.data
rescue Google::Cloud::NotFoundError => e
  logger.info "WARN: secret #{name} not found".color(:yellow)
  logger.info e.message
  "NOT FOUND #{name}" # simple string so Kubernetes YAML is valid
rescue Google::Cloud::InvalidArgumentError => e
  logger.info "WARN: secret #{name} not found".color(:yellow)
  logger.info e.message
  "NOT FOUND #{name}" # simple string so Kubernetes YAML is valid
end