mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Pass CSRF token along with Jenkins POST requests (#23250)
This commit is contained in:
parent
b71b157fb3
commit
4540853a50
1 changed files with 27 additions and 0 deletions
|
@ -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…
Reference in a new issue