/usr/lib/python2.7/dist-packages/jenkinsapi/credentials.py is in python-jenkinsapi 0.2.30-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | """
This module implements the Credentials class, which is intended to be a
container-like interface for all of the Global credentials defined on a single
Jenkins node.
"""
import logging
try:
from urllib import urlencode
except ImportError:
# Python3
from urllib.parse import urlencode
from jenkinsapi.credential import Credential
from jenkinsapi.credential import UsernamePasswordCredential
from jenkinsapi.credential import SSHKeyCredential
from jenkinsapi.jenkinsbase import JenkinsBase
from jenkinsapi.custom_exceptions import JenkinsAPIException
log = logging.getLogger(__name__)
class Credentials(JenkinsBase):
"""
This class provides a container-like API which gives
access to all global credentials on a Jenkins node.
Returns a list of Credential Objects.
"""
def __init__(self, baseurl, jenkins_obj):
self.baseurl = baseurl
self.jenkins = jenkins_obj
JenkinsBase.__init__(self, baseurl)
self.credentials = self._data['credentials']
def _poll(self, tree=None):
url = self.python_api_url(self.baseurl) + '?depth=2'
data = self.get_data(url, tree=tree)
credentials = data['credentials']
for cred_id, cred_dict in credentials.items():
cred_dict['credential_id'] = cred_id
credentials[cred_id] = self._make_credential(cred_dict)
return data
def __str__(self):
return 'Global Credentials @ %s' % self.baseurl
def get_jenkins_obj(self):
return self.jenkins
def __iter__(self):
for cred in self.credentials.values():
yield cred.description
def __contains__(self, description):
return description in self.keys()
def iterkeys(self):
return self.__iter__()
def keys(self):
return list(self.iterkeys())
def iteritems(self):
for cred in self.credentials.values():
yield cred.description, cred
def __getitem__(self, description):
for cred in self.credentials.values():
if cred.description == description:
return cred
raise KeyError('Credential with description "%s" not found'
% description)
def __len__(self):
return len(self.keys())
def __setitem__(self, description, credential):
"""
Creates Credential in Jenkins using username, password and description
Description must be unique in Jenkins instance
because it is used to find Credential later.
If description already exists - this method is going to update
existing Credential
:param str description: Credential description
:param tuple credential_tuple: (username, password, description) tuple.
"""
if description not in self:
params = credential.get_attributes()
url = (
'%s/credential-store/domain/_/createCredentials'
% self.jenkins.baseurl
)
else:
raise JenkinsAPIException('Updating credentials is not supported '
'by jenkinsapi')
try:
self.jenkins.requester.post_and_confirm_status(
url, params={}, data=urlencode(params)
)
except JenkinsAPIException as jae:
raise JenkinsAPIException('Latest version of Credentials '
'plugin is required to be able '
'to create/update credentials. '
'Original exception: %s' % str(jae))
self.poll()
self.credentials = self._data['credentials']
if description not in self:
raise JenkinsAPIException('Problem creating/updating credential.')
def get(self, item, default):
return self[item] if item in self else default
def __delitem__(self, description):
params = {
'Submit': 'OK',
'json': {}
}
url = ('%s/credential-store/domain/_/credential/%s/doDelete'
% (self.jenkins.baseurl, self[description].credential_id))
try:
self.jenkins.requester.post_and_confirm_status(
url, params={}, data=urlencode(params)
)
except JenkinsAPIException as jae:
raise JenkinsAPIException('Latest version of Credentials '
'required to be able to create '
'credentials. Original exception: %s'
% str(jae))
self.poll()
self.credentials = self._data['credentials']
if description in self:
raise JenkinsAPIException('Problem deleting credential.')
def _make_credential(self, cred_dict):
if cred_dict['typeName'] == 'Username with password':
cr = UsernamePasswordCredential(cred_dict)
elif cred_dict['typeName'] == 'SSH Username with private key':
cr = SSHKeyCredential(cred_dict)
else:
cr = Credential(cred_dict)
return cr
|