Skip to content

Commit abbd338

Browse files
committed
Generate helpers for servicepermissions
1 parent 0bdb570 commit abbd338

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+231
-51
lines changed

generate/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ go-mocks:
1717
--user $$(id -u):$$(id -g) \
1818
-w /work \
1919
-v $(PWD):/work \
20-
vektra/mockery:v2.52.3 --keeptree --inpackage --dir go --output go/tests/mocks --all --log-level debug
20+
vektra/mockery:v2.53.0 --keeptree --inpackage --dir go --output go/tests/mocks --all --log-level debug

generate/generate.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ func servicePermissions(root string) (*permissions.ServicePermissions, error) {
9999
serverReflectionInfov1alpha1: true,
100100
serverReflectionInfo: true,
101101
},
102-
Self: map[string]bool{},
102+
Self: map[string]bool{},
103+
Admin: map[string]bool{},
104+
Tenant: map[string]bool{},
105+
Project: map[string]bool{},
103106
}
104107
auditable = permissions.Auditable{}
105108
services = []string{}
@@ -135,28 +138,37 @@ func servicePermissions(root string) (*permissions.ServicePermissions, error) {
135138
switch *methodOpt.IdentifierValue {
136139
case v1.TenantRole_TENANT_ROLE_OWNER.String():
137140
roles.Tenant[v1.TenantRole_TENANT_ROLE_OWNER.String()] = append(roles.Tenant[v1.TenantRole_TENANT_ROLE_OWNER.String()], methodName)
141+
visibility.Tenant[methodName] = true
138142
case v1.TenantRole_TENANT_ROLE_EDITOR.String():
139143
roles.Tenant[v1.TenantRole_TENANT_ROLE_EDITOR.String()] = append(roles.Tenant[v1.TenantRole_TENANT_ROLE_EDITOR.String()], methodName)
144+
visibility.Tenant[methodName] = true
140145
case v1.TenantRole_TENANT_ROLE_VIEWER.String():
141146
roles.Tenant[v1.TenantRole_TENANT_ROLE_VIEWER.String()] = append(roles.Tenant[v1.TenantRole_TENANT_ROLE_VIEWER.String()], methodName)
147+
visibility.Tenant[methodName] = true
142148
case v1.TenantRole_TENANT_ROLE_GUEST.String():
143149
roles.Tenant[v1.TenantRole_TENANT_ROLE_GUEST.String()] = append(roles.Tenant[v1.TenantRole_TENANT_ROLE_GUEST.String()], methodName)
150+
visibility.Tenant[methodName] = true
144151
case v1.TenantRole_TENANT_ROLE_UNSPECIFIED.String():
145152
// noop
146153
// Project
147154
case v1.ProjectRole_PROJECT_ROLE_OWNER.String():
148155
roles.Project[v1.ProjectRole_PROJECT_ROLE_OWNER.String()] = append(roles.Project[v1.ProjectRole_PROJECT_ROLE_OWNER.String()], methodName)
156+
visibility.Project[methodName] = true
149157
case v1.ProjectRole_PROJECT_ROLE_EDITOR.String():
158+
visibility.Project[methodName] = true
150159
roles.Project[v1.ProjectRole_PROJECT_ROLE_EDITOR.String()] = append(roles.Project[v1.ProjectRole_PROJECT_ROLE_EDITOR.String()], methodName)
151160
case v1.ProjectRole_PROJECT_ROLE_VIEWER.String():
161+
visibility.Project[methodName] = true
152162
roles.Project[v1.ProjectRole_PROJECT_ROLE_VIEWER.String()] = append(roles.Project[v1.ProjectRole_PROJECT_ROLE_VIEWER.String()], methodName)
153163
case v1.ProjectRole_PROJECT_ROLE_UNSPECIFIED.String():
154164
// noop
155165
// Admin
156166
case v1.AdminRole_ADMIN_ROLE_EDITOR.String():
157167
roles.Admin[v1.AdminRole_ADMIN_ROLE_EDITOR.String()] = append(roles.Admin[v1.AdminRole_ADMIN_ROLE_EDITOR.String()], methodName)
168+
visibility.Admin[methodName] = true
158169
case v1.AdminRole_ADMIN_ROLE_VIEWER.String():
159170
roles.Admin[v1.AdminRole_ADMIN_ROLE_VIEWER.String()] = append(roles.Admin[v1.AdminRole_ADMIN_ROLE_VIEWER.String()], methodName)
171+
visibility.Admin[methodName] = true
160172
case v1.AdminRole_ADMIN_ROLE_UNSPECIFIED.String():
161173
// noop
162174
// Visibility

generate/go_servicepermissions.tpl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Code generated discover.go. DO NOT EDIT.
22
package permissions
33

4+
import (
5+
"connectrpc.com/connect"
6+
)
7+
48
func GetServices() []string {
59
return []string{
610
{{- range $s := .Services }}
@@ -54,6 +58,21 @@ func GetServicePermissions() *ServicePermissions {
5458
Self: map[string]bool{
5559
{{- range $key, $value := .Visibility.Self }}
5660
"{{ $key }}": {{ $value }} ,
61+
{{- end }}
62+
},
63+
Admin: map[string]bool{
64+
{{- range $key, $value := .Visibility.Admin }}
65+
"{{ $key }}": {{ $value }} ,
66+
{{- end }}
67+
},
68+
Tenant: map[string]bool{
69+
{{- range $key, $value := .Visibility.Tenant }}
70+
"{{ $key }}": {{ $value }} ,
71+
{{- end }}
72+
},
73+
Project: map[string]bool{
74+
{{- range $key, $value := .Visibility.Project }}
75+
"{{ $key }}": {{ $value }} ,
5776
{{- end }}
5877
},
5978
},
@@ -64,3 +83,50 @@ func GetServicePermissions() *ServicePermissions {
6483
},
6584
}
6685
}
86+
87+
func IsPublicScope(req connect.AnyRequest) bool {
88+
_, ok := GetServicePermissions().Visibility.Public[req.Spec().Procedure]
89+
return ok
90+
}
91+
92+
func IsSelfScope(req connect.AnyRequest) bool {
93+
_, ok := GetServicePermissions().Visibility.Self[req.Spec().Procedure]
94+
return ok
95+
}
96+
97+
func IsAdminScope(req connect.AnyRequest) bool {
98+
_, ok := GetServicePermissions().Visibility.Admin[req.Spec().Procedure]
99+
return ok
100+
}
101+
102+
func IsTenantScope(req connect.AnyRequest) bool {
103+
_, ok := GetServicePermissions().Visibility.Tenant[req.Spec().Procedure]
104+
return ok
105+
}
106+
107+
func IsProjectScope(req connect.AnyRequest) bool {
108+
_, ok := GetServicePermissions().Visibility.Project[req.Spec().Procedure]
109+
return ok
110+
}
111+
112+
func GetTenantFromRequest(req connect.AnyRequest) (string, bool) {
113+
if !IsTenantScope(req) {
114+
return "", false
115+
}
116+
switch rq := req.Any().(type) {
117+
case interface{ GetLogin() string }:
118+
return rq.GetLogin(), true
119+
}
120+
return "", false
121+
}
122+
123+
func GetProjectFromRequest(req connect.AnyRequest) (string, bool) {
124+
if !IsProjectScope(req) {
125+
return "", false
126+
}
127+
switch rq := req.Any().(type) {
128+
case interface{ GetProject() string }:
129+
return rq.GetProject(), true
130+
}
131+
return "", false
132+
}

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ require (
1818
cel.dev/expr v0.21.2 // indirect
1919
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
2020
github.com/davecgh/go-spew v1.1.1 // indirect
21-
github.com/google/cel-go v0.23.2 // indirect
21+
github.com/google/cel-go v0.24.1 // indirect
2222
github.com/klauspost/compress v1.18.0 // indirect
2323
github.com/kr/pretty v0.3.1 // indirect
2424
github.com/pmezard/go-difflib v1.0.0 // indirect
2525
github.com/stoewer/go-strcase v1.3.0 // indirect
2626
github.com/stretchr/objx v0.5.2 // indirect
27-
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
27+
golang.org/x/exp v0.0.0-20250228200357-dead58393ab7 // indirect
2828
golang.org/x/text v0.22.0 // indirect
29-
google.golang.org/genproto/googleapis/api v0.0.0-20250219182151-9fdb1cabc7b2 // indirect
30-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 // indirect
29+
google.golang.org/genproto/googleapis/api v0.0.0-20250227231956-55c901821b1e // indirect
30+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250227231956-55c901821b1e // indirect
3131
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
3232
gopkg.in/yaml.v3 v3.0.1 // indirect
3333
)

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ github.com/envoyproxy/protoc-gen-validate v1.2.1 h1:DEo3O99U8j4hBFwbJfrz9VtgcDfU
1818
github.com/envoyproxy/protoc-gen-validate v1.2.1/go.mod h1:d/C80l/jxXLdfEIhX1W2TmLfsJ31lvEjwamM4DxlWXU=
1919
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
2020
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
21-
github.com/google/cel-go v0.23.2 h1:UdEe3CvQh3Nv+E/j9r1Y//WO0K0cSyD7/y0bzyLIMI4=
22-
github.com/google/cel-go v0.23.2/go.mod h1:52Pb6QsDbC5kvgxvZhiL9QX1oZEkcUF/ZqaPx1J5Wwo=
21+
github.com/google/cel-go v0.24.1 h1:jsBCtxG8mM5wiUJDSGUqU0K7Mtr3w7Eyv00rw4DiZxI=
22+
github.com/google/cel-go v0.24.1/go.mod h1:Hdf9TqOaTNSFQA1ybQaRqATVoK7m/zcf7IMhGXP5zI8=
2323
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
2424
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
2525
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
@@ -50,16 +50,16 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
5050
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
5151
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
5252
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
53-
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa h1:t2QcU6V556bFjYgu4L6C+6VrCPyJZ+eyRsABUPs1mz4=
54-
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk=
53+
golang.org/x/exp v0.0.0-20250228200357-dead58393ab7 h1:aWwlzYV971S4BXRS9AmqwDLAD85ouC6X+pocatKY58c=
54+
golang.org/x/exp v0.0.0-20250228200357-dead58393ab7/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk=
5555
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
5656
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
5757
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
5858
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
59-
google.golang.org/genproto/googleapis/api v0.0.0-20250219182151-9fdb1cabc7b2 h1:35ZFtrCgaAjF7AFAK0+lRSf+4AyYnWRbH7og13p7rZ4=
60-
google.golang.org/genproto/googleapis/api v0.0.0-20250219182151-9fdb1cabc7b2/go.mod h1:W9ynFDP/shebLB1Hl/ESTOap2jHd6pmLXPNZC7SVDbA=
61-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 h1:DMTIbak9GhdaSxEjvVzAeNZvyc03I61duqNbnm3SU0M=
62-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
59+
google.golang.org/genproto/googleapis/api v0.0.0-20250227231956-55c901821b1e h1:nsxey/MfoGzYNduN0NN/+hqP9iiCIYsrVbXb/8hjFM8=
60+
google.golang.org/genproto/googleapis/api v0.0.0-20250227231956-55c901821b1e/go.mod h1:Xsh8gBVxGCcbV8ZeTB9wI5XPyZ5RvC6V3CTeeplHbiA=
61+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250227231956-55c901821b1e h1:YA5lmSs3zc/5w+xsRcHqpETkaYyK63ivEPzNTcUUlSA=
62+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250227231956-55c901821b1e/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
6363
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
6464
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
6565
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

go/permissions/permissions.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ type Roles struct {
3030
}
3131

3232
type Visibility struct {
33-
Public map[string]bool `json:"public,omitempty"`
34-
Self map[string]bool `json:"self,omitempty"`
33+
Public map[string]bool `json:"public,omitempty"`
34+
Self map[string]bool `json:"self,omitempty"`
35+
Admin map[string]bool `json:"admin,omitempty"`
36+
Tenant map[string]bool `json:"tenant,omitempty"`
37+
Project map[string]bool `json:"project,omitempty"`
3538
}

go/permissions/permissions_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ func TestGetServicePermissions(t *testing.T) {
1212
// TODO more coverage
1313
require.Contains(t, perms.Methods, "/metalstack.api.v2.IPService/List")
1414
require.Contains(t, perms.Visibility.Self, "/metalstack.api.v2.TokenService/Create")
15+
require.Contains(t, perms.Visibility.Admin, "/metalstack.admin.v2.TenantService/List")
16+
require.Contains(t, perms.Visibility.Project, "/metalstack.api.v2.IPService/List")
1517
}

go/permissions/servicepermissions.go

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/client/Adminv2.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/client/Apiv2.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)