class Cased::CLI::Session

Attributes

current[W]
api_record_url[R]

Public: The CLI session record API URL @example

session.api_record_url #=> "https://api.cased.com/cli/sessions/guard_session_1oFqm5GBQYwhH8pfIpnS0A5QgFJ/record"

@return [String, nil]

api_url[R]

Public: The CLI session API URL @example

session.api_url #=> "https://api.cased.com/cli/sessions/guard_session_1oFqm5GBQYwhH8pfIpnS0A5QgFJ"

@return [String, nil]

authentication[R]

@return [Cased::CLI::Authentication]

command[RW]

Public: Command that invoked CLI session. @example

session.command #=> "/usr/local/bin/rails console"

@return [String]

error[R]
forwarded_ip_address[RW]

Public: The forwarded IP V4 or IP V6 address of the user that initiated the CLI session.

@example

session.forwarded_ip_address #=> "1.1.1.1"

@return [String, nil]

guard_application[R]

Public: The CLI application that the CLI session belongs to. @example

session.guard_application #=> {"id" => "guard_application_1oFqltbMqSEtJQKRCAYQNrQoXsS"}

@return [Hash, nil]

id[R]

Public: The CLI session ID @example

session.id #=> "guard_session_1oFqm5GBQYwhH8pfIpnS0A5QgFJ"

@return [String, nil]

ip_address[R]

Public: The client's IP V4 or IP V6 address that initiated the CLI session. @example

session.ip_address #=> "1.1.1.1"

@return [String, nil]

metadata[RW]

Public: Additional user supplied metadata about the CLI session. @example

session.metadata #=> {"hostname" => "Mac.local"}

@return [Hash]

original_command[R]

Public: Cased may filter out sensitive data in the command, we shouldn't execute what is returned from the server.

@return [String, nil]

reason[RW]

Public: The user supplied reason for the CLI session for taking place. @example

session.reason #=> "Investigating customer support ticket."

@return [String, nil]

requester[R]

Public: The Cased user that requested the CLI session. @example

session.requester #=> {"id" => "user_1oFqlROLNRGVLOXJSsHkJiVmylr"}

@return [Hash, nil]

responded_at[R]

Public: The Cased user that requested the CLI session. @example

session.responded_at #=> "2021-02-10 12:08:44 -0800"

@return [Time, nil]

responder[R]

Public: The Cased user that responded to the CLI session. @example

session.responder #=> {"id" => "user_1oFqlROLNRGVLOXJSsHkJiVmylr"}

@return [Hash, nil]

state[R]

Public: The current state the CLI session is in @example

session.api_url #=> "approved"

@return [String, nil]

url[R]

Public: The CLI session web URL @example

session.url #=> "https://api.cased.com/cli/programs/ruby/sessions/guard_session_1oFqm5GBQYwhH8pfIpnS0A5QgFJ"

@return [String, nil]

Public Class Methods

current() click to toggle source

If we're inside of a recorded session we can lookup the session we're in.

# File lib/cased/cli/session.rb, line 24
def self.current
  @current ||= if ENV['GUARD_SESSION_ID']
    Cased::CLI::Session.find(ENV['GUARD_SESSION_ID'])
  end
end
current?() click to toggle source
# File lib/cased/cli/session.rb, line 30
def self.current?
  current.present?
end
find(guard_session_id) click to toggle source
# File lib/cased/cli/session.rb, line 13
def self.find(guard_session_id)
  authentication = Cased::CLI::Authentication.new

  response = Cased.clients.cli.get("cli/sessions/#{guard_session_id}", user_token: authentication.token)
  new.tap do |session|
    session.session = response.body
  end
end
new(reason: nil, command: nil, metadata: {}, authentication: nil) click to toggle source
# File lib/cased/cli/session.rb, line 133
def initialize(reason: nil, command: nil, metadata: {}, authentication: nil)
  @authentication = authentication || Cased::CLI::Authentication.new
  @reason = reason
  @original_command = command || [$PROGRAM_NAME, *ARGV].join(' ')
  @command = @original_command
  @metadata = Cased.config.cli.metadata.merge(metadata)
  @requester = {}
  @responder = {}
  @guard_application = {}
end

Public Instance Methods

approved?() click to toggle source
# File lib/cased/cli/session.rb, line 174
def approved?
  state == 'approved'
end
cancel() click to toggle source
# File lib/cased/cli/session.rb, line 268
def cancel
  response = Cased.clients.cli.post("#{api_url}/cancel", user_token: authentication.token)
  self.session = response.body

  canceled?
