blob: c21f77e5a13bac39370a886d6364fcb00eea7b4d [file] [log] [blame]
Jakub Josef8e7385e2016-12-07 21:20:34 +01001import logging
2logger = logging.getLogger(__name__)
3
4create_credential_groovy = u"""\
5import jenkins.*;
6import jenkins.model.*;
7import hudson.*;
8import hudson.model.*;
9
10import com.cloudbees.plugins.credentials.domains.Domain;
11import com.cloudbees.plugins.credentials.CredentialsScope;
12
Jakub Josef98123ab2016-12-14 14:05:01 +010013def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
14 com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
15 Jenkins.instance
16 )
Jakub Josef8e7385e2016-12-07 21:20:34 +010017
Jakub Josef98123ab2016-12-14 14:05:01 +010018def result = creds.find{{ it.username == "{username}" && it.password.toString() == "{password}" }}
19if(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 Josef8e7385e2016-12-07 21:20:34 +010026
Jakub Josef98123ab2016-12-14 14:05:01 +010027 credentials_new = new {clazz}(
28 {params}
29 )
Jakub Josef8e7385e2016-12-07 21:20:34 +010030
Jakub Josef98123ab2016-12-14 14:05:01 +010031 creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
32 {clazz}.class, Jenkins.instance
33 );
Jakub Josef8e7385e2016-12-07 21:20:34 +010034 ret = store.addCredentials(domain, credentials_new)
Jakub Josef98123ab2016-12-14 14:05:01 +010035 if (ret) {{
36 print("CREATED");
37 }} else {{
38 print("FAILED");
39 }}
Jakub Josef8e7385e2016-12-07 21:20:34 +010040}}
41""" # noqa
42
Jakub Josefb6c60bc2016-12-21 13:53:33 +010043
Jakub Josef8e7385e2016-12-07 21:20:34 +010044def 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 Josef98123ab2016-12-14 14:05:01 +010067 ret['comment'] = 'Credentials %s %s' % (name, status.lower())
Jakub Josef8e7385e2016-12-07 21:20:34 +010068 else:
69 clazz = ""
70 if key:
71 clazz = "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
Jakub Josefb6c60bc2016-12-21 13:53:33 +010072 params = 'CredentialsScope.{}, "{}", "{}", new com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource("{}"), "{}", "{}"'.format(
73 scope, name, username, key, password, desc)
Jakub Josef8e7385e2016-12-07 21:20:34 +010074 else:
75 clazz = "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
Jakub Josef98123ab2016-12-14 14:05:01 +010076 params = 'CredentialsScope.{}, "{}", "{}", "{}", "{}"'.format(
77 scope, name, desc, username, password)
Jakub Josef8e7385e2016-12-07 21:20:34 +010078
Jakub Josef98123ab2016-12-14 14:05:01 +010079 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 Josef8e7385e2016-12-07 21:20:34 +010082 status = call_result["msg"]
Jakub Josef98123ab2016-12-14 14:05:01 +010083 if call_result["msg"] == "CREATED":
84 ret['changes'][name] = status
85 ret['comment'] = 'Credentials %s %s' % (name, status.lower())
Jakub Josef8e7385e2016-12-07 21:20:34 +010086 result = True
87 else:
88 status = 'FAILED'
Jakub Josef98123ab2016-12-14 14:05:01 +010089 logger.error(
90 "Jenkins credentials API call failure: %s", call_result["msg"])
Jakub Josefb6c60bc2016-12-21 13:53:33 +010091 ret['comment'] = 'Jenkins credentials API call failure: %s' % (call_result[
92 "msg"])
Jakub Josef8e7385e2016-12-07 21:20:34 +010093 ret['result'] = None if test else result
94 return ret