Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extra annotations to customize path of injected token and permissions of the token file sink on vault #650

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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