end
canceled?() click to toggle source
# File lib/cased/cli/session.rb, line 182
def canceled?
  state == 'canceled'
end
cased_category() click to toggle source
# File lib/cased/cli/session.rb, line 275
def cased_category
  :cli
end
cased_context(category: cased_category) click to toggle source
# File lib/cased/cli/session.rb, line 283
def cased_context(category: cased_category)
  {
    "#{category}_id".to_sym => cased_id,
    category.to_sym => to_s,
  }
end
cased_id() click to toggle source
# File lib/cased/cli/session.rb, line 279
def cased_id
  id
end
create() click to toggle source
# File lib/cased/cli/session.rb, line 240
def create
  return false unless id.nil?

  response = Cased.clients.cli.post('cli/sessions',
    user_token: authentication.token,
    forwarded_ip_address: forwarded_ip_address,
    reason: reason,
    metadata: metadata,
    command: command)
  if response.success?
    self.session = response.body
  else
    case response.body['error']
    when 'reason_required'
      @error = :reason_required
    when 'unauthorized'
      @error = :unauthorized
    when 'reauthenticate'
      @error = :reauthenticate
    else
      @error = true
      return false
    end
  end

  response.success?
end
denied?() click to toggle source
# File lib/cased/cli/session.rb, line 178
def denied?
  state == 'denied'
end
error?() click to toggle source
# File lib/cased/cli/session.rb, line 197
def error?
  !error.nil?
end
reason_required?() click to toggle source
# File lib/cased/cli/session.rb, line 205
def reason_required?
  error == :reason_required || guard_application.dig('settings', 'reason_required')
end
reauthenticate?() click to toggle source
# File lib/cased/cli/session.rb, line 213
def reauthenticate?
  error == :reauthenticate
end
record() click to toggle source
# File lib/cased/cli/session.rb, line 221
def record
  return false unless recordable? && record_output?

  Cased::CLI::Log.log 'CLI session is now recording'

  recorder = Cased::CLI::Recorder.new(original_command.split(' '), env: {
    'GUARD_SESSION_ID' => id,
    'GUARD_APPLICATION_ID' => guard_application.fetch('id'),
    'GUARD_USER_TOKEN' => requester.fetch('id'),
  })
  recorder.start

  Cased.clients.cli.put(api_record_url,
    recording: recorder.writer.to_cast,
    user_token: authentication.token)

  Cased::CLI::Log.log 'CLI session recorded'
end
record_output?() click to toggle source
# File lib/cased/cli/session.rb, line 217
def record_output?
  guard_application.dig('settings', 'record_output') || false
end
recordable?() click to toggle source
# File lib/cased/cli/session.rb, line 290
def recordable?
  STDOUT.isatty
end
refresh() click to toggle source
# File lib/cased/cli/session.rb, line 190
def refresh
  return false unless api_url

  response = Cased.clients.cli.get(api_url, user_token: authentication.token)
  self.session = response.body
end
requested?() click to toggle source
# File lib/cased/cli/session.rb, line 170
def requested?
  state == 'requested'
end
session=(session) click to toggle source
# File lib/cased/cli/session.rb, line 152
def session=(session)
  @error = nil
  @id = session.fetch('id')
  @api_url = session.fetch('api_url')
  @api_record_url = session.fetch('api_record_url')
  @url = session.fetch('url')
  @state = session.fetch('state')
  @command = session.fetch('command')
  @metadata = session.fetch('metadata')
  @reason = session.fetch('reason')
  @forwarded_ip_address = session.fetch('forwarded_ip_address')
  @ip_address = session.fetch('ip_address')
  @requester = session.fetch('requester')
  @responded_at = session['responded_at']
  @responder = session['responder'] || {}
  @guard_application = session.fetch('guard_application')
end
success?() click to toggle source
# File lib/cased/cli/session.rb, line 201
def success?
  id && !error?
end
timed_out?() click to toggle source
# File lib/cased/cli/session.rb, line 186
def timed_out?
  state == 'timed_out'
end
to_param() click to toggle source
# File lib/cased/cli/session.rb, line 148
def to_param
  id
end
to_s() click to toggle source
# File lib/cased/cli/session.rb, line 144
def to_s
  command
end
unauthorized?() click to toggle source
# File lib/cased/cli/session.rb, line 209
def unauthorized?
  error == :unauthorized
end