Skip to content

Commit

Permalink
feat: add extra annotations to customize path of injected token and p…
Browse files Browse the repository at this point in the history
…ermissions of the token file sink on vault

adds:
* `vault.hashicorp.com/agent-inject-token-file` to specify a different
  path other than `${vault_volume_name}/token`
* `vault.hashicorp.com/agent-inject-token-perms` to specify a different
  permission for the token file, other than the current default, 0640
  • Loading branch information
srevinsaju committed Jun 1, 2024
1 parent 6750a2e commit 9db0d8a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
10 changes: 9 additions & 1 deletion agent-inject/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
DefaultAgentCacheExitOnErr = false
DefaultAgentUseLeaderElector = false
DefaultAgentInjectToken = false
DefaultAgentInjectTokenFile = "token"
DefaultAgentInjectTokenPermissions = 0640
DefaultTemplateConfigExitOnRetryFailure = true
DefaultServiceAccountMount = "/var/run/secrets/vault.hashicorp.com/serviceaccount"
DefaultEnableQuit = false
Expand Down Expand Up @@ -169,6 +171,12 @@ type Agent struct {
// secrets volume (e.g. /vault/secrets/token)
InjectToken bool

// InjectTokenFile is the file path where the auto-auth token is injected
InjectTokenFile string

// InjectTokenPermissions is the file permissions for the auto-auth token file
InjectTokenPermissions int64

// EnableQuit controls whether the quit endpoint is enabled on a localhost
// listener
EnableQuit bool
Expand Down Expand Up @@ -497,7 +505,7 @@ func New(pod *corev1.Pod) (*Agent, error) {
return agent, fmt.Errorf("invalid default template type: %s", agent.DefaultTemplate)
}

agent.InjectToken, err = agent.injectToken()
err = agent.injectToken()
if err != nil {
return agent, err
}
Expand Down
43 changes: 40 additions & 3 deletions agent-inject/agent/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ const (
// auto-auth token into the secrets volume (e.g. /vault/secrets/token)
AnnotationAgentInjectToken = "vault.hashicorp.com/agent-inject-token"

// AnnotationAgentInjectTokenFile is the annotation key for specifying the
// path to which the auto-auth token should be written to disk.
// (defaults to 'token')
AnnotationAgentInjectTokenFile = "vault.hashicorp.com/agent-inject-token-file"

// AnnotationAgentInjectTokenPermission is the annotation key for specifying the
// permission of the token file written to disk. (defaults to '0640')
AnnotationAgentInjectTokenPermission = "vault.hashicorp.com/agent-inject-token-perms"

// AnnotationAgentInjectCommand is the key annotation that configures Vault Agent
// to run a command after the secret is rendered. The name of the template is any
// unique string after "vault.hashicorp.com/agent-inject-command-". This should map
Expand Down Expand Up @@ -880,12 +889,40 @@ func (a *Agent) cacheExitOnErr() (bool, error) {
return parseutil.ParseBool(raw)
}

func (a *Agent) injectToken() (bool, error) {
func (a *Agent) injectToken() error {
raw, ok := a.Annotations[AnnotationAgentInjectToken]
if !ok {
return DefaultAgentInjectToken, nil
a.InjectToken = DefaultAgentInjectToken
return nil
}
return parseutil.ParseBool(raw)
injectToken, err := parseutil.ParseBool(raw)
if err != nil {
return err
}
a.InjectToken = injectToken

raw, ok = a.Annotations[AnnotationAgentInjectTokenFile]
if !ok {
a.InjectTokenFile = DefaultAgentInjectTokenFile
return nil
}
injectTokenPath, err := parseutil.ParseString(raw)
if err != nil {
return err
}
a.InjectTokenFile = injectTokenPath

raw, ok = a.Annotations[AnnotationAgentInjectTokenPermission]
if !ok {
a.InjectTokenPermissions = DefaultAgentInjectTokenPermissions
return nil
}
injectTokenPermissions, err := parseutil.ParseInt(raw)
if err != nil {
return err
}
a.InjectTokenPermissions = injectTokenPermissions
return nil
}

// telemetryConfig accumulates the agent-telemetry annotations into a map which is
Expand Down
3 changes: 2 additions & 1 deletion agent-inject/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func (a *Agent) newConfig(init bool) ([]byte, error) {
config.AutoAuth.Sinks = append(config.AutoAuth.Sinks, &Sink{
Type: "file",
Config: map[string]interface{}{
"path": path.Join(a.Annotations[AnnotationVaultSecretVolumePath], "token"),
"path": path.Join(a.Annotations[AnnotationVaultSecretVolumePath], a.InjectTokenFile),
"mode": a.InjectTokenPermissions,
},
})
}
Expand Down

0 comments on commit 9db0d8a

Please sign in to comment.