Class: MacSetup::Secrets

Inherits:
Object
  • Object
show all
Defined in:
lib/mac_setup/secrets.rb

Constant Summary collapse

CRYPTO_LIB =
"openssl"
CIPHER =
"aes-256-cbc"
PLAINTEXT_EXT =
"priv"
CIPHERTEXT_EXT =
"crypt"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ Secrets

Returns a new instance of Secrets.



36
37
38
# File 'lib/mac_setup/secrets.rb', line 36

def initialize(files)
  @files = files
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



8
9
10
# File 'lib/mac_setup/secrets.rb', line 8

def files
  @files
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/mac_setup/secrets.rb', line 8

def password
  @password
end

Class Method Details

.decrypt(dir_or_files) ⇒ Object



14
15
16
# File 'lib/mac_setup/secrets.rb', line 14

def self.decrypt(dir_or_files)
  new(filter_files(dir_or_files, CIPHERTEXT_EXT)).decrypt
end

.encrypt(dir_or_files) ⇒ Object



10
11
12
# File 'lib/mac_setup/secrets.rb', line 10

def self.encrypt(dir_or_files)
  new(filter_files(dir_or_files, PLAINTEXT_EXT)).encrypt
end

.encrypted?(file) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/mac_setup/secrets.rb', line 18

def self.encrypted?(file)
  file.to_s.end_with?(CIPHERTEXT_EXT)
end

.filter_files(dir_or_files, extension) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/mac_setup/secrets.rb', line 28

def self.filter_files(dir_or_files, extension)
  if dir_or_files.is_a?(Array)
    dir_or_files.select { |file| file.to_s.end_with?(extension) }
  else
    Dir.glob("#{File.expand_path(dir_or_files)}/**/*.#{extension}")
  end
end

.strip_extension(file) ⇒ Object



22
23
24
25
26
# File 'lib/mac_setup/secrets.rb', line 22

def self.strip_extension(file)
  return file unless file.to_s.end_with?(PLAINTEXT_EXT)

  file.sub(/\.#{PLAINTEXT_EXT}$/, "")
end

Instance Method Details

#decryptObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mac_setup/secrets.rb', line 44

def decrypt
  if files.any?
    MacSetup.log "Decrypting files:"
  else
    MacSetup.log "No files to decrypt. Skipping"
  end

  list_files
  get_password
  decrypt_files
end

#encryptObject



40
41
42
# File 'lib/mac_setup/secrets.rb', line 40

def encrypt
  do_crypt("encrypt") { encrypt_files }
end