Class: Cased::CLI::Session

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/cased/cli/session.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#cased, #cased_human, #cased_payload

Constructor Details

#initialize(reason: nil, command: nil, metadata: {}, authentication: nil) ⇒ Session

Returns a new instance of Session.



133
134
135
136
137
138
139
140
141
142
# 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..merge()
  @requester = {}
  @responder = {}
  @guard_application = {}
end

Class Attribute Details

.currentObject

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



24
25
26
27
28
# 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

Instance Attribute Details

#api_record_urlString? (readonly)

Public: The CLI session record API URL

Examples:

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

Returns:

  • (String, nil)


63
64
65
# File 'lib/cased/cli/session.rb', line 63

def api_record_url
  @api_record_url
end

#api_urlString? (readonly)

Public: The CLI session API URL

Examples:

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

Returns:

  • (String, nil)


57
58
59
# File 'lib/cased/cli/session.rb', line 57

def api_url
  @api_url
end

#authenticationCased::CLI::Authentication (readonly)



39
40
41
# File 'lib/cased/cli/session.rb', line 39

def authentication
  @authentication
end

#commandString

Public: Command that invoked CLI session.

Examples:

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

Returns:

  • (String)


75
76
77
# File 'lib/cased/cli/session.rb', line 75

def command
  @command
end

#forwarded_ip_addressString?

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

Examples:

session.forwarded_ip_address #=> "1.1.1.1"

Returns:

  • (String, nil)


95
96
97
# File 'lib/cased/cli/session.rb', line 95

def forwarded_ip_address
  @forwarded_ip_address
end

#guard_applicationHash? (readonly)

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

Examples:

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

Returns:

  • (Hash, nil)


125
126
127
# File 'lib/cased/cli/session.rb', line 125

def guard_application
  @guard_application
end

#idString? (readonly)

Public: The CLI session ID

Examples:

session.id #=> "guard_session_1oFqm5GBQYwhH8pfIpnS0A5QgFJ"

Returns:

  • (String, nil)


45
46
47
# File 'lib/cased/cli/session.rb', line 45

def id
  @id
end

#ip_addressString? (readonly)

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

Examples:

session.ip_address #=> "1.1.1.1"

Returns:

  • (String, nil)


101
102
103
# File 'lib/cased/cli/session.rb', line 101

def ip_address
  @ip_address
end

#metadataHash

Public: Additional user supplied metadata about the CLI session.

Examples:

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

Returns:

  • (Hash)


81
82
83
# File 'lib/cased/cli/session.rb', line 81

def 
  @metadata
end

#original_commandString? (readonly)

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

Returns:

  • (String, nil)


131
132
133
# File 'lib/cased/cli/session.rb', line 131

def original_command
  @original_command
end

#reasonString?

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

Examples:

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

Returns:

  • (String, nil)


87
88
89
# File 'lib/cased/cli/session.rb', line 87

def reason
  @reason
end

#requesterHash? (readonly)

Public: The Cased user that requested the CLI session.

Examples:

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

Returns:

  • (Hash, nil)


107
108
109
# File 'lib/cased/cli/session.rb', line 107

def requester
  @requester
end

#responded_atTime? (readonly)

Public: The Cased user that requested the CLI session.

Examples:

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

Returns:

  • (Time, nil)


113
114
115
# File 'lib/cased/cli/session.rb', line 113

def responded_at
  @responded_at
end

#responderHash? (readonly)

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

Examples:

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

Returns:

  • (Hash, nil)


119
120
121
# File 'lib/cased/cli/session.rb', line 119

def responder
  @responder
end

#stateString? (readonly)

Public: The current state the CLI session is in

Examples:

session.api_url #=> "approved"

Returns:

  • (String, nil)


69
70
71
# File 'lib/cased/cli/session.rb', line 69

def state
  @state
end

#urlString? (readonly)

Public: The CLI session web URL

Examples:

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

Returns:

  • (String, nil)


51
52
53
# File 'lib/cased/cli/session.rb', line 51

def url
  @url
end

Class Method Details

.current?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cased/cli/session.rb', line 30

def self.current?
  current.present?
end

.find(guard_session_id) ⇒ Object



13
14
15
16
17
18
19
20
# 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

Instance Method Details

#approved?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/cased/cli/session.rb', line 174

def approved?
  state == 'approved'
end

#cancelObject



268
269
270
271
272
273
# 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?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/cased/cli/session.rb', line 182

def canceled?
  state == 'canceled'
end

#cased_categoryObject



275
276
277
# File 'lib/cased/cli/session.rb', line 275

def cased_category
  :cli
end

#cased_context(category: cased_category) ⇒ Object



283
284
285
286
287
288
# 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_idObject



279
280
281
# File 'lib/cased/cli/session.rb', line 279

def cased_id
  id
end

#createObject



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# 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: ,
    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?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/cased/cli/session.rb', line 178

def denied?
  state == 'denied'
end

#error?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/cased/cli/session.rb', line 197

def error?
  !error.nil?
end

#reason_required?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/cased/cli/session.rb', line 205

def reason_required?
  error == :reason_required || guard_application.dig('settings', 'reason_required')
end

#reauthenticate?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/cased/cli/session.rb', line 213

def reauthenticate?
  error == :reauthenticate
end

#recordObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# 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?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/cased/cli/session.rb', line 217

def record_output?
  guard_application.dig('settings', 'record_output') || false
end

#recordable?Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/cased/cli/session.rb', line 290

def recordable?
  $stdout.isatty
end

#refreshObject



190
191
192
193
194
195
# 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?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/cased/cli/session.rb', line 170

def requested?
  state == 'requested'
end

#session=(session) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/cased/cli/session.rb', line 201

def success?
  id && !error?
end

#timed_out?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/cased/cli/session.rb', line 186

def timed_out?
  state == 'timed_out'
end

#to_paramObject



148
149
150
# File 'lib/cased/cli/session.rb', line 148

def to_param
  id
end

#to_sObject



144
145
146
# File 'lib/cased/cli/session.rb', line 144

def to_s
  command
end

#unauthorized?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/cased/cli/session.rb', line 209

def unauthorized?
  error == :unauthorized
end