Module: Jenkins2::Client::CredentialCommands

Included in:
Jenkins2::Client
Defined in:
lib/jenkins2/client/credential_commands.rb

Constant Summary collapse

BOUNDARY =
'----Jenkins2RubyMultipartClient' + rand(1000000).to_s

Instance Method Summary collapse

Instance Method Details

#create_credential(**args) ⇒ Object

Creates credentials based on provided arguments. Calls either:

  • create_credential_username_password

  • create_credential_ssh

  • create_credential_secret_text

  • create_credential_secret_file

See those functions’ documentation for the exact parameters required



12
13
14
15
16
17
# File 'lib/jenkins2/client/credential_commands.rb', line 12

def create_credential( **args )
  return create_credential_username_password( args ) if args[:password]
  return create_credential_ssh( args ) if args[:private_key]
  return create_credential_secret_text( args ) if args[:secret]
  return create_credential_secret_file( args ) if args[:content]
end

#create_credential_secret_file(**args) ⇒ Object

Creates a secret file credential. Jenkins must have plain-credentials plugin installed, to use this functionality. Accepts hash with the following parameters.

scope

Scope of the credential. GLOBAL or SYSTEM

id

Id of the credential. Will be Generated by Jenkins, if not provided.

description

Human readable text, what this credential is used for.

filename

Name of the file.

content

File content.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jenkins2/client/credential_commands.rb', line 91

def create_credential_secret_file( **args )
  filename = args.delete :filename
  content = args.delete :content
  body = "--#{BOUNDARY}\r\n"
  body << "Content-Disposition: form-data; name=\"file0\"; filename=\"#{filename}\"\r\n"
  body << "Content-Type: application/octet-stream\r\n\r\n"
  body << content
  body << "\r\n"
  body << "--#{BOUNDARY}\r\n"
  body << "Content-Disposition: form-data; name=\"json\"\r\n\r\n"
  body << { "" => "2",
    credentials: args.merge(
      '$class' => 'org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl',
      'file' => 'file0'
    )
  }.to_json
  body << "\r\n\r\n--#{BOUNDARY}--\r\n"
  api_request( :post, '/credentials/store/system/domain/_/createCredentials' ) do |req|
    req.add_field 'Content-Type', "multipart/form-data, boundary=#{BOUNDARY}"
    req.body = body
  end
end

#create_credential_secret_text(**args) ⇒ Object

Creates a secret text credential. Jenkins must have plain-credentials plugin installed, to use this functionality. Accepts hash with the following parameters.

scope

Scope of the credential. GLOBAL or SYSTEM

id

Id of the credential. Will be Generated by Jenkins, if not provided.

description

Human readable text, what this credential is used for.

secret

Some secret text.



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jenkins2/client/credential_commands.rb', line 72

def create_credential_secret_text( **args )
  json_body = { "" => "3",
    credentials: args.merge(
      '$class' => 'org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl'
    )
  }.to_json
  api_request( :post, '/credentials/store/system/domain/_/createCredentials' ) do |req|
    req.body = "json=#{CGI::escape json_body}"
    req.content_type = 'application/x-www-form-urlencoded'
  end
end

#create_credential_ssh(**args) ⇒ Object

Creates ssh username with private key credential. Jenkins must have ssh-credentials plugin installed, to use this functionality. Accepts hash with the following parameters.

scope

Scope of the credential. GLOBAL or SYSTEM

id

Id of the credential. Will be Generated by Jenkins, if not provided.

description

Human readable text, what this credential is used for.

username

Ssh username.

private_key

Ssh private key, with new lines replaced by \n sequence.

passphrase

Passphrase for the private key. Empty string, if not provided.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jenkins2/client/credential_commands.rb', line 45

def create_credential_ssh( **args )
  json_body = { "" => "1",
    credentials: {
      scope: args[:scope],
      username: args[:username],
      privateKeySource: {
        value: "0",
        privateKey: args[:private_key],
        'stapler-class' => 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource'
      },
      passphrase: args[:passphrase],
      id: args[:id],
      description: args[:description],
      '$class' => 'com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey'
    }
  }.to_json
  api_request( :post, '/credentials/store/system/domain/_/createCredentials' ) do |req|
    req.body = "json=#{CGI::escape json_body}"
    req.content_type = 'application/x-www-form-urlencoded'
  end
end

#create_credential_username_password(**args) ⇒ Object

Creates username and password credential. Accepts hash with the following parameters.

scope

Scope of the credential. GLOBAL or SYSTEM

id

Id of the credential. Will be Generated by Jenkins, if not provided.

description

Human readable text, what this credential is used for.

username

Username.

password

Password in plain text.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jenkins2/client/credential_commands.rb', line 25

def create_credential_username_password( **args )
  json_body = { "" => "0",
    credentials: args.merge(
      '$class' => 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl'
    )
  }.to_json
  api_request( :post, '/credentials/store/system/domain/_/createCredentials' ) do |req|
    req.body = "json=#{CGI::escape json_body}"
    req.content_type = 'application/x-www-form-urlencoded'
  end
end

#delete_credential(id) ⇒ Object

Deletes credential

id

Credential’s id



116
117
118
119
120
# File 'lib/jenkins2/client/credential_commands.rb', line 116

def delete_credential( id )
  api_request( :post, "/credentials/store/system/domain/_/credential/#{id}/doDelete" ) do |req|
    req.content_type = 'application/x-www-form-urlencoded'
  end
end

#get_credential(id) ⇒ Object

Returns credential as json. Raises Net::HTTPNotFound, if no such credential

id

Credential’s id



124
125
126
# File 'lib/jenkins2/client/credential_commands.rb', line 124

def get_credential( id )
  api_request( :get, "/credentials/store/system/domain/_/credential/#{id}/api/json" )
end

#list_credentials(store: 'system', domain: '_') ⇒ Object

Lists all credentials



129
130
131
# File 'lib/jenkins2/client/credential_commands.rb', line 129

def list_credentials( store: 'system', domain: '_' )
  api_request( :get, "/credentials/store/#{store}/domain/#{domain}/api/json?depth=1" )['credentials']
end