Module: PWN::Plugins::JiraServer

Defined in:
lib/pwn/plugins/jira_server.rb

Overview

This plugin is used for interacting w/ on-prem Jira Server’s REST API using the ‘rest’ browser type of PWN::Plugins::TransparentBrowser. This is based on the following Jira API Specification: developer.atlassian.com/server/jira/platform/rest-apis/

Constant Summary collapse

@@logger =
PWN::Plugins::PWNLogger.create

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <[email protected]>



510
511
512
513
514
# File 'lib/pwn/plugins/jira_server.rb', line 510

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.create_issue(opts = {}) ⇒ Object

Supported Method Parameters

issue_resp = PWN::Plugins::JiraServer.create_issue(

base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
token: 'required - personal access token',
project_key: 'required - project key (e.g. PWN)',
summary: 'required - summary of the issue (e.g. Epic for PWN-1337)',
issue_type: 'required - issue type (e.g. :epic, :story, :bug)',
description: 'optional - description of the issue',
epic_name: 'optional - name of the epic',
additional_fields: 'optional - additional fields to set in the issue (e.g. labels, components, custom fields, etc.)'
attachments: 'optional - array of attachment paths to upload to the issue (e.g. ["/tmp/file1.txt", "/tmp/file2.txt"])',
comment: 'optional - comment to add to the issue (e.g. "This is a comment")'

)



231
232
233
234
235
236
237
238
239
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/pwn/plugins/jira_server.rb', line 231

public_class_method def self.create_issue(opts = {})
  base_api_uri = opts[:base_api_uri]

  token = opts[:token]
  token ||= PWN::Plugins::AuthenticationHelper.mask_password(
    prompt: 'Personal Access Token'
  )
  project_key = opts[:project_key]
  raise 'ERROR: project_key cannot be nil.' if project_key.nil?

  summary = opts[:summary]
  raise 'ERROR: summary cannot be nil.' if summary.nil?

  issue_type = opts[:issue_type]
  raise 'ERROR: issue_type values must be one of :epic, :story, or :bug.' unless %i[epic story bug].include?(issue_type)

  description = opts[:description].to_s.scrub

  additional_fields = opts[:additional_fields] ||= { fields: {} }
  raise 'ERROR: additional_fields Hash must contain a :fields key that is also a Hash.' unless additional_fields.is_a?(Hash) && additional_fields.key?(:fields) && additional_fields[:fields].is_a?(Hash)

  attachments = opts[:attachments] ||= []
  raise 'ERROR: attachments must be an Array.' unless attachments.is_a?(Array)

  comment = opts[:comment]

  all_fields = get_all_fields(base_api_uri: base_api_uri, token: token)
  epic_name_field_key = all_fields.find { |field| field[:name] == 'Epic Name' }[:id]

  epic_name = opts[:epic_name]

  http_body = {
    fields: {
      project: {
        key: project_key
      },
      summary: summary,
      issuetype: {
        name: issue_type.to_s.capitalize
      },
      "#{epic_name_field_key}": epic_name,
      description: description
    }
  }

  http_body[:fields].merge!(additional_fields[:fields])

  issue_resp = rest_call(
    http_method: :post,
    base_api_uri: base_api_uri,
    token: token,
    rest_call: 'issue',
    http_body: http_body
  )
  issue = issue_resp[:key]

  if attachments.any?
    attachments.each do |attachment|
      raise "ERROR: #{attachment} not found." unless File.exist?(attachment)

      http_body = {
        multipart: true,
        file: File.new(attachment, 'rb')
      }

      rest_call(
        http_method: :post,
        base_api_uri: base_api_uri,
        token: token,
        rest_call: "issue/#{issue}/attachments",
        http_body: http_body
      )
    end
  end

  if comment
    issue_comment(
      base_api_uri: base_api_uri,
      token: token,
      issue: issue,
      comment_action: :add,
      comment: comment
    )
  end

  get_issue(
    base_api_uri: base_api_uri,
    token: token,
    issue: issue
  )
