2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-04-14 16:24:46 +00:00
|
|
|
package configentry
|
proxycfg: server-local config entry data sources
This is the OSS portion of enterprise PR 2056.
This commit provides server-local implementations of the proxycfg.ConfigEntry
and proxycfg.ConfigEntryList interfaces, that source data from streaming events.
It makes use of the LocalMaterializer type introduced for peering replication,
adding the necessary support for authorization.
It also adds support for "wildcard" subscriptions (within a topic) to the event
publisher, as this is needed to fetch service-resolvers for all services when
configuring mesh gateways.
Currently, events will be emitted for just the ingress-gateway, service-resolver,
and mesh config entry types, as these are the only entries required by proxycfg
— the events will be emitted on topics named IngressGateway, ServiceResolver,
and MeshConfig topics respectively.
Though these events will only be consumed "locally" for now, they can also be
consumed via the gRPC endpoint (confirmed using grpcurl) so using them from
client agents should be a case of swapping the LocalMaterializer for an
RPCMaterializer.
2022-07-01 15:09:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbconfigentry"
|
|
|
|
"github.com/hashicorp/consul/proto/private/pbsubscribe"
|
proxycfg: server-local config entry data sources
This is the OSS portion of enterprise PR 2056.
This commit provides server-local implementations of the proxycfg.ConfigEntry
and proxycfg.ConfigEntryList interfaces, that source data from streaming events.
It makes use of the LocalMaterializer type introduced for peering replication,
adding the necessary support for authorization.
It also adds support for "wildcard" subscriptions (within a topic) to the event
publisher, as this is needed to fetch service-resolvers for all services when
configuring mesh gateways.
Currently, events will be emitted for just the ingress-gateway, service-resolver,
and mesh config entry types, as these are the only entries required by proxycfg
— the events will be emitted on topics named IngressGateway, ServiceResolver,
and MeshConfig topics respectively.
Though these events will only be consumed "locally" for now, they can also be
consumed via the gRPC endpoint (confirmed using grpcurl) so using them from
client agents should be a case of swapping the LocalMaterializer for an
RPCMaterializer.
2022-07-01 15:09:47 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigEntryView(t *testing.T) {
|
|
|
|
const index uint64 = 123
|
|
|
|
|
2023-04-14 16:24:46 +00:00
|
|
|
view := &ConfigEntryView{}
|
proxycfg: server-local config entry data sources
This is the OSS portion of enterprise PR 2056.
This commit provides server-local implementations of the proxycfg.ConfigEntry
and proxycfg.ConfigEntryList interfaces, that source data from streaming events.
It makes use of the LocalMaterializer type introduced for peering replication,
adding the necessary support for authorization.
It also adds support for "wildcard" subscriptions (within a topic) to the event
publisher, as this is needed to fetch service-resolvers for all services when
configuring mesh gateways.
Currently, events will be emitted for just the ingress-gateway, service-resolver,
and mesh config entry types, as these are the only entries required by proxycfg
— the events will be emitted on topics named IngressGateway, ServiceResolver,
and MeshConfig topics respectively.
Though these events will only be consumed "locally" for now, they can also be
consumed via the gRPC endpoint (confirmed using grpcurl) so using them from
client agents should be a case of swapping the LocalMaterializer for an
RPCMaterializer.
2022-07-01 15:09:47 +00:00
|
|
|
|
|
|
|
testutil.RunStep(t, "initial state", func(t *testing.T) {
|
|
|
|
result := view.Result(index)
|
|
|
|
resp, ok := result.(*structs.ConfigEntryResponse)
|
|
|
|
require.Truef(t, ok, "expected ConfigEntryResponse, got: %T", result)
|
|
|
|
require.Nil(t, resp.Entry)
|
|
|
|
require.Equal(t, index, resp.QueryMeta.Index)
|
|
|
|
})
|
|
|
|
|
|
|
|
testutil.RunStep(t, "upsert event", func(t *testing.T) {
|
|
|
|
err := view.Update([]*pbsubscribe.Event{
|
|
|
|
{
|
|
|
|
Index: index,
|
|
|
|
Payload: &pbsubscribe.Event_ConfigEntry{
|
|
|
|
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
|
|
|
|
Op: pbsubscribe.ConfigEntryUpdate_Upsert,
|
|
|
|
ConfigEntry: &pbconfigentry.ConfigEntry{
|
|
|
|
Kind: pbconfigentry.Kind_KindServiceResolver,
|
|
|
|
Name: "web",
|
|
|
|
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
|
|
|
|
ServiceResolver: &pbconfigentry.ServiceResolver{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := view.Result(index)
|
|
|
|
resp, ok := result.(*structs.ConfigEntryResponse)
|
|
|
|
require.Truef(t, ok, "expected ConfigEntryResponse, got: %T", result)
|
|
|
|
|
|
|
|
serviceResolver, ok := resp.Entry.(*structs.ServiceResolverConfigEntry)
|
|
|
|
require.Truef(t, ok, "expected ServiceResolverConfigEntry, got: %T", resp.Entry)
|
|
|
|
require.Equal(t, "web", serviceResolver.Name)
|
|
|
|
})
|
|
|
|
|
|
|
|
testutil.RunStep(t, "delete event", func(t *testing.T) {
|
|
|
|
err := view.Update([]*pbsubscribe.Event{
|
|
|
|
{
|
|
|
|
Index: index,
|
|
|
|
Payload: &pbsubscribe.Event_ConfigEntry{
|
|
|
|
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
|
|
|
|
Op: pbsubscribe.ConfigEntryUpdate_Delete,
|
|
|
|
ConfigEntry: &pbconfigentry.ConfigEntry{
|
|
|
|
Kind: pbconfigentry.Kind_KindServiceResolver,
|
|
|
|
Name: "web",
|
|
|
|
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
|
|
|
|
ServiceResolver: &pbconfigentry.ServiceResolver{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := view.Result(index)
|
|
|
|
resp, ok := result.(*structs.ConfigEntryResponse)
|
|
|
|
require.Truef(t, ok, "expected ConfigEntryResponse, got: %T", result)
|
|
|
|
require.Nil(t, resp.Entry)
|
|
|
|
})
|
|
|
|
|
|
|
|
testutil.RunStep(t, "bogus event", func(t *testing.T) {
|
|
|
|
err := view.Update([]*pbsubscribe.Event{
|
|
|
|
{
|
|
|
|
Index: index,
|
|
|
|
Payload: &pbsubscribe.Event_ServiceHealth{},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := view.Result(index)
|
|
|
|
resp, ok := result.(*structs.ConfigEntryResponse)
|
|
|
|
require.Truef(t, ok, "expected ConfigEntryResponse, got: %T", result)
|
|
|
|
require.Nil(t, resp.Entry)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigEntryListView(t *testing.T) {
|
|
|
|
const index uint64 = 123
|
|
|
|
|
2023-04-14 16:24:46 +00:00
|
|
|
view := NewConfigEntryListView(structs.ServiceResolver, *acl.DefaultEnterpriseMeta())
|
proxycfg: server-local config entry data sources
This is the OSS portion of enterprise PR 2056.
This commit provides server-local implementations of the proxycfg.ConfigEntry
and proxycfg.ConfigEntryList interfaces, that source data from streaming events.
It makes use of the LocalMaterializer type introduced for peering replication,
adding the necessary support for authorization.
It also adds support for "wildcard" subscriptions (within a topic) to the event
publisher, as this is needed to fetch service-resolvers for all services when
configuring mesh gateways.
Currently, events will be emitted for just the ingress-gateway, service-resolver,
and mesh config entry types, as these are the only entries required by proxycfg
— the events will be emitted on topics named IngressGateway, ServiceResolver,
and MeshConfig topics respectively.
Though these events will only be consumed "locally" for now, they can also be
consumed via the gRPC endpoint (confirmed using grpcurl) so using them from
client agents should be a case of swapping the LocalMaterializer for an
RPCMaterializer.
2022-07-01 15:09:47 +00:00
|
|
|
|
|
|
|
testutil.RunStep(t, "initial state", func(t *testing.T) {
|
|
|
|
result := view.Result(index)
|
|
|
|
|
|
|
|
resp, ok := result.(*structs.IndexedConfigEntries)
|
|
|
|
require.Truef(t, ok, "expected IndexedConfigEntries, got: %T", result)
|
|
|
|
require.Empty(t, resp.Entries)
|
|
|
|
require.Equal(t, index, resp.QueryMeta.Index)
|
|
|
|
})
|
|
|
|
|
|
|
|
testutil.RunStep(t, "upsert events", func(t *testing.T) {
|
|
|
|
err := view.Update([]*pbsubscribe.Event{
|
|
|
|
{
|
|
|
|
Index: index,
|
|
|
|
Payload: &pbsubscribe.Event_ConfigEntry{
|
|
|
|
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
|
|
|
|
Op: pbsubscribe.ConfigEntryUpdate_Upsert,
|
|
|
|
ConfigEntry: &pbconfigentry.ConfigEntry{
|
|
|
|
Kind: pbconfigentry.Kind_KindServiceResolver,
|
|
|
|
Name: "web",
|
|
|
|
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
|
|
|
|
ServiceResolver: &pbconfigentry.ServiceResolver{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Index: index,
|
|
|
|
Payload: &pbsubscribe.Event_ConfigEntry{
|
|
|
|
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
|
|
|
|
Op: pbsubscribe.ConfigEntryUpdate_Upsert,
|
|
|
|
ConfigEntry: &pbconfigentry.ConfigEntry{
|
|
|
|
Kind: pbconfigentry.Kind_KindServiceResolver,
|
|
|
|
Name: "db",
|
|
|
|
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
|
|
|
|
ServiceResolver: &pbconfigentry.ServiceResolver{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := view.Result(index)
|
|
|
|
resp, ok := result.(*structs.IndexedConfigEntries)
|
|
|
|
require.Truef(t, ok, "expected IndexedConfigEntries, got: %T", result)
|
|
|
|
require.Len(t, resp.Entries, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
testutil.RunStep(t, "delete event", func(t *testing.T) {
|
|
|
|
err := view.Update([]*pbsubscribe.Event{
|
|
|
|
{
|
|
|
|
Index: index,
|
|
|
|
Payload: &pbsubscribe.Event_ConfigEntry{
|
|
|
|
ConfigEntry: &pbsubscribe.ConfigEntryUpdate{
|
|
|
|
Op: pbsubscribe.ConfigEntryUpdate_Delete,
|
|
|
|
ConfigEntry: &pbconfigentry.ConfigEntry{
|
|
|
|
Kind: pbconfigentry.Kind_KindServiceResolver,
|
|
|
|
Name: "web",
|
|
|
|
Entry: &pbconfigentry.ConfigEntry_ServiceResolver{
|
|
|
|
ServiceResolver: &pbconfigentry.ServiceResolver{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
result := view.Result(index)
|
|
|
|
resp, ok := result.(*structs.IndexedConfigEntries)
|
|
|
|
require.Truef(t, ok, "expected IndexedConfigEntries, got: %T", result)
|
|
|
|
require.Len(t, resp.Entries, 1)
|
|
|
|
|
|
|
|
serviceResolver, ok := resp.Entries[0].(*structs.ServiceResolverConfigEntry)
|
|
|
|
require.Truef(t, ok, "expected ServiceResolverConfigEntry, got: %T", resp.Entries[0])
|
|
|
|
require.Equal(t, "db", serviceResolver.Name)
|
|
|
|
})
|
|
|
|
}
|
2023-04-14 16:24:46 +00:00
|
|
|
|
|
|
|
func TestConfigEntryListView_Reset(t *testing.T) {
|
|
|
|
view := &ConfigEntryView{state: &structs.SamenessGroupConfigEntry{}}
|
|
|
|
view.Reset()
|
|
|
|
require.Nil(t, view.state)
|
|
|
|
}
|