Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to encrypt logs via KMS #156

Merged
merged 5 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

- Add option to encrypt logs via KMS.

## 4.8.0 - 2019-11-01
- Upgraded the runners (docker-machine) to ubuntu 18.04. You can stay on 16.04 by setting: `runner_ami_filter = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]`
- Upgraded GitLab runner to 12.4.1
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ terraform destroy
| docker\_machine\_version | Version of docker-machine. | string | `"0.16.2"` | no |
| enable\_cloudwatch\_logging | Boolean used to enable or disable the CloudWatch logging. | bool | `"true"` | no |
| enable\_gitlab\_runner\_ssh\_access | Enables SSH Access to the gitlab runner instance. | bool | `"false"` | no |
| enable\_kms | Let the module manage a KMS key, logs will be encrypted via KMS. Be-aware of the costs of an custom key. | bool | `"false"` | no |
| enable\_manage\_gitlab\_token | Boolean to enable the management of the GitLab token in SSM. If `true` the token will be stored in SSM, which means the SSM property is a terraform managed resource. If `false` the Gitlab token will be stored in the SSM by the user-data script during creation of the the instance. However the SSM parameter is not managed by terraform and will remain in SSM after a `terraform destroy`. | bool | `"true"` | no |
| enable\_runner\_ssm\_access | Add IAM policies to the runner agent instance to connect via the Session Manager. | bool | `"false"` | no |
| enable\_runner\_user\_data\_trace\_log | Enable bash xtrace for the user data script that creates the EC2 instance for the runner agent. Be aware this could log sensitive data such as you GitLab runner token. | bool | `"false"` | no |
Expand All @@ -255,6 +256,8 @@ terraform destroy
| gitlab\_runner\_version | Version of the GitLab runner. | string | `"12.4.1"` | no |
| instance\_role\_json | Default runner instance override policy, expected to be in JSON format. | string | `""` | no |
| instance\_type | Instance type used for the GitLab runner. | string | `"t3.micro"` | no |
| kms\_deletion\_window\_in\_days | Key rotation window, set to 0 for no rotation. Only used when `enable_kms` is set to `true`. | number | `"7"` | no |
| kms\_key\_id | KMS key id to encrypted the CloudWatch logs. Ensure CloudWatch has access to the provided KMS key. | string | `""` | no |
| overrides | This maps provides the possibility to override some defaults. The following attributes are supported: `name_sg` overwrite the `Name` tag for all security groups created by this module. `name_runner_agent_instance` override the `Name` tag for the ec2 instance defined in the auto launch configuration. `name_docker_machine_runners` ovverrid the `Name` tag spot instances created by the runner agent. | map(string) | `<map>` | no |
| runner\_ami\_filter | List of maps used to create the AMI filter for the Gitlab runner docker-machine AMI. | map(list(string)) | `<map>` | no |
| runner\_ami\_owners | The list of owners used to select the AMI of Gitlab runner docker-machine instances. | list(string) | `<list>` | no |
Expand Down Expand Up @@ -284,6 +287,7 @@ terraform destroy
| runners\_privileged | Runners will run in privileged mode, will be used in the runner config.toml | bool | `"true"` | no |
| runners\_pull\_policy | pull_policy for the runners, will be used in the runner config.toml | string | `"always"` | no |
| runners\_request\_concurrency | Limit number of concurrent requests for new jobs from GitLab (default 1) | number | `"1"` | no |
| runners\_request\_spot\_instance | Whether or not to request spot instances via docker-machine | bool | `"true"` | no |
| runners\_root\_size | Runner instance root size in GB. | number | `"16"` | no |
| runners\_services\_volumes\_tmpfs | Mount temporary file systems to service containers. Must consist of pairs of strings e.g. "/var/lib/mysql" = "rw,noexec", see example | list | `<list>` | no |
| runners\_shm\_size | shm_size for the runners, will be used in the runner config.toml | number | `"0"` | no |
Expand Down
4 changes: 4 additions & 0 deletions _docs/TF_MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
| docker\_machine\_version | Version of docker-machine. | string | `"0.16.2"` | no |
| enable\_cloudwatch\_logging | Boolean used to enable or disable the CloudWatch logging. | bool | `"true"` | no |
| enable\_gitlab\_runner\_ssh\_access | Enables SSH Access to the gitlab runner instance. | bool | `"false"` | no |
| enable\_kms | Let the module manage a KMS key, logs will be encrypted via KMS. Be-aware of the costs of an custom key. | bool | `"false"` | no |
| enable\_manage\_gitlab\_token | Boolean to enable the management of the GitLab token in SSM. If `true` the token will be stored in SSM, which means the SSM property is a terraform managed resource. If `false` the Gitlab token will be stored in the SSM by the user-data script during creation of the the instance. However the SSM parameter is not managed by terraform and will remain in SSM after a `terraform destroy`. | bool | `"true"` | no |
| enable\_runner\_ssm\_access | Add IAM policies to the runner agent instance to connect via the Session Manager. | bool | `"false"` | no |
| enable\_runner\_user\_data\_trace\_log | Enable bash xtrace for the user data script that creates the EC2 instance for the runner agent. Be aware this could log sensitive data such as you GitLab runner token. | bool | `"false"` | no |
Expand All @@ -31,6 +32,8 @@
| gitlab\_runner\_version | Version of the GitLab runner. | string | `"12.4.1"` | no |
| instance\_role\_json | Default runner instance override policy, expected to be in JSON format. | string | `""` | no |
| instance\_type | Instance type used for the GitLab runner. | string | `"t3.micro"` | no |
| kms\_deletion\_window\_in\_days | Key rotation window, set to 0 for no rotation. Only used when `enable_kms` is set to `true`. | number | `"7"` | no |
| kms\_key\_id | KMS key id to encrypted the CloudWatch logs. Ensure CloudWatch has access to the provided KMS key. | string | `""` | no |
| overrides | This maps provides the possibility to override some defaults. The following attributes are supported: `name_sg` overwrite the `Name` tag for all security groups created by this module. `name_runner_agent_instance` override the `Name` tag for the ec2 instance defined in the auto launch configuration. `name_docker_machine_runners` ovverrid the `Name` tag spot instances created by the runner agent. | map(string) | `<map>` | no |
| runner\_ami\_filter | List of maps used to create the AMI filter for the Gitlab runner docker-machine AMI. | map(list(string)) | `<map>` | no |
| runner\_ami\_owners | The list of owners used to select the AMI of Gitlab runner docker-machine instances. | list(string) | `<list>` | no |
Expand Down Expand Up @@ -60,6 +63,7 @@
| runners\_privileged | Runners will run in privileged mode, will be used in the runner config.toml | bool | `"true"` | no |
| runners\_pull\_policy | pull_policy for the runners, will be used in the runner config.toml | string | `"always"` | no |
| runners\_request\_concurrency | Limit number of concurrent requests for new jobs from GitLab (default 1) | number | `"1"` | no |
| runners\_request\_spot\_instance | Whether or not to request spot instances via docker-machine | bool | `"true"` | no |
| runners\_root\_size | Runner instance root size in GB. | number | `"16"` | no |
| runners\_services\_volumes\_tmpfs | Mount temporary file systems to service containers. Must consist of pairs of strings e.g. "/var/lib/mysql" = "rw,noexec", see example | list | `<list>` | no |
| runners\_shm\_size | shm_size for the runners, will be used in the runner config.toml | number | `"0"` | no |
Expand Down
22 changes: 22 additions & 0 deletions kms.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resource "aws_kms_key" "default" {
count = var.enable_kms ? 1 : 0

description = "GitLab Runner module managed key - ${var.environment}"
deletion_window_in_days = var.kms_deletion_window_in_days > 0 ? var.kms_deletion_window_in_days : null
enable_key_rotation = var.kms_deletion_window_in_days > 0 ? true : false
tags = local.tags
policy = data.template_file.kms_policy[0].rendered
}

