-
Notifications
You must be signed in to change notification settings - Fork 12
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
add support for using elastic cloud api key for auth #33
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
import pprint | ||
import fnmatch | ||
import concurrent.futures | ||
from typing import Any | ||
|
||
import six | ||
import pytest | ||
|
@@ -70,6 +71,14 @@ def pytest_addoption(parser): | |
help="Elasticsearch password", | ||
) | ||
|
||
group.addoption( | ||
"--es-api-key", | ||
action="store", | ||
dest="es_api_key", | ||
default=None, | ||
help="Elasticsearch api key in base64 format", | ||
) | ||
|
||
group.addoption( | ||
"--es-timeout", | ||
action="store", | ||
|
@@ -106,6 +115,9 @@ def pytest_addoption(parser): | |
parser.addini("es_address", help="Elasticsearch address", default=None) | ||
parser.addini("es_username", help="Elasticsearch username", default=None) | ||
parser.addini("es_password", help="Elasticsearch password", default=None) | ||
parser.addini( | ||
"es_api_key", help="Elasticsearch api key in base64 format", default=None | ||
) | ||
parser.addini( | ||
"es_index_name", | ||
help="name of the elasticsearch index to save results to", | ||
|
@@ -154,6 +166,7 @@ def __init__(self, config): | |
else: # default to True | ||
self.es_post_reports = True | ||
self.es_address = config.getoption("es_address") or config.getini("es_address") | ||
self.es_api_key = config.getoption("es_api_key") or config.getini("es_api_key") | ||
self.es_username = config.getoption("es_username") or config.getini( | ||
"es_username" | ||
) | ||
|
@@ -192,8 +205,12 @@ def __init__(self, config): | |
self.is_slave = False | ||
|
||
@property | ||
def es_auth(self): | ||
return self.es_username, self.es_password | ||
def es_auth_args(self) -> dict[str, Any]: | ||
if self.es_api_key: | ||
return dict(headers={"Authorization": f"ApiKey {self.es_api_key}"}) | ||
if self.es_username and self.es_password: | ||
return dict(auth=(self.es_username, self.es_password)) | ||
return {} | ||
|
||
@property | ||
def es_url(self): | ||
|
@@ -283,7 +300,7 @@ def report_test(self, item_report, outcome, old_report=None): | |
outcome=outcome, | ||
duration=item_report.duration, | ||
markers=item_report.keywords, | ||
**self.session_data | ||
**self.session_data, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay.. |
||
) | ||
test_data.update(self.test_data[item_report.nodeid]) | ||
del self.test_data[item_report.nodeid] | ||
|
@@ -318,7 +335,7 @@ def pytest_internalerror(self, excrepr): | |
timestamp=datetime.datetime.utcnow().isoformat(), | ||
outcome="internal-error", | ||
faiure_message=str(excrepr), | ||
**self.session_data | ||
**self.session_data, | ||
) | ||
self.post_to_elasticsearch(test_data) | ||
|
||
|
@@ -327,7 +344,7 @@ def post_to_elasticsearch(self, test_data): | |
try: | ||
url = "{0.es_url}/{0.es_index_name}/_doc".format(self) | ||
res = requests.post( | ||
url, json=test_data, auth=self.es_auth, timeout=self.es_timeout | ||
url, json=test_data, timeout=self.es_timeout, **self.es_auth_args | ||
) | ||
res.raise_for_status() | ||
except Exception as ex: # pylint: disable=broad-except | ||
|
@@ -363,7 +380,7 @@ def get_test_stats(test_id): | |
} | ||
try: | ||
res = session.post( | ||
url, json=body, auth=self.es_auth, timeout=self.es_timeout | ||
url, json=body, timeout=self.es_timeout, **self.es_auth_args | ||
) | ||
res.raise_for_status() | ||
return dict( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
IIUC, this function always returns a
dict[str, dict[str, str]]
. if that's the case, probably we can just usedict[str, dict[str, str]]
here?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.
it can return
dict[str, tuple[str]]
, but is not important, it can return any request parameter and I don't want to document it all