Skip to content

Commit ec41eba

Browse files
Merge pull request #527 from netscaler/removeBasicAuth
Removing Basic auth in ansible collections netscaler adc
2 parents 81424b9 + 9595eea commit ec41eba

2 files changed

Lines changed: 63 additions & 18 deletions

File tree

galaxy.yml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ readme: README.md
1313
# A list of the collection's content authors. Can be just the name or in the format 'Full Name <email> (url)
1414
# @nicks:irc/im.site#channel'
1515
authors:
16-
- Sumanth Lingappa <sumanth.lingappa@cloud.com>
17-
- Shiva Shankar Vaddepally <shivashankar.vaddepally@cloud.com>
16+
- Sumanth Lingappa <sumanth.lingappa@cloud.com>
17+
- Shiva Shankar Vaddepally <shivashankar.vaddepally@cloud.com>
1818
### OPTIONAL but strongly recommended
1919
# A short summary description of the collection
2020
description: Ansible Collection Modules for NetScaler ADC
2121
# Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only
2222
# accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file'
2323
license:
24-
- MIT
24+
- MIT
2525
# The path to the license file for the collection. This path is relative to the root of the collection. This key is
2626
# mutually exclusive with 'license'
2727
# license_file: '' # TODO
2828

2929
# A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character
3030
# requirements as 'namespace' and 'name'
3131
tags:
32-
- netscaler
33-
- adc
34-
- networking
32+
- netscaler
33+
- adc
34+
- networking
3535
# Collections that this collection requires to be installed for it to be usable. The key of the dict is the
3636
# collection label 'namespace.name'. The value is a version range
3737
# L(specifiers,https://python-semanticversion.readthedocs.io/en/latest/#requirement-specification). Multiple version
@@ -50,18 +50,18 @@ issues: https://github.com/netscaler/ansible-collection-netscaleradc/issues
5050
# uses 'fnmatch' to match the files or directories. Some directories and files like 'galaxy.yml', '*.pyc', '*.retry',
5151
# and '.git' are always filtered. Mutually exclusive with 'manifest'
5252
build_ignore:
53-
# https://docs.ansible.com/ansible/latest/dev_guide/developing_collections_distributing.html#ignoring-files-and-folders
54-
- "_built_docs*"
55-
- ".github*"
56-
- ".vscode*"
57-
- "assets*"
58-
- .DS_Store
59-
- docs
60-
- examples
61-
- tools
62-
- issues
63-
- "**/netscaler-adc-*.tar.gz"
64-
- Makefile
53+
# https://docs.ansible.com/ansible/latest/dev_guide/developing_collections_distributing.html#ignoring-files-and-folders
54+
- "_built_docs*"
55+
- ".github*"
56+
- ".vscode*"
57+
- "assets*"
58+
- ".DS_Store"
59+
- "docs"
60+
- "examples"
61+
- "tools"
62+
- "issues"
63+
- "**/netscaler-adc-*.tar.gz"
64+
- "Makefile"
6565

6666
# A dict controlling use of manifest directives used in building the collection artifact. The key 'directives' is a
6767
# list of MANIFEST.in style

plugins/module_utils/module_executor.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,37 @@ def __init__(self, resource_name, supports_check_mode=True):
138138
self.module.params["api_path"] = "nitro/v2/config"
139139

140140
self.client = NitroAPIClient(self.module, self.resource_name)
141+
have_userpass = all([
142+
self.module.params.get("nitro_user"),
143+
self.module.params.get("nitro_pass")
144+
])
145+
if have_userpass and not self.module.check_mode:
146+
self.client._headers.pop("X-NITRO-USER", None)
147+
self.client._headers.pop("X-NITRO-PASS", None)
148+
self.client._headers.pop("Cookie", None)
149+
ok, response = adc_login(
150+
self.client,
151+
self.module.params["nitro_user"],
152+
self.module.params["nitro_pass"],
153+
)
154+
if not ok:
155+
self.client._headers["Cookie"] = ""
156+
self.return_failure("ERROR: Login failed: %s" % response)
157+
158+
else:
159+
if self.netscaler_console_as_proxy_server:
160+
self.module.params["nitro_auth_token"] = response["login"][0].get("sessionid", None)
161+
else:
162+
self.module.params["nitro_auth_token"] = response.get("sessionid", None)
163+
164+
if not self.module.params["nitro_auth_token"]:
165+
self.return_failure("ERROR: Login failed. No sessionid returned from the NetScaler ADC")
166+
log("INFO: Login successful. Session ID: %s" % self.module.params["nitro_auth_token"])
167+
168+
self.client._headers["Cookie"] = (
169+
"NITRO_AUTH_TOKEN=%s" % self.module.params["nitro_auth_token"]
170+
)
171+
141172
self.ns_major_version, self.ns_minor_version = get_netscaler_version(
142173
self.client
143174
)
@@ -181,6 +212,13 @@ def return_success(self):
181212
# }
182213
if self.resource_name == "login":
183214
self.module_result["sessionid"] = self.sessionid
215+
if self.client._headers.get("Cookie", None) not in (None, "") and not self.module.check_mode:
216+
ok, response = adc_logout(self.client)
217+
if not ok:
218+
log("ERROR: Logout failed: %s" % response)
219+
else:
220+
log("INFO: Logout successful")
221+
self.client._headers["Cookie"] = ""
184222
self.module.exit_json(**self.module_result)
185223

186224
@trace
@@ -204,6 +242,13 @@ def update_diff_list(self, existing=None, desired=None, delete=False, **kwargs):
204242

205243
@trace
206244
def return_failure(self, msg):
245+
if self.client._headers["Cookie"] != "" and not self.module.check_mode:
246+
ok, response = adc_logout(self.client)
247+
if not ok:
248+
log("ERROR: Logout failed: %s" % response)
249+
else:
250+
log("INFO: Logout successful")
251+
self.client._headers["Cookie"] = ""
207252
self.module.fail_json(msg=msg, **self.module_result)
208253

209254
@trace

0 commit comments

Comments
 (0)