Class: Cased::CLI::Session
- Inherits:
-
Object
- Object
- Cased::CLI::Session
show all
- Includes:
- Model
- Defined in:
- lib/cased/cli/session.rb
Class Attribute Summary collapse
-
.current ⇒ Object
If we’re inside of a recorded session we can lookup the session we’re in.
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.metadata.merge(metadata)
@requester = {}
@responder = {}
@guard_application = {}
end
|
Class Attribute Details
.current ⇒ Object
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_url ⇒ String?
Public: The CLI session record API URL
63
64
65
|
# File 'lib/cased/cli/session.rb', line 63
def api_record_url
@api_record_url
end
|
#api_url ⇒ String?
Public: The CLI session API URL
57
58
59
|
# File 'lib/cased/cli/session.rb', line 57
def api_url
@api_url
end
|
39
40
41
|
# File 'lib/cased/cli/session.rb', line 39
def authentication
@authentication
end
|
#command ⇒ String
Public: Command that invoked CLI session.
75
76
77
|
# File 'lib/cased/cli/session.rb', line 75
def command
@command
end
|
#forwarded_ip_address ⇒ String?
Public: The forwarded IP V4 or IP V6 address of the user that initiated the CLI session.
95
96
97
|
# File 'lib/cased/cli/session.rb', line 95
def forwarded_ip_address
@forwarded_ip_address
end
|
#guard_application ⇒ Hash?
Public: The CLI application that the CLI session belongs to.
125
126
127
|
# File 'lib/cased/cli/session.rb', line 125
def guard_application
@guard_application
end
|
#id ⇒ String?
Public: The CLI session ID
45
46
47
|
# File 'lib/cased/cli/session.rb', line 45
def id
@id
end
|
#ip_address ⇒ String?
Public: The client’s IP V4 or IP V6 address that initiated the CLI session.
101
102
103
|
# File 'lib/cased/cli/session.rb', line 101
def ip_address
@ip_address
end
|
Public: Additional user supplied metadata about the CLI session.
81
82
83
|
# File 'lib/cased/cli/session.rb', line 81
def metadata
@metadata
end
|
#original_command ⇒ String?
Public: Cased may filter out sensitive data in the command, we shouldn’t execute what is returned from the server.
131
132
133
|
# File 'lib/cased/cli/session.rb', line 131
def original_command
@original_command
end
|
#reason ⇒ String?
Public: The user supplied reason for the CLI session for taking place.
87
88
89
|
# File 'lib/cased/cli/session.rb', line 87
def reason
@reason
end
|
#requester ⇒ Hash?
Public: The Cased user that requested the CLI session.
107
108
109
|
# File 'lib/cased/cli/session.rb', line 107
def requester
@requester
end
|
#responded_at ⇒ Time?
Public: The Cased user that requested the CLI session.
113
114
115
|
# File 'lib/cased/cli/session.rb', line 113
def responded_at
@responded_at
end
|
#responder ⇒ Hash?
Public: The Cased user that responded to the CLI session.
119
120
121
|
# File 'lib/cased/cli/session.rb', line 119
def responder
@responder
end
|
#state ⇒ String?
Public: The current state the CLI session is in
69
70
71
|
# File 'lib/cased/cli/session.rb', line 69
def state
@state
end
|
#url ⇒ String?
Public: The CLI session web URL
51
52
53
|
# File 'lib/cased/cli/session.rb', line 51
def url
@url
end
|
Class Method Details
.current? ⇒ 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
174
175
176
|
# File 'lib/cased/cli/session.rb', line 174
def approved?
state == 'approved'
end
|
#cancel ⇒ Object
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
182
183
184
|
# File 'lib/cased/cli/session.rb', line 182
def canceled?
state == 'canceled'
end
|
#cased_category ⇒ Object
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_id ⇒ Object
279
280
281
|
# File 'lib/cased/cli/session.rb', line 279
def cased_id
id
end
|
#create ⇒ Object
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: 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
178
179
180
|
# File 'lib/cased/cli/session.rb', line 178
def denied?
state == 'denied'
end
|
#error? ⇒ Boolean
197
198
199
|
# File 'lib/cased/cli/session.rb', line 197
def error?
!error.nil?
end
|
#reason_required? ⇒ 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
213
214
215
|
# File 'lib/cased/cli/session.rb', line 213
def reauthenticate?
error == :reauthenticate
end
|
#record ⇒ Object
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
217
218
219
|
# File 'lib/cased/cli/session.rb', line 217
def record_output?
guard_application.dig('settings', 'record_output') || false
end
|
#recordable? ⇒ Boolean
290
291
292
|
# File 'lib/cased/cli/session.rb', line 290
def recordable?
$stdout.isatty
end
|
#refresh ⇒ Object
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
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
201
202
203
|
# File 'lib/cased/cli/session.rb', line 201
def success?
id && !error?
end
|
#timed_out? ⇒ Boolean
186
187
188
|
# File 'lib/cased/cli/session.rb', line 186
def timed_out?
state == 'timed_out'
end
|
#to_param ⇒ Object
148
149
150
|
# File 'lib/cased/cli/session.rb', line 148
def to_param
id
end
|
#to_s ⇒ Object
144
145
146
|
# File 'lib/cased/cli/session.rb', line 144
def to_s
command
end
|
#unauthorized? ⇒ Boolean
209
210
211
|
# File 'lib/cased/cli/session.rb', line 209
def unauthorized?
error == :unauthorized
end
|