Module: PWN::Plugins::Jenkins

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

Overview

This plugin is used to interact w/ the Jenkins API and can be used to carry out tasks when certain events occur w/in Jenkins.

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.add_job_to_nested_view(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.add_job_to_nested_view(

jenkins_obj: 'required jenkins_obj returned from #connect method',
view_path: 'required view path associate job',
job_name: 'required view path attach to a view',

)



341
342
343
344
345
346
347
348
# File 'lib/pwn/plugins/jenkins.rb', line 341

def self.add_job_to_nested_view(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  view_path = opts[:view_path].to_s.scrub
  job_name = opts[:job_name].to_s.scrub
  jenkins_obj.api_post_request("#{view_path}/addJobToView?name=#{job_name}")
rescue StandardError => e
  raise e
end

.authorsObject

Author(s)

0day Inc. <[email protected]>



444
445
446
447
448
# File 'lib/pwn/plugins/jenkins.rb', line 444

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

.clear_build_queue(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.clear_build_queue(

jenkins_obj: 'required jenkins_obj returned from #connect method',

)



417
418
419
420
421
422
423
424
425
426
# File 'lib/pwn/plugins/jenkins.rb', line 417

public_class_method def self.clear_build_queue(opts = {})
  jenkins_obj = opts[:jenkins_obj]

  jenkins_obj.queue.list.each do |job_name|
    @@logger.info("Clearing #{job_name} Build from Queue")
    jenkins_obj.job.stop_build(job_name)
  end
rescue StandardError => e
  raise e
end

.connect(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.connect(

jenkins_ip: 'required host/ip of Jenkins Server',
port: 'optional tcp port (defaults to 8080),
username: 'optional username (functionality will be limited if ommitted)',
password: 'optional password (functionality will be limited if ommitted)'
identity_file: 'optional ssh private key path to AuthN w/ Jenkins PREFERRED over username/password',
ssl: 'optional connect over TLS (defaults to true),
proxy: 'optional debug proxy rest api requests to jenkins (e.g. "http://127.0.0.1:8080")''

)



24
25
26
27
28
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pwn/plugins/jenkins.rb', line 24

public_class_method def self.connect(opts = {})
  jenkins_ip = opts[:jenkins_ip]
  port = if opts[:port]
           opts[:port].to_i
         else
           8080
         end
  username = opts[:username].to_s.scrub
  base_jenkins_api_uri = "https://#{jenkins_ip}/ase/services".to_s.scrub
  password = opts[:password].to_s.scrub
  identity_file = opts[:identity_file].to_s.scrub
  ssl_bool = if opts[:ssl] == true
               opts[:ssl]
             else
               false
             end

  if opts[:proxy]
    proxy = URI(opts[:proxy])
    proxy_protocol = proxy.scheme
    proxy_ip = proxy.host
    proxy_port = proxy.port
  end

  @@logger.info("Logging into Jenkins Server: #{jenkins_ip}")
  if username == '' && password == ''
    if identity_file == ''
      jenkins_obj = JenkinsApi::Client.new(
        server_ip: jenkins_ip,
        server_port: port,
        follow_redirects: true,
        ssl: ssl_bool,
        proxy_protocol: proxy_protocol,
        proxy_ip: proxy_ip,
        proxy_port: proxy_port
      )
    else
      jenkins_obj = JenkinsApi::Client.new(
        server_ip: jenkins_ip,
        server_port: port,
        identity_file: identity_file,
        follow_redirects: true,
        ssl: ssl_bool,
        proxy_protocol: proxy_protocol,
        proxy_ip: proxy_ip,
        proxy_port: proxy_port
      )
    end
  else
    password = PWN::Plugins::AuthenticationHelper.mask_password if password == ''
    jenkins_obj = JenkinsApi::Client.new(
      server_ip: jenkins_ip,
      server_port: port,
      username: username,
      password: password,
      follow_redirects: true,
      ssl: ssl_bool,
      proxy_protocol: proxy_protocol,
      proxy_ip: proxy_ip,
      proxy_port: proxy_port
    )
  end
  jenkins_obj.system.wait_for_ready
  jenkins_obj
rescue StandardError => e
  raise e
end

.copy_job_no_fail_on_exist(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.copy_job_no_fail_on_exist(

jenkins_obj: 'required jenkins_obj returned from #connect method',
existing_job_name: 'required existing job to copt to new job',
new_job_name: 'required name of new job'

)



357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/pwn/plugins/jenkins.rb', line 357

public_class_method def self.copy_job_no_fail_on_exist(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  existing_job_name = opts[:existing_job_name]
  new_job_name = opts[:new_job_name]

  copy_job_resp = jenkins_obj.job.copy(existing_job_name, new_job_name)
rescue JenkinsApi::Exceptions::JobAlreadyExists => e
  @@logger.warn("Jenkins job: #{new_job_name} already exists")
  e.class
rescue StandardError => e
  raise e
end

.create_nested_view(opts = {}) ⇒ Object

PWN::Plugins::Jenkins.create_nested_view(

jenkins_obj: 'required jenkins_obj returned from #connect method',
view_path: 'required view path create',
create_in_view_path: 'optional creates nested view within an existing nested view, defaults to / views'

)



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
324
325
326
327
328
329
330
331
332
333
# File 'lib/pwn/plugins/jenkins.rb', line 287

public_class_method def self.create_nested_view(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  view_name = opts[:view_name].to_s.scrub
  create_in_view_path = opts[:create_in_view_path].to_s.scrub
  # TODO: pass parameter for modes and use case statement to build dynamically post_body
  # mode = 'hudson.plugins.nested_view.NestedView' # Requires Jenkins Nested View Plugin to Work Properly
  mode = 'hudson.model.ListView'

  post_body = {
    'name' => view_name,
    'mode' => mode,
    'json' => {
      'name' => view_name,
      'mode' => mode
    }.to_json
  }

  root_view_paths_arr = [
    '',
    '/'
  ]

  if root_view_paths_arr.include?(create_in_view_path)
    @@logger.info('Creating Nested View in /...')

    resp = jenkins_obj.api_post_request(
      '/createView',
      post_body
    )
  else
    @@logger.info("Creating Nested View in #{create_in_view_path}...")

    # Example view_path would be '/view/Projects/PROJECT_NAME/view/RELEASES'
    # This is taken out of the Jenkins URI when residing in the view in which
    # you want to create your view...simply drop the domain name.
    resp = jenkins_obj.api_post_request(
      "#{create_in_view_path}/createView",
      post_body
    )
  end
  resp == '302'
rescue JenkinsApi::Exceptions::ViewAlreadyExists => e
  @@logger.warn("Jenkins view: #{view_name} already exists")
  e.class
rescue StandardError => e
  raise e
end

.create_ssh_credential(opts = {}) ⇒ Object

PWN::Plugins::Jenkins.create_ssh_credential(

jenkins_obj: 'required - jenkins_obj returned from #connect method',
username: 'required - username for new credential'
private_key_path: 'required - path of private ssh key for new credential'
key_passphrase: 'optional - private key passphrase for new credential'
credential_id: 'optional but recommended - useful when creating userland jobs',
description: 'optional - description of new credential'
domain: 'optional - defaults to GLOBAL',
scope: 'optional - GLOBAL or SYSTEM (defaults to GLOBAL)'

)



149
150
151
152
153
154
155
156
157
158
159
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
185
186
187
188
189
190
191
192
193
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/jenkins.rb', line 149

public_class_method def self.create_ssh_credential(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  username = opts[:username].to_s.scrub
  private_key_path = opts[:private_key_path].to_s.strip.chomp.scrub
  key_passphrase = opts[:key_passphrase].to_s.scrub
  credential_id = opts[:credential_id].to_s.scrub
  description = opts[:description].to_s.scrub

  if opts[:domain].to_s.strip.chomp.scrub == 'GLOBAL' || opts[:domain].nil?
    uri_path = '/credentials/store/system/domain/_/createCredentials'
  else
    domain = opts[:domain].to_s.strip.chomp.scrub
    uri_path = "/credentials/store/system/domain/#{domain}/createCredentials"
  end

  if opts[:scope].to_s.strip.chomp.scrub == 'SYSTEM'
    scope = 'SYSTEM'
  else
    scope = 'GLOBAL'
  end

  if credential_id == ''
    post_body = {
      'json' => {
        '' => '0',
        'credentials' => {
          'scope' => scope,
          'username' => username,
          'privateKeySource' => {
            'stapler-class' => 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource',
            'privateKey' => File.read(private_key_path)
          },
          'passphrase' => key_passphrase,
          'description' => description,
          'stapler-class' => 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey'
        }
      }.to_json
    }
  else
    post_body = {
      'json' => {
        '' => '0',
        'credentials' => {
          'scope' => scope,
          'username' => username,
          'privateKeySource' => {
            'stapler-class' => 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource',
            'privateKey' => File.read(private_key_path)
          },
          'passphrase' => key_passphrase,
          'id' => credential_id,
          'description' => description,
          'stapler-class' => 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey'
        }
      }.to_json
    }
  end

  resp = jenkins_obj.api_post_request(
    uri_path,
    post_body
  )

  resp == '302'
rescue StandardError => e
  raise e
end

.create_user(opts = {}) ⇒ Object

PWN::Plugins::Jenkins.create_user(

jenkins_obj: 'required - jenkins_obj returned from #connect method',
username: 'required - user to create',
password: 'required - password for new user'
fullname: 'required - full name of new user'
email: 'required - email address of new user'

)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/pwn/plugins/jenkins.rb', line 100

public_class_method def self.create_user(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  username = opts[:username].to_s.scrub
  password = opts[:password].to_s.scrub
  password = PWN::Plugins::AuthenticationHelper.mask_password if password == ''
  fullname = opts[:fullname].to_s.scrub
  email = opts[:email].to_s.scrub

  post_body = {
    'username' => username,
    'password1' => password,
    'password2' => password,
    'fullname' => fullname,
    'email' => email,
    'json' => {
      'username' => username,
      'password1' => password,
      'password2' => password,
      'fullname' => fullname,
      'email' => email
    }.to_json
  }

  @@logger.info("Creating #{username}...")

  resp = jenkins_obj.api_post_request(
    '/securityRealm/createAccountByAdmin',
    post_body
  )

  resp == '302'
# rescue JenkinsApi::Exceptions::UserAlreadyExists => e
#   @@logger.warn("Jenkins view: #{view_name} already exists")
#   return e.class
rescue StandardError => e
  raise e
end

.delete_jobs_by_regex(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.delete_jobs_by_regex(

jenkins_obj: 'required jenkins_obj returned from #connect method',
regex: 'required regex pattern for matching jobs to disable e.g. :regex => "^M[0-9]"',

)



397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/pwn/plugins/jenkins.rb', line 397

public_class_method def self.delete_jobs_by_regex(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  regex = opts[:regex].to_s.scrub

  jenkins_obj.job.list_all_with_details.each do |job|
    job_name = job['name']
    if job_name.match?(/#{regex}/)
      @@logger.info("Deleting #{job_name}")
      jenkins_obj.job.delete(job_name)
    end
  end
rescue StandardError => e
  raise e
end

.disable_jobs_by_regex(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.disable_jobs_by_regex(

jenkins_obj: 'required jenkins_obj returned from #connect method',
regex: 'required regex pattern for matching jobs to disable e.g. :regex => "^M[0-9]"',

)



376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/pwn/plugins/jenkins.rb', line 376

public_class_method def self.disable_jobs_by_regex(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  regex = opts[:regex].to_s.scrub

  jenkins_obj.job.list_all_with_details.each do |job|
    job_name = job['name']
    if job_name.match?(/#{regex}/)
      @@logger.info("Disabling #{job_name}")
      jenkins_obj.job.disable(job_name)
    end
  end
rescue StandardError => e
  raise e
end

.disconnect(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.disconnect(

jenkins_obj: 'required jenkins_obj returned from #connect method'

)



433
434
435
436
437
438
439
440
# File 'lib/pwn/plugins/jenkins.rb', line 433

public_class_method def self.disconnect(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  @@logger.info('Disconnecting from Jenkins...')
  jenkins_obj = nil
  'complete'
rescue StandardError => e
  raise e
end

.get_all_job_git_repos(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.get_all_job_git_repos(

jenkins_obj: 'required jenkins_obj returned from #connect method'

)



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

public_class_method def self.get_all_job_git_repos(opts = {})
  jenkins_obj = opts[:jenkins_obj]

  @@logger.info('Retrieving a List of Git Repos from Every Job...')

  git_repo_arr = []

  jenkins_obj.job.list_all_with_details.each do |job|
    this_config = Nokogiri::XML(jenkins_obj.job.get_config(job['name']))
    this_git_repo = this_config.xpath('//scm/userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/url').text
    this_git_branch = this_config.xpath('//scm/branches/hudson.plugins.git.BranchSpec/name').text
    next if this_git_repo == ''

    # Obtain all jobs' git repos
    job_git_repo = {}
    job_git_repo[:name] = job['name']
    job_git_repo[:url] = job['url']
    job_git_repo[:job_state] = job['color']
    job_git_repo[:git_repo] = this_git_repo
    job_git_repo[:git_branch] = this_git_branch
    job_git_repo[:config_xml_response] = this_config
    git_repo_arr.push(job_git_repo)
  end

  git_repo_arr
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



452
453
454
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
# File 'lib/pwn/plugins/jenkins.rb', line 452

public_class_method def self.help
  puts %{USAGE:
    jenkins_obj = #{self}.connect(
      jenkins_ip: 'required host/ip of Jenkins Server',
      port: 'optional tcp port (defaults to 8080),
      username: 'optional username (functionality will be limited if ommitted)',
      password: 'optional password (functionality will be limited if ommitted)',
      identity_file: 'optional ssh private key path to AuthN w/ Jenkins PREFERRED over username/password',
      ssl: 'optional connect over TLS (defaults to true),
      proxy: 'optional debug proxy rest api requests to jenkins (e.g. "http://127.0.0.1:8080")''
    )
    puts jenkins_obj.public_methods

    #{self}.create_user(
      jenkins_obj: 'required - jenkins_obj returned from #connect method',
      username: 'required - user to create',
      password: 'optional - password for new user (will prompt if nil)'
      fullname: 'required - full name of new user'
      email: 'required - email address of new user'
    )

    #{self}.create_ssh_credential(
      jenkins_obj: 'required - jenkins_obj returned from #connect method',
      username: 'required - username for new credential'
      private_key_path: 'required - path of private ssh key for new credential'
      key_passphrase: 'optional - private key passphrase for new credential'
      credential_id: 'optional but recommended - useful when creating userland jobs',
      description: 'optional - description of new credential'
      domain: 'optional - defaults to GLOBAL',
      scope: 'optional - GLOBAL or SYSTEM (defaults to GLOBAL)'
    )

    git_repo_arr = #{self}.get_all_job_git_repos(
      jenkins_obj: 'required jenkins_obj returned from connect method'
    )

    git_repo_branches = #{self}.get_all_git_repo_branches_by_commit_date(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
      job_name: 'required jenkins job name',
      git_url: 'required git url for git_repo'
    )

    nested_jobs_arr = #{self}.list_nested_jobs(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
      view_path: 'required view path list jobs'
    )

    nested_views_arr = #{self}.list_nested_views(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
      view_path: 'required view path list sub-views'
    )

    view_created_bool = #{self}.create_nested_view(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
      view_path: 'required view path create',
      create_in_view_path: 'optional creates nested view within an existing nested view, defaults to / views'
    )

    add_job_to_nested_view_resp = #{self}.add_job_to_nested_view(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
      view_path: 'required view path associate job',
      job_name: 'required view path attach to a view',
    )

    copy_job_resp = #{self}.copy_job_no_fail_on_exist(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
      existing_job_name: 'required existing job to copt to new job',
      new_job_name: 'required name of new job'
    )

    #{self}.disable_jobs_by_regex(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
      regex: 'required regex pattern for matching jobs to disable e.g. :regex => "^M[0-9]"',
    )

    #{self}.delete_job_by_regex(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
      regex: 'required regex pattern for matching jobs to disable e.g. :regex => "^M[0-9]"',
    )

    #{self}.clear_build_queue(
      jenkins_obj: 'required jenkins_obj returned from #connect method',
    )

    #{self}.disconnect(
      jenkins_obj: 'required jenkins_obj returned from connect method'
    )

    #{self}.authors
  }
end

.list_nested_jobs(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.list_nested_jobs(

jenkins_obj: 'required jenkins_obj returned from #connect method',
view_path: 'required view path to list jobs'

)



257
258
259
260
261
262
263
264
# File 'lib/pwn/plugins/jenkins.rb', line 257

public_class_method def self.list_nested_jobs(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  view_path = opts[:view_path].to_s.scrub
  nested_view_resp = jenkins_obj.api_get_request(view_path)
  nested_view_resp['jobs']
rescue StandardError => e
  raise e
end

.list_nested_views(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Jenkins.list_nested_views(

jenkins_obj: 'required jenkins_obj returned from #connect method',
view_path: 'required view path list sub-views'

)



272
273
274
275
276
277
278
279
# File 'lib/pwn/plugins/jenkins.rb', line 272

public_class_method def self.list_nested_views(opts = {})
  jenkins_obj = opts[:jenkins_obj]
  view_path = opts[:view_path].to_s.scrub
  nested_view_resp = jenkins_obj.api_get_request(view_path)
  nested_view_resp['views']
rescue StandardError => e
  raise e
end