rescue StandardError => e
  raise e
end

.delete_attachment(opts = {}) ⇒ Object

Supported Method Parameters

issue_resp = PWN::Plugins::JiraServer.delete_attachment(

base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
token: 'required - personal access token',
id: 'required - attachment ID to delete (e.g. 10000) found in #get_issue method'

)



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/pwn/plugins/jira_server.rb', line 487

public_class_method def self.delete_attachment(opts = {})
  base_api_uri = opts[:base_api_uri]

  token = opts[:token]
  token ||= PWN::Plugins::AuthenticationHelper.mask_password(
    prompt: 'Personal Access Token'
  )

  id = opts[:id]
  raise 'ERROR: attachment_id cannot be nil.' if id.nil?

  rest_call(
    http_method: :delete,
    base_api_uri: base_api_uri,
    token: token,
    rest_call: "attachment/#{id}"
  )
rescue StandardError => e
  raise e
end

.delete_issue(opts = {}) ⇒ Object

Supported Method Parameters

issue_resp = PWN::Plugins::JiraServer.delete_issue(

base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
token: 'required - personal access token',
issue: 'required - issue to delete (e.g. Bug, Issue, Story, or Epic ID)'

)



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/pwn/plugins/jira_server.rb', line 459

public_class_method def self.delete_issue(opts = {})
  base_api_uri = opts[:base_api_uri]

  token = opts[:token]
  token ||= PWN::Plugins::AuthenticationHelper.mask_password(
    prompt: 'Personal Access Token'
  )

  issue = opts[:issue]
  raise 'ERROR: issue cannot be nil.' if issue.nil?

  rest_call(
    http_method: :delete,
    base_api_uri: base_api_uri,
    token: token,
    rest_call: "issue/#{issue}"
  )
rescue StandardError => e
  raise e
end

.get_all_fields(opts = {}) ⇒ Object

Supported Method Parameters

all_fields = PWN::Plugins::JiraServer.get_all_fields(

base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
token: 'required - personal access token'

)



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pwn/plugins/jira_server.rb', line 135

public_class_method def self.get_all_fields(opts = {})
  base_api_uri = opts[:base_api_uri]

  token = opts[:token]
  token ||= PWN::Plugins::AuthenticationHelper.mask_password(
    prompt: 'Personal Access Token'
  )

  rest_call(
    base_api_uri: base_api_uri,
    token: token,
    rest_call: 'field'
  )
rescue StandardError => e
  raise e
end

.get_issue(opts = {}) ⇒ Object

Supported Method Parameters

issue_resp = PWN::Plugins::JiraServer.get_issue(

base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
token: 'required - personal access token',
issue: 'required - issue to lookup (e.g. Bug, Issue, Story, or Epic ID)',
params: 'optional - additional parameters to pass in the URI (e.g. fields, expand, etc.)'

)



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/pwn/plugins/jira_server.rb', line 194

public_class_method def self.get_issue(opts = {})
  base_api_uri = opts[:base_api_uri]

  token = opts[:token]
  token ||= PWN::Plugins::AuthenticationHelper.mask_password(
    prompt: 'Personal Access Token'
  )

  issue = opts[:issue]
  params = opts[:params]

  raise 'ERROR: issue cannot be nil.' if issue.nil?

  rest_call(
    base_api_uri: base_api_uri,
    token: token,
    rest_call: "issue/#{issue}",
    params: params
  )
rescue StandardError => e
  raise e
end

.get_user(opts = {}) ⇒ Object

Supported Method Parameters

user = PWN::Plugins::JiraServer.get_user(

base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
token: 'required - personal access token',
username: 'required - username to lookup (e.g. jane.doe)',
params: 'optional - additional parameters to pass in the URI (e.g. expand, etc.)'

)



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
# File 'lib/pwn/plugins/jira_server.rb', line 160

