Skip to content

Commit 39455f0

Browse files
authored
GitlabBot - Enable pulling secrets from SecretsManager (#9)
* Enable pulling secrets from SecrersManager * move logic into secrets_manager module * add new output to docs * docs update * new gitlab source code * fix dynamic statement syntax * fix secrets manager module reference * formatting * split policies and attach to role * formatting * formatting * improve syntax * cancel rename * update source code
1 parent 68d8f4c commit 39455f0

File tree

12 files changed

+106
-24
lines changed

12 files changed

+106
-24
lines changed

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Terraform configuration used to create the required AWS resources for integratin
3030
| `lambda_function_timeout` | Amount of time your Lambda Function has to run in seconds. | `number` | 300 | No |
3131
| `lambda_function_memory_size` | Amount of memory in MB your Lambda Function can use at runtime. | `number` | 1024 | No |
3232
| `lambda_publish` | Whether to publish creation/change as new Lambda Function Version. | `bool` | `false` | No |
33+
| `store_secret_in_secrets_manager` | Whether to store secrets values on a vault (currently supporting AWS secrets manager on GitLab integration). | `bool` | `false` | No |
3334

3435
### env_vars
3536

@@ -79,7 +80,7 @@ This variable holds a collection of tags grouped by key representing its target
7980

8081
```tcl
8182
module "spectral_lambda_integration" {
82-
source = "github.com/SpectralOps/spectral-terraform-lambda-integration?ref=v1.0.2"
83+
source = "github.com/SpectralOps/spectral-terraform-lambda-integration"
8384
8485
environment = "prod"
8586
integration_type = "terraform"
@@ -91,7 +92,7 @@ module "spectral_lambda_integration" {
9192
9293
# Environment variables used by the integration
9394
env_vars = {
94-
# Mandatory - Your spectral DSN retrieved from SpectralOps
95+
# Mandatory (unless you are using vault) - Your spectral DSN retrieved from SpectralOps
9596
SPECTRAL_DSN = ""
9697
# Additional env-vars should go here
9798
}
@@ -120,15 +121,6 @@ module "spectral_lambda_integration" {
120121
}
121122
```
122123

123-
Don't forget to configure your provider:
124-
```tcl
125-
provider "aws" {
126-
allowed_account_ids = ["11111111111"]
127-
region = "us-east-1"
128-
profile = "example-profile"
129-
}
130-
```
131-
132124
## Resources
133125

134126
| Name | Type |
@@ -159,3 +151,4 @@ provider "aws" {
159151
7. `lambda_function_name` - The name of the lambda function.
160152
8. `lambda_iam_role_arn` - Amazon Resource Name (ARN) specifying the role.
161153
9. `lambda_iam_role_name` - Name of the role.
154+
10. `secrets_arns` - Arns of created secrets in secrets manager.

main.tf

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ locals {
33
}
44

55
module "lambda_function" {
6-
source = "./modules/lambda"
7-
global_tags = var.global_tags
8-
tags = var.tags
9-
environment = var.environment
10-
integration_type = var.integration_type
11-
resource_name_pattern = local.resource_name_pattern
12-
env_vars = var.env_vars
13-
logs_retention_in_days = var.lambda_logs_retention_in_days
14-
should_write_logs = var.lambda_enable_logs
15-
timeout = var.lambda_function_timeout
16-
memory_size = var.lambda_function_memory_size
17-
publish = var.lambda_publish
6+
source = "./modules/lambda"
7+
global_tags = var.global_tags
8+
tags = var.tags
9+
environment = var.environment
10+
integration_type = var.integration_type
11+
resource_name_pattern = local.resource_name_pattern
12+
env_vars = var.env_vars
13+
logs_retention_in_days = var.lambda_logs_retention_in_days
14+
should_write_logs = var.lambda_enable_logs
15+
timeout = var.lambda_function_timeout
16+
memory_size = var.lambda_function_memory_size
17+
publish = var.lambda_publish
18+
secrets_arns = var.store_secret_in_secrets_manager ? module.secrets_manager[0].secrets_arns : []
19+
store_secret_in_secrets_manager = var.store_secret_in_secrets_manager
1820
}
1921

2022
module "api_gateway" {
@@ -25,4 +27,10 @@ module "api_gateway" {
2527
integration_type = var.integration_type
2628
resource_name_pattern = local.resource_name_pattern
2729
lambda_function_arn = module.lambda_function.lambda_function_arn
30+
}
31+
32+
module "secrets_manager" {
33+
count = var.store_secret_in_secrets_manager ? 1 : 0
34+
integration_type = var.integration_type
35+
source = "./modules/secrets_manager"
2836
}

modules/lambda/lambda.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,32 @@ resource "aws_iam_role" "lambda_execution_role" {
5959
)
6060
}
6161

62+
data "aws_iam_policy_document" "secrets_policy_document" {
63+
statement {
64+
sid = ""
65+
effect = "Allow"
66+
actions = ["secretsmanager:GetSecretValue"]
67+
resources = var.secrets_arns
68+
}
69+
}
70+
71+
resource "aws_iam_policy" "secrets_iam_policy" {
72+
count = var.store_secret_in_secrets_manager ? 1 : 0
73+
policy = data.aws_iam_policy_document.secrets_policy_document.json
74+
75+
tags = merge(
76+
var.global_tags,
77+
lookup(var.tags, "iam", {}),
78+
)
79+
}
80+
6281
resource "aws_iam_role_policy_attachment" "lambda_execution_role" {
6382
role = aws_iam_role.lambda_execution_role.name
6483
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
84+
}
85+
86+
resource "aws_iam_role_policy_attachment" "lambda_secrets_role_attachment" {
87+
count = var.store_secret_in_secrets_manager ? 1 : 0
88+
role = aws_iam_role.lambda_execution_role.name
89+
policy_arn = aws_iam_policy.secrets_iam_policy[count.index].arn
6590
}
277 KB
Binary file not shown.

modules/lambda/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,15 @@ variable "publish" {
6363
type = bool
6464
description = "Whether to publish creation/change as new Lambda Function Version."
6565
default = false
66+
}
67+
68+
variable "secrets_arns" {
69+
description = "List of secrets associated with the lambda"
70+
type = list(string)
71+
default = []
72+
}
73+
74+
variable "store_secret_in_secrets_manager" {
75+
description = "Whether to store your secrets in secrets manager, default is false"
76+
type = bool
6677
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "aws_secretsmanager_secret" "gitlab_webhook_secret" {
2+
name = "Spectral_GitlabBot_WebhookSecret"
3+
}
4+
5+
resource "aws_secretsmanager_secret" "gitlab_token" {
6+
name = "Spectral_GitlabBot_GitlabToken"
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
output "secrets_arns" {
2+
value = [
3+
aws_secretsmanager_secret.gitlab_token.arn,
4+
aws_secretsmanager_secret.gitlab_webhook_secret.arn
5+
]
6+
}

modules/secrets_manager/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "secrets_arns" {
2+
value = local.secrets_arns
3+
}

modules/secrets_manager/secrets.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
locals {
2+
secrets_arns = concat(
3+
try(module.gitlab[0].secrets_arns, []),
4+
[aws_secretsmanager_secret.spectral_dsn.arn]
5+
)
6+
}
7+
8+
resource "aws_secretsmanager_secret" "spectral_dsn" {
9+
name = "Spectral_Dsn"
10+
}
11+
12+
module "gitlab" {
13+
count = var.integration_type == "gitlab" ? 1 : 0
14+
source = "./gitlab"
15+
}

modules/secrets_manager/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "integration_type" {
2+
description = "Integration type to create secrets for"
3+
type = string
4+
}

outputs.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ output "lambda_iam_role_arn" {
3232

3333
output "lambda_iam_role_name" {
3434
value = module.lambda_function.lambda_iam_role_name
35-
}
35+
}
36+
37+
output "secrets_arns" {
38+
value = module.secrets_manager[*].secrets_arns
39+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ variable "lambda_publish" {
6868
type = bool
6969
description = "Whether to publish creation/change as new Lambda Function Version."
7070
default = false
71+
}
72+
73+
variable "store_secret_in_secrets_manager" {
74+
type = bool
75+
description = "Whether to store your secrets in secrets manager, default is false"
76+
default = false
7177
}

0 commit comments

Comments
 (0)