Class: Match::Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/match/encrypt.rb

Instance Method Summary collapse

Instance Method Details

#clear_password(git_url) ⇒ Object

removes the password from the keychain again



33
34
35
# File 'lib/match/encrypt.rb', line 33

def clear_password(git_url)
  Security::InternetPassword.delete(server: server_name(git_url))
end

#decrypt_repo(path: nil, git_url: nil, manual_password: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/match/encrypt.rb', line 47

def decrypt_repo(path: nil, git_url: nil, manual_password: nil)
  iterate(path) do |current|
    begin
      crypt(path: current,
        password: manual_password || password(git_url),
         encrypt: false)
    rescue
      UI.error "Couldn't decrypt the repo, please make sure you enter the right password!"
      UI.user_error!("Invalid password passed via 'MATCH_PASSWORD'") if ENV["MATCH_PASSWORD"]
      clear_password(git_url)
      decrypt_repo(path: path, git_url: git_url)
      return
    end
    UI.success "🔓  Decrypted '#{File.basename(current)}'" if $verbose
  end
  UI.success "🔓  Successfully decrypted certificates repo"
end

#encrypt_repo(path: nil, git_url: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/match/encrypt.rb', line 37

def encrypt_repo(path: nil, git_url: nil)
  iterate(path) do |current|
    crypt(path: current,
      password: password(git_url),
       encrypt: true)
    UI.success "🔒  Encrypted '#{File.basename(current)}'" if $verbose
  end
  UI.success "🔒  Successfully encrypted certificates repo"
end

#password(git_url) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/match/encrypt.rb', line 10

def password(git_url)
  password = ENV["MATCH_PASSWORD"]
  unless password
    item = Security::InternetPassword.find(server: server_name(git_url))
    password = item.password if item
  end

  unless password
    UI.important "Enter the passphrase that should be used to encrypt/decrypt your certificates"
    UI.important "This passphrase is specific per repository and will be stored in your local keychain"
    UI.important "Make sure to remember the password, as you'll need it when you run match on a different machine"
    password = ChangePassword.ask_password(confirm: true)
    store_password(git_url, password)
  end

  return password
end

#server_name(git_url) ⇒ Object



6
7
8
# File 'lib/match/encrypt.rb', line 6

def server_name(git_url)
  ["match", git_url].join("_")
end

#store_password(git_url, password) ⇒ Object



28
29
30
# File 'lib/match/encrypt.rb', line 28

def store_password(git_url, password)
  Security::InternetPassword.add(server_name(git_url), "", password)
end