Skip to content

Commit 74ec60c

Browse files
committed
support fuzzy match
1 parent 4b4686a commit 74ec60c

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ jobs:
1616
uses: golangci/golangci-lint-action@v2
1717
with:
1818
version: v1.29
19-
args: --skip-dirs=discovery --out-format=colored-line-number --skip-files=.*_test.go$
19+
args: --skip-dirs=discovery --skip-files=.*_test.go$

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ github.com/deckarep/golang-set v1.7.1 h1:SCQV0S6gTtp6itiFrTqI+pfmJ4LN85S1YzhDf9r
44
github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
55
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
66
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
7+
github.com/kisielk/errcheck v1.2.0 h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E=
78
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
9+
github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg=
810
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
911
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1012
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
1114
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1215
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
1316
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17+
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563 h1:NIou6eNFigscvKJmsbyez16S2cIS6idossORlFtSt2E=
1418
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
1519
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1620
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

rbac/resource_map.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,36 @@
1717

1818
package rbac
1919

20-
//as a user, he only understand resource of this system,
21-
//but to decouple authorization code from business code,
20+
import "strings"
21+
22+
//as a user of a backend service, he only understands resource of this service,
23+
//to decouple authorization code from business code,
2224
//a middleware should handle all the authorization logic, and this middleware only understand rest API,
2325
//a resource mapping helps to maintain relations between api and resource.
2426
var resourceMap = map[string]string{}
2527

28+
//PartialMap saves api partial matching
29+
var PartialMap = map[string]string{}
30+
2631
func GetResource(api string) string {
2732
r, ok := resourceMap[api]
28-
if !ok {
29-
return resourceMap["*"]
33+
if ok {
34+
return r
35+
}
36+
for partialAPI, resource := range PartialMap {
37+
if strings.Contains(api, partialAPI) {
38+
return resource
39+
}
3040
}
31-
return r
41+
return resourceMap["*"]
3242
}
3343

34-
// MapResource save the mapping from api to resource
44+
// MapResource saves the mapping from api to resource, it must be exactly match
3545
func MapResource(api, resource string) {
3646
resourceMap[api] = resource
3747
}
48+
49+
// PartialMapResource saves the mapping from api to resource, it is partial match
50+
func PartialMapResource(api, resource string) {
51+
PartialMap[api] = resource
52+
}

rbac/resource_map_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ func TestGetResource(t *testing.T) {
3838
res := rbac.GetResource("/v1/a/1")
3939
assert.Empty(t, res)
4040
})
41+
t.Run("given partial string and resource mapping,return right resource", func(t *testing.T) {
42+
rbac.PartialMapResource("part", "partRes")
43+
res := rbac.GetResource("/part/service/some")
44+
assert.Equal(t, "partRes", res)
45+
})
4146
}

0 commit comments

Comments
 (0)