Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 1 | import logging |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 2 | |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 3 | logger = logging.getLogger(__name__) |
| 4 | |
| 5 | create_credential_groovy = u"""\ |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 6 | import com.cloudbees.plugins.credentials.domains.Domain; |
| 7 | import com.cloudbees.plugins.credentials.CredentialsScope; |
Matias Gianni | 143b9ed | 2017-11-19 02:14:50 -0300 | [diff] [blame^] | 8 | import hudson.util.Secret |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 9 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 10 | def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( |
Matias Gianni | 143b9ed | 2017-11-19 02:14:50 -0300 | [diff] [blame^] | 11 | com.cloudbees.plugins.credentials.common.StandardCredentials.class, |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 12 | Jenkins.instance |
| 13 | ) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 14 | def key = \"\"\"${key} |
Jakub Josef | ff34813 | 2016-12-21 17:11:37 +0100 | [diff] [blame] | 15 | \"\"\" |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 16 | |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 17 | def result = creds.find{ |
Jakub Josef | 3555305 | 2017-03-16 17:30:30 +0100 | [diff] [blame] | 18 | (it instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl && |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 19 | it.username == "${username}" && |
| 20 | it.id == "${name}" && |
| 21 | it.description == "${desc}" && |
| 22 | it.password.toString() == "${password}") || |
Jakub Josef | 3555305 | 2017-03-16 17:30:30 +0100 | [diff] [blame] | 23 | (it instanceof com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey && |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 24 | it.username == "${username}" && |
| 25 | it.id == "${name}" && |
| 26 | ("${password}" == "" || it.passphrase.toString() == "${password}") && |
| 27 | it.description == "${desc}" && |
Matias Gianni | 143b9ed | 2017-11-19 02:14:50 -0300 | [diff] [blame^] | 28 | 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 33 | } |
Jakub Josef | ff34813 | 2016-12-21 17:11:37 +0100 | [diff] [blame] | 34 | |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 35 | if(result){ |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 36 | print("EXISTS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 37 | }else{ |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 38 | domain = Domain.global() |
| 39 | store = Jenkins.instance.getExtensionList( |
| 40 | 'com.cloudbees.plugins.credentials.SystemCredentialsProvider' |
| 41 | )[0].getStore() |
Matias Gianni | 143b9ed | 2017-11-19 02:14:50 -0300 | [diff] [blame^] | 42 | 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 Josef | 3555305 | 2017-03-16 17:30:30 +0100 | [diff] [blame] | 53 | // remove credentails with same if before created new one, if exists |
| 54 | def existingCreds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( |
| 55 | com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 56 | Jenkins.instance).find{it -> it.id.equals("${name}")} |
| 57 | if(existingCreds){ |
Jakub Josef | 3555305 | 2017-03-16 17:30:30 +0100 | [diff] [blame] | 58 | store.removeCredentials(domain, existingCreds) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 59 | } |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 60 | ret = store.addCredentials(domain, credentials_new) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 61 | if (ret) { |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 62 | print("CREATED"); |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 63 | } else { |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 64 | print("FAILED"); |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 67 | """ # noqa |
| 68 | |
Jakub Josef | b6c60bc | 2016-12-21 13:53:33 +0100 | [diff] [blame] | 69 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 70 | def __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 Gianni | 143b9ed | 2017-11-19 02:14:50 -0300 | [diff] [blame^] | 82 | def present(name, scope, username, password="", desc="", key=None, secret=None): |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 83 | """ |
| 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 Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 105 | ret['comment'] = 'Credentials %s %s' % (name, status.lower()) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 106 | else: |
| 107 | clazz = "" |
| 108 | if key: |
| 109 | clazz = "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" |
Jakub Josef | ff34813 | 2016-12-21 17:11:37 +0100 | [diff] [blame] | 110 | 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 Gianni | 143b9ed | 2017-11-19 02:14:50 -0300 | [diff] [blame^] | 112 | elif secret: |
| 113 | clazz = "org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl" |
| 114 | params = 'CredentialsScope.{}, "{}", "{}"'.format( |
| 115 | scope, name, desc if desc else "") |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 116 | else: |
| 117 | clazz = "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl" |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 118 | params = 'CredentialsScope.{}, "{}", "{}", "{}", "{}"'.format( |
Jakub Josef | b395d8e | 2017-01-02 16:31:56 +0100 | [diff] [blame] | 119 | scope, name, desc if desc else "", username, password) |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 120 | call_result = __salt__['jenkins_common.call_groovy_script']( |
Matias Gianni | 143b9ed | 2017-11-19 02:14:50 -0300 | [diff] [blame^] | 121 | 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 122 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 123 | "CREATED", "EXISTS"]: |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 124 | status = call_result["msg"] |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 125 | if call_result["msg"] == "CREATED": |
| 126 | ret['changes'][name] = status |
| 127 | ret['comment'] = 'Credentials %s %s' % (name, status.lower()) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 128 | result = True |
| 129 | else: |
| 130 | status = 'FAILED' |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 131 | logger.error( |
| 132 | "Jenkins credentials API call failure: %s", call_result["msg"]) |
Jakub Josef | b6c60bc | 2016-12-21 13:53:33 +0100 | [diff] [blame] | 133 | ret['comment'] = 'Jenkins credentials API call failure: %s' % (call_result[ |
| 134 | "msg"]) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 135 | ret['result'] = None if test else result |
| 136 | return ret |