Slide 1

Slide 1 text

Increasing the Security Posture of your Pipelines

Slide 2

Slide 2 text

Getting the Security Team to stop bothering you daily

Slide 3

Slide 3 text

Sr. Developer Advocate at HashiCorp he / him @ksatirli Kerim Satirli

Slide 4

Slide 4 text

Define the Pipeline ! react to repository events # deal with code quality issues " ensure code quality

Slide 5

Slide 5 text

!!" # when to run this pipeline # all the stuff it needs to do # handle errors other people introduced pipeline.yml Define the Pipeline

Slide 6

Slide 6 text

!!" # when to run this pipeline on: push: jobs: # all the stuff it needs to do happy_path: steps: - run: terraform fmt -check -recursive !# terraform validate # handle errors other people introduced sad_path: steps: - if: $!$ failure() !% uses: upload-artifacts terraform.yml Define the Pipeline

Slide 7

Slide 7 text

!!" # when to run this pipeline on: push: jobs: # all the stuff it needs to do happy_path: steps: - run: terraform fmt -check -recursive !# terraform validate # handle errors other people introduced sad_path: steps: - if: $!$ failure() !% uses: upload-artifacts terraform.yml Define the Pipeline

Slide 8

Slide 8 text

!!" # when to run this pipeline on: push: jobs: # all the stuff it needs to do happy_path: steps: - run: terraform fmt -check -recursive !# terraform validate with: version: "1.6.0" terraform.yml Define the Pipeline

Slide 9

Slide 9 text

terraform.yml !!" # when to run this pipeline on: push: jobs: # all the stuff it needs to do happy_path: steps: - uses: "hashicorp/setup-terraform" - run: terraform fmt -check -recursive !# terraform validate with: version: "1.6.0" Define the Pipeline

Slide 10

Slide 10 text

terraform.yml !!" # when to run this pipeline on: push: jobs: # all the stuff it needs to do happy_path: steps: - uses: "hashicorp/[email protected]" - run: terraform fmt -check -recursive !# terraform validate with: version: "1.6.0" Define the Pipeline

Slide 11

Slide 11 text

Ship it.

Slide 12

Slide 12 text

$ from the Security Team

Slide 13

Slide 13 text

terraform.yml My Pipeline Definition !!" # when to run this pipeline on: push: jobs: # all the stuff it needs to do happy_path: steps: - uses: "hashicorp/[email protected]" - run: terraform fmt -check -recursive !# terraform validate with: version: "1.6.0"

Slide 14

Slide 14 text

terraform.yml Their Pipeline Definition !!" # when to run this pipeline on: push: jobs: # all the stuff it needs to do happy_path: steps: - uses: "hashicorp/setup-terraform@633b725c73b2cacd13a8fdd1" - run: terraform fmt -check -recursive !# terraform validate with: version: "1.6.0"

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Getting Release Information https://docs.github.com/en/rest/releases/releases

Slide 17

Slide 17 text

variables.tf variable "actions_config" { type = map(object({ owner = string repository = string version = string })) default = { # see https:!"github.com/hashicorp/setup-terraform/releases terraform = { owner = "hashicorp" repository = "setup-terraform" version = "v2.0.3" } } } Define a Set of Actions

Slide 18

Slide 18 text

variables.tf data "github_release" "actions" { for_each = { for id, action in var.actions_config : id !& action } repository = each.value.repository owner = each.value.owner retrieve_by = "tag" release_tag = each.value.version } data "github_ref" "actions" { for_each. = data.github_release.actions repository = each.value.repository owner = each.value.owner ref = "tags/${each.value.release_tag}" } Retrieve Release Information

Slide 19

Slide 19 text

variables.tf Transform Release Information locals { actions_config = { # This place is not a place of honor. # no highly esteemed deed is commemorated here. # (but we really needed these values) for action in tolist(keys(var.actions_config)) : action !& { owner = var.actions_config[action].owner path = var.actions_config[action].path ref = data.github_ref.actions[action].ref repo = var.actions_config[action].repository sha = data.github_ref.actions[action].sha version = var.actions_config[action].version } } }

Slide 20

Slide 20 text

Terminal Verify Transformed Data > terraform output github_actions_releases { "terraform" = { "repo" = "hashicorp/setup-terraform" "sha" = "633666f66e0061ca3b725c73b2ec20cd13a8fdd1" "version" = "v2.0.3" } }

Slide 21

Slide 21 text

terraform.tftpl.yml Prepare Template !!' jobs: workflow: name: Terraform runs-on: ubuntu-latest steps: # github.com/${owner}/${repo}/releases/tag/${version} - name: Set up Terraform uses: "${owner}/${repo}@${sha}" # ref: `${ref}` with: terraform_version: "1.6.0" !!'

Slide 22

Slide 22 text

variables.tf Render Template locals { repository_files = [ { file = ".github/workflows/terraform.yml" content = templatefile("./tmpl/terraform.tftpl.yml", { checkout = local.actions_config["checkout"] terraform = local.actions_config["terraform"] } ) }, ] }

Slide 23

Slide 23 text

terraform.yml Render Template !!' jobs: workflow: name: Terraform runs-on: ubuntu-latest steps: # github.com/hashicorp/setup-terraform/releases/tag/v2.0.3 - name: Set up Terraform uses: "hashicorp/setup-terraform@633!!'dd1" # ref: `tags/v2.0.3` with: terraform_version: "1.6.0" !!'

Slide 24

Slide 24 text

Add Rendered Template to Repository https://github.com/workloads/workspaces/blob/main/.github/workflows/terraform.yml

Slide 25

Slide 25 text

organization.tf Update Allow List for Actions resource "github_actions_organization_permissions" "main" { allowed_actions = "selected" enabled_repositories = "all" allowed_actions_config { github_owned_allowed = false verified_allowed = false } }

Slide 26

Slide 26 text

organization.tf Update Allow List for Actions resource "github_actions_organization_permissions" "main" { allowed_actions = "selected" enabled_repositories = "all" allowed_actions_config { github_owned_allowed = false verified_allowed = false patterns_allowed = [ for action in local.actions_config : action.path !( null ? "${action.owner}/$ {action.repository}/${action.path}@${action.sha}" : "$ {action.owner}/${action.repository}@${action.sha}" ] } }

Slide 27

Slide 27 text

Update Allow List for Actions https://github.com/workloads/workspaces/blob/main/.github/workflows/terraform.yml

Slide 28

Slide 28 text

Update Allow List for Actions https://github.com/workloads/workspaces/blob/main/.github/workflows/terraform.yml

Slide 29

Slide 29 text

Demo Code https://github.com/workloads/github-organization

Slide 30

Slide 30 text

What's next?

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Software Security is a Team Sport.

Slide 33

Slide 33 text

Thank you speakerdeck.com/ksatirli