Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 1 | import logging |
| 2 | logger = logging.getLogger(__name__) |
| 3 | |
| 4 | create_credential_groovy = u"""\ |
| 5 | import jenkins.*; |
| 6 | import jenkins.model.*; |
| 7 | import hudson.*; |
| 8 | import hudson.model.*; |
| 9 | |
| 10 | import com.cloudbees.plugins.credentials.domains.Domain; |
| 11 | import com.cloudbees.plugins.credentials.CredentialsScope; |
| 12 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 13 | def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( |
| 14 | com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, |
| 15 | Jenkins.instance |
| 16 | ) |
Jakub Josef | ff34813 | 2016-12-21 17:11:37 +0100 | [diff] [blame] | 17 | def key = \"\"\"{key} |
| 18 | \"\"\" |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 19 | |
Jakub Josef | ff34813 | 2016-12-21 17:11:37 +0100 | [diff] [blame] | 20 | def result = creds.find{{ |
| 21 | (it instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl && |
| 22 | it.username == "{username}" && |
| 23 | it.id == "{name}" && |
| 24 | it.description == "{desc}" && |
| 25 | it.password.toString() == "{password}") || |
| 26 | (it instanceof com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey && |
| 27 | it.username == "{username}" && |
| 28 | it.id == "{name}" && |
| 29 | it.passphrase.toString() == "{password}" && |
| 30 | it.description == "{desc}" && |
| 31 | it.privateKeySource.privateKey.equals(key.trim())) |
| 32 | }} |
| 33 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 34 | if(result){{ |
| 35 | print("EXISTS") |
| 36 | }}else{{ |
| 37 | domain = Domain.global() |
| 38 | store = Jenkins.instance.getExtensionList( |
| 39 | 'com.cloudbees.plugins.credentials.SystemCredentialsProvider' |
| 40 | )[0].getStore() |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 41 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 42 | credentials_new = new {clazz}( |
| 43 | {params} |
| 44 | ) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 45 | |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 46 | ret = store.addCredentials(domain, credentials_new) |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 47 | if (ret) {{ |
| 48 | print("CREATED"); |
| 49 | }} else {{ |
| 50 | print("FAILED"); |
| 51 | }} |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 52 | }} |
| 53 | """ # noqa |
| 54 | |
Jakub Josef | b6c60bc | 2016-12-21 13:53:33 +0100 | [diff] [blame] | 55 | |
Jakub Josef | ff34813 | 2016-12-21 17:11:37 +0100 | [diff] [blame] | 56 | def present(name, scope, username, password="", desc="", key=None): |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 57 | """ |
| 58 | Main jenkins credentials state method |
| 59 | |
| 60 | :param name: credential name |
| 61 | :param scope: credential scope |
| 62 | :param username: username |
| 63 | :param password: password (optional) |
| 64 | :param desc: credential description (optional) |
| 65 | :param key: credential key (optional) |
| 66 | :returns: salt-specified state dict |
| 67 | """ |
| 68 | test = __opts__['test'] # noqa |
| 69 | ret = { |
| 70 | 'name': name, |
| 71 | 'changes': {}, |
| 72 | 'result': False, |
| 73 | 'comment': '', |
| 74 | } |
| 75 | result = False |
| 76 | if test: |
| 77 | status = 'CREATED' |
| 78 | ret['changes'][name] = status |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 79 | ret['comment'] = 'Credentials %s %s' % (name, status.lower()) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 80 | else: |
| 81 | clazz = "" |
| 82 | if key: |
| 83 | clazz = "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" |
Jakub Josef | ff34813 | 2016-12-21 17:11:37 +0100 | [diff] [blame] | 84 | params = 'CredentialsScope.{}, "{}", "{}", new com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(key.trim()), "{}", "{}"'.format( |
| 85 | scope, name, username, password if password else "", desc if desc else "") |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 86 | else: |
| 87 | clazz = "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl" |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 88 | params = 'CredentialsScope.{}, "{}", "{}", "{}", "{}"'.format( |
Jakub Josef | b395d8e | 2017-01-02 16:31:56 +0100 | [diff] [blame^] | 89 | scope, name, desc if desc else "", username, password) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 90 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 91 | call_result = __salt__['jenkins_common.call_groovy_script']( |
Jakub Josef | ff34813 | 2016-12-21 17:11:37 +0100 | [diff] [blame] | 92 | create_credential_groovy, {"name": name, "username": username, "password": password if password else "", "clazz": clazz, "params": params, "key": key if key else "", "desc": desc if desc else ""}) |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 93 | if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]: |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 94 | status = call_result["msg"] |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 95 | if call_result["msg"] == "CREATED": |
| 96 | ret['changes'][name] = status |
| 97 | ret['comment'] = 'Credentials %s %s' % (name, status.lower()) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 98 | result = True |
| 99 | else: |
| 100 | status = 'FAILED' |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 101 | logger.error( |
| 102 | "Jenkins credentials API call failure: %s", call_result["msg"]) |
Jakub Josef | b6c60bc | 2016-12-21 13:53:33 +0100 | [diff] [blame] | 103 | ret['comment'] = 'Jenkins credentials API call failure: %s' % (call_result[ |
| 104 | "msg"]) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 105 | ret['result'] = None if test else result |
| 106 | return ret |