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 | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 17 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 18 | def result = creds.find{{ it.username == "{username}" && it.password.toString() == "{password}" }} |
| 19 | if(result){{ |
| 20 | print("EXISTS") |
| 21 | }}else{{ |
| 22 | domain = Domain.global() |
| 23 | store = Jenkins.instance.getExtensionList( |
| 24 | 'com.cloudbees.plugins.credentials.SystemCredentialsProvider' |
| 25 | )[0].getStore() |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 26 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 27 | credentials_new = new {clazz}( |
| 28 | {params} |
| 29 | ) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 30 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 31 | creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( |
| 32 | {clazz}.class, Jenkins.instance |
| 33 | ); |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 34 | ret = store.addCredentials(domain, credentials_new) |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 35 | if (ret) {{ |
| 36 | print("CREATED"); |
| 37 | }} else {{ |
| 38 | print("FAILED"); |
| 39 | }} |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 40 | }} |
| 41 | """ # noqa |
| 42 | |
Jakub Josef | b6c60bc | 2016-12-21 13:53:33 +0100 | [diff] [blame] | 43 | |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 44 | def present(name, scope, username, password=None, desc="", key=None): |
| 45 | """ |
| 46 | Main jenkins credentials state method |
| 47 | |
| 48 | :param name: credential name |
| 49 | :param scope: credential scope |
| 50 | :param username: username |
| 51 | :param password: password (optional) |
| 52 | :param desc: credential description (optional) |
| 53 | :param key: credential key (optional) |
| 54 | :returns: salt-specified state dict |
| 55 | """ |
| 56 | test = __opts__['test'] # noqa |
| 57 | ret = { |
| 58 | 'name': name, |
| 59 | 'changes': {}, |
| 60 | 'result': False, |
| 61 | 'comment': '', |
| 62 | } |
| 63 | result = False |
| 64 | if test: |
| 65 | status = 'CREATED' |
| 66 | ret['changes'][name] = status |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 67 | ret['comment'] = 'Credentials %s %s' % (name, status.lower()) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 68 | else: |
| 69 | clazz = "" |
| 70 | if key: |
| 71 | clazz = "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" |
Jakub Josef | b6c60bc | 2016-12-21 13:53:33 +0100 | [diff] [blame] | 72 | params = 'CredentialsScope.{}, "{}", "{}", new com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("{}"), "{}", "{}"'.format( |
| 73 | scope, name, username, key, password, desc) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 74 | else: |
| 75 | clazz = "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl" |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 76 | params = 'CredentialsScope.{}, "{}", "{}", "{}", "{}"'.format( |
| 77 | scope, name, desc, username, password) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 78 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 79 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 80 | create_credential_groovy, {"username": username, "password": password, "clazz": clazz, "params": params}) |
| 81 | if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]: |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 82 | status = call_result["msg"] |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 83 | if call_result["msg"] == "CREATED": |
| 84 | ret['changes'][name] = status |
| 85 | ret['comment'] = 'Credentials %s %s' % (name, status.lower()) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 86 | result = True |
| 87 | else: |
| 88 | status = 'FAILED' |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 89 | logger.error( |
| 90 | "Jenkins credentials API call failure: %s", call_result["msg"]) |
Jakub Josef | b6c60bc | 2016-12-21 13:53:33 +0100 | [diff] [blame] | 91 | ret['comment'] = 'Jenkins credentials API call failure: %s' % (call_result[ |
| 92 | "msg"]) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 93 | ret['result'] = None if test else result |
| 94 | return ret |