Skip to content

Commit 6d8dcb8

Browse files
committed
Scope down permissions
1 parent 5529745 commit 6d8dcb8

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

api/PclusterApiHandler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
AUTH_PATH = os.getenv("AUTH_PATH")
3434
API_BASE_URL = os.getenv("API_BASE_URL")
3535
API_VERSION = sorted(os.getenv("API_VERSION", "3.1.0").strip().split(","), key=lambda x: [-int(n) for n in x.split('.')])
36+
# Default version must be highest version so that it can be used for read operations due to backwards compatibility
3637
DEFAULT_API_VERSION = API_VERSION[0]
3738
API_USER_ROLE = os.getenv("API_USER_ROLE")
3839
OIDC_PROVIDER = os.getenv("OIDC_PROVIDER")

infrastructure/environments/demo-cfn-create-args.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Parameters:
33
- ParameterKey: AdminUserEmail
44
ParameterValue: [email protected]
55
- ParameterKey: Version
6-
ParameterValue: 3.13.0,3.11.0
6+
ParameterValue: 3.11.0,3.13.0
77
- ParameterKey: InfrastructureBucket
88
ParameterValue: BUCKET_URL_PLACEHOLDER
99
- ParameterKey: PublicEcrImageUri

infrastructure/environments/demo-cfn-update-args.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Parameters:
33
- ParameterKey: AdminUserEmail
44
UsePreviousValue: true
55
- ParameterKey: Version
6-
ParameterValue: 3.12.0,3.11.0,3.9.0
6+
ParameterValue: 3.11.0,3.12.0,3.9.0
77
- ParameterKey: InfrastructureBucket
88
ParameterValue: BUCKET_URL_PLACEHOLDER
99
- ParameterKey: PublicEcrImageUri

infrastructure/parallelcluster-ui.yaml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Parameters:
3636
Description: Version of AWS ParallelCluster to deploy.
3737
Type: String
3838
AllowedPattern: "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(,([0-9]+)\\.([0-9]+)\\.([0-9]+))*$"
39-
ConstraintDescription: Please specify a valid ParallelCluster version.
39+
ConstraintDescription: Please specify a comma separated list of valid ParallelCluster versions.
4040
ImageBuilderVpcId:
4141
Description: (Optional) Select the VPC to use for building the container images. If not selected, default VPC will be used.
4242
Type: String
@@ -240,7 +240,15 @@ Resources:
240240
import os
241241
import re
242242
import time
243-
243+
244+
def get_partition(region):
245+
if region.startswith('us-gov-'):
246+
return 'aws-us-gov'
247+
elif region.startswith('cn-'):
248+
return 'aws-cn'
249+
else:
250+
return 'aws'
251+
244252
def handler(event, context):
245253
response_data = {}
246254
response_status = cfnresponse.SUCCESS
@@ -255,7 +263,13 @@ Resources:
255263
if event['RequestType'] in ['Create', 'Update']:
256264
response_data["Message"] = "Resource creation successful!"
257265
cfn = boto3.client('cloudformation')
266+
267+
sts_client = boto3.client('sts')
268+
caller_identity = sts_client.get_caller_identity()
269+
account_id = caller_identity['Account']
270+
258271
result = ""
272+
api_gateway_arns = []
259273

260274
api_id = event['ResourceProperties'].get('ApiGatewayRestApiId')
261275
print(f"ApiGatewayRestApiId: {api_id}")
@@ -304,23 +318,28 @@ Resources:
304318
if output['OutputKey'] == 'ParallelClusterApiInvokeUrl':
305319
# Construct the result string
306320
result = f"{result}{version}={output['OutputValue']},"
307-
print(f"Version={version}, ApiURL={output['OutputValue']}")
321+
322+
parsed_url = urlparse(output['OutputValue']).hostname.split('.')[0]
323+
api_gateway_arns.append(f"arn:{get_partition(os.environ['AWS_REGION'])}:execute-api:{os.environ['AWS_REGION']}:{account_id}/{parsed_url}/*/*")
324+
print(f"API arn: {parsed_url}")
325+
326+
print(f"Version={version}, ApiURL={output['OutputValue']}, ")
308327
break
309328

310329
except Exception as e:
311330
print(f"Error processing stack {stack['StackName']}: {str(e)}")
312331
continue
313332
print(f"Result: {result}")
314333

315-
response_data = {"ApiVersionMapping": result}
334+
response_data = {"ApiVersionMapping": result, "ApiArns": ','.join(api_gateway_arns)}
316335
cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data)
317336

318337
except Exception as e:
319338
response_status = cfnresponse.FAILED
320339
reason = "Failed {}: {}".format(event["RequestType"], e)
321340

322341
Timeout: 300
323-
MemorySize: 128
342+
MemorySize: 256
324343

325344
ApiVersionMapFunctionRole:
326345
Type: AWS::IAM::Role
@@ -996,6 +1015,7 @@ Resources:
9961015
- { ApiGateway: !Ref ApiGatewayRestApi }
9971016

9981017
ParallelClusterApiGatewayInvoke:
1018+
DependsOn: ApiVersionMap
9991019
Type: AWS::IAM::ManagedPolicy
10001020
Properties:
10011021
ManagedPolicyName: !Sub
@@ -1007,7 +1027,7 @@ Resources:
10071027
- Action:
10081028
- execute-api:Invoke
10091029
Effect: Allow
1010-
Resource: !Sub "arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:*/*/*"
1030+
Resource: !Split [",", !GetAtt ApiVersionMap.ApiArns]
10111031

10121032
CognitoPolicy:
10131033
Type: AWS::IAM::ManagedPolicy

0 commit comments

Comments
 (0)