-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Net 5875 - Create the Exported Services Resources #19117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
88 commits
Select commit
Hold shift + click to select a range
714f8a3
init
c1e1e4e
computed exported service
8be5941
make proto
f7f396e
exported services resource
55ba121
exported services test
755b08d
added some tests and namespace exported service
10af828
partition exported services
c97f8c2
computed service
074ed36
computed services tests
4221562
register types
7415265
fix comment
75e2b49
Merge branch 'main' into NET-5875
asheshvidyut 689de78
make proto lint
5375a1a
fix proto format make proto
e15f0eb
Merge branch 'NET-5875' of ssh://github.com/hashicorp/consul into NET…
13e59ff
make codegen
a681ded
Update proto-public/pbmulticluster/v1alpha1/computed_exported_service…
asheshvidyut ff6a831
Update internal/multicluster/internal/types/computed_exported_service…
asheshvidyut f2a7e22
using different way of resource creation in tests
13a5ed0
make proto
2e21983
Merge branch 'main' into NET-5875
asheshvidyut 6cfa2a0
fix computed exported services test
d12e785
fix tests
3df55fb
differnet validation for computed services for ent and ce
e1a8e96
Acls for exported services
8a922ae
added validations for enterprise features in ce
bbb7592
fix error
08d9d6e
fix acls test
393e5eb
Update internal/multicluster/internal/types/validation_exported_servi…
asheshvidyut 59cb443
removed the create method
15febaf
update proto
9d5e2da
removed namespace
437490b
created seperate function for ce and ent
d97bfd8
test files updated and validations fixed
2985849
added nil checks
2103241
fix tests
5c1ef95
added comments
792e493
removed tenancy check
9a6f4b6
added mutation function
022be04
fix mutation method
c0757c8
fix list permissions in test
617b297
fix pr comments
a9202a6
fix tests
63aa314
lisence
9ff14cc
busl license
c7469ca
Merge branch 'main' into NET-5875
asheshvidyut f78152f
Merge branch 'main' into NET-5875
asheshvidyut 79eda17
Update internal/multicluster/internal/types/helpers_ce.go
asheshvidyut 25d0993
Update internal/multicluster/internal/types/helpers_ce.go
asheshvidyut d48c01e
Update internal/multicluster/internal/types/helpers_ce.go
asheshvidyut c44b424
make proto
e6bb0db
Merge branch 'NET-5875' of ssh://github.com/hashicorp/consul into NET…
fc29782
some pr comments addressed
20e5781
some pr comments addressed
d729d59
acls helper
1c4cf98
Merge branch 'main' of ssh://github.com/hashicorp/consul into NET-5875
112ad62
Merge branch 'main' into NET-5875
asheshvidyut 7732609
some comment changes
82c0ddb
Merge branch 'NET-5875' of ssh://github.com/hashicorp/consul into NET…
5308961
removed unused files
d091b46
fixes
b4d0247
fix function in file
e6380c2
caps
07f3932
Merge branch 'main' into NET-5875
asheshvidyut e2b2052
Merge branch 'NET-5875' of ssh://github.com/hashicorp/consul into NET…
5f9589e
some positioing
d51bbdf
added test for validation error
e84e237
fix names
1c7efd8
made valid a function
e2e379b
remvoed patch
d982a03
Merge branch 'main' into NET-5875
asheshvidyut 3aac88d
removed mutations
678b311
Merge branch 'NET-5875' of ssh://github.com/hashicorp/consul into NET…
c6ea870
v2 beta1
999dd6f
v2beta1
3b37a0a
rmeoved v1alpha1
732e2ca
validate error
add5b45
Merge branch 'main' into NET-5875
asheshvidyut cd29bed
Merge branch 'NET-5875' of ssh://github.com/hashicorp/consul into NET…
11fd797
Merge branch 'main' of ssh://github.com/hashicorp/consul into NET-5875
a11ad98
merge ent
d5a4ccc
some nits
a837887
Merge branch 'main' into NET-5875
asheshvidyut 00984f1
removed dup func
5407a04
Merge branch 'NET-5875' of ssh://github.com/hashicorp/consul into NET…
4f963f0
removed nil check
b15d668
Merge branch 'main' into NET-5875
asheshvidyut d1892da
Merge branch 'NET-5875' of ssh://github.com/hashicorp/consul into NET…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package multicluster | ||
|
|
||
| import ( | ||
| "github.com/hashicorp/consul/internal/multicluster/internal/types" | ||
| "github.com/hashicorp/consul/internal/resource" | ||
| ) | ||
|
|
||
| var ( | ||
| // API Group Information | ||
| APIGroup = types.GroupName | ||
| VersionV2Beta1 = types.VersionV2Beta1 | ||
| CurrentVersion = types.CurrentVersion | ||
| ) | ||
|
|
||
| // RegisterTypes adds all resource types within the "multicluster" API group | ||
| // to the given type registry | ||
| func RegisterTypes(r resource.Registry) { | ||
| types.Register(r) | ||
| } |
37 changes: 37 additions & 0 deletions
37
internal/multicluster/internal/types/computed_exported_services.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "github.com/hashicorp/consul/acl" | ||
| "github.com/hashicorp/consul/internal/resource" | ||
| pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1" | ||
| "github.com/hashicorp/consul/proto-public/pbresource" | ||
| ) | ||
|
|
||
| const ( | ||
| ComputedExportedServicesName = "global" | ||
| ) | ||
|
|
||
| func RegisterComputedExportedServices(r resource.Registry) { | ||
| r.Register(resource.Registration{ | ||
| Type: pbmulticluster.ComputedExportedServicesType, | ||
| Proto: &pbmulticluster.ComputedExportedServices{}, | ||
| Scope: resource.ScopePartition, | ||
| Validate: ValidateComputedExportedServices, | ||
| ACLs: &resource.ACLHooks{ | ||
| Read: aclReadHookComputedExportedServices, | ||
| Write: aclWriteHookComputedExportedServices, | ||
| List: resource.NoOpACLListHook, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func aclReadHookComputedExportedServices(authorizer acl.Authorizer, authzContext *acl.AuthorizerContext, _ *pbresource.ID, res *pbresource.Resource) error { | ||
| return authorizer.ToAllowAuthorizer().MeshReadAllowed(authzContext) | ||
| } | ||
|
|
||
| func aclWriteHookComputedExportedServices(authorizer acl.Authorizer, authzContext *acl.AuthorizerContext, _ *pbresource.Resource) error { | ||
| return authorizer.ToAllowAuthorizer().MeshWriteAllowed(authzContext) | ||
| } |
178 changes: 178 additions & 0 deletions
178
internal/multicluster/internal/types/computed_exported_services_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "errors" | ||
| "github.com/hashicorp/consul/agent/structs" | ||
| "github.com/hashicorp/consul/internal/resource" | ||
| "github.com/hashicorp/consul/internal/resource/resourcetest" | ||
| pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1" | ||
| "github.com/hashicorp/consul/proto-public/pbresource" | ||
| "github.com/stretchr/testify/require" | ||
| "testing" | ||
| ) | ||
|
|
||
| func computedExportedServicesWithPartition(partitionName string) *pbmulticluster.ComputedExportedServices { | ||
| consumers := []*pbmulticluster.ComputedExportedService{ | ||
| { | ||
| Consumers: []*pbmulticluster.ComputedExportedServicesConsumer{ | ||
| { | ||
| ConsumerTenancy: &pbmulticluster.ComputedExportedServicesConsumer_Partition{ | ||
| Partition: partitionName, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| return &pbmulticluster.ComputedExportedServices{ | ||
| Consumers: consumers, | ||
| } | ||
| } | ||
|
|
||
| func computedExportedServicesWithPeer(peerName string) *pbmulticluster.ComputedExportedServices { | ||
| consumers := []*pbmulticluster.ComputedExportedService{ | ||
| { | ||
| Consumers: []*pbmulticluster.ComputedExportedServicesConsumer{ | ||
| { | ||
| ConsumerTenancy: &pbmulticluster.ComputedExportedServicesConsumer_Peer{ | ||
| Peer: peerName, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| return &pbmulticluster.ComputedExportedServices{ | ||
| Consumers: consumers, | ||
| } | ||
| } | ||
|
|
||
| func TestComputedExportedServicesValidations_InvalidName(t *testing.T) { | ||
| res := resourcetest.Resource(pbmulticluster.ComputedExportedServicesType, "computed-exported-services"). | ||
| WithData(t, computedExportedServicesWithPeer("peer")). | ||
| Build() | ||
|
|
||
| err := ValidateComputedExportedServices(res) | ||
| require.Error(t, err) | ||
| expectedError := errors.New("invalid \"name\" field: name can only be \"global\"") | ||
| require.ErrorAs(t, err, &expectedError) | ||
| } | ||
|
|
||
| func TestComputedExportedServicesACLs(t *testing.T) { | ||
| // Wire up a registry to generically invoke hooks | ||
| registry := resource.NewRegistry() | ||
| Register(registry) | ||
|
|
||
| type testcase struct { | ||
| rules string | ||
| readOK string | ||
| writeOK string | ||
| listOK string | ||
| } | ||
|
|
||
| const ( | ||
| DENY = resourcetest.DENY | ||
| ALLOW = resourcetest.ALLOW | ||
| DEFAULT = resourcetest.DEFAULT | ||
| ) | ||
|
|
||
| exportedServiceData := &pbmulticluster.ComputedExportedServices{} | ||
| res := resourcetest.Resource(pbmulticluster.ComputedExportedServicesType, "global"). | ||
| WithData(t, exportedServiceData). | ||
| Build() | ||
| resourcetest.ValidateAndNormalize(t, registry, res) | ||
|
|
||
| cases := map[string]testcase{ | ||
| "no rules": { | ||
| rules: ``, | ||
| readOK: DENY, | ||
| writeOK: DENY, | ||
| listOK: DEFAULT, | ||
| }, | ||
| "mesh read policy": { | ||
| rules: `mesh = "read"`, | ||
| readOK: ALLOW, | ||
| writeOK: DENY, | ||
| listOK: DEFAULT, | ||
| }, | ||
| "mesh write policy": { | ||
| rules: `mesh = "write"`, | ||
| readOK: ALLOW, | ||
| writeOK: ALLOW, | ||
| listOK: DEFAULT, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| aclTestCase := resourcetest.ACLTestCase{ | ||
| Rules: tc.rules, | ||
| Res: res, | ||
| ReadOK: tc.readOK, | ||
| WriteOK: tc.writeOK, | ||
| ListOK: tc.listOK, | ||
| } | ||
| resourcetest.RunACLTestCase(t, aclTestCase, registry) | ||
| } | ||
| } | ||
|
|
||
| func TestComputedExportedServicesValidations(t *testing.T) { | ||
| type testcase struct { | ||
| Resource *pbresource.Resource | ||
| expectErrorCE []string | ||
| expectErrorENT []string | ||
| } | ||
|
|
||
| isEnterprise := structs.NodeEnterpriseMetaInDefaultPartition().PartitionOrEmpty() == "default" | ||
|
|
||
| run := func(t *testing.T, tc testcase) { | ||
| expectError := tc.expectErrorCE | ||
| if isEnterprise { | ||
| expectError = tc.expectErrorENT | ||
| } | ||
| err := ValidateComputedExportedServices(tc.Resource) | ||
| if len(expectError) == 0 { | ||
| require.NoError(t, err) | ||
| } else { | ||
| require.Error(t, err) | ||
| for _, er := range expectError { | ||
| require.ErrorContains(t, err, er) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| cases := map[string]testcase{ | ||
| "computed exported services with peer": { | ||
| Resource: resourcetest.Resource(pbmulticluster.ComputedExportedServicesType, ComputedExportedServicesName). | ||
| WithData(t, computedExportedServicesWithPeer("peer")). | ||
| Build(), | ||
| }, | ||
| "computed exported services with partition": { | ||
| Resource: resourcetest.Resource(pbmulticluster.ComputedExportedServicesType, ComputedExportedServicesName). | ||
| WithData(t, computedExportedServicesWithPartition("partition")). | ||
| Build(), | ||
| expectErrorCE: []string{`invalid element at index 0 of list "partition": can only be set in Enterprise`}, | ||
| }, | ||
| "computed exported services with peer empty": { | ||
| Resource: resourcetest.Resource(pbmulticluster.ComputedExportedServicesType, ComputedExportedServicesName). | ||
| WithData(t, computedExportedServicesWithPeer("")). | ||
| Build(), | ||
| expectErrorCE: []string{`invalid element at index 0 of list "peer": can not be empty`}, | ||
| expectErrorENT: []string{`invalid element at index 0 of list "peer": can not be empty`}, | ||
| }, | ||
| "computed exported services with partition empty": { | ||
| Resource: resourcetest.Resource(pbmulticluster.ComputedExportedServicesType, ComputedExportedServicesName). | ||
| WithData(t, computedExportedServicesWithPartition("")). | ||
| Build(), | ||
| expectErrorCE: []string{`invalid element at index 0 of list "partition": can not be empty`, | ||
| `invalid element at index 0 of list "partition": can only be set in Enterprise`}, | ||
| expectErrorENT: []string{`invalid element at index 0 of list "partition": can not be empty`}, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| run(t, tc) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "github.com/hashicorp/consul/acl" | ||
| "github.com/hashicorp/consul/internal/resource" | ||
| pbmulticluster "github.com/hashicorp/consul/proto-public/pbmulticluster/v2beta1" | ||
| "github.com/hashicorp/consul/proto-public/pbresource" | ||
| ) | ||
|
|
||
| func RegisterExportedServices(r resource.Registry) { | ||
| r.Register(resource.Registration{ | ||
| Type: pbmulticluster.ExportedServicesType, | ||
| Proto: &pbmulticluster.ExportedServices{}, | ||
| Scope: resource.ScopeNamespace, | ||
| Validate: ValidateExportedServices, | ||
| ACLs: &resource.ACLHooks{ | ||
| Read: aclReadHookExportedServices, | ||
| Write: aclWriteHookExportedServices, | ||
| List: resource.NoOpACLListHook, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func aclReadHookExportedServices(authorizer acl.Authorizer, authzContext *acl.AuthorizerContext, _ *pbresource.ID, res *pbresource.Resource) error { | ||
| if res == nil { | ||
| return resource.ErrNeedResource | ||
| } | ||
|
|
||
| var exportedService pbmulticluster.ExportedServices | ||
|
|
||
| if err := res.Data.UnmarshalTo(&exportedService); err != nil { | ||
| return resource.NewErrDataParse(&exportedService, err) | ||
| } | ||
|
|
||
| for _, serviceName := range exportedService.Services { | ||
| if err := authorizer.ToAllowAuthorizer().ServiceReadAllowed(serviceName, authzContext); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func aclWriteHookExportedServices(authorizer acl.Authorizer, authzContext *acl.AuthorizerContext, res *pbresource.Resource) error { | ||
| var exportedService pbmulticluster.ExportedServices | ||
|
|
||
| if err := res.Data.UnmarshalTo(&exportedService); err != nil { | ||
| return resource.NewErrDataParse(&exportedService, err) | ||
| } | ||
asheshvidyut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for _, serviceName := range exportedService.Services { | ||
| if err := authorizer.ToAllowAuthorizer().ServiceWriteAllowed(serviceName, authzContext); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.