-
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.
gchqgh-35 Restrict graph access to specific cognito user pool users.
- Loading branch information
Showing
9 changed files
with
185 additions
and
71 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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import boto3 | ||
import os | ||
|
||
|
||
class Graph: | ||
|
||
def __init__(self): | ||
dynamodb = boto3.resource("dynamodb") | ||
graph_table_name = os.getenv("graph_table_name") | ||
self.table = dynamodb.Table(graph_table_name) | ||
|
||
|
||
def get_all_graphs(self, requesting_user): | ||
""" | ||
Gets all graphs from Dynamodb table | ||
""" | ||
graphs = self.table.scan()["Items"] | ||
if requesting_user is None: | ||
return graphs | ||
else: | ||
return list(filter(lambda graph: requesting_user in graph["administrators"], graphs)) | ||
|
||
|
||
def get_graph(self, graph_id): | ||
""" | ||
Gets a specific graph from Dynamodb table | ||
""" | ||
response = self.table.get_item( | ||
Key={ | ||
"graphId": graph_id | ||
} | ||
) | ||
if "Item" in response: | ||
return response["Item"] | ||
raise Exception | ||
|
||
|
||
def update_graph(self, graph_id, status): | ||
self.table.update_item( | ||
Key={ | ||
"graphId": graph_id | ||
}, | ||
UpdateExpression="SET currentState = :state", | ||
ExpressionAttributeValues={ | ||
":state": status | ||
}, | ||
ConditionExpression=boto3.dynamodb.conditions.Attr("graphId").exists() | ||
) | ||
|
||
def create_graph(self, graph_id, status, administrators): | ||
self.table.put_item( | ||
Item={ | ||
"graphId": graph_id, | ||
"currentState": status, | ||
"administrators": administrators | ||
}, | ||
ConditionExpression=boto3.dynamodb.conditions.Attr("graphId").not_exists() | ||
) | ||
|
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,40 @@ | ||
import boto3 | ||
from graph import Graph | ||
import os | ||
|
||
|
||
class User: | ||
|
||
def __init__(self): | ||
self.cognito_client = boto3.client('cognito-idp') | ||
self.user_pool_id = os.getenv("user_pool_id") | ||
self.graph = Graph() | ||
|
||
def valid_cognito_users(self, users): | ||
response = self.cognito_client.list_users(UserPoolId=self.user_pool_id) | ||
cognito_users = map(self.to_user_name, response["Users"]) | ||
return set(users).issubset(set(cognito_users)) | ||
|
||
def to_user_name(self, user): | ||
return user["Username"] | ||
|
||
def contains_duplicates(self, items): | ||
return set([item for item in items if items.count(item) > 1]) | ||
|
||
def get_requesting_cognito_user(self, request): | ||
if ("authorizer" not in request["requestContext"] | ||
or "claims" not in request["requestContext"]["authorizer"] | ||
or "cognito:username" not in request["requestContext"]["authorizer"]["claims"]): | ||
return None | ||
return request["requestContext"]["authorizer"]["claims"]["cognito:username"] | ||
|
||
def is_authorized(self, user, graphId): | ||
# If Authenticated through AWS account treat as admin for all graphs | ||
if (user is None): | ||
return True | ||
# Otherwise check the list of administrators configured on the graph | ||
try: | ||
graph_record = self.graph.get_graph(graphId) | ||
return user in graph_record["administrators"] | ||
except Exception as e: | ||
return False |
Oops, something went wrong.