Skip to content

Commit

Permalink
Merge pull request #3 from wcm-io-devops/feature/csrf-cookie-support
Browse files Browse the repository at this point in the history
Login into jenkins instance to retrieve jenkins_facts_csrf_ crumb and jenkins_facts_cookie
  • Loading branch information
tobias-richter authored May 26, 2020
2 parents 57c754c + d3ce214 commit 9b832f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ The following facts are supplied by the `jenkins_facts` role:
* `{{ jenkins_facts_plugins_failed }}`: List of plugins that failed to
load with the cause
* `{{ jenkins_facts_csrf_enabled }}`: Status if CSRF protection is
enabled or disabled
enabled or disabled (in newer Jenkins instances CSRF protection is mandatory)
* `{{ jenkins_facts_csrf_crumb }}`: Fresh/current CSRD Jenkins-Crumb token
* `{{ jenkins_facts_cookie }}`: Fresh/current Cookie token

## Example Playbook

Expand Down
20 changes: 13 additions & 7 deletions tasks/gather_jenkins_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@
jenkins_facts_plugins_failed: "{{ jenkins_facts_raw | json_query('pluginmanager.failedPlugins') }}"
jenkins_facts_csrf_enabled: "{{ jenkins_facts_raw | json_query('instance.csrf.enabled') | bool }}"
jenkins_facts_csrf_crumb: None
jenkins_facts_cookie: None

- block:
- name: "gather_jenkins_facts : retrieve crumb."

- name: "gather_jenkins_facts : get cookie and crumb."
uri:
url: "{{ jenkins_facts_jenkins_base_url }}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"
force_basic_auth: true
url: "{{ jenkins_facts_jenkins_base_url }}/crumbIssuer/api/json"
user: "{{ jenkins_facts_admin_username }}"
password: "{{ jenkins_facts_admin_password }}"
timeout: "{{ jenkins_facts_uri_timeout }}"
return_content: yes

return_content: true
force_basic_auth: true
method: GET
status_code: 200
register: _jenkins_facts_csrf_crumb_result
changed_when: false

- name: "gather_jenkins_facts : set jenkins csrf crumb."
set_fact:
jenkins_facts_csrf_crumb: "{{ _jenkins_facts_csrf_crumb_result.content
| regex_replace('^Jenkins-Crumb:([0-9a-zA-Z]+)$', '\\1') }}"
jenkins_facts_csrf_crumb: "{{ _jenkins_facts_csrf_crumb_result.json.crumb }}"
jenkins_facts_cookie: "{{ _jenkins_facts_csrf_crumb_result.set_cookie }}"

when: jenkins_facts_csrf_enabled | bool

Expand All @@ -57,4 +61,6 @@
- "{{ jenkins_facts_plugins_failed }}"
- jenkins_facts_csrf_crumb
- "{{ jenkins_facts_csrf_crumb }}"
- jenkins_facts_csrf_crumb
- "{{ jenkins_facts_cookie }}"
verbosity: 1

0 comments on commit 9b832f2

Please sign in to comment.