-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SSO to retrieve credentials for calling Cognito related queries (…
…#41) * WIP: added boto3 to query cognito * Removed env variables and added dynamic credential retrieval using AWS SSO profiles * removed unnecessary TODO --------- Co-authored-by: gcarvellas <[email protected]>
- Loading branch information
1 parent
62a3a92
commit 8a818ca
Showing
11 changed files
with
84 additions
and
20 deletions.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .users import UsersDB | ||
from .contracts import ContractsDB | ||
from .contracts import ContractsDB | ||
from .cognito import CognitoIdentityProviderWrapper |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# https://docs.aws.amazon.com/code-library/latest/ug/python_3_cognito-identity-provider_code_examples.html | ||
|
||
from botocore.exceptions import ClientError | ||
import logging | ||
import boto3 | ||
from config.env import COGNITO_USERPOOL_ID | ||
from utilities.types import JSONDict | ||
from distutils.util import strtobool | ||
|
||
|
||
class CognitoUser: | ||
|
||
def __init__(self, cognito_response_json: JSONDict) -> None: | ||
assert cognito_response_json.get("UserAttributes") | ||
for attribute_dict in cognito_response_json['UserAttributes']: | ||
assert "Name" in attribute_dict | ||
assert attribute_dict.get("Value") | ||
match attribute_dict['Name']: | ||
case 'sub': | ||
self.sub: str = attribute_dict['Value'] | ||
case 'email_verified': | ||
self.email_verified: bool = bool(strtobool(attribute_dict['Value'])) | ||
case 'email': | ||
self.email: str = attribute_dict['Value'] | ||
assert self.sub is not None | ||
assert self.email_verified is not None | ||
assert self.email is not None | ||
|
||
|
||
class CognitoIdentityProviderWrapper: | ||
"""Encapsulates Amazon Cognito actions""" | ||
def __init__(self) -> None: | ||
self.cognito_idp_client = boto3.client('cognito-idp') | ||
|
||
def get_user(self, username: str) -> CognitoUser: | ||
""" | ||
Gets a user in Cognito by it's username. | ||
:return: user | ||
""" | ||
try: | ||
response: JSONDict = self.cognito_idp_client.admin_get_user(UserPoolId=COGNITO_USERPOOL_ID, Username=username) | ||
assert type(response) is dict | ||
return CognitoUser(response) | ||
except ClientError as err: | ||
logging.error( | ||
"Couldn't list users for %s. Here's why: %s: %s", COGNITO_USERPOOL_ID, | ||
err.response['Error']['Code'], err.response['Error']['Message']) | ||
raise err |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from database import UsersDB | ||
from utilities import NoApproverException | ||
from typing import List | ||
from database import CognitoIdentityProviderWrapper | ||
|
||
|
||
class ContractManager(): | ||
|
@@ -28,11 +29,14 @@ async def create_contract( | |
approver = await UsersDB.get_random_artist_reviewer() | ||
if approver is None: | ||
raise NoApproverException() | ||
# TODO cannot do this to get approver. Need to grab from cognito DB | ||
# approver_email = approver.get("email") | ||
# approver_name = approver.get('name') | ||
approver_email = "[email protected]" | ||
approver_name = "test" | ||
|
||
# Get the email and username from CognitoDB | ||
assert "username" in approver | ||
approver_name = approver["username"] | ||
approver_cognito_data = CognitoIdentityProviderWrapper().get_user(approver_name) | ||
approver_email = approver_cognito_data.email | ||
|
||
# TODO the approver_name should be the user's name, not username | ||
|
||
assert type(approver_email) is str, "Expected a string for approver email" | ||
assert type(approver_name) is str, "Expected a string for approver name" | ||
|
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