Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 1 | import logging |
| 2 | logger = logging.getLogger(__name__) |
| 3 | |
| 4 | set_ldap_groovy = """\ |
| 5 | import jenkins.model.* |
| 6 | import hudson.security.* |
| 7 | import org.jenkinsci.plugins.* |
| 8 | |
| 9 | def server = 'ldap://{server}' |
| 10 | def rootDN = '{rootDN}' |
| 11 | def userSearchBase = '{userSearchBase}' |
| 12 | def userSearch = '{userSearch}' |
| 13 | def groupSearchBase = '{groupSearchBase}' |
| 14 | def managerDN = '{managerDN}' |
| 15 | def managerPassword = '{managerPassword}' |
| 16 | boolean inhibitInferRootDN = {inhibitInferRootDN} |
| 17 | |
| 18 | try{{ |
| 19 | ldapRealm = Class.forName("hudson.security.LDAPSecurityRealm").getConstructor(String.class, String.class, String.class, String.class, String.class, String.class, String.class, Boolean.TYPE) |
| 20 | .newInstance(server, rootDN, userSearchBase, userSearch, groupSearchBase, managerDN, managerPassword, inhibitInferRootDN) |
| 21 | Jenkins.instance.setSecurityRealm(ldapRealm) |
| 22 | Jenkins.instance.save() |
| 23 | print("SUCCESS") |
| 24 | }}catch(ClassNotFoundException e){{ |
| 25 | print("Cannot instantiate LDAPSecurityRealm, maybe ldap plugin not installed") |
| 26 | }} |
| 27 | """ # noqa |
| 28 | |
| 29 | set_matrix_groovy = """\ |
| 30 | import jenkins.model.* |
| 31 | import hudson.security.* |
| 32 | import com.cloudbees.plugins.credentials.* |
| 33 | |
| 34 | def instance = Jenkins.getInstance() |
| 35 | try{{ |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 36 | def strategy = Class.forName("hudson.security.{matrix_class}").newInstance() |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 37 | {strategies} |
| 38 | instance.setAuthorizationStrategy(strategy) |
| 39 | instance.save() |
| 40 | print("SUCCESS") |
| 41 | }}catch(ClassNotFoundException e){{ |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 42 | print("Cannot instantiate {matrix_class}, maybe auth-matrix plugin not installed") |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 43 | }} |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 44 | """ # noqa |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 45 | |
| 46 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 47 | def __virtual__(): |
| 48 | ''' |
| 49 | Only load if jenkins_common module exist. |
| 50 | ''' |
| 51 | if 'jenkins_common.call_groovy_script' not in __salt__: |
| 52 | return ( |
| 53 | False, |
| 54 | 'The jenkins_security state module cannot be loaded: ' |
| 55 | 'jenkins_common not found') |
| 56 | return True |
| 57 | |
| 58 | |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 59 | def ldap(name, server, root_dn, user_search_base, manager_dn, manager_password, user_search="", group_search_base="", inhibit_infer_root_dn=False): |
| 60 | """ |
| 61 | Jenkins ldap state method |
| 62 | |
| 63 | :param name: ldap state name |
| 64 | :param server: ldap server host (without ldap://) |
| 65 | :param root_dn: root domain names |
| 66 | :param user_search_base: |
| 67 | :param manager_dn: |
| 68 | :param manager_password: |
| 69 | :param user_search: optional, default empty string |
| 70 | :param group_search_base: optional, default empty string |
| 71 | :param inhibit_infer_root_dn: optional, default false |
| 72 | :returns: salt-specified state dict |
| 73 | """ |
| 74 | test = __opts__['test'] # noqa |
| 75 | ret = { |
| 76 | 'name': name, |
| 77 | 'changes': {}, |
| 78 | 'result': False, |
| 79 | 'comment': '', |
| 80 | } |
| 81 | result = False |
| 82 | if test: |
| 83 | status = 'CREATED' |
| 84 | ret['changes'][name] = status |
| 85 | ret['comment'] = 'LDAP setup %s %s' % (name, status.lower()) |
| 86 | else: |
| 87 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 88 | set_ldap_groovy, {"name": name, "server": server, "rootDN": root_dn, |
Jakub Josef | 95ad980 | 2017-01-17 15:59:00 +0100 | [diff] [blame] | 89 | "userSearchBase": user_search_base if user_search_base else "", |
| 90 | "managerDN": manager_dn if manager_dn else "", |
| 91 | "managerPassword": manager_password if manager_password else "", |
| 92 | "userSearch": user_search if user_search else "", |
| 93 | "groupSearchBase": group_search_base if group_search_base else "", |
| 94 | "inhibitInferRootDN": "true" if inhibit_infer_root_dn else "false"}) |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 95 | if call_result["code"] == 200 and call_result["msg"] == "SUCCESS": |
| 96 | status = call_result["msg"] |
| 97 | ret['changes'][name] = status |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 98 | ret['comment'] = 'Jenkins LDAP setting %s %s' % ( |
| 99 | name, status.lower()) |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 100 | result = True |
| 101 | else: |
| 102 | status = 'FAILED' |
| 103 | logger.error( |
| 104 | "Jenkins security API call failure: %s", call_result["msg"]) |
| 105 | ret['comment'] = 'Jenkins security API call failure: %s' % (call_result[ |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 106 | "msg"]) |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 107 | ret['result'] = None if test else result |
| 108 | return ret |
| 109 | |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 110 | |
| 111 | def matrix(name, strategies, project_based=False): |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 112 | """ |
| 113 | Jenkins matrix security state method |
| 114 | |
| 115 | :param name: ldap state name |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 116 | :param strategies: dict with matrix strategies |
| 117 | :param procect_based: flag if we configuring |
| 118 | GlobalMatrix security or ProjectMatrix security |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 119 | :returns: salt-specified state dict |
| 120 | """ |
| 121 | test = __opts__['test'] # noqa |
| 122 | ret = { |
| 123 | 'name': name, |
| 124 | 'changes': {}, |
| 125 | 'result': False, |
| 126 | 'comment': '', |
| 127 | } |
| 128 | result = False |
| 129 | if test: |
| 130 | status = 'CREATED' |
| 131 | ret['changes'][name] = status |
| 132 | ret['comment'] = 'LDAP setup %s %s' % (name, status.lower()) |
| 133 | else: |
| 134 | call_result = __salt__['jenkins_common.call_groovy_script']( |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 135 | set_matrix_groovy, {"strategies": _build_strategies(strategies), |
| 136 | "matrix_class": "ProjectMatrixAuthorizationStrategy" if project_based else "GlobalMatrixAuthorizationStrategy"}) |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 137 | if call_result["code"] == 200 and call_result["msg"] == "SUCCESS": |
| 138 | status = call_result["msg"] |
| 139 | ret['changes'][name] = status |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 140 | ret['comment'] = 'Jenkins Matrix security setting %s %s' % ( |
| 141 | name, status.lower()) |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 142 | result = True |
| 143 | else: |
| 144 | status = 'FAILED' |
| 145 | logger.error( |
| 146 | "Jenkins security API call failure: %s", call_result["msg"]) |
| 147 | ret['comment'] = 'Jenkins security API call failure: %s' % (call_result[ |
Jakub Josef | 0ee470e | 2017-01-17 11:46:58 +0100 | [diff] [blame] | 148 | "msg"]) |
Jakub Josef | 063a753 | 2017-01-11 15:48:01 +0100 | [diff] [blame] | 149 | ret['result'] = None if test else result |
| 150 | return ret |
| 151 | |
| 152 | |
| 153 | def _build_strategies(permissions): |
| 154 | strategies_str = "" |
| 155 | for strategy in _to_strategies_list("strategy.add({},\"{}\")", _to_one_dict(permissions, "")): |
| 156 | strategies_str += "{}\n".format(strategy) |
| 157 | return strategies_str |
| 158 | |
| 159 | |
| 160 | def _to_strategies_list(strategy_format, strategy_dict): |
| 161 | res = [] |
| 162 | for key, value in strategy_dict.items(): |
| 163 | if isinstance(value, list): |
| 164 | for user in value: |
| 165 | res.append(strategy_format.format(key, user)) |
| 166 | else: |
| 167 | res.append(strategy_format.format(key, value)) |
| 168 | return res |
| 169 | |
| 170 | |
| 171 | def _to_one_dict(input_dict, input_key): |
| 172 | res = {} |
| 173 | for key, value in input_dict.items(): |
| 174 | new_key = key if input_key == "" else "{}.{}".format(input_key, key) |
| 175 | if isinstance(value, dict): |
| 176 | res.update(_to_one_dict(value, new_key)) |
| 177 | else: |
| 178 | res[new_key] = value |
| 179 | return res |