blob: e8389baf040c1a73da0474e2e5a2dc7dd95c631d [file] [log] [blame]
Jakub Josef063a7532017-01-11 15:48:01 +01001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josef063a7532017-01-11 15:48:01 +01003logger = logging.getLogger(__name__)
4
Ilya Kharin3d8bffe2017-06-22 17:40:31 +04005def __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_security state module cannot be loaded: '
13 'jenkins_common not found')
14 return True
15
16
Adam Tengler70763e02017-08-21 16:50:32 +000017def ldap(name, server, root_dn, user_search_base, manager_dn, manager_password,
18 user_search="", group_search_base="", inhibit_infer_root_dn=False):
Jakub Josef063a7532017-01-11 15:48:01 +010019 """
20 Jenkins ldap state method
21
22 :param name: ldap state name
Andrey6606be02017-08-02 17:09:42 -050023 :param server: ldap server host
Jakub Josef063a7532017-01-11 15:48:01 +010024 :param root_dn: root domain names
25 :param user_search_base:
26 :param manager_dn:
27 :param manager_password:
28 :param user_search: optional, default empty string
29 :param group_search_base: optional, default empty string
30 :param inhibit_infer_root_dn: optional, default false
31 :returns: salt-specified state dict
32 """
Andrey6606be02017-08-02 17:09:42 -050033 if not server.startswith("ldap:") and not server.startswith("ldaps:"):
34 server = "ldap://{server}".format(server=server)
35
Dmitry Burmistrov2af1da72018-05-24 11:24:17 +040036 template = __salt__['jenkins_common.load_template'](
37 'salt://jenkins/files/groovy/security.ldap.template',
38 __env__)
39 return __salt__['jenkins_common.api_call'](name, template,
40 ["CHANGED", "EXISTS"],
41 {
42 "name": name,
43 "server": server,
44 "rootDN": root_dn,
45 "userSearchBase": user_search_base if user_search_base else "",
46 "managerDN": manager_dn if manager_dn else "",
47 "managerPassword": manager_password if manager_password else "",
48 "userSearch": user_search if user_search else "",
49 "groupSearchBase": group_search_base if group_search_base else "",
50 "inhibitInferRootDN": "true" if inhibit_infer_root_dn else "false"
51 },
52 "Jenkins LDAP Settings")
Jakub Josef063a7532017-01-11 15:48:01 +010053
Jakub Josef0ee470e2017-01-17 11:46:58 +010054
55def matrix(name, strategies, project_based=False):
Jakub Josef063a7532017-01-11 15:48:01 +010056 """
57 Jenkins matrix security state method
58
59 :param name: ldap state name
Jakub Josef0ee470e2017-01-17 11:46:58 +010060 :param strategies: dict with matrix strategies
61 :param procect_based: flag if we configuring
62 GlobalMatrix security or ProjectMatrix security
Jakub Josef063a7532017-01-11 15:48:01 +010063 :returns: salt-specified state dict
64 """
Dmitry Burmistrov2af1da72018-05-24 11:24:17 +040065 template = __salt__['jenkins_common.load_template'](
66 'salt://jenkins/files/groovy/security.matrix.template',
67 __env__)
68 return __salt__['jenkins_common.api_call'](name, template,
69 ["CHANGED", "EXISTS"],
70 {
71 "strategies": _build_strategies(strategies),
72 "matrix_class": "ProjectMatrixAuthorizationStrategy" if project_based else "GlobalMatrixAuthorizationStrategy"},
73 "Jenkins Matrix security setting")
Jakub Josef063a7532017-01-11 15:48:01 +010074
75def _build_strategies(permissions):
76 strategies_str = ""
Adam Tengler70763e02017-08-21 16:50:32 +000077 for strategy in _to_strategies_list(
78 "strategy.add({},\"{}\")", _to_one_dict(permissions, "")):
Jakub Josef063a7532017-01-11 15:48:01 +010079 strategies_str += "{}\n".format(strategy)
80 return strategies_str
81
82
83def _to_strategies_list(strategy_format, strategy_dict):
84 res = []
85 for key, value in strategy_dict.items():
86 if isinstance(value, list):
87 for user in value:
88 res.append(strategy_format.format(key, user))
89 else:
90 res.append(strategy_format.format(key, value))
91 return res
92
93
94def _to_one_dict(input_dict, input_key):
95 res = {}
96 for key, value in input_dict.items():
97 new_key = key if input_key == "" else "{}.{}".format(input_key, key)
98 if isinstance(value, dict):
99 res.update(_to_one_dict(value, new_key))
100 else:
101 res[new_key] = value
102 return res