2020-06-10 20:47:35 +00:00
|
|
|
package autoconf
|
|
|
|
|
|
|
|
import (
|
2020-08-31 17:12:17 +00:00
|
|
|
"fmt"
|
2020-06-10 20:47:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-08-09 20:04:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2020-08-31 17:12:17 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-07-23 15:24:20 +00:00
|
|
|
pbconfig "github.com/hashicorp/consul/proto/pbconfig"
|
2020-08-31 17:12:17 +00:00
|
|
|
"github.com/hashicorp/consul/proto/pbconnect"
|
2020-06-10 20:47:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func stringPointer(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolPointer(b bool) *bool {
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:12:17 +00:00
|
|
|
func translateCARootToProtobuf(in *structs.CARoot) (*pbconnect.CARoot, error) {
|
|
|
|
var out pbconnect.CARoot
|
|
|
|
if err := mapstructureTranslateToProtobuf(in, &out); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to re-encode CA Roots: %w", err)
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustTranslateCARootToProtobuf(t *testing.T, in *structs.CARoot) *pbconnect.CARoot {
|
|
|
|
out, err := translateCARootToProtobuf(in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustTranslateCARootsToStructs(t *testing.T, in *pbconnect.CARoots) *structs.IndexedCARoots {
|
|
|
|
out, err := translateCARootsToStructs(in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustTranslateCARootsToProtobuf(t *testing.T, in *structs.IndexedCARoots) *pbconnect.CARoots {
|
|
|
|
out, err := translateCARootsToProtobuf(in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustTranslateIssuedCertToProtobuf(t *testing.T, in *structs.IssuedCert) *pbconnect.IssuedCert {
|
|
|
|
out, err := translateIssuedCertToProtobuf(in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:03:33 +00:00
|
|
|
func TestTranslateConfig(t *testing.T) {
|
2020-06-10 20:47:35 +00:00
|
|
|
original := pbconfig.Config{
|
|
|
|
Datacenter: "abc",
|
|
|
|
PrimaryDatacenter: "def",
|
|
|
|
NodeName: "ghi",
|
|
|
|
SegmentName: "jkl",
|
|
|
|
ACL: &pbconfig.ACL{
|
|
|
|
Enabled: true,
|
|
|
|
PolicyTTL: "1s",
|
|
|
|
RoleTTL: "2s",
|
|
|
|
TokenTTL: "3s",
|
|
|
|
DownPolicy: "deny",
|
|
|
|
DefaultPolicy: "deny",
|
|
|
|
EnableKeyListPolicy: true,
|
|
|
|
EnableTokenPersistence: true,
|
|
|
|
MSPDisableBootstrap: false,
|
|
|
|
Tokens: &pbconfig.ACLTokens{
|
2021-12-07 19:59:38 +00:00
|
|
|
InitialManagement: "99e7e490-6baf-43fc-9010-78b6aa9a6813",
|
|
|
|
Replication: "51308d40-465c-4ac6-a636-7c0747edec89",
|
|
|
|
AgentRecovery: "e012e1ea-78a2-41cc-bc8b-231a44196f39",
|
|
|
|
Default: "8781a3f5-de46-4b45-83e1-c92f4cfd0332",
|
|
|
|
Agent: "ddb8f1b0-8a99-4032-b601-87926bce244e",
|
2020-06-10 20:47:35 +00:00
|
|
|
ManagedServiceProvider: []*pbconfig.ACLServiceProviderToken{
|
|
|
|
{
|
|
|
|
AccessorID: "23f37987-7b9e-4e5b-acae-dbc9bc137bae",
|
|
|
|
SecretID: "e28b820a-438e-4e2b-ad24-fe59e6a4914f",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AutoEncrypt: &pbconfig.AutoEncrypt{
|
|
|
|
TLS: true,
|
|
|
|
DNSSAN: []string{"dns"},
|
|
|
|
IPSAN: []string{"198.18.0.1"},
|
|
|
|
AllowTLS: false,
|
|
|
|
},
|
|
|
|
Gossip: &pbconfig.Gossip{
|
|
|
|
RetryJoinLAN: []string{"10.0.0.1"},
|
|
|
|
Encryption: &pbconfig.GossipEncryption{
|
|
|
|
Key: "blarg",
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
VerifyIncoming: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TLS: &pbconfig.TLS{
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CipherSuites: "stuff",
|
|
|
|
MinVersion: "tls13",
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:03:33 +00:00
|
|
|
expected := config.Config{
|
2020-06-10 20:47:35 +00:00
|
|
|
Datacenter: stringPointer("abc"),
|
|
|
|
PrimaryDatacenter: stringPointer("def"),
|
|
|
|
NodeName: stringPointer("ghi"),
|
|
|
|
SegmentName: stringPointer("jkl"),
|
|
|
|
RetryJoinLAN: []string{"10.0.0.1"},
|
|
|
|
EncryptKey: stringPointer("blarg"),
|
|
|
|
EncryptVerifyIncoming: boolPointer(true),
|
|
|
|
EncryptVerifyOutgoing: boolPointer(true),
|
|
|
|
VerifyOutgoing: boolPointer(true),
|
|
|
|
VerifyServerHostname: boolPointer(true),
|
|
|
|
TLSCipherSuites: stringPointer("stuff"),
|
|
|
|
TLSMinVersion: stringPointer("tls13"),
|
|
|
|
TLSPreferServerCipherSuites: boolPointer(true),
|
|
|
|
ACL: config.ACL{
|
|
|
|
Enabled: boolPointer(true),
|
|
|
|
PolicyTTL: stringPointer("1s"),
|
|
|
|
RoleTTL: stringPointer("2s"),
|
|
|
|
TokenTTL: stringPointer("3s"),
|
|
|
|
DownPolicy: stringPointer("deny"),
|
|
|
|
DefaultPolicy: stringPointer("deny"),
|
|
|
|
EnableKeyListPolicy: boolPointer(true),
|
|
|
|
EnableTokenPersistence: boolPointer(true),
|
|
|
|
Tokens: config.Tokens{
|
2021-12-07 19:59:38 +00:00
|
|
|
InitialManagement: stringPointer("99e7e490-6baf-43fc-9010-78b6aa9a6813"),
|
|
|
|
AgentRecovery: stringPointer("e012e1ea-78a2-41cc-bc8b-231a44196f39"),
|
|
|
|
Replication: stringPointer("51308d40-465c-4ac6-a636-7c0747edec89"),
|
|
|
|
Default: stringPointer("8781a3f5-de46-4b45-83e1-c92f4cfd0332"),
|
|
|
|
Agent: stringPointer("ddb8f1b0-8a99-4032-b601-87926bce244e"),
|
2020-06-10 20:47:35 +00:00
|
|
|
ManagedServiceProvider: []config.ServiceProviderToken{
|
|
|
|
{
|
|
|
|
AccessorID: stringPointer("23f37987-7b9e-4e5b-acae-dbc9bc137bae"),
|
|
|
|
SecretID: stringPointer("e28b820a-438e-4e2b-ad24-fe59e6a4914f"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AutoEncrypt: config.AutoEncrypt{
|
|
|
|
TLS: boolPointer(true),
|
|
|
|
DNSSAN: []string{"dns"},
|
|
|
|
IPSAN: []string{"198.18.0.1"},
|
|
|
|
AllowTLS: boolPointer(false),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-26 18:53:07 +00:00
|
|
|
translated := translateConfig(&original)
|
2020-08-10 17:03:33 +00:00
|
|
|
require.Equal(t, expected, translated)
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
2020-08-31 17:12:17 +00:00
|
|
|
|
|
|
|
func TestCArootsTranslation(t *testing.T) {
|
|
|
|
_, indexedRoots, _ := testCerts(t, "autoconf", "dc1")
|
|
|
|
protoRoots := mustTranslateCARootsToProtobuf(t, indexedRoots)
|
|
|
|
require.Equal(t, indexedRoots, mustTranslateCARootsToStructs(t, protoRoots))
|
|
|
|
}
|