Class: VpsCli::Access

Inherits:
Object
  • Object
show all
Extended by:
AccessHelper
Defined in:
lib/vps_cli/access.rb

Overview

Used for various things related to logins, ssh keys, etc

Class Method Summary collapse

Methods included from AccessHelper

decrypt, dig_for_path, export_tty, heroku_api_string, heroku_git_string, make_string, my_inject_with_count, netrc_error, write_to_netrc

Class Method Details

.command_line_loginObject

Logs in via the command line if no yaml_file given



46
47
48
49
# File 'lib/vps_cli/access.rb', line 46

def self.
  set_git_config
  
end

.file_login(yaml_file:, netrc_file: nil) ⇒ Object

Provides all login credentials via a SOPS encrypted yaml file

Parameters:

  • yaml_file (File)

    File formatted in yaml and encrypted with SOPS

  • netrc_file (File) (defaults to: nil)

    ($HOME/.netrc) Location of the .netrc for heroku

Returns:

  • void



39
40
41
42
43
# File 'lib/vps_cli/access.rb', line 39

def self.(yaml_file:, netrc_file: nil)
  netrc_file ||= File.join(Dir.home, '.netrc')
  (yaml_file: yaml_file)
  (yaml_file: yaml_file, netrc_file: netrc_file)
end

.generate_ssh_keyObject

Generates an ssh key with the given values for opts this has not been extensively tested by myself so proceed with caution

Parameters:

  • opts (Hash)

    Options hash



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/vps_cli/access.rb', line 122

def self.generate_ssh_key
  o_file = File.join(Dir.home, '.ssh', 'id_rsa')

  return puts 'ssh key already exists' if File.exist?(o_file)

  type = 'rsa'
  bits = 4096
  email = get_email

  no_pass = ' -P ""' unless opts[:create_password]

  # if opts[:create_password] is false, make a blank password
  # if its true, go through ssh-keygen
  # this will also autoprompt overwrite as well
  cmd = "ssh-keygen -t #{type} -b #{bits} -C #{email} -f #{o_file}#{no_pass}"
  Rake.sh(cmd)
end

.get_emailObject



164
165
166
167
168
# File 'lib/vps_cli/access.rb', line 164

def self.get_email
  puts "\n"
  puts 'please enter an email for your ssh key:'
  $stdin.gets.chomp
end

.get_titleObject



170
171
172
173
174
# File 'lib/vps_cli/access.rb', line 170

def self.get_title
  puts "\n"
  puts 'please enter a title for your ssh key'
  $stdin.gets.chomp
end

.git_file_login(yaml_file:) ⇒ Object

Logs into git by setting it in your .gitconfig file

Parameters:

  • yaml_file (File)

    Sets your git login via the values in your yaml_file

Returns:

  • nil



85
86
87
88
89
90
91
92
93
# File 'lib/vps_cli/access.rb', line 85

def self.(yaml_file:)
  username_key = dig_for_path(:github, :username)
  email_key = dig_for_path(:github, :email)

  username = decrypt(yaml_file: yaml_file, path: username_key)
  email = decrypt(yaml_file: yaml_file, path: email_key)

  set_git_config(username, email)
end

.heroku_file_login(yaml_file:, netrc_file: nil) ⇒ Object

Logs into heroku if given an encrypted yaml_file

Parameters:

  • yaml_file (File)

    The yaml file to be decrypted

  • netrc_file (File) (defaults to: nil)

    The netrc file to write to

Returns:

  • void



99
100
101
102
103
104
105
106
107
108
# File 'lib/vps_cli/access.rb', line 99

def self.(yaml_file:, netrc_file: nil)
  netrc_file ||= File.join(Dir.home, '.netrc')

  api_string = heroku_api_string(yaml_file: yaml_file)
  git_string = heroku_git_string(yaml_file: yaml_file)

  netrc_string = api_string + "\n" + git_string

  write_to_netrc(netrc_file: netrc_file, string: netrc_string)
end

.heroku_loginObject

Command line heroku login

Returns:

  • void



73
74
75
76
77
78
79
# File 'lib/vps_cli/access.rb', line 73

def self.
  puts 'Please login to heroku:'
  Rake.sh('heroku login --interactive')
rescue RuntimeError => error
  message = "\nUnable not login to heroku. To login, type: 'heroku login'"
  VpsCli.errors << error.exception("#{error}\n\n#{message}")
end

.no_credentials_foundObject



176
177
178
179
# File 'lib/vps_cli/access.rb', line 176

def self.no_credentials_found
  puts "No credentials file found. Please check that your .vps_cli contains
  a valid .credentials file path"
end

.post_github_ssh_key(config = VpsCli.configuration) ⇒ Object

the wrapper around the GithubHTTP class to be able to send the ssh key

Parameters:

  • opts (Hash)

    Options parameter meant for cli usage



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/vps_cli/access.rb', line 146

def self.post_github_ssh_key(config = VpsCli.configuration)
  return puts no_credentials_found unless File.exist?(config.credentials)
  uri = URI('https://api.github.com/user/keys')

  api_token = proc { |yaml, path| decrypt(yaml_file: yaml, path: path) }

  # only create the token if the credentials file exists
  api_path = dig_for_path(:github, :api_token)
  token = api_token.call(config.credentials, api_path)

  ssh_file = File.join(Dir.home, '.ssh', 'id_rsa.pub')
  title = get_title

  github = GithubHTTP.new(uri: uri, token: token,
                          ssh_file: ssh_file, title: title)
  github.push_ssh_key
end

.provide_credentials(config = VpsCli.configuration) ⇒ Object

fixing to accept a configuration



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vps_cli/access.rb', line 23

def self.provide_credentials(config = VpsCli.configuration)
  if File.exist?(config.credentials)
    (yaml_file: config.credentials, netrc_file: config.netrc)
    post_github_ssh_key(config)
  else
    
  end

  # originally accepts an opts arguemnt
  generate_ssh_key
end

.set_git_config(username = nil, email = nil) ⇒ Object

Sets the .gitconfig file

Parameters:

  • username (String) (defaults to: nil)

    Username to set in .gitconfig

  • email (String) (defaults to: nil)

    email to use for .gitconfig

Returns:

  • void



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vps_cli/access.rb', line 55

def self.set_git_config(username = nil, email = nil)
  puts 'Please enter your git username:'
  username ||= $stdin.gets.chomp
  Rake.sh("git config --global user.name #{username}")

  puts 'Please enter your email:'
  email ||= $stdin.gets.chomp
  Rake.sh("git config --global user.email #{email}")

  puts "Git config complete.\n"
rescue RuntimeError => error
  msg = 'Something went wrong. Make sure to set your .gitconfig manually'

  VpsCli.errors << error.exception("#{error}\n\n#{msg}")
end