public_class_method def self.get_user(opts = {})
  base_api_uri = opts[:base_api_uri]

  token = opts[:token]
  token ||= PWN::Plugins::AuthenticationHelper.mask_password(
    prompt: 'Personal Access Token'
  )

  username = opts[:username]
  raise 'ERROR: username cannot be nil.' if username.nil?

  params = { key: username }
  additional_params = opts[:params]

  params.merge!(additional_params) if additional_params.is_a?(Hash)

  rest_call(
    base_api_uri: base_api_uri,
    token: token,
    rest_call: 'user',
    params: params
  )
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/pwn/plugins/jira_server.rb', line 518

public_class_method def self.help
  puts "USAGE:
    all_fields = #{self}.get_all_fields(
      base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
      token: 'required - personal access token'
    )

    user = #{self}.get_user(
      base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
      token: 'required - personal access token',
      username: 'required - username to lookup (e.g. jane.doe')',
      params: 'optional - additional parameters to pass in the URI (e.g. expand, etc.)'
    )

    issue_resp = #{self}.get_issue(
      base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
      token: 'required - personal access token',
      issue: 'required - issue to lookup (e.g. Bug, Issue, Story, or Epic ID)',
      params: 'optional - additional parameters to pass in the URI'
    )

    issue_resp = #{self}.create_issue(
      base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
      token: 'required - personal access token',
      project_key: 'required - project key (e.g. PWN)',
      summary: 'required - summary of the issue (e.g. Epic for PWN-1337)',
      issue_type: 'required - issue type (e.g. :epic, :story, :bug)',
      description: 'optional - description of the issue',
      epic_name: 'optional - name of the epic',
      additional_fields: 'optional - additional fields to set in the issue (e.g. labels, components, custom fields, etc.)',
      attachments: 'optional - array of attachment paths to upload to the issue (e.g. [\"/tmp/file1.txt\", \"/tmp/file2.txt\"])',
      comment: 'optional - comment to add to the issue (e.g. \"This is a comment\")'
    )

    issue_resp = #{self}.update_issue(
      base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
      token: 'required - personal access token',
      issue: 'required - issue to update (e.g. Bug, Issue, Story, or Epic ID)',
      fields: 'required - fields to update in the issue (e.g. summary, description, labels, components, custom fields, etc.)',
      attachments: 'optional - array of attachment paths to upload to the issue (e.g. [\"/tmp/file1.txt\", \"/tmp/file2.txt\"])'
    )

    issue_resp = #{self}.issue_comment(
      base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
      token: 'required - personal access token',
      issue: 'required - issue to comment on (e.g. Bug, Issue, Story, or Epic ID)',
      comment_action: 'required - action to perform on the issue comment (e.g. :delete, :add, :update - Defaults to :add)',
      comment_id: 'optional - comment ID to delete or update (e.g. 10000)',
      author: 'optional - author of the comment (e.g. \"jane.doe\")',
      comment: 'optional - comment to add or update in the issue (e.g. \"This is a comment\")'
    )

    issue_resp = #{self}.delete_issue(
      base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
      token: 'required - personal access token',
      issue: 'required - issue to delete (e.g. Bug, Issue, Story, or Epic ID)'
    )

    issue_resp = #{self}.delete_attachment(
      base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
      token: 'required - personal access token',
      id: 'required - attachment ID to delete (e.g. 10000) found in #get_issue method'
    )

    **********************************************************************
    * For more information on the Jira Server REST API, see:
    * https://developer.atlassian.com/server/jira/platform/rest-apis/

    #{self}.authors
  "
end

.issue_comment(opts = {}) ⇒ Object

Supported Method Parameters

issue_resp = PWN::Plugins::JiraServer.issue_comment(

base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
token: 'required - personal access token',
issue: 'required - issue to delete (e.g. Bug, Issue, Story, or Epic ID)',
comment_action: 'required - action to perform on the issue comment (e.g. :delete, :add, :update - Defaults to :add)',
comment_id: 'optional - comment ID to delete or update (e.g. 10000)',
author: 'optional - author of the comment (e.g. "jane.doe")',
comment: 'optional - comment to add or update in the issue (e.g. "This is a comment")'

)



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/pwn/plugins/jira_server.rb', line 398

