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 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 5 | def __virtual__(): |
| 6 | ''' |
| 7 | Only load if jenkins_common module exist. |
| 8 | ''' |
| 9 | if 'jenkins_common.call_groovy_script' not in __salt__: |
| 10 | return ( |
| 11 | False, |
| 12 | 'The jenkins_credentials state module cannot be loaded: ' |
| 13 | 'jenkins_common not found') |
| 14 | return True |
| 15 | |
| 16 | |
Yuriy Taraday | 2e21e3c | 2018-02-06 20:17:58 +0400 | [diff] [blame] | 17 | def present(name, scope, username, password="", desc="", key=None, secret=None, filename=None, content=None): |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 18 | """ |
| 19 | Main jenkins credentials state method |
| 20 | |
| 21 | :param name: credential name |
| 22 | :param scope: credential scope |
Yuriy Taraday | 2e21e3c | 2018-02-06 20:17:58 +0400 | [diff] [blame] | 23 | :param username: username (optional) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 24 | :param password: password (optional) |
| 25 | :param desc: credential description (optional) |
| 26 | :param key: credential key (optional) |
Yuriy Taraday | 2e21e3c | 2018-02-06 20:17:58 +0400 | [diff] [blame] | 27 | :param filename: file name for file credential (optional) |
| 28 | :param content: file content for file credential (optional) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 29 | :returns: salt-specified state dict |
| 30 | """ |
Yuriy Taraday | 2e21e3c | 2018-02-06 20:17:58 +0400 | [diff] [blame] | 31 | template = __salt__['jenkins_common.load_template']( |
| 32 | 'salt://jenkins/files/groovy/credential.template', |
| 33 | __env__) |
| 34 | clazz = "" |
| 35 | if key: |
| 36 | clazz = "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" |
| 37 | params = 'CredentialsScope.{}, "{}", "{}", new com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(key.trim()), "{}", "{}"'.format( |
| 38 | scope, name, username, password if password else "", desc if desc else "") |
| 39 | elif secret: |
| 40 | clazz = "org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl" |
| 41 | params = 'CredentialsScope.{}, "{}", "{}"'.format( |
| 42 | scope, name, desc if desc else "") |
| 43 | elif filename: |
| 44 | clazz = "org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl" |
| 45 | params = 'CredentialsScope.{}, "{}", "{}", "{}", SecretBytes.fromBytes("""{}""".getBytes())'.format( |
| 46 | scope, name, desc if desc else "", filename, content.encode('unicode_escape')) |
Jakub Josef | 8e7385e | 2016-12-07 21:20:34 +0100 | [diff] [blame] | 47 | else: |
Yuriy Taraday | 2e21e3c | 2018-02-06 20:17:58 +0400 | [diff] [blame] | 48 | clazz = "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl" |
| 49 | params = 'CredentialsScope.{}, "{}", "{}", "{}", "{}"'.format( |
| 50 | scope, name, desc if desc else "", username, password) |
| 51 | return __salt__['jenkins_common.api_call'](name, template, |
| 52 | ["CREATED", "EXISTS"], |
| 53 | { |
| 54 | "scope": scope, |
| 55 | "name": name, |
| 56 | "username": username if username else "", |
| 57 | "password": password if password else "", |
| 58 | "clazz": clazz, |
| 59 | "params": params, |
| 60 | "key": key if key else "", |
| 61 | "desc": desc if desc else "", |
| 62 | "secret": secret if secret else "", |
| 63 | "fileName": filename if filename else "", |
| 64 | "content": content.encode('unicode_escape') if content else "" |
| 65 | }, |
| 66 | "Credentials") |