@@ -11,6 +11,18 @@ import (
1111)
1212
1313// VaultManager implements secret management using HashiCorp Vault
14+ const (
15+ slash = "/"
16+ valueKey = "value"
17+ dataKey = "data"
18+ vaultAddrKey = "VAULT_ADDR"
19+ vaultTokenFileKey = "VAULT_TOKEN_FILE"
20+ vaultTokenKey = "VAULT_TOKEN"
21+ vaultSecretPathKey = "VAULT_SECRET_PATH"
22+ vaultK8sRoleKey = "VAULT_K8S_ROLE"
23+ serviceAccountPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
24+ )
25+
1426type VaultManager struct {
1527 client * api.Client
1628 prefix string
@@ -21,12 +33,12 @@ func NewVaultManager(cfg Config) (*VaultManager, error) {
2133 vaultConfig := api .DefaultConfig ()
2234
2335 // Set Vault address
24- if addr := cfg .Extra ["VAULT_ADDR" ]; addr != "" {
36+ if addr := cfg .Extra [vaultAddrKey ]; addr != emptyString {
2537 vaultConfig .Address = addr
26- } else if addr := os .Getenv ("VAULT_ADDR" ); addr != "" {
38+ } else if addr := os .Getenv (vaultAddrKey ); addr != emptyString {
2739 vaultConfig .Address = addr
2840 } else {
29- return nil , fmt .Errorf ("VAULT_ADDR is required" )
41+ return nil , fmt .Errorf ("%s is required" , vaultAddrKey )
3042 }
3143
3244 client , err := api .NewClient (vaultConfig )
@@ -41,14 +53,14 @@ func NewVaultManager(cfg Config) (*VaultManager, error) {
4153
4254 return & VaultManager {
4355 client : client ,
44- prefix : cfg .Extra ["VAULT_SECRET_PATH" ],
56+ prefix : cfg .Extra [vaultSecretPathKey ],
4557 }, nil
4658}
4759
4860// authenticateVault handles Vault authentication using various methods
4961func authenticateVault (client * api.Client , cfg Config ) error {
5062 // Try token file first
51- if tokenFile := cfg .Extra ["VAULT_TOKEN_FILE" ]; tokenFile != "" {
63+ if tokenFile := cfg .Extra [vaultTokenFileKey ]; tokenFile != emptyString {
5264 token , err := readSecretFile (tokenFile )
5365 if err != nil {
5466 return fmt .Errorf ("failed to read token file %s: %w" , tokenFile , err )
@@ -58,21 +70,21 @@ func authenticateVault(client *api.Client, cfg Config) error {
5870 }
5971
6072 // Try direct token
61- if token := cfg .Extra ["VAULT_TOKEN" ]; token != "" {
73+ if token := cfg .Extra [vaultTokenKey ]; token != emptyString {
6274 client .SetToken (token )
6375 return nil
6476 }
6577
6678 // Try environment token
67- if token := os .Getenv ("VAULT_TOKEN" ); token != "" {
79+ if token := os .Getenv (vaultTokenKey ); token != emptyString {
6880 client .SetToken (token )
6981 return nil
7082 }
7183
7284 // Try Kubernetes authentication
73- if serviceAccountTokenFile := "/var/run/secrets/kubernetes.io/serviceaccount/token" ; fileExists (serviceAccountTokenFile ) {
74- if role := cfg .Extra ["VAULT_K8S_ROLE" ]; role != "" {
75- return authenticateKubernetes (client , serviceAccountTokenFile , role )
85+ if fileExists (serviceAccountPath ) {
86+ if role := cfg .Extra [vaultK8sRoleKey ]; role != emptyString {
87+ return authenticateKubernetes (client , serviceAccountPath , role )
7688 }
7789 }
7890
@@ -105,7 +117,7 @@ func authenticateKubernetes(client *api.Client, tokenFile, role string) error {
105117}
106118
107119// GetSecret retrieves a secret from Vault
108- func (m * VaultManager ) GetSecret (ctx context.Context , key string ) (string , error ) {
120+ func (m * VaultManager ) GetSecret (_ context.Context , key string ) (string , error ) {
109121 secretPath := m .buildSecretPath (key )
110122
111123 secret , err := m .client .Logical ().Read (secretPath )
@@ -119,12 +131,12 @@ func (m *VaultManager) GetSecret(ctx context.Context, key string) (string, error
119131
120132 // Handle KV v2 (data wrapper)
121133 data := secret .Data
122- if dataMap , ok := data ["data" ].(map [string ]interface {}); ok {
134+ if dataMap , ok := data [dataKey ].(map [string ]interface {}); ok {
123135 data = dataMap
124136 }
125137
126138 // Get the value
127- value , ok := data ["value" ]
139+ value , ok := data [valueKey ]
128140 if ! ok {
129141 // Try the key name itself
130142 value , ok = data [filepath .Base (key )]
@@ -161,13 +173,13 @@ func (m *VaultManager) SetSecret(ctx context.Context, key, value string) error {
161173 secretPath := m .buildSecretPath (key )
162174
163175 data := map [string ]interface {}{
164- "value" : value ,
176+ valueKey : value ,
165177 }
166178
167179 // Handle KV v2 (data wrapper)
168180 if m .isKVv2 () {
169181 data = map [string ]interface {}{
170- "data" : data ,
182+ dataKey : data ,
171183 }
172184 }
173185
@@ -181,11 +193,11 @@ func (m *VaultManager) SetSecret(ctx context.Context, key, value string) error {
181193
182194// buildSecretPath constructs the full secret path with prefix
183195func (m * VaultManager ) buildSecretPath (key string ) string {
184- if m .prefix == "" {
196+ if m .prefix == emptyString {
185197 return key
186198 }
187199
188- return strings .TrimSuffix (m .prefix , "/" ) + "/" + strings .TrimPrefix (key , "/" )
200+ return strings .TrimSuffix (m .prefix , slash ) + slash + strings .TrimPrefix (key , slash )
189201}
190202
191203// isKVv2 checks if we're using KV secrets engine v2
0 commit comments