public_class_method def self.issue_comment(opts = {})
  base_api_uri = opts[:base_api_uri]

  token = opts[:token]
  token ||= PWN::Plugins::AuthenticationHelper.mask_password(
    prompt: 'Personal Access Token'
  )

  issue = opts[:issue]
  raise 'ERROR: issue cannot be nil.' if issue.nil?

  comment_action = opts[:comment_action] ||= :add
  raise 'ERROR: comment_action must be one of :delete, :add, or :update.' unless %i[delete add update].include?(comment_action)

  comment_id = opts[:comment_id]
  raise 'ERROR: comment_id cannot be nil when comment_action is :delete or :update.' unless %i[delete update].include?(comment_action) || comment_id.nil?

  author = opts[:author]
  comment = opts[:comment].to_s.scrub

  case comment_action
  when :add
    http_method = :post
    rest_call = "issue/#{issue}/comment"
    http_body = { body: comment }
    http_body[:author] = { key: author } if author
  when :delete
    http_method = :delete
    rest_call = "issue/#{issue}/comment/#{comment_id}"
    http_body = nil
  when :update
    http_method = :put
    rest_call = "issue/#{issue}/comment/#{comment_id}"
    http_body = { body: comment }
    http_body[:author] = { key: author } if author
  end

  rest_call(
    http_method: http_method,
    base_api_uri: base_api_uri,
    token: token,
    rest_call: rest_call,
    http_body: http_body
  )

  get_issue(
    base_api_uri: base_api_uri,
    token: token,
    issue: issue
  )
rescue StandardError => e
  raise e
end

.update_issue(opts = {}) ⇒ Object

Supported Method Parameters

issue_resp = PWN::Plugins::JiraServer.update_issue(

base_api_uri: 'required - base URI for Jira (e.g. https:/jira.corp.com/rest/api/latest)',
token: 'required - personal access token',
fields: 'required - fields to update in the issue (e.g. summary, description, labels, components, custom fields, etc.)',
attachments: 'optional - array of attachment paths to upload to the issue (e.g. ["/tmp/file1.txt", "/tmp/file2.txt"])',

)



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/pwn/plugins/jira_server.rb', line 333

public_class_method def self.update_issue(opts = {})
  base_api_uri = opts[:base_api_uri]

  token = opts[:token]
  token ||= PWN::Plugins::AuthenticationHelper.mask_password(
    prompt: 'Personal Access Token'
  )
  issue = opts[:issue]
  raise 'ERROR: project_key cannot be nil.' if issue.nil?

  fields = opts[:fields] ||= { fields: {} }
  raise 'ERROR: fields Hash must contain a :fields key that is also a Hash.' unless fields.is_a?(Hash) && fields.key?(:fields) && fields[:fields].is_a?(Hash)

  attachments = opts[:attachments] ||= []
  raise 'ERROR: attachments must be an Array.' unless attachments.is_a?(Array)

  http_body = fields

  rest_call(
    http_method: :put,
    base_api_uri: base_api_uri,
    token: token,
    rest_call: "issue/#{issue}",
    http_body: http_body
  )

  if attachments.any?
    attachments.each do |attachment|
      raise "ERROR: #{attachment} not found." unless File.exist?(attachment)

      http_body = {
        multipart: true,
        file: File.new(attachment, 'rb')
      }

      rest_call(
        http_method: :post,
        base_api_uri: base_api_uri,
        token: token,
        rest_call: "issue/#{issue}/attachments",
        http_body: http_body
      )
    end
  end

  get_issue(
    base_api_uri: base_api_uri,
    token: token,
    issue: issue
  )
rescue StandardError => e
  raise e
end