Skip to content

Commit

Permalink
require an env interpolation function for service.IdentityHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Apr 11, 2024
1 parent de3943b commit 825efce
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions client/allocrunner/consul_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ func (h *consulHook) prepareConsulTokensForServices(services []*structs.Service,
}

// Find signed identity workload.
identity := taskenv.InterpolateWIHandle(env, *service.IdentityHandle())
jwt, err := h.widmgr.Get(identity)
handle := *service.IdentityHandle(env.ReplaceEnv)
jwt, err := h.widmgr.Get(handle)
if err != nil {
mErr = multierror.Append(mErr, fmt.Errorf(
"error getting signed identity for service %s: %v",
Expand All @@ -196,7 +196,7 @@ func (h *consulHook) prepareConsulTokensForServices(services []*structs.Service,
JWT: jwt.JWT,
AuthMethodName: consulConfig.ServiceIdentityAuthMethod,
Meta: map[string]string{
"requested_by": fmt.Sprintf("nomad_service_%s", identity.InterpolatedWorkloadIdentifier),
"requested_by": fmt.Sprintf("nomad_service_%s", handle.InterpolatedWorkloadIdentifier),
},
}
token, err := h.getConsulToken(clusterName, req)
Expand Down
2 changes: 1 addition & 1 deletion client/allocrunner/consul_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func Test_consulHook_prepareConsulTokensForServices(t *testing.T) {
hashedJWT := make(map[string]string)

for _, s := range services {
widHandle := taskenv.InterpolateWIHandle(env, *s.IdentityHandle())
widHandle := *s.IdentityHandle(env.ReplaceEnv)
jwt, err := hook.widmgr.Get(widHandle)
must.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions client/widmgr/widmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewWIDMgr(signer IdentitySigner, a *structs.Allocation, db cstate.StateDB,

for _, service := range tg.Services {
if service.Identity != nil {
handle := taskenv.InterpolateWIHandle(allocEnv, *service.IdentityHandle())
handle := *service.IdentityHandle(allocEnv.ReplaceEnv)
widspecs[handle] = service.Identity
}
}
Expand All @@ -77,7 +77,7 @@ func NewWIDMgr(signer IdentitySigner, a *structs.Allocation, db cstate.StateDB,
taskEnv := envBuilder.UpdateTask(a, task).Build()
for _, service := range task.Services {
if service.Identity != nil {
handle := taskenv.InterpolateWIHandle(taskEnv, *service.IdentityHandle())
handle := *service.IdentityHandle(taskEnv.ReplaceEnv)
widspecs[handle] = service.Identity
}
}
Expand Down
5 changes: 1 addition & 4 deletions client/widmgr/widmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ func TestWIDMgr_Restore(t *testing.T) {
widSpecs[2].TTL = time.Second
signer.setWIDs(widSpecs)

wiHandle := service.IdentityHandle()
wiHandle.InterpolatedWorkloadIdentifier = envBuilder.Build().ReplaceEnv(
wiHandle.WorkloadIdentifier)

wiHandle := service.IdentityHandle(envBuilder.Build().ReplaceEnv)
mgr.widSpecs[*wiHandle].TTL = time.Second

// force a re-sign to re-populate the lastToken and save to the db
Expand Down
4 changes: 2 additions & 2 deletions nomad/alloc_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,13 @@ func (a *Alloc) signServices(
// services can be on the level of task groups or tasks
for _, tg := range job.TaskGroups {
for _, service := range tg.Services {
if service.IdentityHandle().Equal(wid) {
if service.IdentityHandle(nil).Equal(wid) {
return true, a.signIdentities(alloc, service.Identity, idReq, reply, now)
}
}
for _, task := range tg.Tasks {
for _, service := range task.Services {
if service.IdentityHandle().Equal(wid) {
if service.IdentityHandle(nil).Equal(wid) {
return true, a.signIdentities(alloc, service.Identity, idReq, reply, now)
}
}
Expand Down
10 changes: 8 additions & 2 deletions nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,15 +802,21 @@ func (s *Service) MakeUniqueIdentityName() string {
return fmt.Sprintf("%s_%v-%v", prefix, s.Name, s.PortLabel)
}

type envReplacer func(string) string

// IdentityHandle returns a WorkloadIdentityHandle which is a pair of service
// identity name and service name.
func (s *Service) IdentityHandle() *WIHandle {
func (s *Service) IdentityHandle(replace envReplacer) *WIHandle {
if s.Identity != nil {
return &WIHandle{
wi := &WIHandle{
IdentityName: s.Identity.Name,
WorkloadIdentifier: s.Name,
WorkloadType: WorkloadTypeService,
}
if replace != nil {
wi.InterpolatedWorkloadIdentifier = replace(s.Name)
}
return wi
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8240,7 +8240,7 @@ func TestNewIdentityClaims(t *testing.T) {
name: path,
group: tg.Name,
wid: s.Identity,
wiHandle: s.IdentityHandle(),
wiHandle: s.IdentityHandle(nil),
expectedClaims: expectedClaims[path],
})
}
Expand Down Expand Up @@ -8269,7 +8269,7 @@ func TestNewIdentityClaims(t *testing.T) {
name: path,
group: tg.Name,
wid: s.Identity,
wiHandle: s.IdentityHandle(),
wiHandle: s.IdentityHandle(nil),
expectedClaims: expectedClaims[path],
})
}
Expand Down

0 comments on commit 825efce

Please sign in to comment.