Skip to content

Commit

Permalink
ca: don't append rootPEM in vault provider
Browse files Browse the repository at this point in the history
See #11783

Also add a test for secondary with external CA
  • Loading branch information
dnephin committed Jan 6, 2022
1 parent 62b7427 commit 2c7e68d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
1 change: 0 additions & 1 deletion agent/connect/ca/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ type SecondaryProvider interface {
//
// The provider should save the certificates and use them to
// Provider.Sign leaf certificates.
// TODO: document exactly how the chain is passed. probably in intermediatePEM
SetIntermediate(intermediatePEM, rootPEM string) error
}

Expand Down
2 changes: 1 addition & 1 deletion agent/connect/ca/provider_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (v *VaultProvider) SetIntermediate(intermediatePEM, rootPEM string) error {
}

_, err = v.client.Logical().Write(v.config.IntermediatePKIPath+"intermediate/set-signed", map[string]interface{}{
"certificate": fmt.Sprintf("%s\n%s", intermediatePEM, rootPEM),
"certificate": intermediatePEM,
})
if err != nil {
return err
Expand Down
54 changes: 41 additions & 13 deletions agent/consul/leader_connect_ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func TestCAManager_Initialize_Vault_Secondary_SharedVault(t *testing.T) {
},
}
})
defer serverDC2.Shutdown()
joinWAN(t, serverDC2, serverDC1)
testrpc.WaitForActiveCARoot(t, serverDC2.RPC, "dc2", nil)

Expand Down Expand Up @@ -637,9 +636,9 @@ func TestCAManager_Initialize_Vault_WithExternalTrustedCA(t *testing.T) {
rootPEM := generateExternalRootCA(t, vclient)

primaryCAPath := "pki-primary"
setupPrimaryCA(t, vclient, primaryCAPath, rootPEM)
primaryPEM := setupPrimaryCA(t, vclient, primaryCAPath, rootPEM)

_, s1 := testServerWithConfig(t, func(c *Config) {
_, serverDC1 := testServerWithConfig(t, func(c *Config) {
c.CAConfig = &structs.CAConfiguration{
Provider: "vault",
Config: map[string]interface{}{
Expand All @@ -654,18 +653,47 @@ func TestCAManager_Initialize_Vault_WithExternalTrustedCA(t *testing.T) {
},
}
})
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
testrpc.WaitForTestAgent(t, serverDC1.RPC, "dc1")

codec := rpcClient(t, s1)
roots := structs.IndexedCARoots{}
err := msgpackrpc.CallWithCodec(codec, "ConnectCA.Roots", &structs.DCSpecificRequest{}, &roots)
require.NoError(t, err)
require.Len(t, roots.Roots, 1)
require.Equal(t, rootPEM, roots.Roots[0].RootCert)
require.Len(t, roots.Roots[0].IntermediateCerts, 2)
runStep(t, "verify primary DC", func(t *testing.T) {
codec := rpcClient(t, serverDC1)
roots := structs.IndexedCARoots{}
err := msgpackrpc.CallWithCodec(codec, "ConnectCA.Roots", &structs.DCSpecificRequest{}, &roots)
require.NoError(t, err)
require.Len(t, roots.Roots, 1)
require.Equal(t, primaryPEM, roots.Roots[0].RootCert)
require.Len(t, roots.Roots[0].IntermediateCerts, 1)

leafCert := getLeafCert(t, codec, roots.TrustDomain, "dc1")
verifyLeafCert(t, roots.Active(), leafCert)
})

leafCert := getLeafCert(t, codec, roots.TrustDomain, "dc1")
verifyLeafCert(t, roots.Active(), leafCert)
runStep(t, "start secondary DC", func(t *testing.T) {
_, serverDC2 := testServerWithConfig(t, func(c *Config) {
c.Datacenter = "dc2"
c.PrimaryDatacenter = "dc1"
c.CAConfig = &structs.CAConfiguration{
Provider: "vault",
Config: map[string]interface{}{
"Address": vault.Addr,
"Token": vault.RootToken,
"RootPKIPath": "should-be-ignored",
"IntermediatePKIPath": "pki-secondary/",
},
}
})
joinWAN(t, serverDC2, serverDC1)
testrpc.WaitForActiveCARoot(t, serverDC2.RPC, "dc2", nil)

codec := rpcClient(t, serverDC2)
roots := structs.IndexedCARoots{}
err := msgpackrpc.CallWithCodec(codec, "ConnectCA.Roots", &structs.DCSpecificRequest{}, &roots)
require.NoError(t, err)
require.Len(t, roots.Roots, 1)

leafPEM := getLeafCert(t, codec, roots.TrustDomain, "dc2")
verifyLeafCert(t, roots.Roots[0], leafPEM)
})
}

func generateExternalRootCA(t *testing.T, client *vaultapi.Client) string {
Expand Down

0 comments on commit 2c7e68d

Please sign in to comment.