Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 8e7b28b

Browse files
committed
This PR has 2 main changes:
1. Change the policy builder's AddPrincipal, to AddIdentity so that the builder accepts OSM constructs as inputs, and returns envoy constructs. 2. Set the trust domain (currently an empty string) on the listener builder so that it will be used in the future. Part of #4754 Signed-off-by: Sean Teeling <[email protected]>
1 parent 914e8f3 commit 8e7b28b

File tree

11 files changed

+65
-40
lines changed

11 files changed

+65
-40
lines changed

pkg/certificate/manager.go

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ func (m *Manager) Start(checkInterval time.Duration, stop <-chan struct{}) {
5656
}()
5757
}
5858

59+
// GetTrustDomain returns the trust domain from the configured signingkey issuer.
60+
// Note that the CRD uses a default, so this value will always be set.
61+
func (m *Manager) GetTrustDomain() string {
62+
// TODO(4754): implement
63+
return ""
64+
}
65+
5966
func (m *Manager) checkAndRotate() {
6067
// NOTE: checkAndRotate can reintroduce a certificate that has been released, thereby creating an unbounded cache.
6168
// A certificate can also have been rotated already, leaving the list of issued certs stale, and we re-rotate.

pkg/certificate/types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ type Issuer interface {
7272

7373
type issuer struct {
7474
Issuer
75-
ID string
75+
ID string
76+
TrustDomain string
7677
// memoized once the first certificate is issued
7778
CertificateAuthority pem.RootCertificate
7879
}

pkg/envoy/lds/inmesh_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func TestGetOutboundFilterChainMatchForService(t *testing.T) {
406406
mockConfigurator := configurator.NewMockConfigurator(mockCtrl)
407407
mockCatalog := catalog.NewMockMeshCataloger(mockCtrl)
408408

409-
lb := newListenerBuilder(mockCatalog, tests.BookbuyerServiceIdentity, mockConfigurator, nil)
409+
lb := newListenerBuilder(mockCatalog, tests.BookbuyerServiceIdentity, mockConfigurator, nil, "cluster.local")
410410

411411
testCases := []struct {
412412
name string
@@ -581,7 +581,7 @@ func TestGetOutboundTCPFilter(t *testing.T) {
581581
mockCatalog := catalog.NewMockMeshCataloger(mockCtrl)
582582
mockConfigurator := configurator.NewMockConfigurator(mockCtrl)
583583

584-
lb := newListenerBuilder(mockCatalog, tests.BookbuyerServiceIdentity, mockConfigurator, nil)
584+
lb := newListenerBuilder(mockCatalog, tests.BookbuyerServiceIdentity, mockConfigurator, nil, "cluster.local")
585585
filter, err := lb.getOutboundTCPFilter(tc.trafficMatch)
586586

587587
assert := tassert.New(t)

pkg/envoy/lds/listener_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func TestNewOutboundListener(t *testing.T) {
196196
EnableEgressPolicy: true,
197197
}).Times(1)
198198

199-
lb := newListenerBuilder(meshCatalog, identity, cfg, nil)
199+
lb := newListenerBuilder(meshCatalog, identity, cfg, nil, "cluster.local")
200200

201201
assert := tassert.New(t)
202202
listener, err := lb.newOutboundListener()

pkg/envoy/lds/rbac.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (lb *listenerBuilder) buildInboundRBACPolicies() (*xds_network_rbac.RBAC, e
5050
rbacPolicies := make(map[string]*xds_rbac.Policy)
5151
// Build an RBAC policies based on SMI TrafficTarget policies
5252
for _, targetPolicy := range trafficTargets {
53-
rbacPolicies[targetPolicy.Name] = buildRBACPolicyFromTrafficTarget(targetPolicy)
53+
rbacPolicies[targetPolicy.Name] = buildRBACPolicyFromTrafficTarget(targetPolicy, lb.trustDomain)
5454
}
5555

5656
log.Debug().Msgf("RBAC policy for proxy with identity %s: %+v", proxyIdentity, rbacPolicies)
@@ -68,12 +68,12 @@ func (lb *listenerBuilder) buildInboundRBACPolicies() (*xds_network_rbac.RBAC, e
6868
}
6969

7070
// buildRBACPolicyFromTrafficTarget creates an XDS RBAC policy from the given traffic target policy
71-
func buildRBACPolicyFromTrafficTarget(trafficTarget trafficpolicy.TrafficTargetWithRoutes) *xds_rbac.Policy {
71+
func buildRBACPolicyFromTrafficTarget(trafficTarget trafficpolicy.TrafficTargetWithRoutes, trustDomain string) *xds_rbac.Policy {
7272
pb := &rbac.PolicyBuilder{}
7373

74-
// Create the list of principals for this policy
75-
for _, downstreamPrincipal := range trafficTarget.Sources {
76-
pb.AddPrincipal(downstreamPrincipal.String())
74+
// Create the list of identities for this policy
75+
for _, downstreamIdentity := range trafficTarget.Sources {
76+
pb.AddIdentity(downstreamIdentity)
7777
}
7878
// Create the list of permissions for this policy
7979
for _, tcpRouteMatch := range trafficTarget.TCPRouteMatches {
@@ -83,5 +83,7 @@ func buildRBACPolicyFromTrafficTarget(trafficTarget trafficpolicy.TrafficTargetW
8383
}
8484
}
8585

86+
pb.SetTrustDomain(trustDomain)
87+
8688
return pb.Build()
8789
}

pkg/envoy/lds/rbac_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestBuildRBACPolicyFromTrafficTarget(t *testing.T) {
8989
assert := tassert.New(t)
9090

9191
// Test the RBAC policies
92-
policy := buildRBACPolicyFromTrafficTarget(tc.trafficTarget)
92+
policy := buildRBACPolicyFromTrafficTarget(tc.trafficTarget, "cluster.local")
9393

9494
assert.Equal(tc.expectedPolicy, policy)
9595
})

pkg/envoy/lds/response.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ import (
1919
// 1. Inbound listener to handle incoming traffic
2020
// 2. Outbound listener to handle outgoing traffic
2121
// 3. Prometheus listener for metrics
22-
func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_discovery.DiscoveryRequest, cfg configurator.Configurator, _ *certificate.Manager, proxyRegistry *registry.ProxyRegistry) ([]types.Resource, error) {
22+
func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_discovery.DiscoveryRequest, cfg configurator.Configurator, cm *certificate.Manager, proxyRegistry *registry.ProxyRegistry) ([]types.Resource, error) {
2323
var ldsResources []types.Resource
2424

2525
var statsHeaders map[string]string
2626
if featureflags := cfg.GetFeatureFlags(); featureflags.EnableWASMStats {
2727
statsHeaders = proxy.StatsHeaders()
2828
}
2929

30-
lb := newListenerBuilder(meshCatalog, proxy.Identity, cfg, statsHeaders)
30+
lb := newListenerBuilder(meshCatalog, proxy.Identity, cfg, statsHeaders, cm.GetTrustDomain())
3131

3232
if proxy.Kind() == envoy.KindGateway && cfg.GetFeatureFlags().EnableMulticlusterMode {
3333
gatewayListener, err := lb.buildMulticlusterGatewayListener()
@@ -96,11 +96,12 @@ func NewResponse(meshCatalog catalog.MeshCataloger, proxy *envoy.Proxy, _ *xds_d
9696
}
9797

9898
// Note: ServiceIdentity must be in the format "name.namespace" [https://github.com/openservicemesh/osm/issues/3188]
99-
func newListenerBuilder(meshCatalog catalog.MeshCataloger, svcIdentity identity.ServiceIdentity, cfg configurator.Configurator, statsHeaders map[string]string) *listenerBuilder {
99+
func newListenerBuilder(meshCatalog catalog.MeshCataloger, svcIdentity identity.ServiceIdentity, cfg configurator.Configurator, statsHeaders map[string]string, trustDomain string) *listenerBuilder {
100100
return &listenerBuilder{
101101
meshCatalog: meshCatalog,
102102
serviceIdentity: svcIdentity,
103103
cfg: cfg,
104104
statsHeaders: statsHeaders,
105+
trustDomain: trustDomain,
105106
}
106107
}

pkg/envoy/lds/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ type listenerBuilder struct {
1919
meshCatalog catalog.MeshCataloger
2020
cfg configurator.Configurator
2121
statsHeaders map[string]string
22+
trustDomain string
2223
}

pkg/envoy/rbac/policy.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@ package rbac
33
import (
44
xds_rbac "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v3"
55
xds_matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
6+
7+
"github.com/openservicemesh/osm/pkg/identity"
68
)
79

810
// PolicyBuilder is a utility for constructing *xds_rbac.Policy's
911
type PolicyBuilder struct {
1012
allowedPorts []uint32
11-
allowedPrincipals []string
13+
allowedIdentities []identity.ServiceIdentity
1214
allowAllPrincipals bool
1315

1416
// All permissions are applied using OR semantics by default. If applyPermissionsAsAnd is set to true, then
1517
// permissions are applied using AND semantics.
1618
applyPermissionsAsAnd bool
19+
trustDomain string
1720
}
1821

1922
// Build constructs an RBAC policy for the policy object on which this method is called
2023
func (p *PolicyBuilder) Build() *xds_rbac.Policy {
2124
policy := &xds_rbac.Policy{}
2225

2326
// Each RuleList follows OR semantics with other RuleList in the list of RuleList
24-
prinicipals := make([]*xds_rbac.Principal, 0, len(p.allowedPrincipals))
25-
for _, principal := range p.allowedPrincipals {
26-
prinicipals = append(prinicipals, GetAuthenticatedPrincipal(principal))
27+
prinicipals := make([]*xds_rbac.Principal, 0, len(p.allowedIdentities))
28+
for _, svcIdentity := range p.allowedIdentities {
29+
prinicipals = append(prinicipals, GetAuthenticatedPrincipal(svcIdentity.AsPrincipal(p.trustDomain)))
2730
}
2831
if len(prinicipals) == 0 {
2932
// No principals specified for this policy, allow ANY
@@ -62,21 +65,21 @@ func (p *PolicyBuilder) UseANDForPermissions(val bool) {
6265
p.applyPermissionsAsAnd = val
6366
}
6467

65-
// AddPrincipal adds a principal to the list of allowed principals
66-
func (p *PolicyBuilder) AddPrincipal(principal string) {
68+
// AddIdentity adds an identity, later to be converted to a principal, to the list of allowed identities.
69+
func (p *PolicyBuilder) AddIdentity(svcIdentity identity.ServiceIdentity) {
6770
// We need this extra defense in depth because it is currently possible to configure a wildcard principal
6871
// in addition to specific principals. Future changes may look to avoid this.
69-
if principal == "*" {
70-
p.AllowAnyPrincipal()
72+
if svcIdentity.IsWildcard() {
73+
p.AllowAnyIdentity()
7174
}
7275
if !p.allowAllPrincipals {
73-
p.allowedPrincipals = append(p.allowedPrincipals, principal)
76+
p.allowedIdentities = append(p.allowedIdentities, svcIdentity)
7477
}
7578
}
7679

77-
// AllowAnyPrincipal allows any principal to access the permissions.
78-
func (p *PolicyBuilder) AllowAnyPrincipal() {
79-
p.allowedPrincipals = nil
80+
// AllowAnyIdentity allows any principal to access the permissions.
81+
func (p *PolicyBuilder) AllowAnyIdentity() {
82+
p.allowedIdentities = nil
8083
p.allowAllPrincipals = true
8184
}
8285

@@ -86,6 +89,11 @@ func (p *PolicyBuilder) AddAllowedDestinationPort(port uint16) {
8689
p.allowedPorts = append(p.allowedPorts, uint32(port))
8790
}
8891

92+
// SetTrustDomain sets the trust domain for the policy, which is used when converting a ServiceIdentity to a Principal.
93+
func (p *PolicyBuilder) SetTrustDomain(td string) {
94+
p.trustDomain = td
95+
}
96+
8997
// GetAuthenticatedPrincipal returns an authenticated RBAC principal object for the given principal
9098
func GetAuthenticatedPrincipal(principalName string) *xds_rbac.Principal {
9199
return &xds_rbac.Principal{

pkg/envoy/rbac/policy_test.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ import (
66

77
tassert "github.com/stretchr/testify/assert"
88

9+
"github.com/openservicemesh/osm/pkg/identity"
10+
911
xds_rbac "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v3"
1012
xds_matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
1113
)
1214

1315
func TestBuild(t *testing.T) {
1416
testCases := []struct {
1517
name string
16-
principals []string
18+
identities []identity.ServiceIdentity
1719
ports []uint16
1820
applyPermissionsAsAND bool
21+
trustDomain string
1922
expectedPolicy *xds_rbac.Policy
2023
}{
2124
{
2225
name: "testing rules for single principal",
23-
principals: []string{"foo.domain", "bar.domain"},
26+
identities: []identity.ServiceIdentity{identity.New("foo", "domain"), identity.New("bar", "domain")},
2427
ports: []uint16{80},
2528
expectedPolicy: &xds_rbac.Policy{
2629
Principals: []*xds_rbac.Principal{
@@ -29,7 +32,7 @@ func TestBuild(t *testing.T) {
2932
Authenticated: &xds_rbac.Principal_Authenticated{
3033
PrincipalName: &xds_matcher.StringMatcher{
3134
MatchPattern: &xds_matcher.StringMatcher_Exact{
32-
Exact: "foo.domain",
35+
Exact: "foo.domain.cluster.local",
3336
},
3437
},
3538
},
@@ -40,7 +43,7 @@ func TestBuild(t *testing.T) {
4043
Authenticated: &xds_rbac.Principal_Authenticated{
4144
PrincipalName: &xds_matcher.StringMatcher{
4245
MatchPattern: &xds_matcher.StringMatcher_Exact{
43-
Exact: "bar.domain",
46+
Exact: "bar.domain.cluster.local",
4447
},
4548
},
4649
},
@@ -57,17 +60,18 @@ func TestBuild(t *testing.T) {
5760
},
5861
},
5962
{
60-
name: "testing rules for single principal",
61-
principals: []string{"foo.domain"},
62-
ports: []uint16{80, 443},
63+
name: "testing rules for single principal",
64+
identities: []identity.ServiceIdentity{identity.New("foo", "domain")},
65+
trustDomain: "cluster.local",
66+
ports: []uint16{80, 443},
6367
expectedPolicy: &xds_rbac.Policy{
6468
Principals: []*xds_rbac.Principal{
6569
{
6670
Identifier: &xds_rbac.Principal_Authenticated_{
6771
Authenticated: &xds_rbac.Principal_Authenticated{
6872
PrincipalName: &xds_matcher.StringMatcher{
6973
MatchPattern: &xds_matcher.StringMatcher_Exact{
70-
Exact: "foo.domain",
74+
Exact: "foo.domain.cluster.local",
7175
},
7276
},
7377
},
@@ -92,17 +96,18 @@ func TestBuild(t *testing.T) {
9296
// Note that AND ports wouldn't make sense, since you can't have 2 ports at once, but we use it to test
9397
// the logic.
9498
name: "testing rules for AND ports",
95-
principals: []string{"foo.domain"},
99+
identities: []identity.ServiceIdentity{identity.New("foo", "domain")},
96100
ports: []uint16{80, 443},
97101
applyPermissionsAsAND: true,
102+
trustDomain: "cluster.local",
98103
expectedPolicy: &xds_rbac.Policy{
99104
Principals: []*xds_rbac.Principal{
100105
{
101106
Identifier: &xds_rbac.Principal_Authenticated_{
102107
Authenticated: &xds_rbac.Principal_Authenticated{
103108
PrincipalName: &xds_matcher.StringMatcher{
104109
MatchPattern: &xds_matcher.StringMatcher_Exact{
105-
Exact: "foo.domain",
110+
Exact: "foo.domain.cluster.local",
106111
},
107112
},
108113
},
@@ -133,7 +138,7 @@ func TestBuild(t *testing.T) {
133138
},
134139
{
135140
name: "testing rule for ANY principal when no ports specified",
136-
principals: []string{"foo.domain", "*"},
141+
identities: []identity.ServiceIdentity{identity.New("foo", "domain"), identity.WildcardServiceIdentity},
137142
expectedPolicy: &xds_rbac.Policy{
138143
Principals: []*xds_rbac.Principal{
139144
{
@@ -169,16 +174,16 @@ func TestBuild(t *testing.T) {
169174
assert := tassert.New(t)
170175

171176
pb := &PolicyBuilder{}
172-
for _, principal := range tc.principals {
173-
pb.AddPrincipal(principal)
177+
for _, svcIdentity := range tc.identities {
178+
pb.AddIdentity(svcIdentity)
174179
}
175180
for _, port := range tc.ports {
176181
pb.AddAllowedDestinationPort(port)
177182
}
178183

179184
pb.UseANDForPermissions(tc.applyPermissionsAsAND)
180185
policy := pb.Build()
181-
assert.Equal(policy, tc.expectedPolicy)
186+
assert.Equal(tc.expectedPolicy, policy)
182187
})
183188
}
184189
}

pkg/envoy/rds/route/rbac.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func buildInboundRBACFilterForRule(rule *trafficpolicy.Rule) (map[string]*any.An
3030

3131
// Create the list of principals for this policy
3232
for downstream := range rule.AllowedServiceIdentities.Iter() {
33-
pb.AddPrincipal(downstream.(identity.ServiceIdentity).String())
33+
pb.AddIdentity(downstream.(identity.ServiceIdentity))
3434
}
3535

3636
// A single RBAC policy per route

0 commit comments

Comments
 (0)