Skip to content

Commit c470009

Browse files
fix(vault-oidc): time conversion fix
1 parent adb31ec commit c470009

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

modules/vault-oidc/example/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@ module "gitlab" {
124124
client_secret = var.gitlab_client_secret
125125
default_token_policies = [vault_policy.default_user_policy.id]
126126
scopes = ["profile", "email"]
127-
max_lease_ttl = "12h"
128-
default_lease_ttl = "2h"
127+
max_ttl = 12 * 3600
128+
default_ttl = 2 * 3600
129129
}

modules/vault-oidc/main.tf

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@
77
* authorizing via GitLab or Google account.
88
*/
99

10+
locals {
11+
time_units = ["d", "h", "m", "s"]
12+
13+
max_ttl_s = var.max_ttl % 60
14+
max_ttl_m = ((var.max_ttl - local.max_ttl_s) / 60) % 60
15+
max_ttl_h = ((var.max_ttl - local.max_ttl_m * 60 - local.max_ttl_s) / 3600) % 24
16+
max_ttl_d = ((var.max_ttl - local.max_ttl_h * 3600 - local.max_ttl_m * 60 - local.max_ttl_s) / 24 * 3600)
17+
max_ttl_parts = [local.max_ttl_d, local.max_ttl_h, local.max_ttl_m, local.max_ttl_s]
18+
max_ttl = join("", [
19+
for i in range(length(local.time_units)) : (local.max_ttl_parts[i] > 0 ? "${local.max_ttl_parts[i]}${local.time_units[i]}" : "")
20+
])
21+
22+
default_ttl_s = var.default_ttl % 60
23+
default_ttl_m = ((var.default_ttl - local.default_ttl_s) / 60) % 60
24+
default_ttl_h = ((var.default_ttl - local.default_ttl_m * 60 - local.default_ttl_s) / 3600) % 24
25+
default_ttl_d = ((var.default_ttl - local.default_ttl_h * 3600 - local.default_ttl_m * 60 - local.default_ttl_s) / 24 * 3600)
26+
default_ttl_parts = [local.default_ttl_d, local.default_ttl_h, local.default_ttl_m, local.default_ttl_s]
27+
default_ttl = join("", [
28+
for i in range(length(local.time_units)) : (local.default_ttl_parts[i] > 0 ? "${local.default_ttl_parts[i]}${local.time_units[i]}" : "")
29+
])
30+
}
31+
1032
resource "vault_jwt_auth_backend" "this" {
1133
description = var.description
1234
path = var.path
@@ -19,8 +41,9 @@ resource "vault_jwt_auth_backend" "this" {
1941

2042
tune {
2143
listing_visibility = "unauth"
22-
default_lease_ttl = var.default_lease_ttl
23-
max_lease_ttl = var.max_lease_ttl
44+
default_lease_ttl = local.default_ttl
45+
max_lease_ttl = local.max_ttl
46+
token_type = "default-service"
2447
}
2548

2649
lifecycle {
@@ -43,9 +66,9 @@ resource "vault_jwt_auth_backend_role" "this" {
4366
token_policies = var.default_token_policies
4467
oidc_scopes = var.scopes
4568

46-
token_ttl = var.default_lease_ttl
47-
token_max_ttl = var.max_lease_ttl
48-
token_explicit_max_ttl = var.max_lease_ttl
69+
token_ttl = var.default_ttl
70+
token_max_ttl = var.max_ttl
71+
token_explicit_max_ttl = var.max_ttl
4972

5073
bound_audiences = [vault_jwt_auth_backend.this.oidc_client_id]
5174
allowed_redirect_uris = concat(local.vault_addresses, ["http://localhost:8250/oidc/callback"])

modules/vault-oidc/variables.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ variable "default_token_policies" {
2828
description = "Default policy for everyone that's authorized using this method. I.e. this policies may allow access to cubbyhole and utilities."
2929
}
3030

31-
variable "default_lease_ttl" {
32-
type = string
33-
default = "12h"
34-
description = "Default Time-To-Live (in time unit format, i.e. 20m or 10h) for Vault tokens generated by this method. It should be set to a time comfortable for all users, yet still short enough to be safe in case of breach. It may be shorter than `max_lease_ttl`, as lease can be renewed."
31+
variable "default_ttl" {
32+
type = number
33+
default = 12 * 60 * 60
34+
description = "Default Time-To-Live (in seconds) for Vault tokens generated by this method. It should be set to a time comfortable for all users, yet still short enough to be safe in case of breach. It may be shorter than `max_lease_ttl`, as lease can be renewed."
3535
}
3636

37-
variable "max_lease_ttl" {
38-
type = string
39-
default = "12h"
40-
description = "Maximum Time-To-Live (in time unit format, i.e. 20m or 10h) for Vault tokens generated by this method. It should be set to a time comfortable for all users, yet still short enough to be safe in case of breach. After this time passes, user needs to authenticate again."
37+
variable "max_ttl" {
38+
type = number
39+
default = 12 * 60 * 60
40+
description = "Maximum Time-To-Live (in seconds) for Vault tokens generated by this method. It should be set to a time comfortable for all users, yet still short enough to be safe in case of breach. After this time passes, user needs to authenticate again."
4141
}
4242

4343
variable "scopes" {

scripts/tf-exec.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ TF_VERSION="${TF_VERSION:-13}"
1010

1111
terraform="$(realpath "./bin/terraform-$TF_VERSION")"
1212

13-
echo "Using Terraform $TF_VERSION binary at $terraform: $(terraform -v)"
13+
echo "Using Terraform $TF_VERSION binary at $terraform: $($terraform -v)"
1414

1515
function retry_with_log() {
1616
$@ &>/dev/null || $@

0 commit comments

Comments
 (0)