Class: SmartMachine::Credentials

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_machine/credentials.rb

Constant Summary collapse

CIPHER =
"aes-256-gcm"

Instance Method Summary collapse

Methods inherited from Base

#machine_has_engine_installed?, #platform_on_machine?, #user_bash

Methods included from Logger

configure_logger_for, included, #logger, logger_for

Constructor Details

#initializeCredentials

Returns a new instance of Credentials.



11
12
# File 'lib/smart_machine/credentials.rb', line 11

def initialize
end

Instance Method Details

#configObject



44
45
46
# File 'lib/smart_machine/credentials.rb', line 44

def config
  @config ||= deserialize(read).deep_symbolize_keys
end

#createObject



14
15
16
17
# File 'lib/smart_machine/credentials.rb', line 14

def create
  write_key
  write credentials_template
end

#editObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/smart_machine/credentials.rb', line 19

def edit
  content_path = Pathname.new "config/credentials.yml.enc"
  tmp_file = "#{Process.pid}.#{content_path.basename.to_s.chomp('.enc')}"
  tmp_path = Pathname.new File.join(Dir.tmpdir, tmp_file)
  contents = read
  tmp_path.binwrite contents

  system("#{ENV['EDITOR']} #{tmp_path}")

  updated_contents = tmp_path.binread

  if updated_contents != contents
    write(updated_contents)
    puts "File encrypted and saved."
  else
    puts "File contents were not changed."
  end
ensure
  FileUtils.rm(tmp_path) if tmp_path&.exist?
end

#read_keyObject



40
41
42
# File 'lib/smart_machine/credentials.rb', line 40

def read_key
  read_env_key || read_key_file || handle_missing_key
end