Skip to content

Commit 2c0998b

Browse files
authored
feat: custom accept json (#29)
handle custom accept json revised documentation README.md
1 parent 81b5b5e commit 2c0998b

File tree

7 files changed

+111
-51
lines changed

7 files changed

+111
-51
lines changed

README.md

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,13 @@ This Environment variable value is specified as a map(string). Example:
4444

4545
### Command Line Interface method
4646

47-
The general steps are:
48-
49-
1. Configure S3 backend for terraform state
50-
2. Setup Terraform input tfvars
51-
3. Invoke the commands defined below to create the Fargate Service that launches corresponding SnykBroker dockerized container.
52-
```
53-
$ terraform init -backend-config="env/dev/config.s3.tfbackend"
54-
$ terraform plan -input=false -var-file="env/dev/terraform.tfvars" -out=tfplan
55-
$ terraform apply "tfplan"
56-
```
57-
5847
### Deployment Modes
5948

60-
| Mode | Description | Variable Settings |
61-
|------------|---------------------------------------------------|-----------------------------------------------------|
62-
| HTTP | No SSL certificate | broker_protocol="http", use_private_ssl_cert=false |
63-
| HTTPS/HTTP | Public SSL certificate, internal HTTP | broker_protocol="https", use_private_ssl_cert=false |
64-
| HTTPS | Public SSL certificate, internal private SSL cert | broker_protocol="https", use_private_ssl_cert=true |
49+
| Mode | Description | Variable Settings |
50+
|------------|---------------------------------------------------|------------------------------------------------|
51+
| HTTP | No SSL certificate | broker_protocol="http", private_ssl_cert=false |
52+
| HTTPS/HTTP | Public SSL certificate, internal HTTP | broker_protocol="https", private_ssl_cert=false |
53+
| HTTPS | Public SSL certificate, internal private SSL cert | broker_protocol="https", private_ssl_cert=true |
6554

6655
#### Public SSL certificate
6756

@@ -70,13 +59,31 @@ Public SSL certificate for `<broker_hostname>.<public_domain_name>` is created a
7059
#### Private SSL certificate/Key
7160

7261
* Upload private SSL certificate (.pem) and its private key (.key) to a S3 bucket
73-
* Ensure S3 bucket and these 2 objects are accessible to Terraform assumed credentials
74-
* Set variable `cert_bucket_name="<S3_bucket_name>"`
62+
* Verify S3 bucket and these objects are accessible to Terraform assumed credentials
63+
* Set variable `config_bucket_name="<S3_bucket_name>"`
7564
* Set variable `broker_private_key_object="<S3_folder>/<key_name.key>"`
7665
* Set variable `broker_ssl_cert_object="<S3_folder>/<cert_name.pem>"`
7766

7867
Private SSL certificate validity and renewal are handled independently by Customer.
7968

69+
#### Custom approved listing filter
70+
71+
* Upload custom [integration type](https://github.com/snyk/broker/tree/master/client-templates) accept.json to S3 bucket
72+
* Verify S3 bucket and accept.json are accessible to Terraform assumed credentials
73+
* Set variable `config_bucket_name="<S3_bucket_name>"`
74+
* Set variable `custom_listing_filter="<S3_folder>/accept.json"`
75+
76+
### Deployment steps
77+
78+
1. Configure S3 backend for terraform state
79+
2. Setup Terraform input tfvars
80+
3. Invoke the commands defined below to create the Fargate Service that launches corresponding SnykBroker dockerized container.
81+
```
82+
$ terraform init -backend-config="env/dev/config.s3.tfbackend"
83+
$ terraform plan -input=false -var-file="env/dev/terraform.tfvars" -out=tfplan
84+
$ terraform apply "tfplan"
85+
```
86+
8087
<!-- BEGIN_TF_DOCS -->
8188
## Requirements
8289

@@ -138,26 +145,33 @@ Private SSL certificate validity and renewal are handled independently by Custom
138145
| Name | Description | Type | Default | Required |
139146
|------|-------------|------|---------|:--------:|
140147
| <a name="input_additional_env_vars"></a> [additional\_env\_vars](#input\_additional\_env\_vars) | Additional environment variables | `map(string)` | `{}` | no |
148+
| <a name="input_broker_accept_json_object"></a> [broker\_accept\_json\_object](#input\_broker\_accept\_json\_object) | S3 object of SnykBroker listing filter accept.json. Example <s3folder>/accept.json | `string` | `null` | no |
141149
| <a name="input_broker_env_vars"></a> [broker\_env\_vars](#input\_broker\_env\_vars) | SnykBroker environment variables key-value pairs. PORT, BROKER\_CLIENT\_URL not required | `map(string)` | `{}` | yes |
142150
| <a name="input_broker_hostname"></a> [broker\_hostname](#input\_broker\_hostname) | SnykBroker hostname. <broker\_hostname>.<public\_domain\_name> forms its FQDN for SCM webhooks calls | `string` | `"snykbroker"` | no |
143151
| <a name="input_broker_port"></a> [broker\_port](#input\_broker\_port) | Default snykbroker client port. Set a non-system port i.e. >= 1024 as container run-as non-root user | `number` | `7341` | no |
144152
| <a name="input_broker_private_key_object"></a> [broker\_private\_key\_object](#input\_broker\_private\_key\_object) | S3 object of SnykBroker certificate private key. Example <s3folder>/<name>.key | `string` | `null` | no |
145153
| <a name="input_broker_protocol"></a> [broker\_protocol](#input\_broker\_protocol) | Protocol for running connections to SnykBroker. Either http or https | `string` | `"https"` | no |
146154
| <a name="input_broker_ssl_cert_object"></a> [broker\_ssl\_cert\_object](#input\_broker\_ssl\_cert\_object) | S3 object of SnykBroker certificate. Example <s3folder>/<name>.pem | `string` | `null` | no |
147-
| <a name="input_cert_bucket_name"></a> [cert\_bucket\_name](#input\_cert\_bucket\_name) | S3 bucket name storing SnykBroker private key, SSL certificate | `string` | `null` | no |
148155
| <a name="input_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#input\_cloudwatch\_log\_group\_name) | SnykBroker CloudWatch log group name | `string` | `"/aws/ecs/snykbroker"` | no |
149156
| <a name="input_cloudwatch_log_retention_days"></a> [cloudwatch\_log\_retention\_days](#input\_cloudwatch\_log\_retention\_days) | SnykBroker CloudWatch log retention in days | `number` | `7` | no |
157+
| <a name="input_config_bucket_name"></a> [config\_bucket\_name](#input\_config\_bucket\_name) | Configuration S3 bucket name storing SnykBroker private key, SSL certificate, accept.json filter, etc | `string` | `null` | no |
150158
| <a name="input_container_name"></a> [container\_name](#input\_container\_name) | Snyk broker container name behind the Service | `string` | `"snykbroker"` | no |
151159
| <a name="input_cpu"></a> [cpu](#input\_cpu) | Broker service task CPU. min 256 i.e. 0.25 vCPU, max 4096 i.e. 4 vCPU | `number` | `256` | no |
160+
| <a name="input_custom_listing_filter"></a> [custom\_listing\_filter](#input\_custom\_listing\_filter) | Use custom approved listing filter i.e. a revised accept.json | `bool` | `false` | no |
152161
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | Default Tags at aws provider scope | `map(string)` | <pre>{<br> "Snyk": "SnykBroker"<br>}</pre> | no |
153162
| <a name="input_dockerhub_access_token"></a> [dockerhub\_access\_token](#input\_dockerhub\_access\_token) | DockerHub personal access token | `string` | `null` | no |
154163
| <a name="input_dockerhub_username"></a> [dockerhub\_username](#input\_dockerhub\_username) | DockerHub username | `string` | `null` | no |
164+
| <a name="input_fargate_capacity_base"></a> [fargate\_capacity\_base](#input\_fargate\_capacity\_base) | Fargate capacity provider base as minimum number of Tasks. Only this or fargate\_spot\_capacity\_base can be >0 | `number` | `0` | no |
165+
| <a name="input_fargate_capacity_weight"></a> [fargate\_capacity\_weight](#input\_fargate\_capacity\_weight) | Fargate capacity provider weight as a relative percentage of total service\_desired\_count Tasks | `number` | `50` | no |
166+
| <a name="input_fargate_spot_capacity_base"></a> [fargate\_spot\_capacity\_base](#input\_fargate\_spot\_capacity\_base) | Fargate Spot capacity provider base as minimum number of Tasks. Only this or fargate\_capacity\_base can be >0 | `number` | `0` | no |
167+
| <a name="input_fargate_spot_capacity_weight"></a> [fargate\_spot\_capacity\_weight](#input\_fargate\_spot\_capacity\_weight) | Fargate Spot capacity provider weight as a relative percentage of total service\_desired\_count Tasks | `number` | `50` | no |
155168
| <a name="input_image"></a> [image](#input\_image) | Broker image to pull from DockerHub. May be custom derived broker image | `string` | `null` | no |
156169
| <a name="input_integration_type"></a> [integration\_type](#input\_integration\_type) | Snyk Integration type. Choice of artifactory, azurerepos, bitbucket, gh, ghe, gitlab, jira or nexus | `string` | `""` | yes |
157170
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | Lambda function runtime. Defined by AWS supported versions. | `string` | `"python3.9"` | no |
158171
| <a name="input_launch_type"></a> [launch\_type](#input\_launch\_type) | SnykBroker service launch type | `string` | `"FARGATE"` | no |
159172
| <a name="input_log_bucket_name"></a> [log\_bucket\_name](#input\_log\_bucket\_name) | snykbbroker requests access log bucket name for logging webhooks requests | `string` | `null` | no |
160173
| <a name="input_memory"></a> [memory](#input\_memory) | Broker service memory in MiB. Min 512, max 30720 | `number` | `512` | no |
174+
| <a name="input_private_ssl_cert"></a> [private\_ssl\_cert](#input\_private\_ssl\_cert) | Use private SSL certificate at SnykBroker client | `bool` | `false` | no |
161175
| <a name="input_public_domain_name"></a> [public\_domain\_name](#input\_public\_domain\_name) | Customer public domain e.g. example.com | `string` | `null` | yes |
162176
| <a name="input_scheduling_strategy"></a> [scheduling\_strategy](#input\_scheduling\_strategy) | Snyk broker scheduling strategy | `string` | `"REPLICA"` | no |
163177
| <a name="input_service_azs"></a> [service\_azs](#input\_service\_azs) | count of service availability zones to use | `number` | `2` | no |
@@ -168,15 +182,14 @@ Private SSL certificate validity and renewal are handled independently by Custom
168182
| <a name="input_snykbroker_repo"></a> [snykbroker\_repo](#input\_snykbroker\_repo) | DockerHub snyk broker repo | `string` | `"snyk/broker"` | no |
169183
| <a name="input_tags"></a> [tags](#input\_tags) | Tags | `map(string)` | `{}` | no |
170184
| <a name="input_use_existing_route53_zone"></a> [use\_existing\_route53\_zone](#input\_use\_existing\_route53\_zone) | Use existing public hosted zone of <public\_domain\_name> or create new zone | `bool` | `true` | no |
171-
| <a name="input_use_private_ssl_cert"></a> [use\_private\_ssl\_cert](#input\_use\_private\_ssl\_cert) | Use private SSL certificate at SnykBroker client | `bool` | `true` | no |
172185
| <a name="input_vpc_cidr"></a> [vpc\_cidr](#input\_vpc\_cidr) | SnykBroker VPC cidr. Linked to service\_azs to be created | `string` | `"192.168.0.0/20"` | no |
173186

174187
## Outputs
175188

176189
| Name | Description |
177190
|------|-------------|
178-
| <a name="output_aws_broker_dns_name"></a> [aws\_broker\_dns\_name](#output\_aws\_broker\_dns\_name) | AWS generated SnykBroker Client DNS name |
191+
| <a name="output_snykbroker_aws_dns_name"></a> [snykbroker\_aws\_dns\_name](#output\_snykbroker\_aws\_dns\_name) | SnykBroker Client AWS DNS name |
179192
| <a name="output_snykbroker_client_healthcheck_url"></a> [snykbroker\_client\_healthcheck\_url](#output\_snykbroker\_client\_healthcheck\_url) | SnykBroker Client healthcheck URL |
180193
| <a name="output_snykbroker_client_systemcheck_url"></a> [snykbroker\_client\_systemcheck\_url](#output\_snykbroker\_client\_systemcheck\_url) | SnykBroker Client systemcheck URL |
181-
| <a name="output_snykbroker_lb_dns_name"></a> [snykbroker\_lb\_dns\_name](#output\_snykbroker\_lb\_dns\_name) | SnykBroker Client load balancer DNS name |
194+
| <a name="output_snykbroker_lb_dns_name"></a> [snykbroker\_lb\_dns\_name](#output\_snykbroker\_lb\_dns\_name) | SnykBroker Client hosted domain DNS name |
182195
<!-- END_TF_DOCS -->

env/dev/terraform.tfvars

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
integration_type="gh"
22
additional_env_vars={"LOG_LEVEL":"info"}
33
public_domain_name="pixelsvc.com"
4-
cert_bucket_name="tfstate-gwnlng"
4+
config_bucket_name="tfstate-gwnlng"
55
broker_private_key_object="myprivate.key"
66
broker_ssl_cert_object="mycert.pem"
7+
broker_accept_json_object="accept.json"

lambda.tf

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# this will set https to be served by the container even though its cert will not be validated at the target
33
# https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html#target-group-routing-configuration
44
module "snykbroker_cert_handler_lambda" {
5-
count = var.use_private_ssl_cert ? 1 : 0
5+
count = local.attach_config ? 1 : 0
66
source = "terraform-aws-modules/lambda/aws"
77
version = "4.0.1"
88

9-
create = var.use_private_ssl_cert
9+
create = local.attach_config
1010
function_name = "snykbroker_cert_copy"
1111
description = "SnykBroker private certificate handler function"
1212
handler = "s3obj_efs_copy.lambda_handler"
@@ -43,11 +43,11 @@ module "snykbroker_cert_handler_lambda" {
4343
}
4444

4545
module "snykbroker_lambda_security_group" {
46-
count = var.use_private_ssl_cert ? 1 : 0
46+
count = local.attach_config ? 1 : 0
4747
source = "terraform-aws-modules/security-group/aws"
4848
version = "4.13.0"
4949

50-
create = var.use_private_ssl_cert
50+
create = local.attach_config
5151
name = "snykbroker_lambda_security"
5252
description = "Security group for cert handler lambda"
5353
vpc_id = module.snykbroker_vpc.vpc_id
@@ -66,7 +66,7 @@ module "snykbroker_lambda_security_group" {
6666
}
6767

6868
resource "aws_efs_access_point" "snykbroker_cert_access_point" {
69-
count = var.use_private_ssl_cert ? 1 : 0
69+
count = local.attach_config ? 1 : 0
7070
file_system_id = module.snykbroker_efs[0].efs_id
7171

7272
posix_user {
@@ -89,7 +89,7 @@ resource "aws_efs_access_point" "snykbroker_cert_access_point" {
8989
# local provisioner waits 90 seconds for efs DNS records to propagate in aws region
9090
# see https://docs.aws.amazon.com/efs/latest/ug/mounting-fs-mount-cmd-dns-name.html
9191
resource "null_resource" "wait_lambda_efs" {
92-
count = var.use_private_ssl_cert ? 1 : 0
92+
count = local.attach_config ? 1 : 0
9393
provisioner "local-exec" {
9494
command = "sleep 90"
9595
}
@@ -98,11 +98,13 @@ resource "null_resource" "wait_lambda_efs" {
9898

9999
# invoke injection lambda function with event map pointing to the key and cert
100100
resource "aws_lambda_invocation" "snykbroker_lambda_invocation" {
101-
count = var.use_private_ssl_cert ? 1 : 0
101+
count = local.attach_config ? 1 : 0
102102
function_name = module.snykbroker_cert_handler_lambda[0].lambda_function_name
103103
input = jsonencode({
104-
"bucket_name" = var.cert_bucket_name
105-
"s3_objects" = [var.broker_private_key_object, var.broker_ssl_cert_object]
104+
"bucket_name" = var.config_bucket_name
105+
"s3_objects" = concat(
106+
var.private_ssl_cert ? [var.broker_private_key_object, var.broker_ssl_cert_object] : [],
107+
var.custom_listing_filter ? [var.broker_accept_json_object] : [])
106108
})
107109
depends_on = [null_resource.wait_lambda_efs]
108110
}

locals.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ locals {
99
broker_client_url = format("%s://%s:%s", var.broker_protocol, try(values(module.snykbroker_lb_route53_record.route53_record_fqdn)[0], module.snykbroker_lb.lb_dns_name), local.broker_lb_port)
1010
# certificate env vars
1111
mount_path = "/mnt/shared"
12-
cert_env_vars = var.use_private_ssl_cert ? tomap({
12+
cert_env_vars = var.private_ssl_cert ? tomap({
1313
"HTTPS_CERT" = format("%s/%s", local.mount_path, element(split("/", var.broker_ssl_cert_object), length(split("/", var.broker_ssl_cert_object))-1))
1414
"HTTPS_KEY" = format("%s/%s", local.mount_path, element(split("/", var.broker_private_key_object), length(split("/", var.broker_private_key_object))-1))
1515
}) : {}
16+
listing_filter_env_var = var.custom_listing_filter ? tomap({
17+
"ACCEPT" = format("%s/%s", local.mount_path, element(split("/", var.broker_accept_json_object), length(split("/", var.broker_accept_json_object))-1))
18+
}) : {}
1619
computed_env_vars = {
1720
"BROKER_CLIENT_URL" = local.broker_client_url
1821
"PORT" = local.broker_port
@@ -25,8 +28,9 @@ locals {
2528
broker_env_vars = merge({
2629
for v in local.env_vars : v => lookup(var.broker_env_vars, v, "")
2730
if length(regexall("(TOKEN)$", v)) == 0
28-
}, local.computed_env_vars, local.cert_env_vars, var.additional_env_vars)
31+
}, local.computed_env_vars, local.cert_env_vars, local.listing_filter_env_var, var.additional_env_vars)
2932

3033
# remove trailing dot from domain if any
3134
domain_name = trimsuffix(var.public_domain_name, ".")
35+
attach_config = var.private_ssl_cert || var.custom_listing_filter
3236
}

main.tf

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ module "snykbroker_security_group" {
8383
number_of_computed_ingress_with_source_security_group_id = 1
8484
egress_cidr_blocks = ["0.0.0.0/0"]
8585
egress_rules = ["https-443-tcp"]
86-
egress_with_source_security_group_id = var.use_private_ssl_cert ? [
86+
egress_with_source_security_group_id = local.attach_config ? [
8787
{
8888
rule = "nfs-tcp"
8989
source_security_group_id = module.snykbroker_efs[0].sg_id
@@ -110,22 +110,26 @@ module "snykbroker_ecs_cluster" {
110110

111111
cluster_configuration = {
112112
execute_command_configuration = {
113-
logging = "OVERRIDE"
113+
kms_key_id = module.snykbroker_kms.key_arn
114+
logging = "OVERRIDE"
114115
log_configuration = {
115-
cloud_watch_log_group_name = module.snykbroker_log_group.cloudwatch_log_group_name
116+
cloud_watch_encryption_enabled = true
117+
cloud_watch_log_group_name = module.snykbroker_log_group.cloudwatch_log_group_name
116118
}
117119
}
118120
}
119121

120122
fargate_capacity_providers = {
121123
FARGATE = {
122124
default_capacity_provider_strategy = {
123-
weight = 50
125+
base = var.fargate_capacity_base
126+
weight = var.fargate_capacity_weight
124127
}
125128
}
126129
FARGATE_SPOT = {
127130
default_capacity_provider_strategy = {
128-
weight = 50
131+
base = var.fargate_spot_capacity_base
132+
weight = var.fargate_spot_capacity_weight
129133
}
130134
}
131135
}
@@ -156,7 +160,7 @@ module "snykbroker_ecs_task_definition" {
156160

157161
task_stop_timeout = 90
158162

159-
task_mount_points = var.use_private_ssl_cert ? [
163+
task_mount_points = local.attach_config ? [
160164
{
161165
sourceVolume = var.service_name
162166
containerPath = local.mount_path
@@ -165,7 +169,7 @@ module "snykbroker_ecs_task_definition" {
165169
] : []
166170

167171
# volume name corresponds to sourceVolume which will contain a cert directory with cert injected by a lambda function
168-
volume = var.use_private_ssl_cert ? [
172+
volume = local.attach_config ? [
169173
{
170174
name = var.service_name
171175
efs_volume_configuration = [
@@ -205,7 +209,7 @@ module "snykbroker_lb" {
205209
target_groups = [
206210
{
207211
name_prefix = "snyk-"
208-
backend_protocol = var.use_private_ssl_cert ? "HTTPS" : "HTTP"
212+
backend_protocol = var.private_ssl_cert ? "HTTPS" : "HTTP"
209213
backend_port = local.broker_port
210214
target_type = "ip"
211215
health_check = {
@@ -216,7 +220,7 @@ module "snykbroker_lb" {
216220
healthy_threshold = 3
217221
unhealthy_threshold = 3
218222
timeout = 10
219-
protocol = var.use_private_ssl_cert ? "HTTPS" : "HTTP"
223+
protocol = var.private_ssl_cert ? "HTTPS" : "HTTP"
220224
matcher = "200-299"
221225
}
222226
}
@@ -243,7 +247,7 @@ module "snykbroker_lb" {
243247
}
244248

245249
module "snykbroker_efs" {
246-
count = var.use_private_ssl_cert ? 1 : 0
250+
count = local.attach_config ? 1 : 0
247251
source = "terraform-iaac/efs/aws"
248252
version = "2.0.4"
249253
# insert the 5 required variables here

outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
output "aws_broker_dns_name" {
2-
description = "AWS generated SnykBroker Client DNS name"
1+
output "snykbroker_aws_dns_name" {
2+
description = "SnykBroker Client AWS DNS name"
33
value = module.snykbroker_lb.lb_dns_name
44
}
55

66
output "snykbroker_lb_dns_name" {
7-
description = "SnykBroker Client load balancer DNS name"
7+
description = "SnykBroker Client hosted domain DNS name"
88
value = try(values(module.snykbroker_lb_route53_record.route53_record_fqdn)[0], "")
99
}
1010

0 commit comments

Comments
 (0)