@@ -6,21 +6,24 @@ import (
6
6
7
7
tassert "github.com/stretchr/testify/assert"
8
8
9
+ "github.com/openservicemesh/osm/pkg/identity"
10
+
9
11
xds_rbac "github.com/envoyproxy/go-control-plane/envoy/config/rbac/v3"
10
12
xds_matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
11
13
)
12
14
13
15
func TestBuild (t * testing.T ) {
14
16
testCases := []struct {
15
17
name string
16
- principals []string
18
+ identities []identity. ServiceIdentity
17
19
ports []uint16
18
20
applyPermissionsAsAND bool
21
+ trustDomain string
19
22
expectedPolicy * xds_rbac.Policy
20
23
}{
21
24
{
22
25
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") },
24
27
ports : []uint16 {80 },
25
28
expectedPolicy : & xds_rbac.Policy {
26
29
Principals : []* xds_rbac.Principal {
@@ -29,7 +32,7 @@ func TestBuild(t *testing.T) {
29
32
Authenticated : & xds_rbac.Principal_Authenticated {
30
33
PrincipalName : & xds_matcher.StringMatcher {
31
34
MatchPattern : & xds_matcher.StringMatcher_Exact {
32
- Exact : "foo.domain" ,
35
+ Exact : "foo.domain.cluster.local " ,
33
36
},
34
37
},
35
38
},
@@ -40,7 +43,7 @@ func TestBuild(t *testing.T) {
40
43
Authenticated : & xds_rbac.Principal_Authenticated {
41
44
PrincipalName : & xds_matcher.StringMatcher {
42
45
MatchPattern : & xds_matcher.StringMatcher_Exact {
43
- Exact : "bar.domain" ,
46
+ Exact : "bar.domain.cluster.local " ,
44
47
},
45
48
},
46
49
},
@@ -57,17 +60,18 @@ func TestBuild(t *testing.T) {
57
60
},
58
61
},
59
62
{
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 },
63
67
expectedPolicy : & xds_rbac.Policy {
64
68
Principals : []* xds_rbac.Principal {
65
69
{
66
70
Identifier : & xds_rbac.Principal_Authenticated_ {
67
71
Authenticated : & xds_rbac.Principal_Authenticated {
68
72
PrincipalName : & xds_matcher.StringMatcher {
69
73
MatchPattern : & xds_matcher.StringMatcher_Exact {
70
- Exact : "foo.domain" ,
74
+ Exact : "foo.domain.cluster.local " ,
71
75
},
72
76
},
73
77
},
@@ -92,17 +96,18 @@ func TestBuild(t *testing.T) {
92
96
// Note that AND ports wouldn't make sense, since you can't have 2 ports at once, but we use it to test
93
97
// the logic.
94
98
name : "testing rules for AND ports" ,
95
- principals : []string { "foo. domain" },
99
+ identities : []identity. ServiceIdentity { identity . New ( "foo" , " domain") },
96
100
ports : []uint16 {80 , 443 },
97
101
applyPermissionsAsAND : true ,
102
+ trustDomain : "cluster.local" ,
98
103
expectedPolicy : & xds_rbac.Policy {
99
104
Principals : []* xds_rbac.Principal {
100
105
{
101
106
Identifier : & xds_rbac.Principal_Authenticated_ {
102
107
Authenticated : & xds_rbac.Principal_Authenticated {
103
108
PrincipalName : & xds_matcher.StringMatcher {
104
109
MatchPattern : & xds_matcher.StringMatcher_Exact {
105
- Exact : "foo.domain" ,
110
+ Exact : "foo.domain.cluster.local " ,
106
111
},
107
112
},
108
113
},
@@ -133,7 +138,7 @@ func TestBuild(t *testing.T) {
133
138
},
134
139
{
135
140
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 },
137
142
expectedPolicy : & xds_rbac.Policy {
138
143
Principals : []* xds_rbac.Principal {
139
144
{
@@ -169,16 +174,16 @@ func TestBuild(t *testing.T) {
169
174
assert := tassert .New (t )
170
175
171
176
pb := & PolicyBuilder {}
172
- for _ , principal := range tc .principals {
173
- pb .AddPrincipal ( principal )
177
+ for _ , svcIdentity := range tc .identities {
178
+ pb .AddIdentity ( svcIdentity )
174
179
}
175
180
for _ , port := range tc .ports {
176
181
pb .AddAllowedDestinationPort (port )
177
182
}
178
183
179
184
pb .UseANDForPermissions (tc .applyPermissionsAsAND )
180
185
policy := pb .Build ()
181
- assert .Equal (policy , tc .expectedPolicy )
186
+ assert .Equal (tc .expectedPolicy , policy )
182
187
})
183
188
}
184
189
}
0 commit comments