2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
package autoconf
|
|
|
|
|
|
|
|
import (
|
2020-08-10 17:03:33 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2020-07-28 19:31:48 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbautoconf"
|
|
|
|
"github.com/hashicorp/consul/proto/private/pbconfig"
|
|
|
|
"github.com/hashicorp/consul/proto/private/pbconnect"
|
2022-03-24 19:32:25 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2020-06-10 20:47:35 +00:00
|
|
|
)
|
|
|
|
|
2020-07-23 15:24:20 +00:00
|
|
|
// translateAgentConfig is meant to take in a proto/pbconfig.Config type
|
2020-06-10 20:47:35 +00:00
|
|
|
// and craft the corresponding agent/config.Config type. The need for this function
|
|
|
|
// should eventually be removed with the protobuf and normal version converging.
|
|
|
|
// In the meantime, its not desirable to have the flatter Config struct in protobufs
|
|
|
|
// as in the long term we want a configuration with more nested groupings.
|
|
|
|
//
|
2020-07-23 15:24:20 +00:00
|
|
|
// Why is this function not in the proto/pbconfig package? The answer, that
|
2020-06-10 20:47:35 +00:00
|
|
|
// package cannot import the agent/config package without running into import cycles.
|
2020-08-10 17:03:33 +00:00
|
|
|
func translateConfig(c *pbconfig.Config) config.Config {
|
|
|
|
result := config.Config{
|
2020-08-31 17:12:17 +00:00
|
|
|
Datacenter: stringPtrOrNil(c.Datacenter),
|
|
|
|
PrimaryDatacenter: stringPtrOrNil(c.PrimaryDatacenter),
|
|
|
|
NodeName: stringPtrOrNil(c.NodeName),
|
2021-12-01 19:32:34 +00:00
|
|
|
// only output the SegmentName in the configuration if it's non-empty
|
2020-08-10 17:03:33 +00:00
|
|
|
// this will avoid a warning later when parsing the persisted configuration
|
|
|
|
SegmentName: stringPtrOrNil(c.SegmentName),
|
2021-12-01 19:32:34 +00:00
|
|
|
// only output the Partition in the configuration if it's non-empty
|
|
|
|
// this will avoid a warning later when parsing the persisted configuration
|
|
|
|
Partition: stringPtrOrNil(c.Partition),
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if a := c.AutoEncrypt; a != nil {
|
2020-08-10 17:03:33 +00:00
|
|
|
result.AutoEncrypt = config.AutoEncrypt{
|
|
|
|
TLS: &a.TLS,
|
|
|
|
DNSSAN: a.DNSSAN,
|
|
|
|
IPSAN: a.IPSAN,
|
|
|
|
AllowTLS: &a.AllowTLS,
|
2020-06-26 18:53:07 +00:00
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if a := c.ACL; a != nil {
|
2020-08-10 17:03:33 +00:00
|
|
|
result.ACL = config.ACL{
|
|
|
|
Enabled: &a.Enabled,
|
2020-08-31 17:12:17 +00:00
|
|
|
PolicyTTL: stringPtrOrNil(a.PolicyTTL),
|
|
|
|
RoleTTL: stringPtrOrNil(a.RoleTTL),
|
|
|
|
TokenTTL: stringPtrOrNil(a.TokenTTL),
|
|
|
|
DownPolicy: stringPtrOrNil(a.DownPolicy),
|
|
|
|
DefaultPolicy: stringPtrOrNil(a.DefaultPolicy),
|
2020-08-10 17:03:33 +00:00
|
|
|
EnableKeyListPolicy: &a.EnableKeyListPolicy,
|
|
|
|
EnableTokenPersistence: &a.EnableTokenPersistence,
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if t := c.ACL.Tokens; t != nil {
|
2020-08-10 17:03:33 +00:00
|
|
|
tokens := make([]config.ServiceProviderToken, 0, len(t.ManagedServiceProvider))
|
2020-06-10 20:47:35 +00:00
|
|
|
for _, mspToken := range t.ManagedServiceProvider {
|
2020-08-10 17:03:33 +00:00
|
|
|
tokens = append(tokens, config.ServiceProviderToken{
|
|
|
|
AccessorID: &mspToken.AccessorID,
|
|
|
|
SecretID: &mspToken.SecretID,
|
2020-06-10 20:47:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:03:33 +00:00
|
|
|
result.ACL.Tokens = config.Tokens{
|
2021-12-07 19:59:38 +00:00
|
|
|
InitialManagement: stringPtrOrNil(t.InitialManagement),
|
|
|
|
AgentRecovery: stringPtrOrNil(t.AgentRecovery),
|
2020-08-10 17:03:33 +00:00
|
|
|
Replication: stringPtrOrNil(t.Replication),
|
|
|
|
Default: stringPtrOrNil(t.Default),
|
|
|
|
Agent: stringPtrOrNil(t.Agent),
|
|
|
|
ManagedServiceProvider: tokens,
|
2020-06-26 18:53:07 +00:00
|
|
|
}
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if g := c.Gossip; g != nil {
|
2020-08-10 17:03:33 +00:00
|
|
|
result.RetryJoinLAN = g.RetryJoinLAN
|
2020-06-10 20:47:35 +00:00
|
|
|
|
|
|
|
if e := c.Gossip.Encryption; e != nil {
|
2020-08-31 17:12:17 +00:00
|
|
|
result.EncryptKey = stringPtrOrNil(e.Key)
|
2020-08-10 17:03:33 +00:00
|
|
|
result.EncryptVerifyIncoming = &e.VerifyIncoming
|
|
|
|
result.EncryptVerifyOutgoing = &e.VerifyOutgoing
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t := c.TLS; t != nil {
|
2022-03-18 10:46:58 +00:00
|
|
|
result.TLS.Defaults = config.TLSProtocolConfig{
|
|
|
|
VerifyOutgoing: &t.VerifyOutgoing,
|
|
|
|
TLSCipherSuites: stringPtrOrNil(t.CipherSuites),
|
|
|
|
}
|
2022-03-24 19:32:25 +00:00
|
|
|
|
|
|
|
// NOTE: This inner check for deprecated values should eventually be
|
|
|
|
// removed, and possibly replaced with a versioning scheme for autoconfig
|
|
|
|
// or a proper integration with the deprecated config handling in
|
|
|
|
// agent/config/deprecated.go
|
|
|
|
if v, ok := types.DeprecatedConsulAgentTLSVersions[t.MinVersion]; ok {
|
|
|
|
result.TLS.Defaults.TLSMinVersion = stringPtrOrNil(v.String())
|
|
|
|
} else {
|
|
|
|
result.TLS.Defaults.TLSMinVersion = stringPtrOrNil(t.MinVersion)
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
result.TLS.InternalRPC.VerifyServerHostname = &t.VerifyServerHostname
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:03:33 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringPtrOrNil(v string) *string {
|
|
|
|
if v == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &v
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
2020-07-28 19:31:48 +00:00
|
|
|
|
|
|
|
func extractSignedResponse(resp *pbautoconf.AutoConfigResponse) (*structs.SignedResponse, error) {
|
2022-03-16 16:12:29 +00:00
|
|
|
roots, err := pbconnect.CARootsToStructs(resp.CARoots)
|
2020-07-28 19:31:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:12:29 +00:00
|
|
|
cert, err := pbconnect.IssuedCertToStructs(resp.Certificate)
|
2020-07-28 19:31:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := &structs.SignedResponse{
|
|
|
|
IssuedCert: *cert,
|
|
|
|
ConnectCARoots: *roots,
|
|
|
|
ManualCARoots: resp.ExtraCACertificates,
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Config != nil && resp.Config.TLS != nil {
|
|
|
|
out.VerifyServerHostname = resp.Config.TLS.VerifyServerHostname
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
}
|