Skip to content

Commit

Permalink
Merge pull request #4 from bmaidics/envvar_changes
Browse files Browse the repository at this point in the history
Fix publicTlsCertificateViaAcm, add zilla plus ami envvar
  • Loading branch information
jfallows authored Sep 30, 2024
2 parents 074313c + 258722d commit 97f5091
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
11 changes: 5 additions & 6 deletions amazon-msk/cdktf/secure-public-access/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ cp terraform.tfvars.example terraform.tfvars
To get a list all MSK clusters run:

```bash
aws kafka list-clusters --query 'ClusterInfoList[*].[ClusterName,ClusterArn]' --output table
aws kafka list-clusters --query 'ClusterInfoList[*].{Name:ClusterName, Arn:ClusterArn, Iam:ClientAuthentication.Iam.Enabled, Scram:ClientAuthentication.Sasl.Scram.Enabled, Tls:ClientAuthentication.Tls.Enabled, mTls:ClientAuthentication.Tls.CertificateAuthorityArnList[*] | join(`,`, @) || None, Unauthenticated:ClientAuthentication.Unauthenticated.Enabled}' --output table
```

Use the `ClusterName` of your desired MSK cluster for this variable.
Set the desired client authentication method based on the MSK cluster setup, using `MSK_ACCESS_METHOD` environment variable.

### `public_tls_certificate_key`: Public TLS Certificate Key

Expand Down Expand Up @@ -118,13 +119,11 @@ cp .env.example .env

### MSK Client Authentication Method

By default Zilla Plus will choose the most secure way configured for your MSK cluster. Order from most to least secure:
To specify which client authentication method Zilla should use set the `MSK_ACCESS_METHOD` environment variable to the desired access method (mTLS, SASL/SCRAM or Unauthorized).

1. mTLS
1. SASL/SCRAM
1. Unauthorized
### Public TLS Certificate Via ACM

If you want to specify which client authentication method Zilla should use set the `MSK_ACCESS_METHOD` environment variable to the desired access method (mTLS, SASL/SCRAM or Unauthorized).
By default Zilla Plus will assume TLS certificate coming from Secrets Manager. You can use Zilla Plus with TLS certificate via ACM. To enable this set `PUBLIC_TLS_CERTIFICATE_VIA_ACM` to `true`.

### Custom Zilla Plus Role

Expand Down
61 changes: 25 additions & 36 deletions amazon-msk/cdktf/secure-public-access/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { UserVariables } from "./variables";
import { AwsProvider } from "@cdktf/provider-aws/lib/provider";
import { ec2EnclaveCertificateIamRoleAssociation } from "./.gen/providers/awscc"
import { AwsccProvider } from "./.gen/providers/awscc/provider";
import { DataAwsccMskCluster } from "./.gen/providers/awscc/data-awscc-msk-cluster";


export class ZillaPlusSecurePublicAccessStack extends TerraformStack {
Expand Down Expand Up @@ -132,22 +131,7 @@ export class ZillaPlusSecurePublicAccessStack extends TerraformStack {
});
}

const awsccMskCluster = new DataAwsccMskCluster(this, "awsccMskCluster", {
id: mskCluster.id
})

const mtlsEnabled = Fn.lengthOf(awsccMskCluster.clientAuthentication.tls.certificateAuthorityArnList) > 0;

let mskClientAuthentication = userVariables.mskClientAuthentication;
if (userVariables.mskClientAuthentication === "Unknown") {
mskClientAuthentication = mtlsEnabled
? "mTLS"
: mskCluster.bootstrapBrokersSaslScram
? "SASL/SCRAM"
: mskCluster.bootstrapBrokers
? "Unauthorized"
: userVariables.mskClientAuthentication;
}

const bootstrapServers =
mskClientAuthentication === "mTLS"
Expand Down Expand Up @@ -204,7 +188,7 @@ export class ZillaPlusSecurePublicAccessStack extends TerraformStack {
description: "TLS Certificate SecretsManager or CertificateManager ARN",
});

const publicTlsCertificateViaAcm = Fn.startswith(publicTlsCertificateKey.stringValue, "arn:aws:acm:");
const publicTlsCertificateViaAcm = userVariables.publicTlsCertificateViaAcm;

