Class: Travis::CLI::Sshkey

Inherits:
RepoCommand show all
Defined in:
lib/travis/cli/sshkey.rb

Constant Summary

Constants inherited from RepoCommand

RepoCommand::GIT_REGEX, RepoCommand::TRAVIS

Constants inherited from Command

Command::DAY, Command::HOUR, Command::MINUTE, Command::WEEK

Constants included from Tools::Assets

Tools::Assets::BASE

Instance Attribute Summary

Attributes inherited from RepoCommand

#slug

Attributes inherited from ApiCommand

#enterprise_name, #session

Attributes inherited from Command

#arguments, #config, #debug, #force_interactive, #formatter, #input, #output

Instance Method Summary collapse

Methods inherited from RepoCommand

#repository, #setup

Methods inherited from ApiCommand

#authenticate, #detected_endpoint?, #endpoint_config, #enterprise?, #initialize, #pro?, #setup, #sync

Methods included from Travis::Client::Methods

#access_token, #access_token=, #account, #accounts, #api_endpoint, #api_endpoint=, #artifact, #broadcasts, #build, #cancel, #explicit_api_endpoint?, #github_auth, #hooks, #job, #lint, #listen, #logout, #regenerate_token, #remove_token, #repo, #repos, #restart, #user

Methods inherited from Command

abstract, abstract?, #check_completion, #check_ruby, #check_version, command_name, #command_name, #debug?, description, #error, #execute, #help, #info, #initialize, #last_check, #on_signal, #parse, #say, #setup, skip, subcommands, #terminal, #time, #usage, #usage_for, #warn, #write_to

Methods included from Tools::Assets

asset, asset_path

Methods included from Parser

#new, #on, #on_initialize

Constructor Details

This class inherits a constructor from Travis::CLI::ApiCommand

Instance Method Details

#check_access(gh) ⇒ Object



98
99
100
101
102
# File 'lib/travis/cli/sshkey.rb', line 98

def check_access(gh)
  gh["repos/#{slug}"]
rescue GH::Error
  error "GitHub account has no read access to #{color slug, :bold}"
end

#delete_keyObject



47
48
49
50
51
52
53
54
# File 'lib/travis/cli/sshkey.rb', line 47

def delete_key
  return if interactive? && !danger_zone?("Remove SSH key for #{color slug, :info}?")

  say "Removing ssh key for #{color slug, :info}"
  ssh_key.delete
rescue Travis::Client::NotFound
  warn 'no key found to remove'
end

#display_keyObject



30
31
32
33
34
35
36
# File 'lib/travis/cli/sshkey.rb', line 30

def display_key
  say "Current SSH key: #{color(ssh_key.description, :info)}"
  say "Finger print:    #{color(ssh_key.fingerprint, :info)}"
rescue Travis::Client::NotFound
  say 'No custom SSH key installed.'
  exit 1 if check?
end

#generate_keyObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/travis/cli/sshkey.rb', line 56

def generate_key
  access_token = nil
  github.with_token do |token|
    access_token = github_auth(token)
  end
  raise Travis::Client::GitHubLoginFailed, 'all GitHub tokens given were invalid' unless access_token

  gh = GH.with(token: github_token)
   = gh['user']['login']
  check_access(gh)
  empty_line

  say 'Generating RSA key.'
  private_key        = Tools::SSLKey.generate_rsa
  self.description ||= "key for fetching dependencies for #{slug} via #{}"

  say 'Uploading public key to GitHub.'
  gh.post('/user/keys', title: "#{description} (Travis CI)",
                        key: Tools::SSLKey.rsa_ssh(private_key.public_key))

  say 'Uploading private key to Travis CI.'
  ssh_key.update(value: private_key.to_s, description:)

  empty_line
  say 'You can store the private key to reuse it for other repositories (travis sshkey --upload FILE).'
  return unless agree('Store private key? ') { |q| q.default = 'no' }

  path = ask('Path: ') { |q| q.default = 'id_travis_rsa' }
  File.write(path, private_key.to_s)
end

#githubObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/travis/cli/sshkey.rb', line 104

def github
  @github ||= begin
    load_gh
    Tools::Github.new(session.config['github']) do |g|
      g.note          = "token for fetching dependencies for #{slug} (Travis CI)"
      g.explode       = explode?
      g.github_token  = github_token
      g.  = proc {  }
      g.debug         = proc { |log| debug(log) }
      g.after_tokens  = proc { g.explode = true and error('no suitable github token found') }
    end
  end
end

#login_headerObject



118
119
120
121
122
# File 'lib/travis/cli/sshkey.rb', line 118

def 
  say 'GitHub deprecated its Authorizations API exchanging a password for a token.'
  say 'Please visit https://github.blog/2020-07-30-token-authentication-requirements-for-api-and-git-operations for more information.'
  say "Try running with #{color('--github-token', :info)} or #{color('--auto-token', :info)} ."
end

#remove_passphrase(value) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/travis/cli/sshkey.rb', line 87

def remove_passphrase(value)
  return value unless Tools::SSLKey.has_passphrase? value
  return Tools::SSLKey.remove_passphrase(value, passphrase) || error('wrong pass phrase') if passphrase

  error 'Key is encrypted, but missing --passphrase option' unless interactive?
  say 'The private key is protected by a pass phrase.'
  result = Tools::SSLKey.remove_passphrase(value, ask('Enter pass phrase: ') { |q| q.echo = '*' }) until result
  empty_line
  result
end

#runObject



22
23
24
25
26
27
28
# File 'lib/travis/cli/sshkey.rb', line 22

def run
  delete_key                            if delete?
  update_key File.read(upload), upload  if upload?
  update_key $stdin.read, 'stdin'       if stdin?
  generate_key                          if generate?
  display_key
end

#update_key(value, file) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/travis/cli/sshkey.rb', line 38

def update_key(value, file)
  error "#{file} does not look like a private key" unless value.lines.first =~ /PRIVATE KEY/
  value = remove_passphrase(value)
  self.description ||= ask('Key description: ') { |q| q.default = 'Custom Key' } if interactive?
  say "Updating ssh key for #{color slug, :info} with key from #{color file, :info}"
  empty_line
  ssh_key.update(value:, description: description || file)
end