Skip to content

Commit 3354854

Browse files
authored
feat: Add subscription module that detects if topic exists (#69)
1 parent 9721c15 commit 3354854

File tree

18 files changed

+185
-94
lines changed

18 files changed

+185
-94
lines changed

examples/organization/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module "secure-for-cloud_example_organization" {
7676
| <a name="module_cloud_connector"></a> [cloud\_connector](#module\_cloud\_connector) | ../../modules/services/cloud-connector | |
7777
| <a name="module_cloud_scanning"></a> [cloud\_scanning](#module\_cloud\_scanning) | ../../modules/services/cloud-scanning | |
7878
| <a name="module_connector_organization_sink"></a> [connector\_organization\_sink](#module\_connector\_organization\_sink) | ../../modules/infrastructure/organization_sink | |
79+
| <a name="module_pubsub_http_subscription"></a> [pubsub\_http\_subscription](#module\_pubsub\_http\_subscription) | ../../modules/infrastructure/pubsub_push_http_subscription | |
7980
| <a name="module_scanning_organization_sink"></a> [scanning\_organization\_sink](#module\_scanning\_organization\_sink) | ../../modules/infrastructure/organization_sink | |
8081
| <a name="module_secure_secrets"></a> [secure\_secrets](#module\_secure\_secrets) | ../../modules/infrastructure/secrets | |
8182

@@ -100,7 +101,6 @@ module "secure-for-cloud_example_organization" {
100101
| <a name="input_benchmark_project_ids"></a> [benchmark\_project\_ids](#input\_benchmark\_project\_ids) | Google cloud project IDs to run Benchmarks on. If empty, all organization projects will be defaulted. | `list(string)` | `[]` | no |
101102
| <a name="input_benchmark_regions"></a> [benchmark\_regions](#input\_benchmark\_regions) | List of regions in which to run the benchmark. If empty, the task will contain all regions by default. | `list(string)` | `[]` | no |
102103
| <a name="input_benchmark_role_name"></a> [benchmark\_role\_name](#input\_benchmark\_role\_name) | The name of the Service Account that will be created. | `string` | `"sysdigcloudbench"` | no |
103-
| <a name="input_create_gcr_topic"></a> [create\_gcr\_topic](#input\_create\_gcr\_topic) | Deploys a PubSub topic called `gcr` as part of this stack, which is needed for GCR scanning. Set to `true` if it doesn't exist yet. If this is not deployed, and no existing `gcr` topic is found, the GCR scanning is ommited and won't be deployed. For more info see [GCR PubSub topic](https://cloud.google.com/container-registry/docs/configuring-notifications#create_a_topic). | `bool` | `true` | no |
104104
| <a name="input_deploy_bench"></a> [deploy\_bench](#input\_deploy\_bench) | whether benchmark module is to be deployed | `bool` | `true` | no |
105105
| <a name="input_location"></a> [location](#input\_location) | Zone where the stack will be deployed | `string` | `"us-central1"` | no |
106106
| <a name="input_max_instances"></a> [max\_instances](#input\_max\_instances) | Max number of instances for the workloads | `number` | `1` | no |

examples/organization/main.tf

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ module "secure_secrets" {
127127
#--------------------
128128
# scanning
129129
#--------------------
130-
locals {
131-
repository_project_ids = length(var.repository_project_ids) == 0 ? [for p in data.google_projects.all_projects.projects : p.project_id] : var.repository_project_ids
132-
}
130+
133131

134132

135133
module "cloud_scanning" {
@@ -144,12 +142,27 @@ module "cloud_scanning" {
144142
cloud_scanning_sa_email = google_service_account.scanning_sa.email
145143
scanning_pubsub_topic_id = module.connector_organization_sink.pubsub_topic_id
146144
project_id = var.project_id
147-
create_gcr_topic = var.create_gcr_topic
148-
repository_project_ids = local.repository_project_ids
149145

150146
max_instances = var.max_instances
151147
}
152148

149+
locals {
150+
repository_project_ids = length(var.repository_project_ids) == 0 ? [for p in data.google_projects.all_projects.projects : p.project_id] : var.repository_project_ids
151+
}
152+
153+
module "pubsub_http_subscription" {
154+
for_each = toset(local.repository_project_ids)
155+
source = "../../modules/infrastructure/pubsub_push_http_subscription"
156+
157+
topic_project_id = each.key
158+
subscription_project_id = var.project_id
159+
topic_name = "gcr"
160+
name = "${var.name}-gcr"
161+
service_account_email = google_service_account.scanning_sa.email
162+
163+
push_http_endpoint = "${module.cloud_scanning.cloud_run_service_url}/gcr_scanning"
164+
}
165+
153166
#--------------------
154167
# benchmark
155168
#--------------------

examples/organization/variables.tf

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ variable "repository_project_ids" {
5555
description = "Projects were a `gcr`-named topic will be to subscribe to its repository events. If empty, all organization projects will be defaulted."
5656
}
5757

58-
variable "create_gcr_topic" {
59-
type = bool
60-
description = "Deploys a PubSub topic called `gcr` as part of this stack, which is needed for GCR scanning. Set to `true` if it doesn't exist yet. If this is not deployed, and no existing `gcr` topic is found, the GCR scanning is ommited and won't be deployed. For more info see [GCR PubSub topic](https://cloud.google.com/container-registry/docs/configuring-notifications#create_a_topic)."
61-
default = true
62-
}
63-
64-
6558
# benchmark
6659

6760
variable "deploy_bench" {

examples/single-project/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ module "secure-for-cloud_example_single-project" {
7070
| <a name="module_cloud_connector"></a> [cloud\_connector](#module\_cloud\_connector) | ../../modules/services/cloud-connector | |
7171
| <a name="module_cloud_scanning"></a> [cloud\_scanning](#module\_cloud\_scanning) | ../../modules/services/cloud-scanning | |
7272
| <a name="module_connector_project_sink"></a> [connector\_project\_sink](#module\_connector\_project\_sink) | ../../modules/infrastructure/project_sink | |
73+
| <a name="module_pubsub_http_subscription"></a> [pubsub\_http\_subscription](#module\_pubsub\_http\_subscription) | ../../modules/infrastructure/pubsub_push_http_subscription | |
7374
| <a name="module_scanning_project_sink"></a> [scanning\_project\_sink](#module\_scanning\_project\_sink) | ../../modules/infrastructure/project_sink | |
7475
| <a name="module_secure_secrets"></a> [secure\_secrets](#module\_secure\_secrets) | ../../modules/infrastructure/secrets | |
7576

@@ -88,7 +89,6 @@ module "secure-for-cloud_example_single-project" {
8889
| <a name="input_sysdig_secure_api_token"></a> [sysdig\_secure\_api\_token](#input\_sysdig\_secure\_api\_token) | Sysdig's Secure API Token | `string` | n/a | yes |
8990
| <a name="input_benchmark_regions"></a> [benchmark\_regions](#input\_benchmark\_regions) | List of regions in which to run the benchmark. If empty, the task will contain all regions by default. | `list(string)` | `[]` | no |
9091
| <a name="input_benchmark_role_name"></a> [benchmark\_role\_name](#input\_benchmark\_role\_name) | The name of the Service Account that will be created. | `string` | `"sysdigcloudbench"` | no |
91-
| <a name="input_create_gcr_topic"></a> [create\_gcr\_topic](#input\_create\_gcr\_topic) | Deploys a PubSub topic called `gcr` as part of this stack, which is needed for GCR scanning. Set to `true` if it doesn't exist yet. If this is not deployed, and no existing `gcr` topic is found, the GCR scanning is ommited and won't be deployed. For more info see [GCR PubSub topic](https://cloud.google.com/container-registry/docs/configuring-notifications#create_a_topic). | `bool` | `true` | no |
9292
| <a name="input_deploy_bench"></a> [deploy\_bench](#input\_deploy\_bench) | whether benchmark module is to be deployed | `bool` | `true` | no |
9393
| <a name="input_location"></a> [location](#input\_location) | Zone where the stack will be deployed | `string` | `"us-central1"` | no |
9494
| <a name="input_name"></a> [name](#input\_name) | Name to be assigned to all child resources. A suffix may be added internally when required. Use default value unless you need to install multiple instances | `string` | `"sfc"` | no |

examples/single-project/main.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ module "cloud_scanning" {
8585

8686
cloud_scanning_sa_email = google_service_account.scanning_sa.email
8787
scanning_pubsub_topic_id = module.scanning_project_sink.pubsub_topic_id
88-
create_gcr_topic = var.create_gcr_topic
8988
project_id = var.project_id
90-
repository_project_ids = [var.project_id]
9189

9290
secure_api_token_secret_id = module.secure_secrets.secure_api_token_secret_name
9391
sysdig_secure_api_token = var.sysdig_secure_api_token
@@ -97,6 +95,18 @@ module "cloud_scanning" {
9795
verify_ssl = local.verify_ssl
9896
}
9997

98+
module "pubsub_http_subscription" {
99+
source = "../../modules/infrastructure/pubsub_push_http_subscription"
100+
101+
topic_project_id = var.project_id
102+
subscription_project_id = var.project_id
103+
topic_name = "gcr"
104+
name = "${var.name}-gcr"
105+
service_account_email = google_service_account.scanning_sa.email
106+
107+
push_http_endpoint = "${module.cloud_scanning.cloud_run_service_url}/gcr_scanning"
108+
}
109+
100110

101111
#######################
102112
# BENCHMARKS #

examples/single-project/variables.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ variable "name" {
3535
}
3636
}
3737

38-
variable "create_gcr_topic" {
39-
type = bool
40-
description = "Deploys a PubSub topic called `gcr` as part of this stack, which is needed for GCR scanning. Set to `true` if it doesn't exist yet. If this is not deployed, and no existing `gcr` topic is found, the GCR scanning is ommited and won't be deployed. For more info see [GCR PubSub topic](https://cloud.google.com/container-registry/docs/configuring-notifications#create_a_topic)."
41-
default = true
42-
}
43-
4438
# benchmark
4539

4640
variable "deploy_bench" {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# PubSub Subscription module
2+
3+
Creates a PubSub Push subscription that sends the events to an HTTP endpoint. It will reuse the specified topic if it
4+
already exists in the project. It will create the topic if it doesn't exist.
5+
6+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
7+
## Requirements
8+
9+
| Name | Version |
10+
|------|---------|
11+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.0 |
12+
| <a name="requirement_google"></a> [google](#requirement\_google) | >= 3.67.0 |
13+
14+
## Providers
15+
16+
| Name | Version |
17+
|------|---------|
18+
| <a name="provider_google"></a> [google](#provider\_google) | >= 3.67.0 |
19+
20+
## Modules
21+
22+
No modules.
23+
24+
## Resources
25+
26+
| Name | Type |
27+
|------|------|
28+
| [google_pubsub_subscription.subscription](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/pubsub_subscription) | resource |
29+
| [google_pubsub_topic.topic](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/pubsub_topic) | resource |
30+
| [google_pubsub_topic.topic](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/pubsub_topic) | data source |
31+
32+
## Inputs
33+
34+
| Name | Description | Type | Default | Required |
35+
|------|-------------|------|---------|:--------:|
36+
| <a name="input_push_http_endpoint"></a> [push\_http\_endpoint](#input\_push\_http\_endpoint) | HTTP endpoint to push the events to | `string` | n/a | yes |
37+
| <a name="input_service_account_email"></a> [service\_account\_email](#input\_service\_account\_email) | Service account email to use | `string` | n/a | yes |
38+
| <a name="input_subscription_project_id"></a> [subscription\_project\_id](#input\_subscription\_project\_id) | Project ID where the subscription must be created | `string` | n/a | yes |
39+
| <a name="input_topic_name"></a> [topic\_name](#input\_topic\_name) | Topic to create a subscription | `string` | n/a | yes |
40+
| <a name="input_topic_project_id"></a> [topic\_project\_id](#input\_topic\_project\_id) | Project ID where the topic exists / must be created | `string` | n/a | yes |
41+
| <a name="input_name"></a> [name](#input\_name) | Name to be assigned to all child resources. A suffix may be added internally when required. Use default value unless you need to install multiple instances | `string` | `"sfc"` | no |
42+
43+
## Outputs
44+
45+
No outputs.
46+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
47+
48+
## Authors
49+
50+
Module is maintained and supported by [Sysdig](https://github.com/sysdiglabs/terraform-google-cloudvision).
51+
52+
## License
53+
54+
Apache 2 Licensed. See LICENSE for full details.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
data "google_pubsub_topic" "topic" {
2+
name = var.topic_name
3+
project = var.topic_project_id
4+
}
5+
6+
locals {
7+
create_topic = (data.google_pubsub_topic.topic.name == null || lookup(coalesce(data.google_pubsub_topic.topic.labels, {}), "sysdig-managed", "false") == "true")
8+
}
9+
10+
resource "google_pubsub_topic" "topic" {
11+
count = local.create_topic ? 1 : 0
12+
name = var.topic_name
13+
project = var.topic_project_id
14+
labels = {
15+
sysdig-managed = "true"
16+
}
17+
}
18+
19+
resource "google_pubsub_subscription" "subscription" {
20+
name = "${var.name}-${var.topic_project_id}"
21+
topic = "projects/${var.topic_project_id}/topics/${var.topic_name}"
22+
project = var.subscription_project_id
23+
24+
ack_deadline_seconds = 10
25+
26+
push_config {
27+
push_endpoint = var.push_http_endpoint
28+
oidc_token {
29+
service_account_email = var.service_account_email
30+
}
31+
}
32+
33+
retry_policy {
34+
minimum_backoff = "10s"
35+
maximum_backoff = "300s"
36+
}
37+
}

modules/infrastructure/pubsub_push_http_subscription/outputs.tf

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
variable "topic_name" {
2+
type = string
3+
description = "Topic to create a subscription"
4+
}
5+
6+
variable "push_http_endpoint" {
7+
type = string
8+
description = "HTTP endpoint to push the events to"
9+
}
10+
11+
variable "subscription_project_id" {
12+
type = string
13+
description = "Project ID where the subscription must be created"
14+
}
15+
16+
variable "service_account_email" {
17+
type = string
18+
description = "Service account email to use"
19+
}
20+
21+
variable "topic_project_id" {
22+
type = string
23+
description = "Project ID where the topic exists / must be created"
24+
}
25+
26+
variable "name" {
27+
type = string
28+
description = "Name to be assigned to all child resources. A suffix may be added internally when required. Use default value unless you need to install multiple instances"
29+
default = "sfc"
30+
}

0 commit comments

Comments
 (0)