Pass CSRF token along with Jenkins POST requests (#23250) (#23330)

(cherry picked from commit 4540853a50)
pull/23337/head
John R Barker 8 years ago committed by GitHub
parent c3e3580baf
commit d6a8f10a70

@ -116,6 +116,8 @@ output:
sample: 'Result: true' sample: 'Result: true'
''' '''
import json
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url from ansible.module_utils.urls import fetch_url
try: try:
@ -125,6 +127,26 @@ except ImportError:
# python3 # python3
from urllib.parse import urlencode from urllib.parse import urlencode
def is_csrf_protection_enabled(module):
resp, info = fetch_url(module,
module.params['url'] + '/api/json',
method='GET')
if info["status"] != 200:
module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"])
content = resp.read()
return json.loads(content).get('useCrumbs', False)
def get_crumb(module):
resp, info = fetch_url(module,
module.params['url'] + '/crumbIssuer/api/json',
method='GET')
if info["status"] != 200:
module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"])
content = resp.read()
return json.loads(content)
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -153,10 +175,15 @@ def main():
else: else:
script_contents = module.params['script'] script_contents = module.params['script']
headers = {}
if is_csrf_protection_enabled(module):
crumb = get_crumb(module)
headers = {crumb['crumbRequestField']: crumb['crumb']}
resp, info = fetch_url(module, resp, info = fetch_url(module,
module.params['url'] + "/scriptText", module.params['url'] + "/scriptText",
data=urlencode({'script': script_contents}), data=urlencode({'script': script_contents}),
headers = headers,
method="POST") method="POST")
if info["status"] != 200: if info["status"] != 200:

Loading…
Cancel
Save