| import logging |
| |
| logger = logging.getLogger(__name__) |
| |
| def __virtual__(): |
| ''' |
| Only load if jenkins_common module exist. |
| ''' |
| if 'jenkins_common.call_groovy_script' not in __salt__: |
| return ( |
| False, |
| 'The jenkins_security state module cannot be loaded: ' |
| 'jenkins_common not found') |
| return True |
| |
| |
| def agent2master(name, enabled, whitelisted, file_path_rules): |
| """ |
| Jenkins Agent to Master Access Control state method |
| |
| :param enabled |
| :param whitelisted: Whitelisted Commands |
| :param file_path_rules: File Access Rules |
| """ |
| |
| template = __salt__['jenkins_common.load_template']( |
| 'salt://jenkins/files/groovy/security.agent2master.template', |
| __env__) |
| return __salt__['jenkins_common.api_call'](name, template, |
| ["CHANGED", "EXISTS"], |
| { |
| 'enabled': enabled, |
| 'newWhitelistedContent': whitelisted, |
| 'newFilePathRulesContent': file_path_rules |
| }, |
| 'Agent to Master Access Control') |
| |
| def csp(name, policy): |
| """ |
| Jenkins Content Security Policy state method |
| |
| :param policy |
| """ |
| default_policy = """\ |
| sandbox; default-src 'none'; img-src 'self'; style-src 'self'; |
| """.strip() |
| |
| template = __salt__['jenkins_common.load_template']( |
| 'salt://jenkins/files/groovy/security.csp.template', |
| __env__) |
| return __salt__['jenkins_common.api_call'](name, template, |
| ["CHANGED", "EXISTS"], |
| { |
| 'policy': policy if policy else default_policy, |
| }, |
| 'Content Security Policy') |
| |
| def csrf(name, enabled, proxy_compat): |
| """ |
| Jenkins CSRF protection state method |
| |
| :param enabled |
| :param proxy_compat |
| """ |
| |
| template = __salt__['jenkins_common.load_template']( |
| 'salt://jenkins/files/groovy/security.csrf.template', |
| __env__) |
| return __salt__['jenkins_common.api_call'](name, template, |
| ["CHANGED", "EXISTS"], |
| { |
| 'csrfEnabled': enabled, |
| 'proxyCompat': proxy_compat |
| }, |
| 'CSRF Protection') |
| |
| def ldap(name, server, root_dn, user_search_base, manager_dn, manager_password, |
| user_search="", group_search_base="", inhibit_infer_root_dn=False): |
| """ |
| Jenkins ldap state method |
| |
| :param name: ldap state name |
| :param server: ldap server host |
| :param root_dn: root domain names |
| :param user_search_base: |
| :param manager_dn: |
| :param manager_password: |
| :param user_search: optional, default empty string |
| :param group_search_base: optional, default empty string |
| :param inhibit_infer_root_dn: optional, default false |
| :returns: salt-specified state dict |
| """ |
| if not server.startswith("ldap:") and not server.startswith("ldaps:"): |
| server = "ldap://{server}".format(server=server) |
| |
| template = __salt__['jenkins_common.load_template']( |
| 'salt://jenkins/files/groovy/security.ldap.template', |
| __env__) |
| return __salt__['jenkins_common.api_call'](name, template, |
| ["CHANGED", "EXISTS"], |
| { |
| "name": name, |
| "server": server, |
| "rootDN": root_dn, |
| "userSearchBase": user_search_base if user_search_base else "", |
| "managerDN": manager_dn if manager_dn else "", |
| "managerPassword": manager_password if manager_password else "", |
| "userSearch": user_search if user_search else "", |
| "groupSearchBase": group_search_base if group_search_base else "", |
| "inhibitInferRootDN": "true" if inhibit_infer_root_dn else "false" |
| }, |
| "Jenkins LDAP Settings") |
| |
| |
| def matrix(name, strategies, project_based=False): |
| """ |
| Jenkins matrix security state method |
| |
| :param name: ldap state name |
| :param strategies: dict with matrix strategies |
| :param procect_based: flag if we configuring |
| GlobalMatrix security or ProjectMatrix security |
| :returns: salt-specified state dict |
| """ |
| template = __salt__['jenkins_common.load_template']( |
| 'salt://jenkins/files/groovy/security.matrix.template', |
| __env__) |
| return __salt__['jenkins_common.api_call'](name, template, |
| ["CHANGED", "EXISTS"], |
| { |
| "strategies": _build_strategies(strategies), |
| "matrix_class": "ProjectMatrixAuthorizationStrategy" if project_based else "GlobalMatrixAuthorizationStrategy"}, |
| "Jenkins Matrix security setting") |
| |
| def _build_strategies(permissions): |
| strategies_str = "" |
| for strategy in _to_strategies_list( |
| "strategy.add({},\"{}\")", _to_one_dict(permissions, "")): |
| strategies_str += "{}\n".format(strategy) |
| return strategies_str |
| |
| |
| def _to_strategies_list(strategy_format, strategy_dict): |
| res = [] |
| for key, value in strategy_dict.items(): |
| if isinstance(value, list): |
| for user in value: |
| res.append(strategy_format.format(key, user)) |
| else: |
| res.append(strategy_format.format(key, value)) |
| return res |
| |
| |
| def _to_one_dict(input_dict, input_key): |
| res = {} |
| for key, value in input_dict.items(): |
| new_key = key if input_key == "" else "{}.{}".format(input_key, key) |
| if isinstance(value, dict): |
| res.update(_to_one_dict(value, new_key)) |
| else: |
| res[new_key] = value |
| return res |