Class: Invar::Rake::Task::SecretsFileHandler

Inherits:
NamespacedFileTask show all
Defined in:
lib/invar/rake/task/secrets.rb

Overview

Rake task handler for actions on the secrets file.

Constant Summary collapse

SECRETS_INSTRUCTIONS =

Instructions hint for how to handle secret keys.

"Generated key. Save this key to a secure password manager, you will need it to edit the secrets.yml file:\n"
SWAP_EXT =
'tmp'

Instance Method Summary collapse

Methods inherited from NamespacedFileTask

#file_path, #initialize

Constructor Details

This class inherits a constructor from Invar::Rake::Task::NamespacedFileTask

Instance Method Details

#create(content: SECRETS_TEMPLATE) ⇒ Object

Creates a new encrypted secrets file and prints the generated encryption key to STDOUT



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/invar/rake/task/secrets.rb', line 18

def create(content: SECRETS_TEMPLATE)
   encryption_key = Lockbox.generate_key

   write_encrypted_file(file_path,
                        encryption_key: encryption_key,
                        content:        content,
                        permissions:    PrivateFile::DEFAULT_PERMISSIONS)

   warn SECRETS_INSTRUCTIONS
   puts encryption_key
end

#editObject

Updates the file with new content.

Either the content is provided over STDIN or the default editor is opened with the decrypted contents of the secrets file. After closing the editor, the file will be updated with the new encrypted contents.



34
35
36
37
38
39
40
# File 'lib/invar/rake/task/secrets.rb', line 34

def edit
   content = $stdin.stat.pipe? ? $stdin.read : nil

   edit_encrypted_file(secrets_file, content: content)

   warn "File saved to #{ secrets_file }"
end

#rotateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/invar/rake/task/secrets.rb', line 42

def rotate
   file_path = secrets_file

   decrypted = read_encrypted_file(file_path, encryption_key: determine_key(file_path))

   swap_file = file_path.dirname / [file_path.basename, SWAP_EXT].join('.')
   file_path.rename swap_file

   begin
      create content: decrypted
      swap_file.delete
   rescue StandardError
      swap_file.rename file_path.to_s
   end
end