Module: PWN::Plugins::JiraDataCenter

Defined in:
lib/pwn/plugins/jira_data_center.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]>



602
603
604
605
606
# File 'lib/pwn/plugins/jira_data_center.rb', line 602

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

.clone_issue(opts = {}) ⇒ Object

Supported Method Parameters

issue_resp = PWN::Plugins::JiraDataCenter.clone_issue(

issue: 'required - issue to clone (e.g. Bug, Issue, Story, or Epic ID)',
copy_attachments: 'optional - boolean to indicate whether to copy attachments (Defaults to false)'

)



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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
# File 'lib/pwn/plugins/jira_data_center.rb', line 455

public_class_method def self.clone_issue(opts = {})
  issue = opts[:issue]
  raise 'ERROR: issue cannot be nil.' if issue.nil?

  copy_attachments = opts[:copy_attachments] ||= false

  # Expand editmeta so we have full field set + attachment info
  issue_data = get_issue(issue: issue, params: { expand: 'editmeta' })

  project_key = issue_data[:fields][:project][:key]
  original_summary = issue_data[:fields][:summary]
  summary = "CLONE - #{original_summary}"

  # Determine issue type early
  issue_type = issue_data[:fields][:issuetype][:name].downcase.to_sym
  issue_type_id = issue_data[:fields][:issuetype][:id]

  # Epic name handling (only if original issue is an epic)
  epic_name = nil
  epic_name_field_key = nil
  if issue_type == :epic
    all_fields = get_all_fields
    epic_field_obj = all_fields.find { |field| field[:name] == 'Epic Name' }
    epic_name_field_key = epic_field_obj[:id] if epic_field_obj
    epic_name = issue_data[:fields][epic_name_field_key.to_sym] if epic_name_field_key
  end

  description = issue_data[:fields][:description]

  # Fetch create metadata to understand which fields can be set
  meta = (
    project_key: project_key,
    issue_type_id: issue_type_id
  )
  candidate_array = meta[:values] ||= []

  # Build list of fields we can set on create (operations include 'set' OR empty operations)
  allowed_fields = []
  candidate_array.each do |field_obj|
    next unless field_obj.is_a?(Hash)

    ops = field_obj[:operations] ||= []
    # Permit if 'set' allowed or operations list empty (often still creatable)
    next unless ops.empty? || ops.include?('set')

    field_key = field_obj[:key] || field_obj[:id] || field_obj[:fieldId]
    next if field_key.nil? || field_key.to_s.empty?

    allowed_fields << field_key.to_s
  end

  reserved_fields = i[project summary issuetype description attachment]
  reserved_fields << epic_name_field_key.to_sym if epic_name_field_key

  filtered_fields = {}
  issue_data[:fields].each do |k, v|
    next if v.nil?

    k_str = k.to_s
    next if reserved_fields.include?(k) || reserved_fields.include?(k_str.to_sym)

    next unless allowed_fields.include?(k_str)

    case v
    when Array
      # Preserve minimal identifying attributes for each element; if element is primitive keep as-is
      filtered_fields[k] = v.map do |elem|
        if elem.is_a?(Hash)
          # Retain common identifier keys; if none present keep full hash
          keys_to_keep = i[id key name value]
          elem.keys.any? { |kk| keys_to_keep.include?(kk) } ? elem.slice(*keys_to_keep) : elem
        else
          elem
        end
      end
    when Hash
      keys_to_keep = i[id key name value]
      filtered_fields[k] =
        if v.keys.any? { |kk| keys_to_keep.include?(kk) }
          v.slice(*keys_to_keep)
        else
          v
        end
    else
      filtered_fields[k] = v
    end
  end

  additional_fields = { fields: filtered_fields }

  attachments = []
  if copy_attachments
    attachments_obj = issue_data[:fields][:attachment] ||= []
    attachments_obj.each do |att_obj|
      attachments.push(att_obj[:filename])
    end
  end

  create_issue(
    project_key: project_key,
    summary: summary,
    issue_type: issue_type,
    epic_name: epic_name,
    description: description,
    additional_fields: additional_fields,
    attachments: attachments
  )
rescue StandardError => e
  raise e
end

.create_issue(opts = {}) ⇒ Object

Supported Method Parameters

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

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")'

)



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
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
# File 'lib/pwn/plugins/jira_data_center.rb', line 197

public_class_method def self.create_issue(opts = {})
  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
  epic_name_field_key = all_fields.find { |field| field[:name] == 'Epic Name' }[:id]

  epic_name = opts[:epic_name]
  raise 'ERROR: epic_name cannot be nil when issue_type is :epic.' if issue_type == :epic && epic_name.nil?

  # Attempt create, dynamically omit unlicensed fields on failure
  create_attempts, get_issue_attempts = 1
  max_create_attempts, max_get_issue_attempts = 7
  created_issue = nil
  begin
    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,
      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,
          rest_call: "issue/#{issue}/attachments",
          http_body: http_body
        )
      end
    end

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

    begin
      created_issue = get_issue(issue: issue)
    rescue RuntimeError
      raise 'ERROR: Max attempts reached for retrieving created issue.' if get_issue_attempts > max_get_issue_attempts

      get_issue_attempts += 1
      sleep 3
      retry
    end
  rescue RestClient::ExceptionWithResponse => e
    raise e if create_attempts >= max_create_attempts || e.response.body.to_s.empty?

    json = JSON.parse(e.response.body, symbolize_names: true)
    errors_hash = json[:errors] if json.is_a?(Hash)
    if errors_hash.is_a?(Hash)
      unlicensed_field_keys = errors_hash.select { |_fk, msg| msg.to_s =~ /unlicensed/i }.keys
      if unlicensed_field_keys.any?
        unlicensed_field_keys.each do |fk|
          additional_fields[:fields].delete(fk.to_sym)
          additional_fields[:fields].delete(fk.to_s)
        end
        @@logger.warn("Omitting unlicensed fields: #{unlicensed_field_keys.join(', ')} (attempt #{create_attempts}/#{max_create_attempts}). Retrying issue creation.") if defined?(@@logger)
        create_attempts += 1
        retry
      end
    end
  end

  created_issue
