Class: Aptible::CLI::Agent
- Inherits:
-
Thor
- Object
- Thor
- Aptible::CLI::Agent
- Includes:
- Helpers::Ssh, Helpers::System, Helpers::Token, Subcommands::Apps, Subcommands::Backup, Subcommands::Config, Subcommands::DB, Subcommands::Deploy, Subcommands::Domains, Subcommands::Endpoints, Subcommands::Environment, Subcommands::Inspect, Subcommands::Logs, Subcommands::Operation, Subcommands::Ps, Subcommands::Rebuild, Subcommands::Restart, Subcommands::SSH, Subcommands::Services, Thor::Actions
- Defined in:
- lib/aptible/cli/agent.rb
Constant Summary
Constants included from Subcommands::Deploy
Subcommands::Deploy::DOCKER_IMAGE_DEPLOY_ARGS, Subcommands::Deploy::NULL_SHA1
Constants included from Helpers::Token
Class Method Summary collapse
-
.exit_on_failure? ⇒ Boolean
Forward return codes on failures.
Instance Method Summary collapse
-
#initialize ⇒ Agent
constructor
A new instance of Agent.
- #login ⇒ Object
- #version ⇒ Object
Methods included from Subcommands::Endpoints
Methods included from Subcommands::Inspect
Methods included from Subcommands::Operation
Methods included from Subcommands::Backup
Methods included from Subcommands::SSH
Methods included from Subcommands::Services
Methods included from Subcommands::Restart
Methods included from Subcommands::Deploy
Methods included from Subcommands::Rebuild
Methods included from Subcommands::Ps
Methods included from Subcommands::Logs
Methods included from Subcommands::Environment
Methods included from Subcommands::Domains
Methods included from Subcommands::DB
Methods included from Subcommands::Config
Methods included from Subcommands::Apps
Methods included from Helpers::System
Methods included from Helpers::Ssh
#connect_to_ssh_portal, #exit_with_ssh_portal, #with_ssh_cmd
Methods included from Helpers::Token
#current_token_hash, #fetch_token, #save_token, #token_file
Constructor Details
Class Method Details
.exit_on_failure? ⇒ Boolean
Forward return codes on failures.
65 66 67 |
# File 'lib/aptible/cli/agent.rb', line 65 def self.exit_on_failure? true end |
Instance Method Details
#login ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/aptible/cli/agent.rb', line 93 def login if [:sso] begin token = [:sso] token = ask('Paste token copied from Dashboard:') if token == 'sso' Base64.urlsafe_decode64(token.split('.').first) save_token(token) CLI.logger.info "Token written to #{token_file}" return rescue StandardError raise Thor::Error, 'Invalid token provided for SSO' end end email = [:email] || ask('Email: ') password = [:password] || ask_then_line( 'Password: ', echo: false ) = { email: email, password: password } otp_token = [:otp_token] [:otp_token] = otp_token if otp_token begin lifetime = '1w' lifetime = '12h' if [:otp_token] || [:u2f] lifetime = [:lifetime] if [:lifetime] duration = ChronicDuration.parse(lifetime) if duration.nil? raise Thor::Error, "Invalid token lifetime requested: #{lifetime}" end [:expires_in] = duration token = Aptible::Auth::Token.create() rescue OAuth2::Error => e # If a MFA is require but a token wasn't provided, # prompt the user for MFA authentication and retry if e.code != 'otp_token_required' raise Thor::Error, 'Could not authenticate with given ' \ "credentials: #{e.code}" end u2f = (e.response.parsed['exception_context'] || {})['u2f'] q = Queue.new mfa_threads = [] # If the user has added a security key and their computer supports it, # allow them to use it if u2f && !which('u2f-host').nil? origin = Aptible::Auth::Resource.new.get.href app_id = Aptible::Auth::Resource.new.utf_trusted_facets.href challenge = u2f.fetch('challenge') devices = u2f.fetch('devices').map do |dev| Helpers::SecurityKey::Device.new( dev.fetch('version'), dev.fetch('key_handle') ) end puts 'Enter your 2FA token or touch your Security Key once it ' \ 'starts blinking.' mfa_threads << Thread.new do [:u2f] = Helpers::SecurityKey.authenticate( origin, app_id, challenge, devices ) puts '' q.push(nil) end end mfa_threads << Thread.new do [:otp_token] = [:otp_token] || ask( '2FA Token: ' ) q.push(nil) end # Block until one of the threads completes q.pop mfa_threads.each do |thr| sleep 0.5 until thr.status != 'run' thr.kill end.each(&:join) retry end save_token(token.access_token) CLI.logger.info "Token written to #{token_file}" lifetime_format = { units: 2, joiner: ', ' } token_lifetime = (token.expires_at - token.created_at).round expires_in = ChronicDuration.output(token_lifetime, lifetime_format) CLI.logger.info "This token will expire after #{expires_in} " \ '(use --lifetime to customize)' end |