Skip to content

Commit

Permalink
add Sip Rule CR
Browse files Browse the repository at this point in the history
  • Loading branch information
docwho2 committed Nov 1, 2023
1 parent b64824b commit a9c94ed
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public String getArn() {
return getResponseField(SMA_ARN);
}

public String getSMAId() {
return getResponseField(SMA_ID);
}

/**
* Required parameters for the CreateSipMediaApplicationCommand API call
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package cloud.cleo.chimesma.squareup.cdk;

import java.util.List;
import java.util.Map;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.customresources.AwsCustomResource;
import software.amazon.awscdk.customresources.AwsCustomResourcePolicy;
import software.amazon.awscdk.customresources.AwsCustomResourceProps;
import software.amazon.awscdk.customresources.AwsSdkCall;
import software.amazon.awscdk.customresources.PhysicalResourceId;
import software.amazon.awscdk.customresources.PhysicalResourceIdReference;
import software.amazon.awscdk.customresources.SdkCallsPolicyOptions;

/**
*
* @author sjensen
*/
public class ChimeSipRule extends AwsCustomResource {

private final static String ID = "SR-CR";

/**
* The SIP Rule ID in the API response
*/
private final static String SR_ID = "SipRule.SipRuleId";

public ChimeSipRule(Stack scope, ChimeVoiceConnector vc, ChimeSipMediaApp sma) {
super(scope, ID, AwsCustomResourceProps.builder()
.resourceType("Custom::SipRule")
.installLatestAwsSdk(Boolean.FALSE)
.policy(AwsCustomResourcePolicy.fromSdkCalls(SdkCallsPolicyOptions.builder().resources(AwsCustomResourcePolicy.ANY_RESOURCE).build()))
.onCreate(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("CreateSipRuleCommand")
.physicalResourceId(PhysicalResourceId.fromResponse(SR_ID))
.parameters(Map.of("Name", scope.getStackName() + "-" + scope.getRegion(),
"TriggerType", "RequestUriHostname",
"TriggerValue",vc.getOutboundName(),
"Disabled", false,
"TargetApplications", List.of(Map.of("SipMediaApplicationId", sma.getSMAId(),"Priority",1,"AwsRegion",scope.getRegion()))
))
.build())
.onDelete(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("DeleteSipRuleCommand")
.parameters(Map.of("SipRuleId", new PhysicalResourceIdReference()))
.build())
.build());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cloud.cleo.chimesma.squareup.cdk;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import software.amazon.awscdk.Stack;
Expand All @@ -15,6 +16,7 @@
import software.amazon.awscdk.customresources.PhysicalResourceId;
import software.amazon.awscdk.customresources.PhysicalResourceIdReference;
import software.amazon.awscdk.customresources.SdkCallsPolicyOptions;
import software.amazon.awscdk.services.iam.PolicyStatement;

/**
*
Expand All @@ -30,11 +32,15 @@ public class ChimeVoiceConnector extends AwsCustomResource {
private final static String VC_ID = "VoiceConnector.VoiceConnectorId";
private final static String VC_ARN = "VoiceConnector.VoiceConnectorArn";

private final AwsCustomResource logging;
private final AwsCustomResource termination;
private final AwsCustomResource origination;

public ChimeVoiceConnector(Stack scope) {
super(scope, ID, AwsCustomResourceProps.builder()
.resourceType("Custom::VoiceConnector")
.installLatestAwsSdk(Boolean.FALSE)
.policy(AwsCustomResourcePolicy.fromSdkCalls(SdkCallsPolicyOptions.builder().resources(AwsCustomResourcePolicy.ANY_RESOURCE).build()))
.policy(AwsCustomResourcePolicy.fromStatements(List.of(PolicyStatement.Builder.create().actions(List.of("chime:*","logs:*")).resources(List.of("*")).build())))
.onCreate(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("CreateVoiceConnectorCommand")
Expand All @@ -48,6 +54,48 @@ public ChimeVoiceConnector(Stack scope) {
.build())
.build());


// Enable SIP Logs
logging = new AwsCustomResource(scope, ID + "-LOG", AwsCustomResourceProps.builder()
.resourceType("Custom::VoiceConnectorLogging")
.installLatestAwsSdk(Boolean.FALSE)
.policy(AwsCustomResourcePolicy.fromStatements(List.of(PolicyStatement.Builder.create().actions(List.of("chime:*","logs:*")).resources(List.of("*")).build())))
.onCreate(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("PutVoiceConnectorLoggingConfigurationCommand")
.physicalResourceId(PhysicalResourceId.of("logging"))
.parameters(Map.of("VoiceConnectorId", getResponseFieldReference(VC_ID),
"LoggingConfiguration",Map.of("EnableSIPLogs", true,"EnableMediaMetricLogs",false)))
.build())
.build());


termination = new AwsCustomResource(scope, ID + "-TERM", AwsCustomResourceProps.builder()
.resourceType("Custom::VoiceConnectorTerm")
.installLatestAwsSdk(Boolean.FALSE)
.policy(AwsCustomResourcePolicy.fromSdkCalls(SdkCallsPolicyOptions.builder().resources(AwsCustomResourcePolicy.ANY_RESOURCE).build()))
.onCreate(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("PutVoiceConnectorTerminationCommand")
.physicalResourceId(PhysicalResourceId.of("termination"))
.parameters(Map.of("VoiceConnectorId", getResponseFieldReference(VC_ID),
"Termination",Map.of("CallingRegions", List.of("US"),"CidrAllowedList",List.of("54.197.158.204/32","54.172.60.0/30","54.244.51.0/30"),"Disabled",false)))
.build())
.build());

origination = new AwsCustomResource(scope, ID + "-ORIG", AwsCustomResourceProps.builder()
.resourceType("Custom::VoiceConnectorOrig")
.installLatestAwsSdk(Boolean.FALSE)
.policy(AwsCustomResourcePolicy.fromSdkCalls(SdkCallsPolicyOptions.builder().resources(AwsCustomResourcePolicy.ANY_RESOURCE).build()))
.onCreate(AwsSdkCall.builder()
.service("@aws-sdk/client-chime-sdk-voice")
.action("PutVoiceConnectorOriginationCommand")
.physicalResourceId(PhysicalResourceId.of("origination"))
.parameters(Map.of("VoiceConnectorId", getResponseFieldReference(VC_ID),
"Origination",Map.of("Routes", List.of(Map.of("Host","54.197.158.204","Port",5060,"Protocol","UDP","Priority",1,"Weight",1)),"Disabled",false)))
.build())
.build());

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public InfrastructureStack(final Construct parent, final String id, final StackP

ChimeVoiceConnector vc = new ChimeVoiceConnector(this);

ChimeSipRule sr = new ChimeSipRule(this, vc, sma);

new CfnOutput(this, "sma-arn", CfnOutputProps.builder()
.description("The ARN for the Session Media App (SMA)")
.value(sma.getArn())
Expand Down

0 comments on commit a9c94ed

Please sign in to comment.