rescue StandardError => e
  raise e
end

.delete_attachment(opts = {}) ⇒ Object

Supported Method Parameters

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

id: 'required - attachment ID to delete (e.g. 10000) found in #get_issue method'

)



588
589
590
591
592
593
594
595
596
597
598
# File 'lib/pwn/plugins/jira_data_center.rb', line 588

public_class_method def self.delete_attachment(opts = {})
  id = opts[:id]
  raise 'ERROR: attachment_id cannot be nil.' if id.nil?

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

.delete_issue(opts = {}) ⇒ Object

Supported Method Parameters

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

issue: 'required - issue to delete (e.g. Bug, Issue, Story, or Epic ID)'

)



571
572
573
574
575
576
577
578
579
580
581
# File 'lib/pwn/plugins/jira_data_center.rb', line 571

public_class_method def self.delete_issue(opts = {})
  issue = opts[:issue]
  raise 'ERROR: issue cannot be nil.' if issue.nil?

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

.get_all_fieldsObject

Supported Method Parameters

all_fields = PWN::Plugins::JiraDataCenter.get_all_fields



136
137
138
139
140
# File 'lib/pwn/plugins/jira_data_center.rb', line 136

public_class_method def self.get_all_fields
  rest_call(rest_call: 'field')
rescue StandardError => e
  raise e
end

.get_issue(opts = {}) ⇒ Object

Supported Method Parameters

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

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.)'

)



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pwn/plugins/jira_data_center.rb', line 171

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

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

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

.get_issue_type_metadata(opts = {}) ⇒ Object

Supported Method Parameters

issue_type_metadata = PWN::Plugins::JiraDataCenter.get_issue_type_metadata(

project_key: 'required - project key (e.g. PWN)',
issue_type_id: 'required - issue type ID (e.g. issue[:fields][:issuetype][:id] from #get_issue method)'

)



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

public_class_method def self.(opts = {})
  project_key = opts[:project_key]
  raise 'ERROR: project_key cannot be nil.' if project_key.nil?

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

  response = { values: [] }
  start_at = 0
  loop do
    params = { startAt: start_at }
    this_resp = rest_call(
      rest_call: "issue/createmeta/#{project_key}/issuetypes/#{issue_type_id}",
      params: params
    )
    max_results = this_resp[:maxResults]
    start_at = this_resp[:startAt]
    total = this_resp[:total]
    is_last = this_resp[:isLast]
    values = this_resp[:values]

    response[:maxResults] = max_results
    response[:startAt] = start_at
    response[:total] = total
    response[:isLast] = is_last
    response[:values].concat(values)
    break if is_last

    start_at += max_results
  end

  response
rescue StandardError => e
  raise e
end

.get_user(opts = {}) ⇒ Object

Supported Method Parameters

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

username: 'required - username to lookup (e.g. jane.doe)',
params: 'optional - additional parameters to pass in the URI (e.g. expand, etc.)'

)



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pwn/plugins/jira_data_center.rb', line 148

public_class_method def self.get_user(opts = {})
  username = opts[:username] || PWN::Plugins::AuthenticationHelper.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(
    rest_call: 'user',
    params: params
  )
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/pwn/plugins/jira_data_center.rb', line 610

public_class_method def self.help
  puts "USAGE:
    all_fields = #{self}.get_all_fields

    user = #{self}.get_user(
      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(
      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.)'
    )

    issue_resp = #{self}.create_issue(
      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(
      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(
      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_type_metadata = #{self}.get_issue_type_metadata(
      project_key: 'required - project key (e.g. PWN)',
      issue_type_id: 'required - issue type ID (e.g. issue[:fields][:issuetype][:id] from #get_issue method)'
    )

    issue_resp = #{self}.clone_issue(
      issue: 'required - issue to clone (e.g. Bug, Issue, Story, or Epic ID)',
      copy_attachments: 'optional - boolean to indicate whether to copy attachments (Defaults to false)'
    )

    issue_resp = #{self}.delete_issue(
      issue: 'required - issue to delete (e.g. Bug, Issue, Story, or Epic ID)'
    )

    issue_resp = #{self}.delete_attachment(
      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::JiraDataCenter.issue_comment(

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")'

)



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/pwn/plugins/jira_data_center.rb', line 366

public_class_method def self.issue_comment(opts = {})
  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,
    rest_call: rest_call,
    http_body: http_body
  )

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

.update_issue(opts = {}) ⇒ Object

Supported Method Parameters

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

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"])',

)



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/pwn/plugins/jira_data_center.rb', line 315

public_class_method def self.update_issue(opts = {})
  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,
    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,
        rest_call: "issue/#{issue}/attachments",
        http_body: http_body
      )
    end
  end

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