-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
catalog, mesh: implement missing ACL hooks #19143
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab434f9
wip: proxy cfg ctrl without tests
ishustava 5361cf9
mesh: add ComputedDestinations and controller that computes them
ishustava 24107fd
review comments + rebase
ishustava 2096263
more review comments
ishustava 9011dd2
mesh,catalog: add missing ACL hooks
ishustava b1056a0
review comments
ishustava 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 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 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 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 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 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
21 changes: 21 additions & 0 deletions
21
internal/catalog/catalogtest/helpers/acl_hooks_test_helpers.go
This file contains 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,21 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/internal/catalog" | ||
"github.com/hashicorp/consul/internal/catalog/internal/testhelpers" | ||
"github.com/hashicorp/consul/internal/resource" | ||
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1" | ||
"github.com/hashicorp/consul/proto-public/pbresource" | ||
) | ||
|
||
func RunWorkloadSelectingTypeACLsTests[T catalog.WorkloadSelecting](t *testing.T, typ *pbresource.Type, | ||
getData func(selector *pbcatalog.WorkloadSelector) T, | ||
registerFunc func(registry resource.Registry), | ||
) { | ||
testhelpers.RunWorkloadSelectingTypeACLsTests[T](t, typ, getData, registerFunc) | ||
} |
This file contains 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 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
113 changes: 113 additions & 0 deletions
113
internal/catalog/internal/testhelpers/acl_hooks_test_helpers.go
This file contains 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,113 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package testhelpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/hashicorp/consul/internal/resource" | ||
"github.com/hashicorp/consul/internal/resource/resourcetest" | ||
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1" | ||
"github.com/hashicorp/consul/proto-public/pbresource" | ||
) | ||
|
||
// WorkloadSelecting denotes a resource type that uses workload selectors. | ||
type WorkloadSelecting interface { | ||
proto.Message | ||
GetWorkloads() *pbcatalog.WorkloadSelector | ||
} | ||
|
||
func RunWorkloadSelectingTypeACLsTests[T WorkloadSelecting](t *testing.T, typ *pbresource.Type, | ||
getData func(selector *pbcatalog.WorkloadSelector) T, | ||
registerFunc func(registry resource.Registry), | ||
) { | ||
// Wire up a registry to generically invoke hooks | ||
registry := resource.NewRegistry() | ||
registerFunc(registry) | ||
|
||
cases := map[string]resourcetest.ACLTestCase{ | ||
"no rules": { | ||
Rules: ``, | ||
Data: getData(&pbcatalog.WorkloadSelector{Names: []string{"workload"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.DENY, | ||
WriteOK: resourcetest.DENY, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
"service test read": { | ||
Rules: `service "test" { policy = "read" }`, | ||
Data: getData(&pbcatalog.WorkloadSelector{Names: []string{"workload"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.ALLOW, | ||
WriteOK: resourcetest.DENY, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
"service test write with named selectors and insufficient policy": { | ||
Rules: `service "test" { policy = "write" }`, | ||
Data: getData(&pbcatalog.WorkloadSelector{Names: []string{"workload"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.ALLOW, | ||
WriteOK: resourcetest.DENY, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
"service test write with prefixed selectors and insufficient policy": { | ||
Rules: `service "test" { policy = "write" }`, | ||
Data: getData(&pbcatalog.WorkloadSelector{Prefixes: []string{"workload"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.ALLOW, | ||
WriteOK: resourcetest.DENY, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
"service test write with named selectors": { | ||
Rules: `service "test" { policy = "write" } service "workload" { policy = "read" }`, | ||
Data: getData(&pbcatalog.WorkloadSelector{Names: []string{"workload"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.ALLOW, | ||
WriteOK: resourcetest.ALLOW, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
"service test write with prefixed selectors": { | ||
Rules: `service "test" { policy = "write" } service_prefix "workload-" { policy = "read" }`, | ||
Data: getData(&pbcatalog.WorkloadSelector{Prefixes: []string{"workload-"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.ALLOW, | ||
WriteOK: resourcetest.ALLOW, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
"service test write with prefixed selectors and a policy with more specific than the selector": { | ||
Rules: `service "test" { policy = "write" } service_prefix "workload-" { policy = "read" }`, | ||
Data: getData(&pbcatalog.WorkloadSelector{Prefixes: []string{"wor"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.ALLOW, | ||
WriteOK: resourcetest.DENY, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
"service test write with prefixed selectors and a policy with less specific than the selector": { | ||
Rules: `service "test" { policy = "write" } service_prefix "wor" { policy = "read" }`, | ||
Data: getData(&pbcatalog.WorkloadSelector{Prefixes: []string{"workload-"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.ALLOW, | ||
WriteOK: resourcetest.ALLOW, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
"service test write with prefixed selectors and a policy with a specific service": { | ||
Rules: `service "test" { policy = "write" } service "workload" { policy = "read" }`, | ||
Data: getData(&pbcatalog.WorkloadSelector{Prefixes: []string{"workload"}}), | ||
Typ: typ, | ||
ReadOK: resourcetest.ALLOW, | ||
// TODO (ishustava): this is wrong and should be fixed in a follow up PR. We should not allow | ||
// a policy for a specific service when only prefixes are specified in the selector. | ||
WriteOK: resourcetest.ALLOW, | ||
ListOK: resourcetest.DEFAULT, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
resourcetest.RunACLTestCase(t, tc, registry) | ||
}) | ||
} | ||
} |
This file contains 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,56 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package types | ||
|
||
import ( | ||
"github.com/hashicorp/consul/acl" | ||
"github.com/hashicorp/consul/internal/resource" | ||
"github.com/hashicorp/consul/proto-public/pbresource" | ||
) | ||
|
||
func aclReadHookResourceWithWorkloadSelector(authorizer acl.Authorizer, authzContext *acl.AuthorizerContext, id *pbresource.ID, _ *pbresource.Resource) error { | ||
return authorizer.ToAllowAuthorizer().ServiceReadAllowed(id.GetName(), authzContext) | ||
} | ||
|
||
func aclWriteHookResourceWithWorkloadSelector[T WorkloadSelecting](authorizer acl.Authorizer, authzContext *acl.AuthorizerContext, res *pbresource.Resource) error { | ||
if res == nil { | ||
return resource.ErrNeedResource | ||
} | ||
|
||
decodedService, err := resource.Decode[T](res) | ||
if err != nil { | ||
return resource.ErrNeedResource | ||
} | ||
|
||
// First check service:write on the name. | ||
err = authorizer.ToAllowAuthorizer().ServiceWriteAllowed(res.GetId().GetName(), authzContext) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Then also check whether we're allowed to select a service. | ||
for _, name := range decodedService.GetData().GetWorkloads().GetNames() { | ||
err = authorizer.ToAllowAuthorizer().ServiceReadAllowed(name, authzContext) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for _, prefix := range decodedService.GetData().GetWorkloads().GetPrefixes() { | ||
err = authorizer.ToAllowAuthorizer().ServiceReadAllowed(prefix, authzContext) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ACLHooksForWorkloadSelectingType[T WorkloadSelecting]() *resource.ACLHooks { | ||
return &resource.ACLHooks{ | ||
Read: aclReadHookResourceWithWorkloadSelector, | ||
Write: aclWriteHookResourceWithWorkloadSelector[T], | ||
List: resource.NoOpACLListHook, | ||
} | ||
} |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this safe? I'm imagining a case where a workload selector is selecting all workloads with a prefix of
api
and they haveservice_prefix "api1" { policy = "read" }
. I believe this check would fail becaue they don't have service read onapi
or service prefix on a subset ofapi
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current behavior is to deny this write (not sure if that's what you meant by this check failing).
I think this is correct because if you have a workload selector
Workloads = { Prefixes = ["api"] }
, your service, for example, can select any workloads that start withapi
(api1
,api2
etc). But your policy only allows you to write a service selecting workloads that start withapi1
. And so we should deny this request because otherwise you can select more workloads than what you're allowed to.Let me know if I misunderstood you though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We talked about this in private and agreed to merging this as is and following up next week.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to properly solve this we need to have a different way to authorize prefixes which will involve more changes to policy authorizer. I think it makes sense to do in a separate PR.