Skip to content
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

switch aws_s3_client to resource. lint lambda policy. #64

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions terraform/json/iam_policy_lambda.json.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"rekognition:DescribeCollection",
"rekognition:IndexFaces",
"dynamodb:PutItem",
"s3:GetObject",
"s3:ListBucket"
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
Expand All @@ -32,6 +27,8 @@
"Action": [
"apigateway:GET",
"iam:ListPolicies",
"iam:GetPolicy",
"iam:GetPolicyVersion",
"s3:ListAllMyBuckets",
"rekognition:IndexFaces",
"rekognition:DescribeCollection",
Expand Down
3 changes: 1 addition & 2 deletions terraform/python/rekognition_api/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# -*- coding: utf-8 -*-
# Managed via automated CI/CD in .github/workflows/semanticVersionBump.yml.
__version__ = "0.2.7-next.1"
__version__ = "0.2.8-next.1"
27 changes: 16 additions & 11 deletions terraform/python/rekognition_api/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,24 @@ def get_iam_policies(self):
"""Return a dict of the AWS IAM policies."""
iam_client = settings.aws_session.client("iam")
policies = iam_client.list_policies()["Policies"]
rekognition_policies = {
policy["PolicyName"]: policy["Arn"]
for policy in policies
if settings.shared_resource_identifier in policy["PolicyName"]
}
return rekognition_policies or {}
rekognition_policies = {}
for policy in policies:
if settings.shared_resource_identifier in policy["PolicyName"]:
policy_version = iam_client.get_policy(PolicyArn=policy["Arn"])["Policy"]["DefaultVersionId"]
policy_document = iam_client.get_policy_version(PolicyArn=policy["Arn"], VersionId=policy_version)[
"PolicyVersion"
]["Document"]
rekognition_policies[policy["PolicyName"]] = {"Arn": policy["Arn"], "Policy": policy_document}
return rekognition_policies

def get_iam_roles(self):
"""Return a dict of the AWS IAM roles."""
iam_client = settings.aws_session.client("iam")
roles = iam_client.list_roles()["Roles"]
rekognition_roles = {
role["RoleName"]: role["Arn"] for role in roles if settings.shared_resource_identifier in role["RoleName"]
role["RoleName"]: {"Arn": role["Arn"], "Role": role}
for role in roles
if settings.shared_resource_identifier in role["RoleName"]
}
return rekognition_roles or {}

Expand Down Expand Up @@ -109,7 +114,7 @@ def aws_connection_works(self):
"""Test that the AWS connection works."""
try:
# Try a benign operation
settings.aws_s3_client.list_buckets()
settings.aws_s3_client.buckets.all()
return True
except Exception: # pylint: disable=broad-exception-caught
return False
Expand All @@ -124,9 +129,9 @@ def domain_exists(self) -> bool:

def get_bucket_by_prefix(self, bucket_prefix) -> str:
"""Return the bucket name given the bucket prefix."""
for bucket in settings.aws_s3_client.list_buckets()["Buckets"]:
if bucket["Name"].startswith(bucket_prefix):
return f"arn:aws:s3:::{bucket['Name']}"
for bucket in settings.aws_s3_client.buckets.all():
if bucket.name.startswith(bucket_prefix):
return f"arn:aws:s3:::{bucket.name}"
return None

def bucket_exists(self, bucket_prefix) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion terraform/python/rekognition_api/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def aws_apigateway_client(self):
def aws_s3_client(self):
"""S3 client"""
if not self._aws_s3_client:
self._aws_s3_client = self.aws_session.client("s3")
self._aws_s3_client = self.aws_session.resource("s3")
return self._aws_s3_client

@property
Expand Down
2 changes: 1 addition & 1 deletion terraform/python/rekognition_api/lambda_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def unpack_s3_object(event, record):
"""extracts the s3 object key, object, and object metadata from the event record"""
s3_bucket_name = get_bucket_name(event)
s3_object_key = unquote_plus(record["s3"]["object"]["key"], encoding="utf-8")
s3_object = settings.s3_client.Object(s3_bucket_name, s3_object_key)
s3_object = settings.aws_s3_client.Object(s3_bucket_name, s3_object_key)
s3_object_metadata = {key.replace("x-amz-meta-", ""): s3_object.metadata[key] for key in s3_object.metadata.keys()}
return s3_object_key, s3_object_metadata

Expand Down