1package autoconf
2
3import (
4	"encoding/json"
5	"testing"
6
7	"github.com/hashicorp/consul/agent/config"
8	pbconfig "github.com/hashicorp/consul/proto/pbconfig"
9	"github.com/stretchr/testify/require"
10)
11
12func stringPointer(s string) *string {
13	return &s
14}
15
16func boolPointer(b bool) *bool {
17	return &b
18}
19
20func TestConfig_translateConfig(t *testing.T) {
21	original := pbconfig.Config{
22		Datacenter:        "abc",
23		PrimaryDatacenter: "def",
24		NodeName:          "ghi",
25		SegmentName:       "jkl",
26		ACL: &pbconfig.ACL{
27			Enabled:                true,
28			PolicyTTL:              "1s",
29			RoleTTL:                "2s",
30			TokenTTL:               "3s",
31			DownPolicy:             "deny",
32			DefaultPolicy:          "deny",
33			EnableKeyListPolicy:    true,
34			DisabledTTL:            "4s",
35			EnableTokenPersistence: true,
36			MSPDisableBootstrap:    false,
37			Tokens: &pbconfig.ACLTokens{
38				Master:      "99e7e490-6baf-43fc-9010-78b6aa9a6813",
39				Replication: "51308d40-465c-4ac6-a636-7c0747edec89",
40				AgentMaster: "e012e1ea-78a2-41cc-bc8b-231a44196f39",
41				Default:     "8781a3f5-de46-4b45-83e1-c92f4cfd0332",
42				Agent:       "ddb8f1b0-8a99-4032-b601-87926bce244e",
43				ManagedServiceProvider: []*pbconfig.ACLServiceProviderToken{
44					{
45						AccessorID: "23f37987-7b9e-4e5b-acae-dbc9bc137bae",
46						SecretID:   "e28b820a-438e-4e2b-ad24-fe59e6a4914f",
47					},
48				},
49			},
50		},
51		AutoEncrypt: &pbconfig.AutoEncrypt{
52			TLS:      true,
53			DNSSAN:   []string{"dns"},
54			IPSAN:    []string{"198.18.0.1"},
55			AllowTLS: false,
56		},
57		Gossip: &pbconfig.Gossip{
58			RetryJoinLAN: []string{"10.0.0.1"},
59			Encryption: &pbconfig.GossipEncryption{
60				Key:            "blarg",
61				VerifyOutgoing: true,
62				VerifyIncoming: true,
63			},
64		},
65		TLS: &pbconfig.TLS{
66			VerifyOutgoing:           true,
67			VerifyServerHostname:     true,
68			CipherSuites:             "stuff",
69			MinVersion:               "tls13",
70			PreferServerCipherSuites: true,
71		},
72	}
73
74	expected := &config.Config{
75		Datacenter:                  stringPointer("abc"),
76		PrimaryDatacenter:           stringPointer("def"),
77		NodeName:                    stringPointer("ghi"),
78		SegmentName:                 stringPointer("jkl"),
79		RetryJoinLAN:                []string{"10.0.0.1"},
80		EncryptKey:                  stringPointer("blarg"),
81		EncryptVerifyIncoming:       boolPointer(true),
82		EncryptVerifyOutgoing:       boolPointer(true),
83		VerifyOutgoing:              boolPointer(true),
84		VerifyServerHostname:        boolPointer(true),
85		TLSCipherSuites:             stringPointer("stuff"),
86		TLSMinVersion:               stringPointer("tls13"),
87		TLSPreferServerCipherSuites: boolPointer(true),
88		ACL: config.ACL{
89			Enabled:                boolPointer(true),
90			PolicyTTL:              stringPointer("1s"),
91			RoleTTL:                stringPointer("2s"),
92			TokenTTL:               stringPointer("3s"),
93			DownPolicy:             stringPointer("deny"),
94			DefaultPolicy:          stringPointer("deny"),
95			EnableKeyListPolicy:    boolPointer(true),
96			DisabledTTL:            stringPointer("4s"),
97			EnableTokenPersistence: boolPointer(true),
98			Tokens: config.Tokens{
99				Master:      stringPointer("99e7e490-6baf-43fc-9010-78b6aa9a6813"),
100				Replication: stringPointer("51308d40-465c-4ac6-a636-7c0747edec89"),
101				AgentMaster: stringPointer("e012e1ea-78a2-41cc-bc8b-231a44196f39"),
102				Default:     stringPointer("8781a3f5-de46-4b45-83e1-c92f4cfd0332"),
103				Agent:       stringPointer("ddb8f1b0-8a99-4032-b601-87926bce244e"),
104				ManagedServiceProvider: []config.ServiceProviderToken{
105					{
106						AccessorID: stringPointer("23f37987-7b9e-4e5b-acae-dbc9bc137bae"),
107						SecretID:   stringPointer("e28b820a-438e-4e2b-ad24-fe59e6a4914f"),
108					},
109				},
110			},
111		},
112		AutoEncrypt: config.AutoEncrypt{
113			TLS:      boolPointer(true),
114			DNSSAN:   []string{"dns"},
115			IPSAN:    []string{"198.18.0.1"},
116			AllowTLS: boolPointer(false),
117		},
118	}
119
120	translated := translateConfig(&original)
121	data, err := json.Marshal(translated)
122	require.NoError(t, err)
123
124	actual, _, err := config.Parse(string(data), "json")
125	require.NoError(t, err)
126	require.Equal(t, expected, &actual)
127}
128