Skip to content

Commit

Permalink
Add Terraform work for X509 setup with AWS IAM Anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattias Gees committed Feb 4, 2025
1 parent c31a511 commit 5c4adfa
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 38 deletions.
39 changes: 39 additions & 0 deletions deploy/terraform/aws/jwt.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
data "tls_certificate" "oidc-certificate" {
count = var.auth-type == "JWT" ? 1 : 0
url = "https://${var.oidc-url}"
}

resource "aws_iam_openid_connect_provider" "oidc-spire" {
count = var.auth-type == "JWT" ? 1 : 0
url = "https://${var.oidc-url}"

client_id_list = [
"demo",
]

thumbprint_list = [data.tls_certificate.oidc-certificate.certificates[0].sha1_fingerprint]
}

resource "aws_iam_role" "oidc-spire-role" {
count = var.auth-type == "JWT" ? 1 : 0
name = "demo-spiffe-role"

assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRoleWithWebIdentity",
Effect = "Allow",
Principal = {
Federated = aws_iam_openid_connect_provider.oidc-spire.arn,
},
Condition = {
StringEquals = {
"${var.oidc-url}:aud" = "demo",
"${var.oidc-url}:sub" = "${var.spiffe-id}"
}
}
},
],
})
}
39 changes: 1 addition & 38 deletions deploy/terraform/aws/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ terraform {
}
}

data "tls_certificate" "oidc-certificate" {
url = "https://${var.oidc-url}"
}

provider "aws" {
region = var.aws-region
}
Expand All @@ -24,42 +20,9 @@ resource "aws_s3_bucket" "oidc-test" {
}
}

resource "aws_iam_openid_connect_provider" "oidc-spire" {
url = "https://${var.oidc-url}"

client_id_list = [
"demo",
]

thumbprint_list = [data.tls_certificate.oidc-certificate.certificates[0].sha1_fingerprint]
}

resource "aws_iam_role" "oidc-spire-role" {
name = "demo-spiffe-role"

assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRoleWithWebIdentity",
Effect = "Allow",
Principal = {
Federated = aws_iam_openid_connect_provider.oidc-spire.arn,
},
Condition = {
StringEquals = {
"${var.oidc-url}:aud" = "demo",
"${var.oidc-url}:sub" = "${var.spiffe-id}"
}
}
},
],
})
}

resource "aws_iam_role_policy" "s3" {
name = "demo-spiffe-policy"
role = aws_iam_role.oidc-spire-role.name
role = var.auth-type == "JWT" ? aws_iam_role.oidc-spire-role.name : aws_iam_role.x509-spire.name

policy = <<EOF
{
Expand Down
9 changes: 9 additions & 0 deletions deploy/terraform/aws/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ variable "spiffe-id" {
variable "aws-region" {
default = "eu-west-2"
}

variable "auth-type" {
description = "Authentication type to use either pick JWT or X509"
default = "JWT"
}

variable "root-CA" {
default = "ROOT_CA"
}
44 changes: 44 additions & 0 deletions deploy/terraform/aws/x509.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
resource "aws_rolesanywhere_trust_anchor" "x509-spire" {
count = var.auth-type == "X509" ? 1 : 0
name = "spire-root-ca"
enabled = true
source {
source_data {
x509_certificate_data = var.root-CA
}
source_type = "CERTIFICATE_BUNDLE"
}
}

resource "aws_rolesanywhere_profile" "x509-spire" {
count = var.auth-type == "X509" ? 1 : 0
name = "spire-x509-profile"
enabled = true
role_arns = [aws_iam_role.otterize-credentials-operator.arn]
}

resource "aws_iam_role" "x509-spire-role" {
count = var.auth-type == "X509" ? 1 : 0
name = "demo-spiffe-role-x509"

assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = ["sts:AssumeRole", "sts:TagSession", "sts:SetSourceIdentity"]
Effect = "Allow",
Principal = {
Service = "rolesanywhere.amazonaws.com",
},
Condition = {
StringLike = {
"aws:PrincipalTag/x509SAN/URI" = "${var.spiffe-id}",
}
ArnEquals = {
"aws:SourceArn" = aws_rolesanywhere_trust_anchor.x509-spire.arn
}
}
},
],
})
}

0 comments on commit 5c4adfa

Please sign in to comment.