Module: AccessHelper

Included in:
VpsCli::Access
Defined in:
lib/vps_cli/helpers/access_helper.rb

Overview

Helper methods to be used within Access to help reduce the file size This is meant for reading files encrypted with sops

Instance Method Summary collapse

Instance Method Details

#decrypt(yaml_file:, path:) ⇒ String

uses an access file via SOPS SOPS is an encryption tool It will decrypt the file, please use a .yaml file

Parameters:

  • file (File)

    The .yaml file encrypted with sops used to login to various accounts

Returns:

  • (String)

    The value of key given in the .yaml file

See Also:



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vps_cli/helpers/access_helper.rb', line 105

def decrypt(yaml_file:, path:)
  # puts all keys into a ["key"] within the array
  sops_cmd = "sops -d --extract '#{path}' #{yaml_file}"

  # this allows you to enter your passphrase
  export_tty
  # this will return in the string form the value you were looking for
  stdout, _stderr, _status = Open3.capture3(sops_cmd)

  stdout
end

#dig_for_path(*path) ⇒ String

Returns a path string to be able to traverse a yaml file

Parameters:

  • The (#to_s, Array<#to_s>)

    ordered path to traverse

Returns:

  • (String)

    Returns a path string to be able to traverse a yaml file

See Also:



120
121
122
123
124
# File 'lib/vps_cli/helpers/access_helper.rb', line 120

def dig_for_path(*path)
  path.flatten.inject('') do |final_path, node|
    final_path + "[#{node.to_s.to_json}]"
  end
end

#export_ttyObject

I noticed needing to export $(tty) while troubleshooting issues with gpg keys. It is here just in case its not in your zshrc / bashrc file

Returns:

  • void



130
131
132
# File 'lib/vps_cli/helpers/access_helper.rb', line 130

def export_tty
  Rake.sh('GPG_TTY=$(tty) && export GPG_TTY')
end

#heroku_api_string(yaml_file:) ⇒ String

retrieves the values of .netrc for heroku and creates a writable string

Returns:

  • (String)

    Returns the string to be written to netrc



13
14
15
16
17
18
19
20
21
# File 'lib/vps_cli/helpers/access_helper.rb', line 13

def heroku_api_string(yaml_file:)
  # initial tree traversal in the .yaml file
  heroku_api = %i[heroku api]
  heroku_api_keys = %i[machine login password]

  make_string(base: heroku_api, keys: heroku_api_keys) do |path|
    decrypt(yaml_file: yaml_file, path: path)
  end
end

#heroku_git_string(yaml_file:) ⇒ String

retries the git values for heroku in your .yaml file

Returns:

  • (String)

    Returns the string to be written to your netrc file



25
26
27
28
29
30
31
32
# File 'lib/vps_cli/helpers/access_helper.rb', line 25

def heroku_git_string(yaml_file:)
  heroku_git = %i[heroku git]
  heroku_git_keys = %i[machine login password]

  make_string(base: heroku_git, keys: heroku_git_keys) do |path|
    decrypt(yaml_file: yaml_file, path: path)
  end
end

#make_string(base:, keys:) ⇒ String

Creates a string to be used to write to .netrc

Parameters:

  • base (String)

    Provides the base string from which to add to

  • keys (Array<String>)

    An array of strings to append to base

Returns:

  • (String)

    Returns the string after concatenating them



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vps_cli/helpers/access_helper.rb', line 57

def make_string(base:, keys:)
  # iterates through the keys to provide a path to each array
  # essentialy is the equivalent of Hash.dig(:heroku, :api, :key)
  my_inject_with_count(keys) do |string, key, count|
    path = dig_for_path(base, key)

    value = yield(path)
    value << "\n  " if count < keys.length - 1
    string + value
  end
end

#my_inject_with_count(array) {|accum, element| ... } ⇒ String

my version of Enumerable#inject intended to return a string provides a count to know what # object youre on

Parameters:

  • array (Array<#to_s>)

    For each element in the array, yield to the block given.

Yields:

  • param count [Integer]

Yield Parameters:

  • accum (String)

    The value that will persist throughout the block

  • element (String)

    The current element in the array

Returns:

  • (String)

    Returns the string returned by the block passed to it



43
44
45
46
47
48
49
50
51
# File 'lib/vps_cli/helpers/access_helper.rb', line 43

def my_inject_with_count(array)
  value = nil
  count = 0
  array.inject('') do |accum, element|
    value = yield(accum, element, count)
    count += 1
    value # if not here, returns the value of count
  end
end

#netrc_error(netrc_file:, error:) ⇒ Object

Adds the error to VpsCli#errors array

Parameters:

  • Location (File)

    of netrc_file

  • The (Exception)

    error to write to the array

Returns:

  • void



90
91
92
93
# File 'lib/vps_cli/helpers/access_helper.rb', line 90

def netrc_error(netrc_file:, error:)
  error_msg = "Unable to write to your #{netrc_file}."
  VpsCli.errors << error.exception(error_msg)
end

#write_to_netrc(netrc_file: nil, string:) ⇒ Object

Writes the value of string to netrc

Parameters:

  • netrc_file (File) (defaults to: nil)

    ($HOME/.netrc) The location of your .netrc file to be read by heroku

  • string (String)

    The String to write to the netrc file

Returns:

  • void



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vps_cli/helpers/access_helper.rb', line 74

def write_to_netrc(netrc_file: nil, string:)
  netrc_file ||= File.join(Dir.home, '.netrc')
  Rake.mkdir_p(File.dirname(netrc_file))
  Rake.touch(netrc_file) unless File.exist?(netrc_file)

  begin
    File.write(netrc_file, string)
  rescue Errno::EACCES => e
    netrc_error(netrc_file: netrc_file, error: e)
  end
end