Module: Kitchen::Driver::CredentialsManager

Includes:
DPAPI
Included in:
Scalr
Defined in:
lib/kitchen/driver/scalr_cred.rb

Constant Summary

Constants included from DPAPI

DPAPI::AUDIT, DPAPI::CRED_SYNC, DPAPI::LOCAL_MACHINE, DPAPI::NO_RECOVERY, DPAPI::UI_FORBIDDEN, DPAPI::VERIFY_PROTECTION

Instance Method Summary collapse

Methods included from DPAPI

#decrypt, #encrypt

Instance Method Details

#loadCredentialsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kitchen/driver/scalr_cred.rb', line 13

def loadCredentials()
  if OS.windows? then
    credentialsFilename = ENV['APPDATA'] + "\\kitchen_scalr.cred"
    if File.file?(credentialsFilename) then
      #Load existing credentials
      encryptedCred = File.open(credentialsFilename, "rb") {|f| f.read}
      decryptedJson = decrypt(encryptedCred)
      cred = JSON.parse(decryptedJson[0])
      config[:scalr_api_key_id] = cred['API_KEY_ID']
      config[:scalr_api_key_secret] = cred['API_KEY_SECRET']
    else
      #Prompt for credentials
      print 'Enter your API Key ID: '
      apiKeyId = STDIN.gets.chomp
      print 'Enter you API Key secret: '
      apiKeySecret = STDIN.noecho(&:gets).chomp
      cred = {
        'API_KEY_ID' => apiKeyId,
        'API_KEY_SECRET' => apiKeySecret
      }
      decryptedJson = cred.to_json
      encryptedCred = encrypt(decryptedJson)
      File.binwrite(credentialsFilename, encryptedCred)
      config[:scalr_api_key_id] = cred['API_KEY_ID']
      config[:scalr_api_key_secret] = cred['API_KEY_SECRET']
    end
  elsif OS.mac? then
    credentialsServiceName = "kitchen_scalr.cred"
    #First, we detect if there is any password in the keychain
    res = `security find-generic-password -l #{credentialsServiceName} -g >/dev/null 2>/dev/null; echo $?`
    res = res.chomp
    if res == '0' then
       = `security find-generic-password -l #{credentialsServiceName} | grep acct | awk -F"=" '{print $2}' | sed 's/"//g'`.chomp
      password = `security find-generic-password -l #{credentialsServiceName} -g 2>&1 | grep password | awk '{print $2}' | sed 's/"//g'`.chomp
      config[:scalr_api_key_id] = 
      config[:scalr_api_key_secret] = password
    else
      #Prompt for credentials
      print 'Enter your API Key ID: '
      apiKeyId = STDIN.gets.chomp
      print 'Enter you API Key secret: '
      apiKeySecret = STDIN.noecho(&:gets).chomp
      #Save those in Keychain
      res = `security add-generic-password -a #{apiKeyId} -s #{credentialsServiceName} -p #{apiKeySecret}`
      config[:scalr_api_key_id] = apiKeyId
      config[:scalr_api_key_secret] = apiKeySecret
    end
  else
    puts 'This OS is currently not supported'
  end
end