Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ishustava committed Oct 13, 2023
1 parent 1dc48cc commit de194fe
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func RunWorkloadSelectingTypeACLsTests[T WorkloadSelecting](t *testing.T, typ *p
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,
WriteOK: resourcetest.DENY,
ListOK: resourcetest.DEFAULT,
},
}

for name, tc := range cases {
Expand Down
5 changes: 2 additions & 3 deletions internal/catalog/internal/types/dns_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ func TestDNSPolicyACLs(t *testing.T) {
Weights: &pbcatalog.Weights{Passing: 1, Warning: 0},
}
},
func(registry resource.Registry) {
RegisterDNSPolicy(registry)
})
RegisterDNSPolicy,
)
}
5 changes: 2 additions & 3 deletions internal/catalog/internal/types/health_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func TestHealthChecksACLs(t *testing.T) {
func(selector *pbcatalog.WorkloadSelector) *pbcatalog.HealthChecks {
return &pbcatalog.HealthChecks{Workloads: selector}
},
func(registry resource.Registry) {
RegisterHealthChecks(registry)
})
RegisterHealthChecks,
)
}
18 changes: 18 additions & 0 deletions internal/catalog/internal/types/health_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ func TestHealthStatusACLs(t *testing.T) {
WriteOK: resourcetest.ALLOW,
ListOK: resourcetest.DEFAULT,
},
"node test read with workload owner": {
Rules: `node "test" { policy = "read" }`,
Data: healthStatusData,
Owner: workload,
Typ: pbcatalog.HealthStatusType,
ReadOK: resourcetest.DENY,
WriteOK: resourcetest.DENY,
ListOK: resourcetest.DEFAULT,
},
"node test write with workload owner": {
Rules: `node "test" { policy = "write" }`,
Data: healthStatusData,
Owner: workload,
Typ: pbcatalog.HealthStatusType,
ReadOK: resourcetest.DENY,
WriteOK: resourcetest.DENY,
ListOK: resourcetest.DEFAULT,
},
}

for name, tc := range cases {
Expand Down
5 changes: 2 additions & 3 deletions internal/catalog/internal/types/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ func TestServiceACLs(t *testing.T) {
func(selector *pbcatalog.WorkloadSelector) *pbcatalog.Service {
return &pbcatalog.Service{Workloads: selector}
},
func(registry resource.Registry) {
RegisterService(registry)
})
RegisterService,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

catalogtesthelpers "github.com/hashicorp/consul/internal/catalog/catalogtest/helpers"
"github.com/hashicorp/consul/internal/resource"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
)
Expand All @@ -17,7 +16,6 @@ func TestDestinationsConfigurationACLs(t *testing.T) {
func(selector *pbcatalog.WorkloadSelector) *pbmesh.DestinationsConfiguration {
return &pbmesh.DestinationsConfiguration{Workloads: selector}
},
func(registry resource.Registry) {
RegisterDestinationsConfiguration(registry)
})
RegisterDestinationsConfiguration,
)
}
5 changes: 2 additions & 3 deletions internal/mesh/internal/types/destinations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func TestDestinationsACLs(t *testing.T) {
func(selector *pbcatalog.WorkloadSelector) *pbmesh.Destinations {
return &pbmesh.Destinations{Workloads: selector}
},
func(registry resource.Registry) {
RegisterDestinations(registry)
})
RegisterDestinations,
)
}
6 changes: 2 additions & 4 deletions internal/mesh/internal/types/proxy_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/require"

catalogtesthelpers "github.com/hashicorp/consul/internal/catalog/catalogtest/helpers"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/resourcetest"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v2beta1"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
Expand All @@ -22,9 +21,8 @@ func TestProxyConfigurationACLs(t *testing.T) {
func(selector *pbcatalog.WorkloadSelector) *pbmesh.ProxyConfiguration {
return &pbmesh.ProxyConfiguration{Workloads: selector}
},
func(registry resource.Registry) {
RegisterProxyConfiguration(registry)
})
RegisterProxyConfiguration,
)
}

func TestMutateProxyConfiguration(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion internal/resource/resourcetest/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ func RunACLTestCase(t *testing.T, tc ACLTestCase, registry resource.Registry) {
reg, ok := registry.Resolve(tc.Typ)
require.True(t, ok)

resolvedType, ok := registry.Resolve(tc.Typ)
require.True(t, ok)

res := Resource(tc.Typ, "test").
WithTenancy(resource.DefaultNamespacedTenancy()).
WithTenancy(DefaultTenancyForType(t, resolvedType)).
WithOwner(tc.Owner).
WithData(t, tc.Data).
Build()

ValidateAndNormalize(t, registry, res)

config := acl.Config{
Expand Down
23 changes: 23 additions & 0 deletions internal/resource/resourcetest/tenancy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package resourcetest

import (
"testing"

"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/proto-public/pbresource"
)

func DefaultTenancyForType(t *testing.T, reg resource.Registration) *pbresource.Tenancy {
switch reg.Scope {
case resource.ScopeNamespace:
return resource.DefaultNamespacedTenancy()
case resource.ScopePartition:
return resource.DefaultPartitionedTenancy()
case resource.ScopeCluster:
return resource.DefaultClusteredTenancy()
default:
t.Fatalf("unsupported resource scope: %v", reg.Scope)
return nil
}
return nil
}

0 comments on commit de194fe

Please sign in to comment.