-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ONYX-26897 To reuse the token #185
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few questions:
- In the PR description, I notice a link to a Salesforce case, but I can't tell the connection between the case and this PR. Any more information here?
- We definitely want a security review with @andytinkham before merging this to make sure we're correctly handling token files.
- Caching the token will only improve performance on requests executed within the token's 8 minute lifetime. Could we implement a batch secret retrieval instead, without caching the token?
- You've added some more test cases, but none of them actually confirm the changed behavior - we need at least one more test that confirms a retrieved Conjur token is cached in a particular file.
if response.getcode() == 200: | ||
display.vvvv('Conjur token was successfully retrieved and authorized with {0} code and {1} username '.format(code, username)) | ||
return response.read() | ||
if response.getcode() == 401: | ||
raise AnsibleError('Conjur request has invalid authorization credentials as {0} and {1} response'.format(code, username)) | ||
if response.getcode() == 403: | ||
raise AnsibleError('The controlling host\'s Conjur identity does not have authorization as \'{0}\' (got {1} response)' | ||
.format(username, code)) | ||
if response.getcode() == 404: | ||
raise AnsibleError('The token does not exist with {0} response '.format(code)) | ||
if response.getcode() == 500: | ||
raise AnsibleError('Internal Server Error with {0} response'.format(code)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the purposes of determining whether an authentication request has failed or not, I don't know if we really need to capture all these conditions, or if we need to expose them to the Ansible client.
if response.getcode() == 200: | |
display.vvvv('Conjur token was successfully retrieved and authorized with {0} code and {1} username '.format(code, username)) | |
return response.read() | |
if response.getcode() == 401: | |
raise AnsibleError('Conjur request has invalid authorization credentials as {0} and {1} response'.format(code, username)) | |
if response.getcode() == 403: | |
raise AnsibleError('The controlling host\'s Conjur identity does not have authorization as \'{0}\' (got {1} response)' | |
.format(username, code)) | |
if response.getcode() == 404: | |
raise AnsibleError('The token does not exist with {0} response '.format(code)) | |
if response.getcode() == 500: | |
raise AnsibleError('Internal Server Error with {0} response'.format(code)) | |
if code == 200: | |
display.vvvv('Conjur user {0} successfully authenticated'.format(username)) | |
return response.read() | |
elif code == 500: | |
raise AnsibleError('Internal Server Error: {0}'.format(response.read())) | |
else: | |
raise AnsibleError('Authentication failed with status code {0}: {1}.'.format(code, response.read()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall I remove all except 200, 500 and 401 code . Please suggest .
@@ -0,0 +1 @@ | |||
token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this file location is used for token storage, it should be added to .gitignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes , it is there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now in new changes , I am not using any new file .
plugins/lookup/conjur_variable.py
Outdated
path = '../../tests/conjur_variable/plugin_token.txt' | ||
isExist = os.path.exists(path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely include a default token file path, but this path is relative to some unknown sub-directory of the project's root, and points to the test directory - probably don't want to hard-code this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure , I will update it .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
plugins/lookup/conjur_variable.py
Outdated
path = '../../tests/conjur_variable/plugin_token.txt' | ||
isExist = os.path.exists(path) | ||
|
||
isEmpty = 0 | ||
if ((isExist is True)): | ||
isEmpty = os.path.getsize(path) | ||
|
||
token = None | ||
if 'authn_token_file' not in conf: | ||
token = _fetch_conjur_token( | ||
conf['appliance_url'], | ||
conf['account'], | ||
identity['id'], | ||
identity['api_key'], | ||
validate_certs, | ||
cert_file | ||
) | ||
if ((isExist is False) or (isEmpty == 0)): | ||
token = _fetch_conjur_token( | ||
conf['appliance_url'], | ||
conf['account'], | ||
identity['id'], | ||
identity['api_key'], | ||
validate_certs, | ||
cert_file | ||
) | ||
with open("plugin_token.txt", "wb") as binary_file: | ||
binary_file.write(token) | ||
else: | ||
with open("plugin_token.txt", "rb") as f: | ||
token = f.read() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we can use the existing authn_token_file
configuration to clean this up a bit. Traditionally, it's used to point the service to an existing file that already contains a Conjur token, but maybe we could repurpose it to indicate that either:
- The file already exists and has a Conjur token, or
- The file does not exist, so we want to authenticate with Conjur and store our token there
Given that, we could probably re-arrange this bit:
if 'authn_token_file' not in conf:
token = _fetch_conjur_token(...)
else:
if os.path.exists(conf['authn_token_file']):
with open(conf['authn_token_file'], 'rb') as f:
token = f.read()
else:
token = _fetch_conjur_token(...)
with open(conf['authn_token_file'], "wb") as f:
f.write(token)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made changes to use the existing file "access_token" .
plugins/lookup/conjur_variable.py
Outdated
@@ -102,6 +102,7 @@ | |||
from ansible.module_utils.urls import open_url | |||
from ansible.utils.display import Display | |||
import ssl | |||
from pathlib import Path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import is not used in these changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
Desired Outcome
The ansible-lookup-plugin does not support retries
Implemented Changes
Code fix is around supporting re-tries with authentication.
Create a mock test which can return 401 on first call and returns a token on subsequent call to prove re-try logic.
Meeting notes from 11/03/2022:
Connected Issue/Story
Jira ticket - ONYX-26897
Bug - https://cyberark.lightning.force.com/lightning/r/Case/5002J00001Y4ffFQAR/view
Definition of Done
At least 1 todo must be completed in the sections below for the PR to be
merged.
Changelog
CHANGELOG update
Test coverage
changes, or
Documentation
README
s) were updated in this PRBehavior
Security