Skip to content

Commit 35155d5

Browse files
authored
Chore: Refactor Authen (#7)
* Update template * migrate policy * update template * add example * update ci * remove validate
1 parent 1e543d5 commit 35155d5

10 files changed

Lines changed: 31 additions & 34 deletions

File tree

.github/workflows/lint.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ jobs:
1111
- uses: actions/checkout@v3
1212
name: Checkout source code
1313

14+
- uses: hashicorp/setup-terraform@v3
15+
1416
- name: init
1517
run: terraform init
1618

1719
- name: fmt
1820
run: terraform fmt -recursive -check
1921

20-
- name: validate
21-
run: terraform validate
22-
env:
23-
VAULT_ADDR: https://example.com
2422
tflint:
2523
runs-on: ubuntu-latest
2624
steps:

authentication/policy.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "vault_policy" "policies" {
2+
name = split(".", each.value)[0]
3+
policy = file("${path.module}/templates/policy/${each.value}")
4+
for_each = fileset("${path.module}/templates/policy/", "*.hcl")
5+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"policies": [%{ for i,policy in policies ~}"${policy}"%{if i < length(policies) -1 }, %{ endif }%{~ endfor ~}],
3-
"password": "password"
3+
"password": "${password}"
44
}

policy/templates/admin_policy.hcl renamed to authentication/templates/policy/admin_policy.hcl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,11 @@ path "kv-v2/*"
7474
path "sys/health"
7575
{
7676
capabilities = ["read", "sudo"]
77-
}
77+
}
78+
79+
# Update Certs
80+
path "certs_int/*"
81+
{
82+
capabilities = ["create", "read", "update", "delete", "list", "sudo"]
83+
}
84+
File renamed without changes.
File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"username": "viewer",
4+
"password": "password123",
5+
"policies": ["view_policy"]
6+
}
7+
]

authentication/userpass.tf

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
1+
locals {
2+
users = jsondecode(file("${path.module}/templates/users.json"))
3+
}
4+
15
resource "vault_auth_backend" "userpass" {
26
type = "userpass"
37
path = var.path
48
description = "Main userpass authentication"
59
}
610

7-
resource "vault_generic_endpoint" "devops_user" {
11+
resource "vault_generic_endpoint" "users" {
12+
for_each = { for user in local.users : user.username => user }
813
depends_on = [vault_auth_backend.userpass]
9-
path = "auth/userpass/users/devops_user"
14+
path = "auth/userpass/users/${each.value.username}"
1015
ignore_absent_fields = true
1116

12-
data_json = templatefile("${path.module}/templates/default_template.tftpl", { policies = sort(["default", "admin_policy"]) })
17+
data_json = templatefile("${path.module}/templates/default_template.tftpl", { policies = sort(each.value.policies), password = each.value.password })
1318
}
14-
15-
resource "vault_generic_endpoint" "root_user" {
16-
depends_on = [vault_auth_backend.userpass]
17-
path = "auth/userpass/users/root"
18-
ignore_absent_fields = true
19-
20-
data_json = templatefile("${path.module}/templates/default_template.tftpl", { policies = sort(["default", "root_policy"]) })
21-
}
22-
23-
resource "vault_generic_endpoint" "view_user" {
24-
depends_on = [vault_auth_backend.userpass]
25-
path = "auth/userpass/users/viewer"
26-
ignore_absent_fields = true
27-
28-
data_json = templatefile("${path.module}/templates/default_template.tftpl", { policies = sort(["default", "view_policy"]) })
29-
}

main.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
module "policy" {
2-
source = "./policy"
3-
}
4-
51
module "authentication" {
62
source = "./authentication"
73
path = "userpass"

policy/main.tf

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

0 commit comments

Comments
 (0)