Skip to content

Commit 67990d4

Browse files
authored
(MINOR) Feature: Optional Vault Namespace (#16)
* auth with namespace only if one is provided, no longer required var * update readme with optional var, fixed broken link
1 parent 8acfba2 commit 67990d4

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The Nutanix Exporter is a Go application that fetches live data from any number
2020
- Hashicorp Vault server with KVv2 Secrets Engine enabled
2121
- Secrets Engine name: defined in `VAULT_ENGINE_NAME` environment variable
2222
- Secret name: defined in `PE_TASK_ACCOUNT` and `PC_TASK_ACCOUNT` environment variables
23+
- Namespace: Optional, but can be defined in `VAULT_NAMESPACE` environment variable
2324
- Fields: username, secret
2425
- Nutanix Prism Central 2023.4 or later
2526

@@ -115,7 +116,7 @@ services:
115116

116117
- [Go](https://golang.org/) - Programming language
117118
- [Go Prometheus Client](https://github.com/prometheus/client_golang) - Prometheus client library for Go
118-
- [Go Hashicorp Vault Client](github.com/hashicorp/vault-client-go) - Hashicorp Vault client library for Go
119+
- [Go Hashicorp Vault Client](https://github.com/hashicorp/vault-client-go) - Hashicorp Vault client library for Go
119120
- [Docker](https://www.docker.com/) - Containerization
120121
- [GitHub Actions](https://docs.github.com/en/actions) - CI/CD pipeline
121122

internal/auth/vault.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func NewVaultClient() (*VaultClient, error) {
6060
addr := getEnvOrFatal("VAULT_ADDR")
6161
roleId := getEnvOrFatal("VAULT_ROLE_ID")
6262
secretId := getEnvOrFatal("VAULT_SECRET_ID")
63-
namespace := getEnvOrFatal("VAULT_NAMESPACE")
6463
PETaskAccount = getEnvOrFatal("PE_TASK_ACCOUNT")
6564
PCTaskAccount = getEnvOrFatal("PC_TASK_ACCOUNT")
6665
EngineName = getEnvOrFatal("VAULT_ENGINE_NAME")
66+
namespace := os.Getenv("VAULT_NAMESPACE")
6767

6868
log.Printf("Creating new Vault client for %s", addr)
6969
client, err := vault.New(
@@ -75,14 +75,28 @@ func NewVaultClient() (*VaultClient, error) {
7575
}
7676

7777
log.Printf("Authenticating with Vault using AppRole")
78-
resp, err := client.Auth.AppRoleLogin(
79-
ctx,
80-
schema.AppRoleLoginRequest{
81-
RoleId: roleId,
82-
SecretId: secretId,
83-
},
84-
vault.WithNamespace(namespace),
85-
)
78+
var resp *vault.Response[map[string]interface{}]
79+
80+
if namespace != "" {
81+
82+
resp, err = client.Auth.AppRoleLogin(
83+
ctx,
84+
schema.AppRoleLoginRequest{
85+
RoleId: roleId,
86+
SecretId: secretId,
87+
},
88+
vault.WithNamespace(namespace),
89+
)
90+
} else {
91+
resp, err = client.Auth.AppRoleLogin(
92+
ctx,
93+
schema.AppRoleLoginRequest{
94+
RoleId: roleId,
95+
SecretId: secretId,
96+
},
97+
)
98+
}
99+
86100
if err != nil {
87101
log.Fatal(err)
88102
}
@@ -92,8 +106,13 @@ func NewVaultClient() (*VaultClient, error) {
92106
log.Fatal(err)
93107
}
94108

95-
if err = client.SetNamespace(namespace); err != nil {
96-
log.Fatal(err)
109+
if namespace != "" {
110+
log.Printf("Setting namespace to %s", namespace)
111+
if err = client.SetNamespace(namespace); err != nil {
112+
log.Fatal(err)
113+
}
114+
} else {
115+
log.Printf("No namespace specified")
97116
}
98117

99118
return &VaultClient{client: client}, nil

0 commit comments

Comments
 (0)