Module: Zanshin::SDK::Alerts

Included in:
Client
Defined in:
lib/zanshin/alerts.rb

Overview

Zanshin SDK Alerts

Instance Method Summary collapse

Instance Method Details

#create_alert_comment(organization_id, scan_target_id, alert_id, comment) ⇒ Object

Parameters:

  • organization_id (UUID)

    of the organization

  • scan_target_id (UUID)

    of the scan target

  • alert_id (UUID)

    of the alert

  • comment (String)

    in HTML format

Returns:

  • a Object representing the alert comment



354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/zanshin/alerts.rb', line 354

def create_alert_comment(organization_id, scan_target_id, alert_id, comment)
  body = {
    'comment' => comment
  }

  @http.request(
    'POST',
    "/organizations/#{validate_uuid(organization_id)}/scantargets/#{
      validate_uuid(scan_target_id)}/alerts/#{validate_uuid(alert_id)}/comments",
    body.compact
  )
end

#get_alert(alert_id) ⇒ Object

Returns the detailed object that describes an alert [#reference](api.zanshin.tenchisecurity.com/#operation/getAlertById)

Parameters:

  • alert_id (UUID)

    of the alert

Returns:

  • a Object representing the alert



287
288
289
# File 'lib/zanshin/alerts.rb', line 287

def get_alert(alert_id)
  @http.request('GET', "/alerts/#{validate_uuid(alert_id)}")
end

#iter_alert_comments(alert_id) ⇒ Object

Alert Comments Enumerator over the comment of an alert [#reference](api.zanshin.tenchisecurity.com/#operation/listAllAlertComments)

Parameters:

  • alert_id (UUID)

    of the alert

Returns:

  • an Alert Comments Enumerator object



311
312
313
314
315
316
317
# File 'lib/zanshin/alerts.rb', line 311

def iter_alert_comments(alert_id)
  Enumerator.new do |yielder|
    @http.request('GET', "/alerts/#{validate_uuid(alert_id)}/comments").each do |e|
      yielder.yield e
    end
  end
end

#iter_alert_history(alert_id) ⇒ Object

Alert History Enumerator over the history of an alert [#reference](api.zanshin.tenchisecurity.com/#operation/listAllAlertHistory)

Parameters:

  • alert_id (UUID)

    of the alert

Returns:

  • an Alert History Enumerator object



297
298
299
300
301
302
303
# File 'lib/zanshin/alerts.rb', line 297

def iter_alert_history(alert_id)
  Enumerator.new do |yielder|
    @http.request('GET', "/alerts/#{validate_uuid(alert_id)}/history").each do |e|
      yielder.yield e
    end
  end
end

#iter_alerts(organization_id, scan_target_ids: [], rule: nil, states: nil, severities: nil, page_size: 100, language: nil, created_at_start: nil, created_at_end: nil, updated_at_start: nil, updated_at_end: nil) ⇒ Object

Alerts Enumerator of an organization by loading them, transparently paginating on the API [#reference](api.zanshin.tenchisecurity.com/#operation/listAllAlert)

Parameters:

  • organization_id (UUID)

    of the organization

  • scan_target_ids (Array<UUID>) (defaults to: [])

    Optional list of scan target IDs to list alerts from, defaults to all

  • rule (String) (defaults to: nil)

    to filter alerts from (rule), not passing the field will fetch all

  • states (Array<'OPEN', 'ACTIVE', 'IN_PROGRESS', 'RISK_ACCEPTED', 'CLOSED'>) (defaults to: nil)

    Optional list of states to filter returned alerts, defaults to all

  • severities (Array<'CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'INFO'>) (defaults to: nil)

    optional list of severities to filter returned alerts, defaults to all

  • page_size (Integer) (defaults to: 100)

    the number of alerts to load from the API at a time

  • language ('en-US', 'pt-BR') (defaults to: nil)

    Language of the rule will be returned

  • created_at_start (String) (defaults to: nil)

    Search alerts by creation date - greater or equals than

  • created_at_end (String) (defaults to: nil)

    Search alerts by creation date - less or equals than

  • updated_at_start (String) (defaults to: nil)

    Search alerts by update date - greater or equals than

  • updated_at_end (String) (defaults to: nil)

    Search alerts by update date - less or equals than

Returns:

  • an Alerts Enumerator object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zanshin/alerts.rb', line 29

def iter_alerts(organization_id,
                scan_target_ids: [],
                rule: nil,
                states: nil,
                severities: nil,
                page_size: 100,
                language: nil,
                created_at_start: nil,
                created_at_end: nil,
                updated_at_start: nil,
                updated_at_end: nil)
  body = {
    'organizationId' => validate_uuid(organization_id),
    'page' => 0,
    'pageSize' => page_size,
    'scanTargetIds' => scan_target_ids.each { |scan_target_id| validate_uuid(scan_target_id) },
    'rule' => rule,
    'states' => states,
    'severities' => severities,
    'lang' => language,
    'CreatedAtStart' => created_at_start,
    'CreatedAtEnd' => created_at_end,
    'UpdatedAtStart' => updated_at_start,
    'UpdatedAtEnd' => updated_at_end
  }

  Enumerator.new do |yielder|
    loop do
      body['page'] += 1
      data = @http.request('POST', '/alerts', body.compact)
      data['data'].each do |e|
        yielder.yield e
      end
      break if body['page'] == (data['total'] / body['pageSize']).ceil
    end
  end
end

#iter_alerts_following_history(organization_id, following_ids: [], page_size: 100, language: nil, cursor: nil) ⇒ Object

Alerts Following History Enumerator of an organization by loading them, transparently paginating on the API [#reference](api.zanshin.tenchisecurity.com/#operation/listAllAlertsHistoryFollowing)

Parameters:

  • organization_id (UUID)

    of the organization

  • following_ids (Array<UUID>) (defaults to: [])

    Optional list of IDs of organizations you are following to list alerts from, defaults to all

  • page_size (Integer) (defaults to: 100)

    the number of alerts to load from the API at a time

  • language ('en-US', 'pt-BR') (defaults to: nil)

    Language of the rule will be returned

  • cursor (String) (defaults to: nil)

    Alert Cursor of the last alert consumed, when this value is passed, subsequent alert histories will be returned

Returns:

  • an Alerts Following History Enumerator object



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
# File 'lib/zanshin/alerts.rb', line 176

def iter_alerts_following_history(organization_id,
                                  following_ids: [],
                                  page_size: 100,
                                  language: nil,
                                  cursor: nil)
  body = {
    'organizationId' => validate_uuid(organization_id),
    'page' => 0,
    'pageSize' => page_size,
    'followingIds' => following_ids.each { |following_id| validate_uuid(following_id) },
    'lang' => language,
    'cursor' => cursor
  }

  Enumerator.new do |yielder|
    loop do
      body['page'] += 1
      data = @http.request('POST', '/alerts/history/following', body.compact)
      break if data['data'].empty?

      body['cursor'] = data['data'].last['cursor']
      data['data'].each do |e|
        yielder.yield e
      end
    end
  end
end

#iter_alerts_history(organization_id, scan_target_ids: [], page_size: 100, language: nil, cursor: nil) ⇒ Object

Alerts History Enumerator of an organization by loading them, transparently paginating on the API [#reference](api.zanshin.tenchisecurity.com/#operation/listAllAlertsHistory)

Parameters:

  • organization_id (UUID)

    of the organization

  • scan_target_ids (Array<UUID>) (defaults to: [])

    Optional list of scan target IDs to list alerts from, defaults to all

  • page_size (Integer) (defaults to: 100)

    the number of alerts to load from the API at a time

  • language ('en-US', 'pt-BR') (defaults to: nil)

    Language of the rule will be returned

  • cursor (String) (defaults to: nil)

    Alert Cursor of the last alert consumed, when this value is passed, subsequent alert histories will be returned

Returns:

  • an Alerts History Enumerator object



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
# File 'lib/zanshin/alerts.rb', line 136

def iter_alerts_history(organization_id,
                        scan_target_ids: [],
                        page_size: 100,
                        language: nil,
                        cursor: nil)
  body = {
    'organizationId' => validate_uuid(organization_id),
    'page' => 0,
    'pageSize' => page_size,
    'scanTargetIds' => scan_target_ids.each { |scan_target_id| validate_uuid(scan_target_id) },
    'lang' => language,
    'cursor' => cursor
  }

  Enumerator.new do |yielder|
    loop do
      body['page'] += 1
      data = @http.request('POST', '/alerts/history', body.compact)
      break if data['data'].empty?

      body['cursor'] = data['data'].last['cursor']
      data['data'].each do |e|
        yielder.yield e
      end
    end
  end
end

#iter_following_alerts(organization_id, following_ids: [], rule: nil, states: nil, severities: nil, page_size: 100, language: nil, created_at_start: nil, created_at_end: nil, updated_at_start: nil, updated_at_end: nil) ⇒ Object

Alerts Following Enumerator over the following alerts froms organizations being followed by

transparently paginating on the API

[#reference](api.zanshin.tenchisecurity.com/#operation/listFollowingAlerts)

Parameters:

  • organization_id (UUID)

    of the organization

  • following_ids (Array<UUID>) (defaults to: [])

    Optional list of IDs of organizations you are following to list alerts from, defaults to all

  • rule (String) (defaults to: nil)

    to filter alerts from (rule), not passing the field will fetch all

  • states (Array<'OPEN', 'ACTIVE', 'IN_PROGRESS', 'RISK_ACCEPTED', 'CLOSED'>) (defaults to: nil)

    Optional list of states to filter returned alerts, defaults to all

  • severities (Array<'CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'INFO'>) (defaults to: nil)

    optional list of severities to filter returned alerts, defaults to all

  • page_size (Integer) (defaults to: 100)

    the number of alerts to load from the API at a time

  • language ('en-US', 'pt-BR') (defaults to: nil)

    Language of the rule will be returned

  • created_at_start (String) (defaults to: nil)

    Search alerts by creation date - greater or equals than

  • created_at_end (String) (defaults to: nil)

    Search alerts by creation date - less or equals than

  • updated_at_start (String) (defaults to: nil)

    Search alerts by update date - greater or equals than

  • updated_at_end (String) (defaults to: nil)

    Search alerts by update date - less or equals than

Returns:

  • an Alerts Enumerator object



87
88
89
90
91
92
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
# File 'lib/zanshin/alerts.rb', line 87

def iter_following_alerts(organization_id,
                          following_ids: [],
                          rule: nil,
                          states: nil,
                          severities: nil,
                          page_size: 100,
                          language: nil,
                          created_at_start: nil,
                          created_at_end: nil,
                          updated_at_start: nil,
                          updated_at_end: nil)
  body = {
    'organizationId' => validate_uuid(organization_id),
    'page' => 0,
    'pageSize' => page_size,
    'followingIds' => following_ids.each { |following_id| validate_uuid(following_id) },
    'rule' => rule,
    'states' => states,
    'severities' => severities,
    'lang' => language,
    'CreatedAtStart' => created_at_start,
    'CreatedAtEnd' => created_at_end,
    'UpdatedAtStart' => updated_at_start,
    'UpdatedAtEnd' => updated_at_end
  }

  Enumerator.new do |yielder|
    loop do
      body['page'] += 1
      data = @http.request('POST', '/alerts/following', body.compact)
      data['data'].each do |e|
        yielder.yield e
      end
      break if body['page'] == (data['total'] / body['pageSize']).ceil
    end
  end
end

#iter_grouped_alerts(organization_id, scan_target_ids: [], states: nil, severities: nil, page_size: 100) ⇒ Object

Grouped Alerts Enumerator of an organization by loading them, transparently paginating on the API [#reference](api.zanshin.tenchisecurity.com/#operation/listAllAlertRules)

Parameters:

  • organization_id (UUID)

    of the organization

  • scan_target_ids (Array<UUID>) (defaults to: [])

    Optional list of scan target IDs to list alerts from, defaults to all

  • states (Array<'OPEN', 'ACTIVE', 'IN_PROGRESS', 'RISK_ACCEPTED', 'CLOSED'>) (defaults to: nil)

    Optional list of states to filter returned alerts, defaults to all

  • severities (Array<'CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'INFO'>) (defaults to: nil)

    optional list of severities to filter returned alerts, defaults to all

  • page_size (Integer) (defaults to: 100)

    the number of alerts to load from the API at a time

Returns:

  • an Grouped Alerts Enumerator object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/zanshin/alerts.rb', line 216

def iter_grouped_alerts(organization_id,
                        scan_target_ids: [],
                        states: nil,
                        severities: nil,
                        page_size: 100)
  body = {
    'organizationId' => validate_uuid(organization_id),
    'page' => 0,
    'pageSize' => page_size,
    'scanTargetIds' => scan_target_ids.each { |scan_target_id| validate_uuid(scan_target_id) },
    'states' => states,
    'severities' => severities
  }

  Enumerator.new do |yielder|
    loop do
      body['page'] += 1
      data = @http.request('POST', '/alerts/rules', body.compact)
      data['data'].each do |e|
        yielder.yield e
      end
      break if body['page'] == (data['total'] / body['pageSize']).ceil
    end
  end
end

#iter_grouped_following_alerts(organization_id, following_ids: [], states: nil, severities: nil, page_size: 100) ⇒ Object

Grouped Alerts Following Enumerator of an organization by loading them, transparently paginating on the API [#reference](api.zanshin.tenchisecurity.com/#operation/listAllAlertRulesFollowing)

Parameters:

  • organization_id (UUID)

    of the organization

  • following_ids (Array<UUID>) (defaults to: [])

    Optional list of IDs of organizations you are following to list alerts from, defaults to all

  • states (Array<'OPEN', 'ACTIVE', 'IN_PROGRESS', 'RISK_ACCEPTED', 'CLOSED'>) (defaults to: nil)

    Optional list of states to filter returned alerts, defaults to all

  • severities (Array<'CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'INFO'>) (defaults to: nil)

    optional list of severities to filter returned alerts, defaults to all

  • page_size (Integer) (defaults to: 100)

    the number of alerts to load from the API at a time

Returns:

  • an Grouped Alerts Following Enumerator object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/zanshin/alerts.rb', line 255

def iter_grouped_following_alerts(organization_id,
                                  following_ids: [],
                                  states: nil,
                                  severities: nil,
                                  page_size: 100)
  body = {
    'organizationId' => validate_uuid(organization_id),
    'page' => 0,
    'pageSize' => page_size,
    'followingIds' => following_ids.each { |following_id| validate_uuid(following_id) },
    'states' => states,
    'severities' => severities
  }

  Enumerator.new do |yielder|
    loop do
      body['page'] += 1
      data = @http.request('POST', '/alerts/rules/following', body.compact)
      data['data'].each do |e|
        yielder.yield e
      end
      break if body['page'] == (data['total'] / body['pageSize']).ceil
    end
  end
end

#update_alert(organization_id, scan_target_id, alert_id, state = nil, labels = nil, comment = nil) ⇒ Object

Parameters:

  • organization_id (UUID)

    of the organization

  • scan_target_id (UUID)

    of the scan target

  • alert_id (UUID)

    of the alert

  • state ('OPEN', 'ACTIVE', 'IN_PROGRESS', 'RISK_ACCEPTED') (defaults to: nil)
  • labels (Array<String>) (defaults to: nil)

    Optional labels to alert

  • comment (String) (defaults to: nil)

    Accepted and required only when change :state to ‘IN_PROGRESS’

Returns:

  • a Object representing the alert updated



330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/zanshin/alerts.rb', line 330

def update_alert(organization_id, scan_target_id, alert_id, state = nil, labels = nil, comment = nil)
  body = {
    'state' => state,
    'labels' => labels,
    'comment' => comment
  }

  @http.request(
    'PUT',
    "/organizations/#{validate_uuid(organization_id)}/scantargets/#{
      validate_uuid(scan_target_id)}/alerts/#{validate_uuid(alert_id)}",
    body.compact
  )
end