blob: 67be9a0e474fb4fbd37b3f738957c1ec0906a694 [file] [log] [blame]
Jakub Josef8e7385e2016-12-07 21:20:34 +01001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josef8e7385e2016-12-07 21:20:34 +01003logger = logging.getLogger(__name__)
4
5create_credential_groovy = u"""\
Jakub Josef8e7385e2016-12-07 21:20:34 +01006import com.cloudbees.plugins.credentials.domains.Domain;
7import com.cloudbees.plugins.credentials.CredentialsScope;
Matias Gianni143b9ed2017-11-19 02:14:50 -03008import hudson.util.Secret
Jakub Josef8e7385e2016-12-07 21:20:34 +01009
Jakub Josef98123ab2016-12-14 14:05:01 +010010def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
Matias Gianni143b9ed2017-11-19 02:14:50 -030011 com.cloudbees.plugins.credentials.common.StandardCredentials.class,
Jakub Josef98123ab2016-12-14 14:05:01 +010012 Jenkins.instance
13 )
Adam Tengler70763e02017-08-21 16:50:32 +000014def key = \"\"\"${key}
Jakub Josefff348132016-12-21 17:11:37 +010015\"\"\"
Jakub Josef8e7385e2016-12-07 21:20:34 +010016
Adam Tengler70763e02017-08-21 16:50:32 +000017def result = creds.find{
Jakub Josef35553052017-03-16 17:30:30 +010018 (it instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl &&
Adam Tengler70763e02017-08-21 16:50:32 +000019 it.username == "${username}" &&
20 it.id == "${name}" &&
21 it.description == "${desc}" &&
22 it.password.toString() == "${password}") ||
Jakub Josef35553052017-03-16 17:30:30 +010023 (it instanceof com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey &&
Adam Tengler70763e02017-08-21 16:50:32 +000024 it.username == "${username}" &&
25 it.id == "${name}" &&
26 ("${password}" == "" || it.passphrase.toString() == "${password}") &&
27 it.description == "${desc}" &&
Matias Gianni143b9ed2017-11-19 02:14:50 -030028 it.privateKeySource.privateKey.equals(key.trim())) ||
29 (it instanceof org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl &&
30 it.id == "${name}" &&
31 it.description == "${desc}" &&
32 it.secret == Secret.fromString("${secret}") )
Adam Tengler70763e02017-08-21 16:50:32 +000033}
Jakub Josefff348132016-12-21 17:11:37 +010034
Adam Tengler70763e02017-08-21 16:50:32 +000035if(result){
Jakub Josef98123ab2016-12-14 14:05:01 +010036 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000037}else{
Jakub Josef98123ab2016-12-14 14:05:01 +010038 domain = Domain.global()
39 store = Jenkins.instance.getExtensionList(
40 'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
41 )[0].getStore()
Matias Gianni143b9ed2017-11-19 02:14:50 -030042 if ( "${clazz}" == "org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl" ){
43 Secret token = Secret.fromString("${secret}")
44 params = 'CredentialsScope.${scope}, ${name}, ${desc}'
45 credentials_new = new ${clazz}(
46 CredentialsScope.${scope}, "${name}", "${desc}", token
47 )
48 } else {
49 credentials_new = new ${clazz}(
50 ${params}
51 )
52 }
Jakub Josef35553052017-03-16 17:30:30 +010053 // remove credentails with same if before created new one, if exists
54 def existingCreds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
Jakub Josef184ee1c2017-11-29 16:03:09 +010055 com.cloudbees.plugins.credentials.common.StandardCredentials.class,
Adam Tengler70763e02017-08-21 16:50:32 +000056 Jenkins.instance).find{it -> it.id.equals("${name}")}
57 if(existingCreds){
Jakub Josef35553052017-03-16 17:30:30 +010058 store.removeCredentials(domain, existingCreds)
Adam Tengler70763e02017-08-21 16:50:32 +000059 }
Jakub Josef8e7385e2016-12-07 21:20:34 +010060 ret = store.addCredentials(domain, credentials_new)
Adam Tengler70763e02017-08-21 16:50:32 +000061 if (ret) {
Jakub Josef98123ab2016-12-14 14:05:01 +010062 print("CREATED");
Adam Tengler70763e02017-08-21 16:50:32 +000063 } else {
Jakub Josef98123ab2016-12-14 14:05:01 +010064 print("FAILED");
Adam Tengler70763e02017-08-21 16:50:32 +000065 }
66}
Jakub Josef8e7385e2016-12-07 21:20:34 +010067""" # noqa
68
Jakub Josefb6c60bc2016-12-21 13:53:33 +010069
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040070def __virtual__():
71 '''
72 Only load if jenkins_common module exist.
73 '''
74 if 'jenkins_common.call_groovy_script' not in __salt__:
75 return (
76 False,
77 'The jenkins_credentials state module cannot be loaded: '
78 'jenkins_common not found')
79 return True
80
81
Matias Gianni143b9ed2017-11-19 02:14:50 -030082def present(name, scope, username, password="", desc="", key=None, secret=None):
Jakub Josef8e7385e2016-12-07 21:20:34 +010083 """
84 Main jenkins credentials state method
85
86 :param name: credential name
87 :param scope: credential scope
88 :param username: username
89 :param password: password (optional)
90 :param desc: credential description (optional)
91 :param key: credential key (optional)
92 :returns: salt-specified state dict
93 """
94 test = __opts__['test'] # noqa
95 ret = {
96 'name': name,
97 'changes': {},
98 'result': False,
99 'comment': '',
100 }
101 result = False
102 if test:
103 status = 'CREATED'
104 ret['changes'][name] = status
Jakub Josef98123ab2016-12-14 14:05:01 +0100105 ret['comment'] = 'Credentials %s %s' % (name, status.lower())
Jakub Josef8e7385e2016-12-07 21:20:34 +0100106 else:
107 clazz = ""
108 if key:
109 clazz = "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
Jakub Josefff348132016-12-21 17:11:37 +0100110 params = 'CredentialsScope.{}, "{}", "{}", new com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(key.trim()), "{}", "{}"'.format(
111 scope, name, username, password if password else "", desc if desc else "")
Matias Gianni143b9ed2017-11-19 02:14:50 -0300112 elif secret:
113 clazz = "org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl"
114 params = 'CredentialsScope.{}, "{}", "{}"'.format(
115 scope, name, desc if desc else "")
Jakub Josef8e7385e2016-12-07 21:20:34 +0100116 else:
117 clazz = "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
Jakub Josef98123ab2016-12-14 14:05:01 +0100118 params = 'CredentialsScope.{}, "{}", "{}", "{}", "{}"'.format(
Jakub Josefb395d8e2017-01-02 16:31:56 +0100119 scope, name, desc if desc else "", username, password)
Jakub Josef98123ab2016-12-14 14:05:01 +0100120 call_result = __salt__['jenkins_common.call_groovy_script'](
Matias Gianni143b9ed2017-11-19 02:14:50 -0300121 create_credential_groovy, {"scope": scope, "name": name, "username": username if username else "", "password": password if password else "", "clazz": clazz, "params": params, "key": key if key else "", "desc": desc if desc else "", "secret": secret if secret else ""})
Adam Tengler70763e02017-08-21 16:50:32 +0000122 if call_result["code"] == 200 and call_result["msg"] in [
123 "CREATED", "EXISTS"]:
Jakub Josef8e7385e2016-12-07 21:20:34 +0100124 status = call_result["msg"]
Jakub Josef98123ab2016-12-14 14:05:01 +0100125 if call_result["msg"] == "CREATED":
126 ret['changes'][name] = status
127 ret['comment'] = 'Credentials %s %s' % (name, status.lower())
Jakub Josef8e7385e2016-12-07 21:20:34 +0100128 result = True
129 else:
130 status = 'FAILED'
Jakub Josef98123ab2016-12-14 14:05:01 +0100131 logger.error(
132 "Jenkins credentials API call failure: %s", call_result["msg"])
Jakub Josefb6c60bc2016-12-21 13:53:33 +0100133 ret['comment'] = 'Jenkins credentials API call failure: %s' % (call_result[
134 "msg"])
Jakub Josef8e7385e2016-12-07 21:20:34 +0100135 ret['result'] = None if test else result
136 return ret