data "template_file" "kms_policy" {
count = var.enable_kms ? 1 : 0

template = file("${path.module}/policies/kms-policy.json")

vars = {
aws_region = var.aws_region
account_id = data.aws_caller_identity.current.account_id
}
}


10 changes: 10 additions & 0 deletions logging.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@



data "template_file" "instance_profile" {
count = var.enable_cloudwatch_logging ? 1 : 0
template = file("${path.module}/policies/instance-logging-policy.json")
Expand All @@ -10,9 +13,16 @@ resource "aws_iam_role_policy" "instance" {
policy = data.template_file.instance_profile[0].rendered
}


locals {
provided_kms_key = var.kms_key_id != "" ? var.kms_key_id : ""
kms_key = local.provided_kms_key == "" && var.enable_kms ? aws_kms_key.default[0].arn : local.provided_kms_key
}

resource "aws_cloudwatch_log_group" "environment" {
count = var.enable_cloudwatch_logging ? 1 : 0
name = var.environment
retention_in_days = var.cloudwatch_logging_retention_in_days
tags = local.tags
kms_key_id = local.kms_key
}
2 changes: 2 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
data "aws_caller_identity" "current" {}

resource "aws_key_pair" "key" {
count = var.ssh_key_pair == "" && var.ssh_public_key != "" ? 1 : 0
key_name = "${var.environment}-gitlab-runner"
Expand Down
33 changes: 33 additions & 0 deletions policies/kms-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::${account_id}:root"
]
},
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "allowLoggingToCloudWatch",
"Effect": "Allow",
"Principal": {
"Service": "logs.${aws_region}.amazonaws.com"
},
"Action": [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
],
"Resource": [
"*"
]
}
]
}
18 changes: 18 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,21 @@ variable "runners_services_volumes_tmpfs" {
type = list
default = []
}

variable "kms_key_id" {
description = "KMS key id to encrypted the CloudWatch logs. Ensure CloudWatch has access to the provided KMS key."
type = string
default = ""
}

variable "enable_kms" {
description = "Let the module manage a KMS key, logs will be encrypted via KMS. Be-aware of the costs of an custom key."
type = bool
default = false
}

variable "kms_deletion_window_in_days" {
description = "Key rotation window, set to 0 for no rotation. Only used when `enable_kms` is set to `true`."
type = number
default = 7
}