Skip to content

Commit 3e3e8d3

Browse files
committed
swagger updates
1 parent 271c705 commit 3e3e8d3

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

templates/1-StartingBlocks-Main-Template.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ Resources:
530530
SNSTopicArn: !Ref SNSTopicArn
531531
SlackWebhookUrl: !Ref SlackWebhookUrl
532532
KmsKeyId: !GetAtt 'SharedResourcesStack.Outputs.KeyId'
533+
DeploySwagger: !Ref 'DeploySwagger'
533534

534535
DatabaseStack:
535536
Type: AWS::CloudFormation::Stack
@@ -725,12 +726,16 @@ Resources:
725726
HostedZoneId: !Ref 'HostedZoneId'
726727
S3SourceBucket: !Ref 'S3SourceBucket'
727728
DomainName: !Ref 'DomainName'
728-
SwaggerDefaultURL: !Sub 'https://swaggerui.${DomainName}/metadata/data/v3/resources/swagger.json'
729+
SwaggerDefaultURL: !If
730+
- UseDefaultWebApiZip
731+
- !Sub 'https://api.ed-fi.org/v${EdFiApiVersion}/api/metadata/data/v3/resources/swagger.json'
732+
- ''
729733
CRHelperLambdaLayer: !GetAtt 'LambdaCoreStack.Outputs.CRHelperLambdaLayer'
730734
Partner: !Ref 'Partner'
731735
LambdaDefaultSGID: !GetAtt 'SharedResourcesStack.Outputs.LambdaDefaultSGID'
732736
PrivateSubnet1Id: !Ref 'PrivateSubnet1Id'
733737
PrivateSubnet2Id: !Ref 'PrivateSubnet2Id'
738+
KmsKeyId: !GetAtt 'SharedResourcesStack.Outputs.KeyId'
734739

735740
StateMachineStack:
736741
Type: AWS::CloudFormation::Stack

templates/x-lambda-core-functions.yml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Parameters:
9292
KmsKeyId:
9393
Description: The KMS Key ID used to encrypt resources in this deployment.
9494
Type: String
95+
DeploySwagger:
96+
Type: String
97+
Description: If enabled, Swagger UI will be deployed to swagger-ui.{DomainName}
9598

9699
Conditions:
97100
UseAdminApi: !Equals [!Ref 'AdminInterface', "Ed-Fi Admin API"]
@@ -728,6 +731,11 @@ Resources:
728731
- kms:Decrypt
729732
Resource:
730733
- !Sub 'arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/${KmsKeyId}'
734+
- Effect: Allow
735+
Action:
736+
- lambda:InvokeFunction
737+
Resource:
738+
- !Sub 'arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:${EnvLabel}-SwaggerUpdate'
731739

732740
TenantManagementFunction:
733741
Type: AWS::Lambda::Function
@@ -753,6 +761,7 @@ Resources:
753761
Variables:
754762
ENVLABEL: !Ref EnvLabel
755763
TENANCY_MODE: !Ref EdFiTenancyMode
764+
DEPLOY_SWAGGER: !Ref 'DeploySwagger'
756765
Code:
757766
ZipFile: |
758767
import json
@@ -1055,7 +1064,13 @@ Resources:
10551064
)
10561065
10571066
return response['Command']['Status']
1058-
1067+
1068+
def update_swagger(env_label):
1069+
lambda_client = boto3.client('lambda')
1070+
lambda_response = lambda_client.invoke(
1071+
FunctionName=f'{env_label}-SwaggerUpdate',
1072+
InvocationType='Event'
1073+
)
10591074
10601075
def lambda_handler(event, context):
10611076
conn = None
@@ -1069,6 +1084,7 @@ Resources:
10691084
10701085
env_label = os.environ['ENVLABEL']
10711086
secret_name = env_label + '-AuroraMasterSecret'
1087+
deploy_swagger = os.environ['DEPLOY_SWAGGER']
10721088
10731089
# Concatenate "-tenants" to env_label for the DynamoDB table name
10741090
table_name = env_label + '-tenants'
@@ -1110,6 +1126,10 @@ Resources:
11101126
# Add item to DynamoDB
11111127
response = add_item(table, tenant_name, allowed_ed_orgs)
11121128
1129+
# Update Swagger if needed
1130+
if deploy_swagger == 'True':
1131+
update_swagger(env_label)
1132+
11131133
print("Item added successfully, and template databases cloned")
11141134
return "Item added successfully, and template databases cloned"
11151135
elif action == 'Remove':
@@ -1131,6 +1151,10 @@ Resources:
11311151
11321152
# Delete tenant-specific databases
11331153
delete_tenant_databases(conn, tenant_name)
1154+
1155+
# Update Swagger if needed
1156+
if deploy_swagger == 'True':
1157+
update_swagger(env_label)
11341158
11351159
print("Item removed successfully, and tenant-specific databases deleted")
11361160
return "Item removed successfully, and tenant-specific databases deleted"

