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 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 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 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 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
|
"""
Module for jenkinsapi Credential class
"""
import logging
import xml.etree.cElementTree as ET
log = logging.getLogger(__name__)
class Credential(object):
"""
Base abstract class for credentials
Credentials returned from Jenkins don't hold any sensitive information,
so there is nothing useful can be done with existing credentials
besides attaching them to Nodes or other objects.
You can create concrete Credential instance: UsernamePasswordCredential or
SSHKeyCredential by passing credential's description and credential dict.
Each class expects specific credential dict, see below.
"""
# pylint: disable=unused-argument
def __init__(self, cred_dict, jenkins_class=""):
"""
Create credential
:param str description: as Jenkins doesn't allow human friendly names
for credentials and makes "displayName" itself,
there is no way to find credential later,
this field is used to distinguish between credentials
:param dict cred_dict: dict containing credential information
"""
self.credential_id = cred_dict.get("credential_id", "")
self.description = cred_dict["description"]
self.fullname = cred_dict.get("fullName", "")
self.displayname = cred_dict.get("displayName", "")
self.jenkins_class = jenkins_class
def __str__(self):
return self.description
def get_attributes(self):
pass
def get_attributes_xml(self):
pass
def _get_attributes_xml(self, data):
root = ET.Element(self.jenkins_class)
for key in data:
value = data[key]
if isinstance(value, dict):
node = ET.SubElement(root, key)
if "stapler-class" in value:
node.attrib["class"] = value["stapler-class"]
for sub_key in value:
ET.SubElement(node, sub_key).text = value[sub_key]
else:
ET.SubElement(root, key).text = data[key]
return ET.tostring(root)
class UsernamePasswordCredential(Credential):
"""
Username and password credential
Constructor expects following dict:
{
'credential_id': str, Automatically set by jenkinsapi
'displayName': str, Automatically set by Jenkins
'fullName': str, Automatically set by Jenkins
'typeName': str, Automatically set by Jenkins
'description': str,
'userName': str,
'password': str
}
When creating credential via jenkinsapi automatic fields not need to be in
dict
"""
def __init__(self, cred_dict: dict) -> None:
jenkins_class: str = (
"com.cloudbees.plugins.credentials.impl."
"UsernamePasswordCredentialsImpl"
)
super(UsernamePasswordCredential, self).__init__(
cred_dict, jenkins_class
)
if "typeName" in cred_dict:
username: str = cred_dict["displayName"].split("/")[0]
else:
username: str = cred_dict["userName"]
self.username: str = username
self.password: str = cred_dict.get("password", "")
def get_attributes(self):
"""
Used by Credentials object to create credential in Jenkins
"""
c_id = "" if self.credential_id is None else self.credential_id
return {
"stapler-class": self.jenkins_class,
"Submit": "OK",
"json": {
"": "1",
"credentials": {
"stapler-class": self.jenkins_class,
"id": c_id,
"username": self.username,
"password": self.password,
"description": self.description,
},
},
}
def get_attributes_xml(self):
"""
Used by Credentials object to update a credential in Jenkins
"""
c_id = "" if self.credential_id is None else self.credential_id
data = {
"id": c_id,
"username": self.username,
"password": self.password,
"description": self.description,
}
return super(UsernamePasswordCredential, self)._get_attributes_xml(
data
)
class SecretTextCredential(Credential):
"""
Secret text credential
Constructor expects following dict:
{
'credential_id': str, Automatically set by jenkinsapi
'displayName': str, Automatically set by Jenkins
'fullName': str, Automatically set by Jenkins
'typeName': str, Automatically set by Jenkins
'description': str,
'secret': str,
}
When creating credential via jenkinsapi automatic fields not need to be in
dict
"""
def __init__(self, cred_dict):
jenkins_class = (
"org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl"
)
super(SecretTextCredential, self).__init__(cred_dict, jenkins_class)
self.secret = cred_dict.get("secret", None)
def get_attributes(self):
"""
Used by Credentials object to create credential in Jenkins
"""
c_id = "" if self.credential_id is None else self.credential_id
return {
"stapler-class": self.jenkins_class,
"Submit": "OK",
"json": {
"": "1",
"credentials": {
"stapler-class": self.jenkins_class,
"$class": self.jenkins_class,
"id": c_id,
"secret": self.secret,
"description": self.description,
},
},
}
def get_attributes_xml(self):
"""
Used by Credentials object to update a credential in Jenkins
"""
c_id = "" if self.credential_id is None else self.credential_id
data = {
"id": c_id,
"secret": self.secret,
"description": self.description,
}
return super(SecretTextCredential, self)._get_attributes_xml(data)
class SSHKeyCredential(Credential):
"""
SSH key credential
Constructr expects following dict:
{
'credential_id': str, Automatically set by jenkinsapi
'displayName': str, Automatically set by Jenkins
'fullName': str, Automatically set by Jenkins
'typeName': str, Automatically set by Jenkins
'description': str,
'userName': str,
'passphrase': str, SSH key passphrase,
'private_key': str Private SSH key
}
private_key value is parsed to find type of credential to create:
private_key starts with - the value is private key itself
These credential variations are no longer supported by SSH Credentials
plugin. jenkinsapi will raise ValueError if they are used:
private_key starts with / the value is a path to key
private_key starts with ~ the value is a key from ~/.ssh
When creating credential via jenkinsapi automatic fields not need to be in
dict
"""
def __init__(self, cred_dict: dict) -> None:
jenkins_class: str = (
"com.cloudbees.jenkins.plugins.sshcredentials.impl."
"BasicSSHUserPrivateKey"
)
super(SSHKeyCredential, self).__init__(cred_dict, jenkins_class)
if "typeName" in cred_dict:
username: str = cred_dict["displayName"].split(" ")[0]
else:
username: str = cred_dict["userName"]
self.username: str = username
self.passphrase: str = cred_dict.get("passphrase", "")
if "private_key" not in cred_dict or cred_dict["private_key"] is None:
self.key_type: int = -1
self.key_value: str = ""
elif cred_dict["private_key"].startswith("-"):
self.key_type: int = 0
self.key_value: str = cred_dict["private_key"]
else:
raise ValueError("Invalid private_key value")
@property
def attrs(self):
if self.key_type == 0:
c_class = self.jenkins_class + "$DirectEntryPrivateKeySource"
elif self.key_type == 1:
c_class = self.jenkins_class + "$FileOnMasterPrivateKeySource"
elif self.key_type == 2:
c_class = self.jenkins_class + "$UsersPrivateKeySource"
else:
c_class = None
attrs = {
"value": self.key_type,
"privateKey": self.key_value,
"stapler-class": c_class,
}
# We need one more attr when using the key file on master.
if self.key_type == 1:
attrs["privateKeyFile"] = self.key_value
return attrs
def get_attributes(self):
"""
Used by Credentials object to create credential in Jenkins
"""
c_id = "" if self.credential_id is None else self.credential_id
return {
"stapler-class": self.attrs["stapler-class"],
"Submit": "OK",
"json": {
"": "1",
"credentials": {
"scope": "GLOBAL",
"id": c_id,
"username": self.username,
"description": self.description,
"privateKeySource": self.attrs,
"passphrase": self.passphrase,
"stapler-class": self.jenkins_class,
"$class": self.jenkins_class,
},
},
}
def get_attributes_xml(self):
"""
Used by Credentials object to update a credential in Jenkins
"""
c_id = "" if self.credential_id is None else self.credential_id
data = {
"id": c_id,
"username": self.username,
"description": self.description,
"privateKeySource": self.attrs,
"passphrase": self.passphrase,
}
return super(SSHKeyCredential, self)._get_attributes_xml(data)
class AmazonWebServicesCredentials(Credential):
"""
AWS credential using the CloudBees AWS Credentials Plugin
See
https://wiki.jenkins.io/display/JENKINS/CloudBees+AWS+Credentials+Plugin
Constructor expects following dict:
{
'credential_id': str, Automatically set by jenkinsapi
'displayName': str, Automatically set by Jenkins
'fullName': str, Automatically set by Jenkins
'description': str,
'accessKey': str,
'secretKey': str,
'iamRoleArn': str,
'iamMfaSerialNumber': str
}
When creating credential via jenkinsapi automatic fields not need to be in
dict
"""
def __init__(self, cred_dict):
jenkins_class = (
"com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl"
)
super(AmazonWebServicesCredentials, self).__init__(
cred_dict, jenkins_class
)
self.access_key = cred_dict["accessKey"]
self.secret_key = cred_dict["secretKey"]
self.iam_role_arn = cred_dict.get("iamRoleArn", "")
self.iam_mfa_serial_number = cred_dict.get("iamMfaSerialNumber", "")
def get_attributes(self):
"""
Used by Credentials object to create credential in Jenkins
"""
c_id = "" if self.credential_id is None else self.credential_id
return {
"stapler-class": self.jenkins_class,
"Submit": "OK",
"json": {
"": "1",
"credentials": {
"stapler-class": self.jenkins_class,
"$class": self.jenkins_class,
"id": c_id,
"accessKey": self.access_key,
"secretKey": self.secret_key,
"iamRoleArn": self.iam_role_arn,
"iamMfaSerialNumber": self.iam_mfa_serial_number,
"description": self.description,
},
},
}
def get_attributes_xml(self):
"""
Used by Credentials object to update a credential in Jenkins
"""
c_id = "" if self.credential_id is None else self.credential_id
data = {
"id": c_id,
"accessKey": self.access_key,
"secretKey": self.secret_key,
"iamRoleArn": self.iam_role_arn,
"iamMfaSerialNumber": self.iam_mfa_serial_number,
"description": self.description,
}
return super(AmazonWebServicesCredentials, self)._get_attributes_xml(
data
)
|