-
Notifications
You must be signed in to change notification settings - Fork 8
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
Multi auth provider credentials #93
Open
kdp-cloud
wants to merge
11
commits into
main
Choose a base branch
from
multi-auth-provider-credentials
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f8ae54b
Add Authentication Provider Enum
kdp-cloud 60a2e5a
Remove header overloading
kdp-cloud 16c7748
Update credentials file to hold multiple credentials
kdp-cloud 32b4ac0
Add validation for credential manager
kdp-cloud 3313b52
Add options
kdp-cloud 21a481c
Implement in submission function
kdp-cloud 4cb56d1
Modify set-password command
kdp-cloud cee80b4
Fix test
kdp-cloud 775f17b
formatting, linting and type checking
kdp-cloud 3e42bdd
Remove default fields
kdp-cloud 83e3e44
Revert change
kdp-cloud 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
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
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 |
---|---|---|
|
@@ -6,7 +6,12 @@ | |
import requests | ||
import json | ||
from typing import Any | ||
from mars_lib.authentication import get_metabolights_auth_token, get_webin_auth_token | ||
from mars_lib.authentication import ( | ||
get_metabolights_auth_token, | ||
get_webin_auth_token, | ||
load_credentials, | ||
AuthProvider, | ||
) | ||
from mars_lib.biosamples_external_references import ( | ||
get_header, | ||
biosamples_endpoints, | ||
|
@@ -44,8 +49,9 @@ def save_step_to_file(time_stamp: float, filename: str, isa_json: IsaJson): | |
|
||
|
||
def submission( | ||
credential_service_name: str, | ||
username_credentials: str, | ||
webin_username: str, | ||
metabolights_username: str, | ||
metabolights_ftp_username: str, | ||
credentials_file: TextIOWrapper, | ||
isa_json_file: str, | ||
target_repositories: list[str], | ||
|
@@ -59,17 +65,24 @@ def submission( | |
# Get password from the credential manager | ||
# Else: | ||
# read credentials from file | ||
if not (credential_service_name is None or username_credentials is None): | ||
cm = CredentialManager(credential_service_name) | ||
if all([webin_username, metabolights_username, metabolights_ftp_username]): | ||
user_credentials = { | ||
"username": username_credentials, | ||
"password": cm.get_password_keyring(username_credentials), | ||
cred_pair[0]: { | ||
"username": cred_pair[1], | ||
"password": CredentialManager(cred_pair[0]).get_password_keyring( | ||
cred_pair[1] | ||
), | ||
} | ||
for cred_pair in zip( | ||
AuthProvider.available_providers(), | ||
[webin_username, metabolights_username, metabolights_ftp_username], | ||
) | ||
} | ||
else: | ||
if credentials_file == "": | ||
raise ValueError("No credentials found") | ||
|
||
user_credentials = json.load(credentials_file) | ||
user_credentials = load_credentials(credentials_file) | ||
|
||
isa_json = load_isa_json(isa_json_file, investigation_is_root) | ||
|
||
|
@@ -101,7 +114,7 @@ def submission( | |
# Submit to Biosamples | ||
biosamples_result = submit_to_biosamples( | ||
isa_json=isa_json, | ||
biosamples_credentials=user_credentials, | ||
biosamples_credentials=user_credentials[TargetRepository.BIOSAMPLES.value], | ||
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. Should this be |
||
biosamples_url=urls["BIOSAMPLES"]["SUBMISSION"], | ||
webin_token_url=urls["WEBIN"]["TOKEN"], | ||
) | ||
|
@@ -124,7 +137,7 @@ def submission( | |
file_paths=[ | ||
Path(df) for df in data_file_map[TargetRepository.ENA.value] | ||
], | ||
user_credentials=user_credentials, | ||
user_credentials=user_credentials[TargetRepository.ENA.value], | ||
submission_url=urls["ENA"]["DATA-SUBMISSION"], | ||
file_transfer=file_transfer, | ||
) | ||
|
@@ -135,7 +148,7 @@ def submission( | |
# Step 2 : submit isa-json to ena | ||
ena_result = submit_to_ena( | ||
isa_json=isa_json, | ||
user_credentials=user_credentials, | ||
user_credentials=user_credentials[TargetRepository.ENA.value], | ||
submission_url=urls["ENA"]["SUBMISSION"], | ||
) | ||
print_and_log( | ||
|
@@ -159,7 +172,9 @@ def submission( | |
file_paths=data_file_map[TargetRepository.METABOLIGHTS.value], | ||
file_transfer=file_transfer, | ||
isa_json=isa_json, | ||
metabolights_credentials=user_credentials, | ||
metabolights_credentials=user_credentials[ | ||
TargetRepository.METABOLIGHTS.value | ||
], | ||
metabolights_url=urls["METABOLIGHTS"]["SUBMISSION"], | ||
metabolights_token_url=urls["METABOLIGHTS"]["TOKEN"], | ||
) | ||
|
@@ -252,9 +267,9 @@ def upload_to_metabolights( | |
"accept": "application/json", | ||
"Authorization": f"Bearer {token}", | ||
} | ||
isa_json_str = isa_json.investigation.model_dump_json( | ||
by_alias=True, exclude_none=True | ||
) | ||
isa_json_str = reduce_isa_json_for_target_repo( | ||
isa_json, TargetRepository.METABOLIGHTS | ||
).investigation.model_dump_json(by_alias=True, exclude_none=True) | ||
json_file = io.StringIO(isa_json_str) | ||
|
||
files = {"isa_json_file": ("isa_json.json", json_file)} | ||
|
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 |
---|---|---|
@@ -1,24 +1,53 @@ | ||
import pytest | ||
import json | ||
import os | ||
from mars_lib.authentication import get_webin_auth_token | ||
from mars_lib.authentication import ( | ||
get_webin_auth_token, | ||
load_credentials, | ||
get_metabolights_auth_token, | ||
) | ||
from requests.exceptions import HTTPError | ||
|
||
|
||
def test_get_webin_auth_token(): | ||
fake_credentials_dict = { | ||
fake_credentials_dict = { | ||
"webin": { | ||
"username": "my_fake_account", | ||
"password": "my_super_secret_password", | ||
}, | ||
"metabolights_metadata": { | ||
"username": "my_fake_account", | ||
"password": "my_super_secret_password", | ||
}, | ||
"metabolights_data": { | ||
"username": "my_fake_account", | ||
"password": "my_super_secret_password", | ||
} | ||
}, | ||
} | ||
|
||
|
||
def test_get_webin_auth_token(): | ||
with pytest.raises( | ||
ValueError, | ||
match="ERROR when generating token. See response's content below:\nBad credentials", | ||
): | ||
get_webin_auth_token(fake_credentials_dict) | ||
get_webin_auth_token(fake_credentials_dict["webin"]) | ||
|
||
file = "./tests/test_credentials.json" | ||
if os.path.exists(file) and os.path.isfile(file): | ||
with open(file, "r") as f: | ||
test_credentials = json.load(f) | ||
test_credentials = load_credentials(file) | ||
|
||
token = get_webin_auth_token(test_credentials) | ||
token = get_webin_auth_token(test_credentials["webin"]) | ||
assert token | ||
|
||
|
||
@pytest.mark.skipif( | ||
not os.path.exists("./tests/test_credentials.json"), | ||
reason="Credentials file not found", | ||
) | ||
def test_get_metabolights_auth_token(): | ||
credentials = load_credentials("./tests/test_credentials.json") | ||
token = get_metabolights_auth_token(credentials["metabolights_metadata"]) | ||
assert token | ||
|
||
# TODO: Currently an 'Internal Server Error' is returned when using the wrong credentials. | ||
# This should be updated to return a more informative error message. | ||
with pytest.raises(HTTPError): | ||
get_metabolights_auth_token(fake_credentials_dict["metabolights_metadata"]) |
Oops, something went wrong.
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.
I think eventually should loosen this up, e.g. if I'm only submitting the ENA, EVA and BioSamples I shouldn't need Metabolights credentials... it's fine for now though.