Class: Aptible::CLI::Agent
- Inherits:
-
Thor
- Object
- Thor
- Aptible::CLI::Agent
- Includes:
- Helpers::ConfigPath, Helpers::DateHelpers, Helpers::Ssh, Helpers::System, Helpers::Token, Subcommands::Apps, Subcommands::Backup, Subcommands::Config, Subcommands::DB, Subcommands::Deploy, Subcommands::Endpoints, Subcommands::Environment, Subcommands::Inspect, Subcommands::LogDrain, Subcommands::Logs, Subcommands::Maintenance, Subcommands::MetricDrain, Subcommands::Operation, Subcommands::Rebuild, Subcommands::Restart, Subcommands::SSH, Subcommands::Services, Thor::Actions
- Defined in:
- lib/aptible/cli/agent.rb
Constant Summary
Constants included from Subcommands::MetricDrain
Subcommands::MetricDrain::PATH, Subcommands::MetricDrain::SITES
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::Maintenance
#explanation, included, #no_maintenances
Methods included from Subcommands::MetricDrain
Methods included from Subcommands::LogDrain
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::Logs
Methods included from Subcommands::Environment
Methods included from Subcommands::DB
Methods included from Subcommands::Config
Methods included from Subcommands::Apps
Methods included from Helpers::DateHelpers
#utc_date, #utc_datetime, #utc_string
Methods included from Helpers::ConfigPath
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.
75 76 77 |
# File 'lib/aptible/cli/agent.rb', line 75 def self.exit_on_failure? true end |
Instance Method Details
#login ⇒ Object
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 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/aptible/cli/agent.rb', line 103 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 # https://developers.yubico.com/libfido2/Manuals # installation: https://github.com/Yubico/libfido2#installation if u2f && !which('fido2-assert').nil? && !which('fido2-token').nil? origin = Aptible::Auth::Resource.new.get.href app_id = Aptible::Auth::Resource.new.utf_trusted_facets.href challenge = u2f.fetch('challenge') device_info = security_key_device(u2f, app_id) if device_info[:locations].count > 0 && device_info[:device] puts "\nEnter 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, device_info[:device], device_info[:locations] ) puts '' q.push(nil) end 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 |