Skip to content

Commit

Permalink
Support hidden parameters in global_parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Oct 16, 2020
1 parent fe0f1d0 commit 08b2e84
Show file tree
Hide file tree
Showing 33 changed files with 5,114 additions and 2,707 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/global_parameter-hidden_values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- allow to set hidden flag on ``global_parameter`` (https://github.com/theforeman/foreman-ansible-modules/issues/1024)
26 changes: 24 additions & 2 deletions plugins/module_utils/foreman_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,24 @@ def _patch_cv_filter_rule_api(self):
if create_param is not None and update_param is None:
_content_view_filter_rule_update['params'].append(create_param)

@_check_patch_needed()
def _patch_common_parameter_index_show_api(self):
"""
This is a workaround for wrong validation of the 'show_missing' querystring in 'common_parameter' index and show.
"""

_common_parameter_methods = self.foremanapi.apidoc['docs']['resources']['common_parameters']['methods']

_common_parameter_index = next(x for x in _common_parameter_methods if x['name'] == 'index')
_common_parameter_show = next(x for x in _common_parameter_methods if x['name'] == 'show')

_index_param = next((x for x in _common_parameter_index['params'] if x['name'] == 'show_hidden'), None)
_show_param = next((x for x in _common_parameter_show['params'] if x['name'] == 'show_hidden'), None)
if _index_param is not None:
_index_param['expected_type'] = 'string'
if _show_param is not None:
_show_param['expected_type'] = 'string'

def check_requirements(self):
if not HAS_APYPIE:
self.fail_json(msg=missing_required_lib("apypie"), exception=APYPIE_IMP_ERR)
Expand Down Expand Up @@ -549,6 +567,7 @@ def apply_apidoc_patches(self):
self._patch_templates_resource_name()
self._patch_location_api()
self._patch_subnet_rex_api()
self._patch_common_parameter_index_show_api()

# Katello
self._patch_content_uploads_update_api()
Expand Down Expand Up @@ -716,7 +735,7 @@ def set_entity(self, key, entity):
self.foreman_params[key] = entity
self.foreman_spec[key]['resolved'] = True

def lookup_entity(self, key):
def lookup_entity(self, key, params=None):
if key not in self.foreman_params:
return None

Expand All @@ -728,7 +747,10 @@ def lookup_entity(self, key):
resource_type = entity_spec['resource_type']
failsafe = entity_spec.get('failsafe', False)
thin = entity_spec.get('thin', True)
params = {}
if params:
params = params.copy()
else:
params = {}
try:
if 'scope' in entity_spec:
for scope in entity_spec['scope']:
Expand Down
17 changes: 15 additions & 2 deletions plugins/modules/global_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
- Value of the Global Parameter
required: false
type: raw
hidden_value:
description:
- Whether the value should be hidden in the GUI
required: false
type: bool
parameter_type:
description:
- Type of value
Expand Down Expand Up @@ -111,14 +116,20 @@


class ForemanCommonParameterModule(ForemanEntityAnsibleModule):
pass
def remove_sensitive_fields(self, entity):
if entity and 'hidden_value?' in entity:
entity['hidden_value'] = entity.pop('hidden_value?')
if entity['hidden_value']:
entity['value'] = None
return super(ForemanCommonParameterModule, self).remove_sensitive_fields(entity)


def main():
module = ForemanCommonParameterModule(
foreman_spec=dict(
name=dict(required=True),
value=dict(type='raw'),
hidden_value=dict(type='bool'),
parameter_type=dict(default='string', choices=['string', 'boolean', 'integer', 'real', 'array', 'hash', 'yaml', 'json']),
),
argument_spec=dict(
Expand All @@ -132,7 +143,7 @@ def main():
)

with module.api_connection():
entity = module.lookup_entity('entity')
entity = module.lookup_entity('entity', params={'show_hidden': "true"})

if not module.desired_absent:
# Convert values according to their corresponding parameter_type
Expand All @@ -141,6 +152,8 @@ def main():
module.foreman_params['value'] = parameter_value_to_str(module.foreman_params['value'], module.foreman_params['parameter_type'])
if entity and 'value' in entity:
entity['value'] = parameter_value_to_str(entity['value'], entity.get('parameter_type', 'string'))
if entity and 'hidden_value?' in entity:
entity['hidden_value'] = entity.pop('hidden_value?')

module.run()

Expand Down
246 changes: 157 additions & 89 deletions tests/test_playbooks/fixtures/global_parameter-0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,175 @@ interactions:
- request:
body: null
headers:
Accept: [application/json;version=2]
Accept:
- application/json;version=2
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- apypie (https://github.com/Apipie/apypie)
method: GET
uri: https://foreman.example.com/api/status
uri: https://foreman.example.org/api/status
response:
body: {string: !!python/unicode '{"result":"ok","status":200,"version":"1.22.1","api_version":2}'}
body:
string: '{"result":"ok","status":200,"version":"2.2.0-rc3","api_version":2}'
headers:
cache-control: ['max-age=0, private, must-revalidate']
content-length: ['63']
content-security-policy: ['default-src ''self''; child-src ''self''; connect-src
''self'' ws: wss:; img-src ''self'' data: *.gravatar.com; script-src ''unsafe-eval''
''unsafe-inline'' ''self''; style-src ''unsafe-inline'' ''self''']
content-type: [application/json; charset=utf-8]
date: ['Fri, 27 Sep 2019 15:26:18 GMT']
etag: [W/"7462024e111aafa1fe0b7de16a6757f0"]
foreman_api_version: ['2']
foreman_version: [1.22.1]
server: [Apache]
set-cookie: [_session_id=1e879874fdbf90f51a257b566ae7a858; path=/; secure; HttpOnly;
SameSite=Lax]
status: [200 OK]
strict-transport-security: [max-age=631139040; includeSubdomains]
x-content-type-options: [nosniff]
x-download-options: [noopen]
x-frame-options: [sameorigin]
x-permitted-cross-domain-policies: [none]
x-powered-by: [Phusion Passenger 4.0.53]
x-request-id: [29e96369-ac31-499c-95cd-e7ce3caeeabd]
x-runtime: ['0.026413']
x-xss-protection: [1; mode=block]
status: {code: 200, message: OK}
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- Keep-Alive
Content-Security-Policy:
- 'default-src ''self''; child-src ''self''; connect-src ''self'' ws: wss:;
img-src ''self'' data:; script-src ''unsafe-eval'' ''unsafe-inline'' ''self'';
style-src ''unsafe-inline'' ''self'''
Content-Type:
- application/json; charset=utf-8
Foreman_api_version:
- '2'
Foreman_current_location:
- ; ANY
Foreman_current_organization:
- ; ANY
Foreman_version:
- 2.2.0-rc3
Keep-Alive:
- timeout=15, max=100
Strict-Transport-Security:
- max-age=631139040; includeSubdomains
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- sameorigin
X-Permitted-Cross-Domain-Policies:
- none
X-XSS-Protection:
- 1; mode=block
content-length:
- '66'
status:
code: 200
message: OK
- request:
body: null
headers:
Accept: [application/json;version=2]
Cookie: [_session_id=1e879874fdbf90f51a257b566ae7a858]
Accept:
- application/json;version=2
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- apypie (https://github.com/Apipie/apypie)
method: GET
uri: https://foreman.example.com/api/common_parameters?per_page=4294967296&search=name%3D%22TheAnswer%22
uri: https://foreman.example.org/api/common_parameters?show_hidden=true&search=name%3D%22TheAnswer%22&per_page=4294967296
response:
body: {string: !!python/unicode "{\n \"total\": 0,\n \"subtotal\": 0,\n \"page\":
1,\n \"per_page\": 4294967296,\n \"search\": \"name=\\\"TheAnswer\\\"\",\n
\ \"sort\": {\n \"by\": null,\n \"order\": null\n },\n \"results\":
[]\n}\n"}
body:
string: "{\n \"total\": 0,\n \"subtotal\": 0,\n \"page\": 1,\n \"per_page\"\
: 4294967296,\n \"search\": \"name=\\\"TheAnswer\\\"\",\n \"sort\": {\n\
\ \"by\": null,\n \"order\": null\n },\n \"results\": []\n}\n"
headers:
cache-control: ['max-age=0, private, must-revalidate']
content-security-policy: ['default-src ''self''; child-src ''self''; connect-src
''self'' ws: wss:; img-src ''self'' data: *.gravatar.com; script-src ''unsafe-eval''
''unsafe-inline'' ''self''; style-src ''unsafe-inline'' ''self''']
content-type: [application/json; charset=utf-8]
date: ['Fri, 27 Sep 2019 15:26:18 GMT']
etag: [W/"2fec3047627a901f18e2388c756e5108"]
foreman_api_version: ['2']
foreman_version: [1.22.1]
server: [Apache]
status: [200 OK]
strict-transport-security: [max-age=631139040; includeSubdomains]
transfer-encoding: [chunked]
vary: [Accept-Encoding]
x-content-type-options: [nosniff]
x-download-options: [noopen]
x-frame-options: [sameorigin]
x-permitted-cross-domain-policies: [none]
x-powered-by: [Phusion Passenger 4.0.53]
x-request-id: [fb3318b3-bd75-448a-ad7d-02d47c071d39]
x-runtime: ['0.017186']
x-xss-protection: [1; mode=block]
status: {code: 200, message: OK}
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- Keep-Alive
Content-Security-Policy:
- 'default-src ''self''; child-src ''self''; connect-src ''self'' ws: wss:;
img-src ''self'' data:; script-src ''unsafe-eval'' ''unsafe-inline'' ''self'';
style-src ''unsafe-inline'' ''self'''
Content-Type:
- application/json; charset=utf-8
Foreman_api_version:
- '2'
Foreman_current_location:
- ; ANY
Foreman_current_organization:
- ; ANY
Foreman_version:
- 2.2.0-rc3
Keep-Alive:
- timeout=15, max=99
Strict-Transport-Security:
- max-age=631139040; includeSubdomains
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- sameorigin
X-Permitted-Cross-Domain-Policies:
- none
X-XSS-Protection:
- 1; mode=block
content-length:
- '175'
status:
code: 200
message: OK
- request:
body: !!python/unicode '{"common_parameter": {"parameter_type": "string", "name":
"TheAnswer", "value": 42}}'
body: '{"common_parameter": {"name": "TheAnswer", "value": 42, "parameter_type":
"string"}}'
headers:
Accept: [application/json;version=2]
Content-Length: ['84']
Content-Type: [application/json]
Cookie: [_session_id=1e879874fdbf90f51a257b566ae7a858]
Accept:
- application/json;version=2
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '84'
Content-Type:
- application/json
User-Agent:
- apypie (https://github.com/Apipie/apypie)
method: POST
uri: https://foreman.example.com/api/common_parameters
uri: https://foreman.example.org/api/common_parameters
response:
body: {string: !!python/unicode '{"created_at":"2019-09-27 15:26:18 UTC","updated_at":"2019-09-27
15:26:18 UTC","hidden_value?":false,"hidden_value":"*****","id":4,"name":"TheAnswer","parameter_type":"string","value":42}'}
body:
string: '{"created_at":"2020-10-16 13:53:30 UTC","updated_at":"2020-10-16 13:53:30
UTC","hidden_value?":false,"hidden_value":"*****","id":11,"name":"TheAnswer","parameter_type":"string","value":42}'
headers:
cache-control: ['max-age=0, private, must-revalidate']
content-security-policy: ['default-src ''self''; child-src ''self''; connect-src
''self'' ws: wss:; img-src ''self'' data: *.gravatar.com; script-src ''unsafe-eval''
''unsafe-inline'' ''self''; style-src ''unsafe-inline'' ''self''']
content-type: [application/json; charset=utf-8]
date: ['Fri, 27 Sep 2019 15:26:18 GMT']
etag: [W/"02d511fb2a98d01f2a0ade08be1b7a07"]
foreman_api_version: ['2']
foreman_version: [1.22.1]
server: [Apache]
set-cookie: [request_method=POST; path=/; secure; HttpOnly; SameSite=Lax]
status: [201 Created]
strict-transport-security: [max-age=631139040; includeSubdomains]
transfer-encoding: [chunked]
x-content-type-options: [nosniff]
x-download-options: [noopen]
x-frame-options: [sameorigin]
x-permitted-cross-domain-policies: [none]
x-powered-by: [Phusion Passenger 4.0.53]
x-request-id: [b4f79b34-d607-4973-8d9c-5eb230876396]
x-runtime: ['0.042700']
x-xss-protection: [1; mode=block]
status: {code: 201, message: Created}
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- Keep-Alive
Content-Security-Policy:
- 'default-src ''self''; child-src ''self''; connect-src ''self'' ws: wss:;
img-src ''self'' data:; script-src ''unsafe-eval'' ''unsafe-inline'' ''self'';
style-src ''unsafe-inline'' ''self'''
Content-Type:
- application/json; charset=utf-8
Foreman_api_version:
- '2'
Foreman_current_location:
- ; ANY
Foreman_current_organization:
- ; ANY
Foreman_version:
- 2.2.0-rc3
Keep-Alive:
- timeout=15, max=98
Strict-Transport-Security:
- max-age=631139040; includeSubdomains
Transfer-Encoding:
- chunked
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- sameorigin
X-Permitted-Cross-Domain-Policies:
- none
X-XSS-Protection:
- 1; mode=block
status:
code: 201
message: Created
version: 1
Loading

0 comments on commit 08b2e84

Please sign in to comment.