|
1 | 1 | # this playbook contains middleware server tests |
2 | 2 |
|
3 | | -- name: wait for middleware port to become available |
4 | | - wait_for: |
5 | | - port: "{{ middleware_web_listener_port }}" |
6 | | - host: "{{ middleware_hostname }}" |
7 | | - connect_timeout: 1 |
8 | | - delay: 10 |
9 | | - timeout: 25 |
| 3 | +- name: wait for middleware port to become available |
| 4 | + ansible.builtin.wait_for: |
| 5 | + port: "{{ middleware_web_listener_port }}" |
| 6 | + host: "{{ middleware_hostname }}" |
| 7 | + connect_timeout: 1 |
| 8 | + delay: 10 |
| 9 | + timeout: 25 |
10 | 10 |
|
11 | 11 | - name: middleware test get jwt valid creds |
12 | | - uri: |
13 | | - url: https://{{ middleware_hostname }}:{{ middleware_web_listener_port }}/api/AuthenticationToken/Get/ |
14 | | - method: POST |
15 | | - headers: |
16 | | - Content-Type: application/json |
17 | | - body: |
18 | | - Username: user1{{ test_postfix }} |
19 | | - Password: "{{ test_user1_pw }}" |
20 | | - body_format: json |
21 | | - validate_certs: false |
22 | | - return_content: true |
| 12 | + ansible.builtin.uri: |
| 13 | + url: "https://{{ middleware_hostname }}:{{ middleware_web_listener_port }}/api/AuthenticationToken/Get/" |
| 14 | + method: POST |
| 15 | + headers: |
| 16 | + Content-Type: application/json |
| 17 | + body: |
| 18 | + Username: "user1{{ test_postfix }}" |
| 19 | + Password: "{{ test_user1_pw }}" |
| 20 | + body_format: json |
| 21 | + validate_certs: false |
| 22 | + return_content: true |
23 | 23 | register: sample_jwt |
24 | 24 | changed_when: false |
25 | 25 | # environment: "{{ proxy_env }}" |
26 | 26 |
|
27 | | -- debug: var=sample_jwt |
| 27 | +- ansible.builtin.debug: |
| 28 | + var: sample_jwt |
28 | 29 |
|
29 | | -- set_fact: jwt_header="{{sample_jwt.content.split('.')[0]}}" |
| 30 | +# --- JWT header decode (base64url -> json) --- |
| 31 | +- name: extract raw jwt header and payload segments |
| 32 | + ansible.builtin.set_fact: |
| 33 | + jwt_header_raw: "{{ sample_jwt.content.split('.')[0] }}" |
| 34 | + jwt_payload_raw: "{{ sample_jwt.content.split('.')[1] }}" |
30 | 35 |
|
31 | | -- debug: var=jwt_header |
| 36 | +# pad and translate base64url for header |
| 37 | +- name: normalize/pad base64url header |
| 38 | + ansible.builtin.set_fact: |
| 39 | + jwt_header_b64: >- |
| 40 | + {{ jwt_header_raw |
| 41 | + | regex_replace('-', '+') |
| 42 | + | regex_replace('_', '/') |
| 43 | + ~ ('=' * ((4 - (jwt_header_raw | length % 4)) % 4)) }} |
32 | 44 |
|
33 | | -## adding potentially missing base64 "=" padding to header: |
34 | | -- set_fact: jwt_header="{{ jwt_header }}=" cacheable=true |
35 | | - when: "jwt_header|length % 4 > 0" |
36 | | - loop: |
37 | | - - 1 |
38 | | - - 2 |
39 | | - - 3 |
| 45 | +- ansible.builtin.debug: { var: jwt_header_b64 } |
40 | 46 |
|
41 | | -- debug: var=jwt_header |
| 47 | +- name: decode + parse header json |
| 48 | + ansible.builtin.set_fact: |
| 49 | + jwt_header: "{{ jwt_header_b64 | b64decode | from_json }}" |
42 | 50 |
|
43 | | -- set_fact: jwt_header="{{ jwt_header|b64decode }}" |
44 | | -- debug: var=jwt_header |
45 | | -- set_fact: jwt_type="{{ jwt_header.typ }}" |
46 | | -- debug: var=jwt_type |
| 51 | +- ansible.builtin.debug: { var: jwt_header } |
| 52 | + |
| 53 | +- name: pick header 'typ' |
| 54 | + ansible.builtin.set_fact: |
| 55 | + jwt_type: "{{ jwt_header['typ'] | default(jwt_header.typ, true) }}" |
| 56 | + |
| 57 | +- ansible.builtin.debug: { var: jwt_type } |
47 | 58 |
|
48 | 59 | - name: middleware get jwt test valid creds output |
49 | | - debug: |
50 | | - msg: "ERROR unexpected jwt test result (jwt_type does not match 'JWT'): {{ jwt_type }}" |
| 60 | + ansible.builtin.debug: |
| 61 | + msg: "ERROR unexpected jwt test result (jwt_type does not match 'JWT'): {{ jwt_type }}" |
51 | 62 | when: "jwt_type is not match('JWT')" |
52 | 63 |
|
53 | | -- set_fact: jwt_encoded_payload="{{sample_jwt.content.split('.')[1]}}" |
| 64 | +# --- JWT payload decode (base64url -> json) --- |
| 65 | +- name: normalize/pad base64url payload |
| 66 | + ansible.builtin.set_fact: |
| 67 | + jwt_payload_b64: >- |
| 68 | + {{ jwt_payload_raw |
| 69 | + | regex_replace('-', '+') |
| 70 | + | regex_replace('_', '/') |
| 71 | + ~ ('=' * ((4 - (jwt_payload_raw | length % 4)) % 4)) }} |
54 | 72 |
|
55 | | -- name: show jwt encoded payload pre padding |
56 | | - debug: var=jwt_encoded_payload |
57 | | -## adding potentially missing base64 = padding to payload: |
58 | | -- set_fact: jwt_encoded_payload="{{ jwt_encoded_payload }}=" cacheable=true |
59 | | - when: "jwt_encoded_payload|length % 4 > 0" |
60 | | - loop: |
61 | | - - 1 |
62 | | - - 2 |
63 | | - - 3 |
| 73 | +- ansible.builtin.debug: |
| 74 | + var: jwt_payload_b64 |
64 | 75 |
|
65 | | -- name: show jwt encoded payload post padding |
66 | | - debug: var=jwt_encoded_payload |
67 | | - |
68 | | -- set_fact: jwt_payload="{{ jwt_encoded_payload|b64decode }}" |
| 76 | +- name: decode + parse payload json |
| 77 | + ansible.builtin.set_fact: |
| 78 | + jwt_payload: "{{ jwt_payload_b64 | b64decode | from_json }}" |
69 | 79 |
|
70 | 80 | - name: show jwt decoded payload |
71 | | - debug: var=jwt_payload |
72 | | - |
73 | | -- set_fact: jwt_unique_user_name="{{ jwt_payload.unique_name }}" |
74 | | - when: "'unique_name' in jwt_payload" |
| 81 | + ansible.builtin.debug: |
| 82 | + var: jwt_payload |
75 | 83 |
|
76 | | -- set_fact: jwt_unique_user_name="{{ jwt_payload.name }}" |
77 | | - when: "'name' in jwt_payload" |
| 84 | +- name: select username from payload (unique_name or name) |
| 85 | + ansible.builtin.set_fact: |
| 86 | + jwt_unique_user_name: >- |
| 87 | + {{ ( 'unique_name' in jwt_payload ) | ternary(jwt_payload.unique_name, jwt_payload.name) }} |
78 | 88 |
|
79 | 89 | - name: Verify JWT Middleware and Check Valid Credentials, Output User 'user1_test' |
80 | | - debug: |
81 | | - msg: "ERROR: Unexpected JWT test result (username does not match 'user1{{ test_postfix }}'): {{ jwt_unique_user_name }}" |
| 90 | + ansible.builtin.debug: |
| 91 | + msg: "ERROR: Unexpected JWT test result (username does not match 'user1{{ test_postfix }}'): {{ jwt_unique_user_name }}" |
82 | 92 | when: "jwt_unique_user_name != 'user1' ~ test_postfix" |
83 | 93 |
|
| 94 | +# --- negative test: wrong credentials --- |
84 | 95 | - name: middleware test get jwt wrong creds |
85 | | - uri: |
86 | | - url: https://{{ middleware_hostname }}:{{ middleware_web_listener_port }}/api/AuthenticationToken/Get/ |
87 | | - method: POST |
88 | | - headers: |
89 | | - Content-Type: application/json |
90 | | - body: |
91 | | - Username: user1{{ test_postfix }} |
92 | | - Password: wrong-pwd |
93 | | - body_format: json |
94 | | - validate_certs: false |
95 | | - return_content: true |
| 96 | + ansible.builtin.uri: |
| 97 | + url: "https://{{ middleware_hostname }}:{{ middleware_web_listener_port }}/api/AuthenticationToken/Get/" |
| 98 | + method: POST |
| 99 | + headers: |
| 100 | + Content-Type: application/json |
| 101 | + body: |
| 102 | + Username: "user1{{ test_postfix }}" |
| 103 | + Password: "wrong-pwd" |
| 104 | + body_format: json |
| 105 | + validate_certs: false |
| 106 | + return_content: true |
96 | 107 | register: sample_jwt |
97 | 108 | changed_when: false |
98 | 109 | ignore_errors: true |
99 | 110 |
|
100 | | -- debug: |
101 | | - var: sample_jwt |
| 111 | +- ansible.builtin.debug: |
| 112 | + var: sample_jwt |
102 | 113 |
|
103 | 114 | - name: middleware get jwt test wrong creds output |
104 | | - debug: |
105 | | - msg: "ERROR unexpected jwt test result (not equal 'error: Invalid credentials.'): {{ sample_jwt.content }}" |
106 | | - when: sample_jwt.content is not match(".*error.*Invalid\scredentials\..*") |
| 115 | + ansible.builtin.debug: |
| 116 | + msg: "ERROR unexpected jwt test result (not equal 'error: Invalid credentials.'): {{ sample_jwt.content }}" |
| 117 | + when: sample_jwt.content is not match(".*error.*Invalid\\scredentials\\..*") |
0 commit comments