Skip to content

Commit

Permalink
vault: fix lookups of default cluster across upgrades (hashicorp#18940)
Browse files Browse the repository at this point in the history
Allocations that were created before Nomad 1.7 will not have the `cluster` field
set for their Vault blocks. While this can be corrected server-side, that
doesn't help allocations already on clients.

Also add extra safety on Consul cluster lookup too
  • Loading branch information
tgross authored and nvanthao committed Mar 1, 2024
1 parent ee01997 commit aba9a18
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions client/allocrunner/taskrunner/template_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,22 @@ func (h *templateHook) Prestart(ctx context.Context, req *interfaces.TaskPrestar
consulTokens := h.config.hookResources.GetConsulTokens()

var found bool
if _, found = consulTokens[req.Task.Consul.Cluster]; !found {
cluster := req.Task.Consul.Cluster
if cluster == "" {
cluster = structs.ConsulDefaultCluster
}
if _, found = consulTokens[cluster]; !found {
return fmt.Errorf(
"consul tokens for cluster %s requested by task %s not found",
req.Task.Consul.Cluster, req.Task.Name,
cluster, req.Task.Name,
)
}

h.consulToken, found = consulTokens[req.Task.Consul.Cluster][req.Task.Consul.IdentityName()]
h.consulToken, found = consulTokens[cluster][req.Task.Consul.IdentityName()]
if !found {
return fmt.Errorf(
"consul tokens for cluster %s and identity %s requested by task %s not found",
req.Task.Consul.Cluster, req.Task.Consul.IdentityName(), req.Task.Name,
cluster, req.Task.Consul.IdentityName(), req.Task.Name,
)
}
}
Expand Down Expand Up @@ -193,6 +197,9 @@ func (h *templateHook) newManager() (unblock chan struct{}, err error) {
var vaultConfig *structsc.VaultConfig
if h.task.Vault != nil {
vaultCluster := h.task.Vault.Cluster
if vaultCluster == "" {
vaultCluster = structs.VaultDefaultCluster
}
vaultConfig = h.config.clientConfig.GetVaultConfigs(h.logger)[vaultCluster]

if vaultConfig == nil {
Expand Down
3 changes: 3 additions & 0 deletions client/allocrunner/taskrunner/vault_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func (h *vaultHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRe
}

cluster := h.vaultBlock.Cluster
if cluster == "" {
cluster = structs.VaultDefaultCluster
}
vclient, err := h.clientFunc(cluster)
if err != nil {
return err
Expand Down

0 comments on commit aba9a18

Please sign in to comment.