-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: fix identity config for Consul service (#18363)
Rename the agent configuraion for workload identity to `WorkloadIdentityConfig` to make its use more explicit and remove the `ServiceName` field since it is never expected to be defined in a configuration file. Also update the job mutation to inject a service identity following these rules: 1. Don't inject identity if `consul.use_identity` is false. 2. Don't inject identity if `consul.service_identity` is not specified. 3. Don't inject identity if service provider is not `consul`. 4. Set name and service name if the service specifies an identity. 5. Inject `consul.service_identity` if service does not specify an identity.
- Loading branch information
Showing
15 changed files
with
904 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package nomad | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/nomad/nomad/structs" | ||
) | ||
|
||
var ( | ||
consulServiceIdentityNamePrefix = "consul-service" | ||
) | ||
|
||
// jobImplicitIdentitiesHook adds implicit `identity` blocks for external | ||
// services, like Consul and Vault. | ||
type jobImplicitIdentitiesHook struct { | ||
srv *Server | ||
} | ||
|
||
func (jobImplicitIdentitiesHook) Name() string { | ||
return "implicit-identities" | ||
} | ||
|
||
func (h jobImplicitIdentitiesHook) Mutate(job *structs.Job) (*structs.Job, []error, error) { | ||
for _, tg := range job.TaskGroups { | ||
for _, s := range tg.Services { | ||
h.handleConsulService(s) | ||
} | ||
|
||
for _, t := range tg.Tasks { | ||
for _, s := range t.Services { | ||
h.handleConsulService(s) | ||
} | ||
} | ||
} | ||
|
||
return job, nil, nil | ||
} | ||
|
||
// handleConsulService injects a workload identity to the service if: | ||
// 1. The service uses the Consul provider. | ||
// 2. The server is configured with `consul.use_identity = true` and a | ||
// `consul.service_identity` is provided. | ||
// | ||
// If the service already has an identity it sets the identity name and service | ||
// name values. | ||
func (h jobImplicitIdentitiesHook) handleConsulService(s *structs.Service) { | ||
if !h.srv.config.UseConsulIdentity() { | ||
return | ||
} | ||
|
||
if s.Provider != "" && s.Provider != "consul" { | ||
return | ||
} | ||
|
||
// Use the identity specified in the service. | ||
serviceWID := s.Identity | ||
if serviceWID == nil { | ||
// If the service doesn't specify an identity, fallback to the service | ||
// identity defined in the server configuration. | ||
serviceWID = h.srv.config.ConsulServiceIdentity() | ||
if serviceWID == nil { | ||
// If no identity is found, skip injecting the implicit identity | ||
// and fallback to the legacy flow. | ||
return | ||
} | ||
} | ||
|
||
// Set the expected identity name and service name. | ||
serviceWID.Name = fmt.Sprintf("%s/%s", consulServiceIdentityNamePrefix, s.Name) | ||
serviceWID.ServiceName = s.Name | ||
|
||
s.Identity = serviceWID | ||
} |
Oops, something went wrong.