blob: e4b21b42169c26d7b4252c92e7dc69c836bbcdc0 [file] [log] [blame]
Jakub Josefbf0b73e2017-03-22 14:32:45 +01001import logging
2logger = logging.getLogger(__name__)
3
4approve_signature_groovy = """\
5import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval
6import org.jenkinsci.plugins.scriptsecurity.scripts.languages.GroovyLanguage
7import org.jenkinsci.plugins.scriptsecurity.scripts.ApprovalContext
Jakub Josef0a03c2c2017-03-22 17:35:40 +01008def signature = '{signature}'
Jakub Josefbf0b73e2017-03-22 14:32:45 +01009def scriptApproval = ScriptApproval.get()
10def approvedSignatures = Arrays.asList(scriptApproval.approvedSignatures)
11if(approvedSignatures.contains(signature)){{
Jakub Josef0a03c2c2017-03-22 17:35:40 +010012 print("EXISTS")
Jakub Josefbf0b73e2017-03-22 14:32:45 +010013}}else{{
14 try{{
15 scriptApproval.pendingSignatures.add(new ScriptApproval.PendingSignature(signature, false, ApprovalContext.create()))
16 scriptApproval.approveSignature(signature)
Jakub Josef0a03c2c2017-03-22 17:35:40 +010017 if(Arrays.asList(scriptApproval.approvedSignatures).contains(signature)){{
Jakub Josefbf0b73e2017-03-22 14:32:45 +010018 print("SUCCESS")
19 }}else{{
20 print("FAILED")
21 }}
22 }}catch(e){{
23 print(e)
24 }}
25}}
26""" # noqa
27
28deny_signature_groovy = """\
29import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval
30import org.jenkinsci.plugins.scriptsecurity.scripts.languages.GroovyLanguage
31import org.jenkinsci.plugins.scriptsecurity.scripts.ApprovalContext
Jakub Josef0a03c2c2017-03-22 17:35:40 +010032def signature = '{signature}'
Jakub Josefbf0b73e2017-03-22 14:32:45 +010033def scriptApproval = ScriptApproval.get()
34def approvedSignatures = Arrays.asList(scriptApproval.approvedSignatures)
35if(approvedSignatures.contains(signature)){{
36 try{{
37 scriptApproval.denySignature(signature)
38 if(!scriptApproval.approvedSignatures.contains(signature)){{
39 print("SUCCESS")
40 }}else{{
41 print("FAILED")
42 }}
43 }}catch(e){{
44 print(e)
45 }}
46}}else{{
47 print("NOT PRESENT")
48}}
49
50
51"""
52
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040053
54def __virtual__():
55 '''
56 Only load if jenkins_common module exist.
57 '''
58 if 'jenkins_common.call_groovy_script' not in __salt__:
59 return (
60 False,
61 'The jenkins_approval state module cannot be loaded: '
62 'jenkins_common not found')
63 return True
64
65
Jakub Josefbf0b73e2017-03-22 14:32:45 +010066def approved(name):
67 """
68 Jenkins Script approval approve state method
69
70 :param name: signature to approve
71 :returns: salt-specified state dict
72 """
73 test = __opts__['test'] # noqa
74 ret = {
75 'name': name,
76 'changes': {},
77 'result': False,
78 'comment': '',
79 }
80 result = False
81 if test:
82 status = "SUCCESS"
83 ret['changes'][name] = status
84 ret['comment'] = 'Jenkins script approval config %s %s' % (name, status.lower())
85 else:
86 call_result = __salt__['jenkins_common.call_groovy_script'](
87 approve_signature_groovy, {"signature":name})
88 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "EXISTS"]:
89 status = call_result["msg"]
90 if status == "SUCCESS":
91 ret['changes'][name] = status
92 ret['comment'] = 'Jenkins script approval config %s %s' % (name, status.lower())
93 result = True
94 else:
95 status = 'FAILED'
96 logger.error(
97 "Jenkins script approval API call failure: %s", call_result["msg"])
98 ret['comment'] = 'Jenkins script approval API call failure: %s' % (call_result[
99 "msg"])
100 ret['result'] = None if test else result
101 return ret
102
103
104def denied(name):
105 """
106 Jenkins Script approval deny state method
107
108 :param name: signature to deny
109 :returns: salt-specified state dict
110 """
111 test = __opts__['test'] # noqa
112 ret = {
113 'name': name,
114 'changes': {},
115 'result': False,
116 'comment': '',
117 }
118 result = False
119 if test:
120 status = "SUCCESS"
121 ret['changes'][name] = status
122 ret['comment'] = 'Jenkins script approval config %s %s' % (name, status.lower())
123 else:
124 call_result = __salt__['jenkins_common.call_groovy_script'](
125 deny_signature_groovy, {"signature":name})
126 if call_result["code"] == 200 and call_result["msg"] in ["SUCCESS", "NOT PRESENT"]:
127 status = call_result["msg"]
128 if status == "SUCCESS":
129 ret['changes'][name] = status
130 ret['comment'] = 'Jenkins script approval config %s %s' % (name, status.lower())
131 result = True
132 else:
133 status = 'FAILED'
134 logger.error(
135 "Jenkins script approval API call failure: %s", call_result["msg"])
136 ret['comment'] = 'Jenkins script approval lib API call failure: %s' % (call_result[
137 "msg"])
138 ret['result'] = None if test else result
139 return ret