let zillaPlusRole;
if (!userVariables.createZillaPlusRole) {
Expand Down Expand Up @@ -309,6 +293,11 @@ export class ZillaPlusSecurePublicAccessStack extends TerraformStack {
"Resource": [ `arn:aws:iam::*:role/${iamRole.name}` ]
}]
);

new ec2EnclaveCertificateIamRoleAssociation.Ec2EnclaveCertificateIamRoleAssociation(this, "ZillaPlusEnclaveIamRoleAssociation", {
roleArn: iamRole.arn,
certificateArn: publicTlsCertificateKey.stringValue
});
}

new IamRolePolicy(this, "ZillaPlusRolePolicy", {
Expand All @@ -317,11 +306,6 @@ export class ZillaPlusSecurePublicAccessStack extends TerraformStack {
});

zillaPlusRole = iamInstanceProfile.name;

new ec2EnclaveCertificateIamRoleAssociation.Ec2EnclaveCertificateIamRoleAssociation(this, "ZillaPlusEnclaveIamRoleAssociation", {
roleArn: iamRole.arn,
certificateArn: publicTlsCertificateKey.stringValue
});
}

let zillaPlusSecurityGroups;
Expand Down Expand Up @@ -488,19 +472,24 @@ ${metricsSection}`;
errorMessage: "must be a valid EC2 instance type.",
});

const ami = new dataAwsAmi.DataAwsAmi(this, "LatestAmi", {
mostRecent: true,
filter: [
{
name: "product-code",
values: ["ca5mgk85pjtbyuhtfluzisgzy"],
},
{
name: "is-public",
values: ["true"],
},
],
});
let imageId = userVariables.zillaPlusAmi;
if (!imageId)
{
const ami = new dataAwsAmi.DataAwsAmi(this, "LatestAmi", {
mostRecent: true,
filter: [
{
name: "product-code",
values: ["ca5mgk85pjtbyuhtfluzisgzy"],
},
{
name: "is-public",
values: ["true"],
},
],
});
imageId = ami.imageId;
}

const nlb = new Lb(this, `NetworkLoadBalancer-${id}`, {
name: "network-load-balancer",
Expand Down Expand Up @@ -643,7 +632,7 @@ systemctl start zilla-plus
`;

const ZillaPlusLaunchTemplate = new launchTemplate.LaunchTemplate(this, "ZillaPlusLaunchTemplate", {
imageId: ami.imageId,
imageId: imageId,
instanceType: instanceType.stringValue,
networkInterfaces: [
{
Expand Down
4 changes: 4 additions & 0 deletions amazon-msk/cdktf/secure-public-access/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export class UserVariables extends Construct {
mskClientAuthentication: "mTLS" | "SASL/SCRAM" | "Unauthorized" | "Unknown";
publicCertificateAuthority: boolean = false;
createZillaPlusRole: boolean = false;
publicTlsCertificateViaAcm: boolean = false;
createZillaPlusSecurityGroup: boolean = false;
sshKeyEnabled: boolean = false;
cloudwatchDisabled: boolean = false;
zillaPlusAmi: string = "";

constructor(scope: Construct, name: string) {
super(scope, name);
Expand All @@ -28,8 +30,10 @@ export class UserVariables extends Construct {
}
this.publicCertificateAuthority = process.env.PUBLIC_CERTIFICATE_AUTHORITY === "true";
this.createZillaPlusRole = process.env.CREATE_ZILLA_PLUS_ROLE !== "false";
this.publicTlsCertificateViaAcm = process.env.PUBLIC_TLS_CERTIFICATE_VIA_ACM === "true";
this.createZillaPlusSecurityGroup = process.env.CREATE_ZILLA_PLUS_SECURITY_GROUP !== "false";
this.sshKeyEnabled = process.env.SSH_KEY_ENABLED === "true";
this.cloudwatchDisabled = process.env.CLOUDWATCH_DISABLED === "true";
this.zillaPlusAmi = process.env.ZILLA_PLUS_AMI ? process.env.ZILLA_PLUS_AMI : "";
}
}

0 comments on commit 97f5091

Please sign in to comment.