Skip to content

Commit d705cb1

Browse files
SSPROD-26919: Add TF module for GCP CSPM only (single and org) (#1)
* SSPROD-26919: Add TF module for GCP CSPM only (single and org) - Adding CSPM (trust-relationship) TF module for GCP - Adding the module for both single-project and org (following same structure as terraform aws repo for consistency) - The module exports sa_email and sa_key as outputs Note: - Using and adding sysdig provider will be a separate PR (phase-2) Testing done: - Validated using terraform plan so far, with the following sample TF snippets * SSPROD-26919: Fix role permissions and default sa name variable * SSPROD-26919: Make service_account_key output sensitive * Bump up go version and go deps in ci-pull-request actions * Fix ginkgo version in go install * Remove ci-master.yaml and comment go build step in github action Change summary: ----------------- 1. Removing ci-master.yaml as it is not required. 2. Commenting out go build step since we don't have any *.go files in the terraform repo to build. Note: Both above can be added back after getting details on whether they are needed. For now they don't seem to be required. * Add Makefile and misc fixes to linting and formatting * Fix role_id used in testing * SSPROD-26919: Adding only required predefined GCP roles Change summary: ----------------- - Updated the CSPM roles to include latest set of roles required. - Updated the CIEM roles to be only required predefined GCP roles for now. Removing the custom role since all those permissions are already available in the predefined/built-in roles in GCP. After confirming with the CIEM team, if we do require custom roles with custom narrowed-down permissions, will push a subsequent PR. * SSPROD-26919: Update service account roles Change summary: --------------- 1. Updated the roles and removed the redundant ones since some of the roles have a subset of permissions already present in other roles. 2. Updated both single-project and org case. Note: Confirmed with CIEM team. No custom roles required.
1 parent a6c9240 commit d705cb1

File tree

10 files changed

+199
-95
lines changed

10 files changed

+199
-95
lines changed

.github/workflows/ci-master.yaml

Lines changed: 0 additions & 71 deletions
This file was deleted.

.github/workflows/ci-pull-request.yaml

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,54 @@ on:
44
pull_request:
55
branches:
66
- master
7+
env:
8+
GO_VERSION: "^1.20"
79

810
jobs:
9-
build-and-test:
10-
name: Build and Test
11+
format:
12+
name: Format
1113
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
- uses: hashicorp/setup-terraform@v2
18+
- run: make fmt
1219

20+
lint:
21+
name: Lint
22+
runs-on: ubuntu-latest
1323
steps:
1424
- name: Set up Go
1525
uses: actions/setup-go@v2
1626
with:
17-
go-version: ^1.14
18-
27+
go-version: ${{ env.GO_VERSION }}
1928
- name: Check out code
2029
uses: actions/checkout@v2
30+
- name: Lint
31+
run: make lint
2132

33+
build-and-test:
34+
name: Build and Test
35+
runs-on: ubuntu-latest
36+
steps:
37+
- name: Set up Go
38+
uses: actions/setup-go@v2
39+
with:
40+
go-version: ${{ env.GO_VERSION }}
41+
- name: Check out code
42+
uses: actions/checkout@v2
2243
- name: Cache modules
2344
uses: actions/cache@v1
2445
with:
2546
path: ~/go/pkg/mod
2647
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
2748
restore-keys: |
2849
${{ runner.os }}-go-
29-
3050
- name: Get dependencies
3151
run: |
32-
go get github.com/onsi/ginkgo/ginkgo
33-
34-
- name: Build
35-
run: go build ./...
36-
52+
go install github.com/onsi/ginkgo/ginkgo@latest
53+
# Check if we need to build any go packages in this repo. If not, remove below.
54+
# - name: Build
55+
# run: go build ./...
3756
- name: Test
3857
run: make test
39-
40-
lint:
41-
name: Lint
42-
runs-on: ubuntu-latest
43-
44-
steps:
45-
- name: Check out code
46-
uses: actions/checkout@v2
47-
48-
- name: Lint
49-
continue-on-error: true
50-
uses: golangci/golangci-lint-action@v1
51-
with:
52-
version: v1.27

Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
PROJECT := terraform-google-secure
2+
GO_BIN := $(shell go env GOPATH)/bin
3+
TFLINT := $(GO_BIN)/tflint
4+
DOCKER ?= docker
5+
6+
SHELL := /bin/bash
7+
8+
$(TFLINT): export TMPDIR = $(shell mktemp -d)
9+
$(TFLINT):
10+
curl -sL https://github.com/terraform-linters/tflint/releases/latest/download/tflint_$(shell go env GOHOSTOS)_$(shell go env GOHOSTARCH).zip -o $${TMPDIR}/tflint.zip
11+
unzip $${TMPDIR}/tflint.zip -d $(GO_BIN) tflint
12+
rm -rf $${TMPDIR}
13+
14+
deps: $(TFLINT)
15+
go install github.com/terraform-docs/[email protected]
16+
go install github.com/hashicorp/terraform-config-inspect@latest
17+
18+
lint: $(TFLINT)
19+
$(MAKE) -C modules lint
20+
21+
fmt:
22+
terraform fmt -check -recursive modules
23+
24+
clean:
25+
find -name ".terraform" -type d | xargs rm -rf
26+
find -name ".terraform.lock.hcl" -type f | xargs rm -f
27+
28+
.PHONY: test
29+
test:
30+
$(MAKE) -C test test

modules/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lint:
2+
tflint --recursive --module
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
###################################################
2+
# Create Service Account and setup permissions
3+
###################################################
4+
5+
resource "google_service_account" "sa" {
6+
project = var.project_id
7+
account_id = var.service_account_name
8+
display_name = "Service account for trust-relationship"
9+
}
10+
11+
#---------------------------------
12+
# role permissions for onboarding
13+
#---------------------------------
14+
resource "google_project_iam_member" "onboarding_role" {
15+
count = var.is_organizational ? 0 : 1
16+
17+
project = var.project_id
18+
role = "roles/browser"
19+
member = "serviceAccount:${google_service_account.sa.email}"
20+
}
21+
22+
#--------------------------------------------------------------------------------------
23+
# role permissions for CSPM (GCP Predefined Roles for Sysdig Cloud Trust Relationship)
24+
#--------------------------------------------------------------------------------------
25+
resource "google_project_iam_member" "trust_relationship_role" {
26+
for_each = var.is_organizational ? [] : toset(["roles/cloudasset.viewer"])
27+
28+
project = var.project_id
29+
role = each.key
30+
member = "serviceAccount:${google_service_account.sa.email}"
31+
}
32+
33+
#---------------------------------------------------------------------------------------
34+
# role permissions for CIEM (GCP Predefined Roles for Sysdig Cloud Identity Management)
35+
#---------------------------------------------------------------------------------------
36+
resource "google_project_iam_member" "identity_mgmt_role" {
37+
for_each = var.is_organizational ? [] : toset(["roles/recommender.viewer", "roles/iam.serviceAccountViewer", "roles/iam.roleViewer"])
38+
39+
project = var.project_id
40+
role = each.key
41+
member = "serviceAccount:${google_service_account.sa.email}"
42+
}
43+
44+
#--------------------------------
45+
# service account private key
46+
#--------------------------------
47+
resource "google_service_account_key" "secure_service_account_key" {
48+
service_account_id = google_service_account.sa.name
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
###################################################
2+
# Fetch & compute required data
3+
###################################################
4+
5+
data "google_organization" "org" {
6+
count = var.is_organizational ? 1 : 0
7+
domain = var.organization_domain
8+
}
9+
10+
###################################################
11+
# Setup Service Account permissions
12+
###################################################
13+
14+
#---------------------------------
15+
# role permissions for onboarding
16+
#---------------------------------
17+
resource "google_organization_iam_member" "onboarding_role" {
18+
count = var.is_organizational ? 1 : 0
19+
20+
org_id = data.google_organization.org[0].org_id
21+
role = "roles/browser"
22+
member = "serviceAccount:${google_service_account.sa.email}"
23+
}
24+
25+
#--------------------------------------------------------------------------------------
26+
# role permissions for CSPM (GCP Predefined Roles for Sysdig Cloud Trust Relationship)
27+
#--------------------------------------------------------------------------------------
28+
resource "google_organization_iam_member" "trust_relationship_role" {
29+
for_each = var.is_organizational ? toset(["roles/cloudasset.viewer"]) : []
30+
31+
org_id = data.google_organization.org[0].org_id
32+
role = each.key
33+
member = "serviceAccount:${google_service_account.sa.email}"
34+
}
35+
36+
#---------------------------------------------------------------------------------------
37+
# role permissions for CIEM (GCP Predefined Roles for Sysdig Cloud Identity Management)
38+
#---------------------------------------------------------------------------------------
39+
resource "google_organization_iam_member" "identity_mgmt_role" {
40+
for_each = var.is_organizational ? toset(["roles/recommender.viewer", "roles/iam.serviceAccountViewer", "roles/iam.organizationRoleViewer"]) : []
41+
42+
org_id = data.google_organization.org[0].org_id
43+
role = each.key
44+
member = "serviceAccount:${google_service_account.sa.email}"
45+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
output "service_account_email" {
2+
value = google_service_account.sa.email
3+
description = "email address of the Service Account created (used to allow Sysdig Secure access)"
4+
}
5+
6+
output "service_account_key" {
7+
value = google_service_account_key.secure_service_account_key.private_key
8+
description = "Private Key of the Service Account created"
9+
sensitive = true
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
variable "project_id" {
2+
type = string
3+
description = "The ID of the target Google cloud project to create resources in."
4+
}
5+
6+
variable "service_account_name" {
7+
type = string
8+
description = "The name of the Service Account that will be created."
9+
default = "sysdig-secure"
10+
}
11+
12+
variable "is_organizational" {
13+
description = "(Optional) Set this field to 'true' to deploy secure-for-cloud to a GCP Organization."
14+
type = bool
15+
default = false
16+
}
17+
18+
variable "organization_domain" {
19+
type = string
20+
description = "Organization domain. e.g. sysdig.com"
21+
default = ""
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.0.0"
3+
4+
required_providers {
5+
google = {
6+
source = "hashicorp/google"
7+
version = ">= 4.21.0"
8+
}
9+
}
10+
}

test/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
@echo "Functional Tests to be added here."

0 commit comments

Comments
 (0)