templates/x-swagger.yml

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Parameters:
3737
PrivateSubnet2Id:
3838
Type: AWS::EC2::Subnet::Id
3939
Description: ID of the private subnet 2 in Availability Zone 2 (e.g., subnet-a0246dcd)
40+
KmsKeyId:
41+
Description: The KMS Key ID used to encrypt dynamodb.
42+
Type: String
4043
Resources:
4144
S3SwaggerFEBucket:
4245
Type: AWS::S3::Bucket
@@ -388,6 +391,16 @@ Resources:
388391
Resource:
389392
- !Sub 'arn:${AWS::Partition}:s3:::${S3SwaggerFEBucket}'
390393
- !Sub 'arn:${AWS::Partition}:s3:::${S3SwaggerFEBucket}/*'
394+
- Effect: Allow
395+
Action:
396+
- dynamodb:Scan
397+
Resource:
398+
- !Sub 'arn:${AWS::Partition}:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${EnvLabel}-tenants*'
399+
- Effect: Allow
400+
Action:
401+
- kms:Decrypt
402+
Resource:
403+
- !Sub 'arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/${KmsKeyId}*'
391404
Roles:
392405
- !Ref 'SwaggerLambdaRole'
393406
SwaggerLambdaAWSCLILayer:
@@ -417,13 +430,16 @@ Resources:
417430
- !Ref SwaggerLambdaAWSCLILayer
418431
- !Ref 'CRHelperLambdaLayer'
419432
ReservedConcurrentExecutions: 1
433+
FunctionName: !Sub '${EnvLabel}-SwaggerUpdate'
420434
Description: Updates and deploys Swagger UI in S3
421435
Handler: index.lambda_handler
422436
Environment:
423437
Variables:
424438
S3_BUCKET: !Ref S3SwaggerFEBucket
425439
CF_DISTRIBUTION: !Ref CloudFront
426440
DEFAULT_URL: !Ref SwaggerDefaultURL
441+
DOMAIN_NAME: !Ref DomainName
442+
ENV_LABEL: !Ref EnvLabel
427443
Runtime: python3.11
428444
MemorySize: 128
429445
Role: !GetAtt 'SwaggerLambdaRole.Arn'
@@ -444,11 +460,15 @@ Resources:
444460
import boto3
445461
from zipfile import ZipFile
446462
from crhelper import CfnResource
447-
463+
from datetime import datetime
464+
448465
helper = CfnResource()
449466
450467
def lambda_handler(event, context):
451-
helper(event, context)
468+
if "StackId" in event:
469+
helper(event, context)
470+
else:
471+
create(event, context)
452472
453473
@helper.create
454474
@helper.update
@@ -457,7 +477,23 @@ Resources:
457477
the_bucket = os.environ['S3_BUCKET']
458478
cf_dist = os.environ['CF_DISTRIBUTION']
459479
swagger_url = os.environ['DEFAULT_URL']
480+
domain_name = os.environ['DOMAIN_NAME']
481+
env_label = os.environ['ENV_LABEL']
460482
http = urllib3.PoolManager()
483+
484+
# Check dynamodb for tenants and replace swagger_url if one exists
485+
ddb_table_name = env_label + '-tenants'
486+
ddb = boto3.resource('dynamodb')
487+
ddb_table = ddb.Table(ddb_table_name)
488+
ddb_response = ddb_table.scan()
489+
ddb_data = ddb_response['Items']
490+
# Handle pagination for large tables.
491+
while ddb_response.get('LastEvaluatedKey'):
492+
ddb_response = ddb_table.scan(ExclusiveStartKey=ddb_response['LastEvaluatedKey'])
493+
ddb_data.extend(ddb_response['Items'])
494+
if len(ddb_data) > 0:
495+
tenant = ddb_data[0]['Name']
496+
swagger_url = f'https://swaggerui.{domain_name}/{tenant}/metadata/data/v3/resources/swagger.json'
461497
print(swagger_url)
462498
463499
url = 'https://github.com/swagger-api/swagger-ui/releases/latest'
@@ -513,7 +549,7 @@ Resources:
513549
'/*',
514550
]
515551
},
516-
'CallerReference': v
552+
'CallerReference': str(datetime.now())
517553
}
518554
)
519555

0 commit comments

Comments
 (0)