-
Notifications
You must be signed in to change notification settings - Fork 82
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
Added Token Validations #1682
Added Token Validations #1682
Changes from 11 commits
155db7a
b70b55c
88c03e5
5d1c2c9
02b5d36
7117d55
cba99e9
09caacb
0e77776
e6fcf8f
58a5440
c04760a
b5c0c08
25334bc
fca14f2
64fa2f1
d416a55
913c1ce
a8cfe08
647b689
eddcb86
3b4418d
f30dd4f
320f652
ff910c7
9b9cbad
451b0d4
0e329a3
5a0893b
8b8eb1a
26bd79c
86db3e8
8db81a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,13 +24,22 @@ | |
] | ||
ENGINE = get_engine(envname=ENVNAME) | ||
ALLOWED_ORIGINS = os.getenv('ALLOWED_ORIGINS', '*') | ||
AWS_REGION = os.getenv('AWS_REGION') | ||
|
||
|
||
def redact_creds(event): | ||
if 'headers' in event and 'Authorization' in event['headers']: | ||
if event.get('headers', {}).get('Authorization'): | ||
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. Can anyone please tell why are we redacting these ? 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. user creds information does not need to be logged and is no longer relevant for the remaining request lifecycle - opting to redact that info 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. Instead of redacting , can we just extract useful information and then pass it onto our lambda ? |
||
event['headers']['Authorization'] = 'XXXXXXXXXXXX' | ||
if 'multiValueHeaders' in event and 'Authorization' in event['multiValueHeaders']: | ||
|
||
if event.get('multiValueHeaders', {}).get('Authorization'): | ||
event['multiValueHeaders']['Authorization'] = 'XXXXXXXXXXXX' | ||
|
||
if event.get('multiValueHeaders', {}).get('accesskeyid'): | ||
event['multiValueHeaders']['accesskeyid'] = 'XXXXXXXXXXXX' | ||
|
||
if event.get('headers', {}).get('accesskeyid'): | ||
event['headers']['accesskeyid'] = 'XXXXXXXXXXXX' | ||
|
||
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. in
|
||
return event | ||
|
||
|
||
|
@@ -115,7 +124,7 @@ def check_reauth(query, auth_time, username): | |
# Determine if there are any Operations that Require ReAuth From SSM Parameter | ||
try: | ||
reauth_apis = ParameterStoreManager.get_parameter_value( | ||
region=os.getenv('AWS_REGION', 'eu-west-1'), parameter_path=f'/dataall/{ENVNAME}/reauth/apis' | ||
region=AWS_REGION, parameter_path=f'/dataall/{ENVNAME}/reauth/apis' | ||
).split(',') | ||
except Exception: | ||
log.info('No ReAuth APIs Found in SSM') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,10 @@ def lambda_handler(incoming_event, context): | |
if not verified_claims: | ||
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. Since the method |
||
raise Exception('Unauthorized. Token is not valid') | ||
|
||
if os.getenv('provider') == 'Cognito': | ||
access_token = incoming_event['headers']['AccessKeyId'] | ||
JWTServices.validate_access_token(access_token) | ||
|
||
effect = 'Allow' | ||
policy = AuthServices.generate_policy(verified_claims, effect, incoming_event['methodArn']) | ||
logger.debug('Generated policy is ', policy) | ||
|
@@ -39,12 +43,13 @@ def lambda_handler(incoming_event, context): | |
# AWS Lambda and any other local environments | ||
if __name__ == '__main__': | ||
# for testing locally you can enter the JWT ID Token here | ||
token = '' | ||
id_token = '' | ||
access_token = '' | ||
account_id = '' | ||
api_gw_id = '' | ||
event = { | ||
'headers': {'Authorization': id_token, 'AccessKeyId': access_token}, | ||
'type': 'TOKEN', | ||
'Authorization': token, | ||
'methodArn': f'arn:aws:execute-api:us-east-1:{account_id}:{api_gw_id}/prod/POST/graphql/api', | ||
} | ||
lambda_handler(event, None) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,9 @@ def validate_jwt_token(jwt_token): | |
except Exception as e: | ||
logger.error(f'Failed to validate token - {str(e)}') | ||
return None | ||
|
||
@staticmethod | ||
def validate_access_token(access_token): | ||
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. Can we put a TTLCache decorator around it with a configurable
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. for TTL Cache - please note custom authorizer already ahs a results_cache_ttl (set to 1 minute for now) at
Would adding an additional caching mechanism at the above be useful? |
||
user_info_url = os.getenv('user_info_url', '') | ||
r = requests.get(user_info_url, headers={'Authorization': access_token}) | ||
r.raise_for_status() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ def __init__( | |
id, | ||
envname: str = 'dev', | ||
resource_prefix='dataall', | ||
tooling_region=None, | ||
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.
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. removed! |
||
tooling_account_id=None, | ||
ecr_repository=None, | ||
image_tag=None, | ||
|
@@ -195,6 +196,8 @@ def __init__( | |
apig_vpce=apig_vpce, | ||
prod_sizing=prod_sizing, | ||
user_pool=cognito_stack.user_pool if custom_auth is None else None, | ||
user_pool_client=cognito_stack.client if custom_auth is None else None, | ||
user_pool_domain=cognito_stack.domain if custom_auth is None else None, | ||
pivot_role_name=self.pivot_role_name, | ||
reauth_ttl=reauth_config.get('ttl', 5) if reauth_config else 5, | ||
email_notification_sender_email_id=email_sender, | ||
|
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.
nit: That's why I don't like this approach, you have to remember to obfuscate stuff