From 497b300c766b366b1abe8411ca298c6560f6256d Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Fri, 11 Mar 2022 19:51:24 -0800 Subject: [PATCH] add new entmeta stuff. Signed-off-by: Mark Anderson --- acl/acl_oss.go | 5 + acl/enterprisemeta_oss.go | 108 ++++++++++++++++++++++ agent/structs/structs.go | 4 +- agent/structs/structs_oss.go | 148 ++++++------------------------ agent/structs/structs_oss_test.go | 14 +-- 5 files changed, 153 insertions(+), 126 deletions(-) create mode 100644 acl/enterprisemeta_oss.go diff --git a/acl/acl_oss.go b/acl/acl_oss.go index 9c62c5bdb..ca2974e4e 100644 --- a/acl/acl_oss.go +++ b/acl/acl_oss.go @@ -5,6 +5,11 @@ package acl const DefaultPartitionName = "" +// Reviewer Note: This is a little bit strange; one might want it to be "" like partition name +// However in consul/structs/intention.go we define IntentionDefaultNamespace as 'default' and so +// we use the same here +const DefaultNamespaceName = "default" + type EnterpriseConfig struct { // no fields in OSS } diff --git a/acl/enterprisemeta_oss.go b/acl/enterprisemeta_oss.go new file mode 100644 index 000000000..2296fdd43 --- /dev/null +++ b/acl/enterprisemeta_oss.go @@ -0,0 +1,108 @@ +//go:build !consulent +// +build !consulent + +package acl + +import "hash" + +var emptyEnterpriseMeta = EnterpriseMeta{} + +// EnterpriseMeta stub +type EnterpriseMeta struct{} + +func (m *EnterpriseMeta) ToEnterprisePolicyMeta() *EnterprisePolicyMeta { + return nil +} + +func DefaultEnterpriseMeta() *EnterpriseMeta { + return &EnterpriseMeta{} +} + +func WildcardEnterpriseMeta() *EnterpriseMeta { + return &EnterpriseMeta{} +} + +func (m *EnterpriseMeta) EstimateSize() int { + return 0 +} + +func (m *EnterpriseMeta) AddToHash(_ hash.Hash, _ bool) { + // do nothing +} + +func (m *EnterpriseMeta) PartitionOrDefault() string { + return "default" +} + +func EqualPartitions(_, _ string) bool { + return true +} + +func IsDefaultPartition(partition string) bool { + return true +} + +func PartitionOrDefault(_ string) string { + return "default" +} + +func (m *EnterpriseMeta) PartitionOrEmpty() string { + return "" +} + +func (m *EnterpriseMeta) InDefaultPartition() bool { + return true +} + +func (m *EnterpriseMeta) NamespaceOrDefault() string { + return DefaultNamespaceName +} + +func NamespaceOrDefault(_ string) string { + return DefaultNamespaceName +} + +func (m *EnterpriseMeta) NamespaceOrEmpty() string { + return "" +} + +func (m *EnterpriseMeta) InDefaultNamespace() bool { + return true +} + +func (m *EnterpriseMeta) Merge(_ *EnterpriseMeta) { + // do nothing +} + +func (m *EnterpriseMeta) MergeNoWildcard(_ *EnterpriseMeta) { + // do nothing +} + +func (_ *EnterpriseMeta) Normalize() {} + +func (m *EnterpriseMeta) Matches(_ *EnterpriseMeta) bool { + return true +} + +func (m *EnterpriseMeta) IsSame(_ *EnterpriseMeta) bool { + return true +} + +func (m *EnterpriseMeta) LessThan(_ *EnterpriseMeta) bool { + return false +} + +func (m *EnterpriseMeta) WithWildcardNamespace() *EnterpriseMeta { + return &emptyEnterpriseMeta +} + +func (m *EnterpriseMeta) UnsetPartition() { + // do nothing +} + +func NewEnterpriseMetaWithPartition(_, _ string) EnterpriseMeta { + return emptyEnterpriseMeta +} + +// FillAuthzContext stub +func (_ *EnterpriseMeta) FillAuthzContext(_ *AuthorizerContext) {} diff --git a/agent/structs/structs.go b/agent/structs/structs.go index 1a678f5c0..9ea65fc3a 100644 --- a/agent/structs/structs.go +++ b/agent/structs/structs.go @@ -1896,13 +1896,13 @@ type CheckID struct { EnterpriseMeta } -// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer +// NamespaceOrDefault exists because acl.EnterpriseMeta uses a pointer // receiver for this method. Remove once that is fixed. func (c CheckID) NamespaceOrDefault() string { return c.EnterpriseMeta.NamespaceOrDefault() } -// PartitionOrDefault exists because structs.EnterpriseMeta uses a pointer +// PartitionOrDefault exists because acl.EnterpriseMeta uses a pointer // receiver for this method. Remove once that is fixed. func (c CheckID) PartitionOrDefault() string { return c.EnterpriseMeta.PartitionOrDefault() diff --git a/agent/structs/structs_oss.go b/agent/structs/structs_oss.go index 669361802..bf911f5a8 100644 --- a/agent/structs/structs_oss.go +++ b/agent/structs/structs_oss.go @@ -4,158 +4,70 @@ package structs import ( - "hash" - "github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/types" ) -var emptyEnterpriseMeta = EnterpriseMeta{} - -// EnterpriseMeta stub -type EnterpriseMeta struct{} - -func (m *EnterpriseMeta) ToEnterprisePolicyMeta() *acl.EnterprisePolicyMeta { - return nil -} - -func (m *EnterpriseMeta) estimateSize() int { - return 0 -} - -func (m *EnterpriseMeta) addToHash(_ hash.Hash, _ bool) { - // do nothing -} - -func (m *EnterpriseMeta) Merge(_ *EnterpriseMeta) { - // do nothing -} - -func (m *EnterpriseMeta) MergeNoWildcard(_ *EnterpriseMeta) { - // do nothing -} - -func (m *EnterpriseMeta) Matches(_ *EnterpriseMeta) bool { - return true -} - -func (m *EnterpriseMeta) IsSame(_ *EnterpriseMeta) bool { - return true -} - -func (m *EnterpriseMeta) LessThan(_ *EnterpriseMeta) bool { - return false -} - -func (m *EnterpriseMeta) WithWildcardNamespace() *EnterpriseMeta { - return &emptyEnterpriseMeta -} - -func (m *EnterpriseMeta) UnsetPartition() { - // do nothing -} +// TODO(acl-move-enterprise-meta) sync this with enterprise +var emptyEnterpriseMeta = acl.EnterpriseMeta{} // TODO(partition): stop using this -func NewEnterpriseMetaInDefaultPartition(_ string) EnterpriseMeta { +func NewEnterpriseMetaInDefaultPartition(_ string) acl.EnterpriseMeta { return emptyEnterpriseMeta } -func NewEnterpriseMetaWithPartition(_, _ string) EnterpriseMeta { - return emptyEnterpriseMeta -} - -func (m *EnterpriseMeta) NamespaceOrDefault() string { - return IntentionDefaultNamespace -} - -func NamespaceOrDefault(_ string) string { - return IntentionDefaultNamespace -} - -func (m *EnterpriseMeta) NamespaceOrEmpty() string { - return "" -} - -func (m *EnterpriseMeta) InDefaultNamespace() bool { - return true -} - -func (m *EnterpriseMeta) PartitionOrDefault() string { - return "default" -} - -func EqualPartitions(_, _ string) bool { - return true -} - -func IsDefaultPartition(partition string) bool { - return true -} - -func PartitionOrDefault(_ string) string { - return "default" -} - -func (m *EnterpriseMeta) PartitionOrEmpty() string { - return "" -} - -func (m *EnterpriseMeta) InDefaultPartition() bool { - return true -} - // ReplicationEnterpriseMeta stub -func ReplicationEnterpriseMeta() *EnterpriseMeta { +func ReplicationEnterpriseMeta() *acl.EnterpriseMeta { return &emptyEnterpriseMeta } // TODO(partition): stop using this -func DefaultEnterpriseMetaInDefaultPartition() *EnterpriseMeta { +func WildcardEnterpriseMetaInDefaultPartition() *acl.EnterpriseMeta { + return &emptyEnterpriseMeta +} + +// TODO(partition): stop using this +func DefaultEnterpriseMetaInDefaultPartition() *acl.EnterpriseMeta { return &emptyEnterpriseMeta } // DefaultEnterpriseMetaInPartition stub -func DefaultEnterpriseMetaInPartition(_ string) *EnterpriseMeta { - return &emptyEnterpriseMeta -} - -func NodeEnterpriseMetaInPartition(_ string) *EnterpriseMeta { - return &emptyEnterpriseMeta -} - -// TODO(partition): stop using this -func NodeEnterpriseMetaInDefaultPartition() *EnterpriseMeta { - return &emptyEnterpriseMeta -} - -// TODO(partition): stop using this -func WildcardEnterpriseMetaInDefaultPartition() *EnterpriseMeta { +func DefaultEnterpriseMetaInPartition(_ string) *acl.EnterpriseMeta { return &emptyEnterpriseMeta } // WildcardEnterpriseMetaInPartition stub -func WildcardEnterpriseMetaInPartition(_ string) *EnterpriseMeta { +func WildcardEnterpriseMetaInPartition(_ string) *acl.EnterpriseMeta { + return &emptyEnterpriseMeta +} + +func NewEnterpriseMetaWithPartition(_, _ string) acl.EnterpriseMeta { + return emptyEnterpriseMeta +} + +func NodeEnterpriseMetaInPartition(_ string) *acl.EnterpriseMeta { + return &emptyEnterpriseMeta +} + +// TODO(partition): stop using this +func NodeEnterpriseMetaInDefaultPartition() *acl.EnterpriseMeta { return &emptyEnterpriseMeta } // FillAuthzContext stub -func (_ *EnterpriseMeta) FillAuthzContext(_ *acl.AuthorizerContext) {} - func (_ *Node) FillAuthzContext(_ *acl.AuthorizerContext) {} func (_ *Coordinate) FillAuthzContext(_ *acl.AuthorizerContext) {} func (_ *NodeInfo) FillAuthzContext(_ *acl.AuthorizerContext) {} -func (_ *EnterpriseMeta) Normalize() {} - // FillAuthzContext stub func (_ *DirEntry) FillAuthzContext(_ *acl.AuthorizerContext) {} // FillAuthzContext stub func (_ *RegisterRequest) FillAuthzContext(_ *acl.AuthorizerContext) {} -func (_ *RegisterRequest) GetEnterpriseMeta() *EnterpriseMeta { +func (_ *RegisterRequest) GetEnterpriseMeta() *acl.EnterpriseMeta { return nil } @@ -168,15 +80,15 @@ func (_ *TxnServiceOp) FillAuthzContext(_ *acl.AuthorizerContext) {} // OSS Stub func (_ *TxnCheckOp) FillAuthzContext(_ *acl.AuthorizerContext) {} -func NodeNameString(node string, _ *EnterpriseMeta) string { +func NodeNameString(node string, _ *acl.EnterpriseMeta) string { return node } -func ServiceIDString(id string, _ *EnterpriseMeta) string { +func ServiceIDString(id string, _ *acl.EnterpriseMeta) string { return id } -func ParseServiceIDString(input string) (string, *EnterpriseMeta) { +func ParseServiceIDString(input string) (string, *acl.EnterpriseMeta) { return input, DefaultEnterpriseMetaInDefaultPartition() } @@ -189,7 +101,7 @@ func ServiceIDFromString(input string) ServiceID { return ServiceID{ID: id} } -func ParseServiceNameString(input string) (string, *EnterpriseMeta) { +func ParseServiceNameString(input string) (string, *acl.EnterpriseMeta) { return input, DefaultEnterpriseMetaInDefaultPartition() } diff --git a/agent/structs/structs_oss_test.go b/agent/structs/structs_oss_test.go index 28b6e3797..54bbb0f2f 100644 --- a/agent/structs/structs_oss_test.go +++ b/agent/structs/structs_oss_test.go @@ -8,39 +8,41 @@ import ( "testing" "github.com/stretchr/testify/require" + + "github.com/hashicorp/consul/acl" ) var enterpriseMetaField = "EnterpriseMeta" func TestServiceID_String(t *testing.T) { t.Run("value", func(t *testing.T) { - sid := NewServiceID("the-id", &EnterpriseMeta{}) + sid := NewServiceID("the-id", &acl.EnterpriseMeta{}) require.Equal(t, "the-id", fmt.Sprintf("%v", sid)) }) t.Run("pointer", func(t *testing.T) { - sid := NewServiceID("the-id", &EnterpriseMeta{}) + sid := NewServiceID("the-id", &acl.EnterpriseMeta{}) require.Equal(t, "the-id", fmt.Sprintf("%v", &sid)) }) } func TestCheckID_String(t *testing.T) { t.Run("value", func(t *testing.T) { - cid := NewCheckID("the-id", &EnterpriseMeta{}) + cid := NewCheckID("the-id", &acl.EnterpriseMeta{}) require.Equal(t, "the-id", fmt.Sprintf("%v", cid)) }) t.Run("pointer", func(t *testing.T) { - cid := NewCheckID("the-id", &EnterpriseMeta{}) + cid := NewCheckID("the-id", &acl.EnterpriseMeta{}) require.Equal(t, "the-id", fmt.Sprintf("%v", &cid)) }) } func TestServiceName_String(t *testing.T) { t.Run("value", func(t *testing.T) { - sn := NewServiceName("the-id", &EnterpriseMeta{}) + sn := NewServiceName("the-id", &acl.EnterpriseMeta{}) require.Equal(t, "the-id", fmt.Sprintf("%v", sn)) }) t.Run("pointer", func(t *testing.T) { - sn := NewServiceName("the-id", &EnterpriseMeta{}) + sn := NewServiceName("the-id", &acl.EnterpriseMeta{}) require.Equal(t, "the-id", fmt.Sprintf("%v", &sn)) }) }