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

client: consul hook not called for templates #19490

Merged
merged 10 commits into from
Dec 15, 2023
3 changes: 3 additions & 0 deletions .changelog/19490.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
client: Fixed a bug where where the environment variable / file for the Consul token weren't written.
```
27 changes: 16 additions & 11 deletions client/allocrunner/taskrunner/consul_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import (
)

const (
// consulTokenFilePrefix is the begging of the name of the file holding the
// Consul SI token inside the task's secret directory. Full name of the file is
// always consulTokenFilePrefix_identityName
consulTokenFilePrefix = "nomad_consul"
// consulTokenFilename is the name of the file holding the Consul SI token
// inside the task's secret directory.
consulTokenFilename = "consul_token"

// consulTokenFilePerms is the level of file permissions granted on the file in
// the secrets directory for the task
Expand All @@ -32,14 +31,15 @@ type consulHook struct {
task *structs.Task
tokenDir string
hookResources *cstructs.AllocHookResources
logger log.Logger

logger log.Logger
}

func newConsulHook(logger log.Logger, tr *TaskRunner, hookResources *cstructs.AllocHookResources) *consulHook {
func newConsulHook(logger log.Logger, tr *TaskRunner) *consulHook {
h := &consulHook{
task: tr.Task(),
tokenDir: tr.taskDir.SecretsDir,
hookResources: hookResources,
hookResources: tr.allocHookResources,
}
h.logger = logger.Named(h.Name())
return h
Expand All @@ -49,13 +49,13 @@ func (*consulHook) Name() string {
return "consul_task"
}

func (h *consulHook) Prestart(context.Context, *interfaces.TaskPrestartRequest, *interfaces.TaskPrestartResponse) error {
func (h *consulHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, resp *interfaces.TaskPrestartResponse) error {
mErr := multierror.Error{}

tokens := h.hookResources.GetConsulTokens()

// Write tokens to tasks' secret dirs
for cluster, t := range tokens {
for _, t := range tokens {
for identity, token := range t {
// do not write tokens that do not belong to any of this task's
// identities
Expand All @@ -66,11 +66,16 @@ func (h *consulHook) Prestart(context.Context, *interfaces.TaskPrestartRequest,
continue
}

filename := fmt.Sprintf("%s_%s_%s", consulTokenFilePrefix, cluster, identity)
tokenPath := filepath.Join(h.tokenDir, filename)
tokenPath := filepath.Join(h.tokenDir, consulTokenFilename)
if err := os.WriteFile(tokenPath, []byte(token.SecretID), consulTokenFilePerms); err != nil {
mErr.Errors = append(mErr.Errors, fmt.Errorf("failed to write Consul SI token: %w", err))
}

env := map[string]string{
"CONSUL_TOKEN": token.SecretID,
}

resp.Env = env
}
}

Expand Down
4 changes: 4 additions & 0 deletions client/allocrunner/taskrunner/task_runner_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (tr *TaskRunner) initHooks() {
// Get the consul namespace for the TG of the allocation.
consulNamespace := tr.alloc.ConsulNamespaceForTask(tr.taskName)

// Add the consul hook (populates task secret dirs and sets the environment if
// consul tokens are present for the task).
tr.runnerHooks = append(tr.runnerHooks, newConsulHook(hookLogger, tr))

// If there are templates is enabled, add the hook
if len(task.Templates) != 0 {
tr.runnerHooks = append(tr.runnerHooks, newTemplateHook(&templateHookConfig{
Expand Down