-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtranslator.go
More file actions
211 lines (188 loc) · 5.5 KB
/
translator.go
File metadata and controls
211 lines (188 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2023 LY Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package authorizerd
import (
"errors"
"fmt"
"net/url"
"strings"
)
const (
placeholderPrefix = "{"
placeholderSuffix = "}"
)
// Translator translates the information given to the argument to action and resource
type Translator interface {
Translate(domain, method, path, query string) (string, string, error)
}
// param keeps information after it has been validated
type param struct {
name string
isPlaceholder bool
}
// Rule represents a rule for translation
type Rule struct {
Method string `yaml:"method"`
Path string `yaml:"path"`
Action string `yaml:"action"`
Resource string `yaml:"resource"`
splitPaths []param
queryValueMap map[string]param
}
// MappingRules keeps the translation rules
type MappingRules struct {
Rules map[string][]Rule
}
// NewMappingRules creates the MappingRules object
func NewMappingRules(rules map[string][]Rule) (*MappingRules, error) {
if rules == nil {
return nil, errors.New("rules is nil")
}
mr := &MappingRules{Rules: rules}
if err := mr.validate(); err != nil {
return nil, err
}
return mr, nil
}
// Validate the given rule information
func (mr *MappingRules) validate() error {
for domain, rules := range mr.Rules {
if domain == "" {
return errors.New("domain is empty")
}
if rules == nil {
return errors.New("rules is nil")
}
for i, rule := range rules {
if rule.Method == "" || rule.Action == "" || rule.Resource == "" {
return fmt.Errorf("rule is empty, method:%s, action:%s, resource:%s",
rule.Method, rule.Action, rule.Resource)
}
if rule.Path != "" && !strings.HasPrefix(rule.Path, "/") {
return fmt.Errorf("path(%s) doesn't start with slash", rule.Path)
}
// For example, `rule.Path` assumes a string like `/path1/path2?param=value`
pathQuery := strings.SplitN(rule.Path, "?", 2)
splitPaths := strings.Split(pathQuery[0], "/")
rules[i].splitPaths = make([]param, len(splitPaths))
for j, path := range splitPaths {
if ok, err := rules[i].isPlaceholder(path); ok {
rules[i].splitPaths[j] = param{name: path, isPlaceholder: true}
} else {
if err != nil {
return err
}
rules[i].splitPaths[j] = param{name: path, isPlaceholder: false}
}
}
rules[i].queryValueMap = make(map[string]param)
// No query parameter
if len(pathQuery) == 1 {
continue
}
values, err := url.ParseQuery(pathQuery[1])
if err != nil {
return err
}
for p, val := range values {
if len(val) != 1 {
return errors.New("query multiple values is not allowed")
}
if ok, err := rules[i].isPlaceholder(val[0]); ok {
rules[i].queryValueMap[p] = param{name: val[0], isPlaceholder: true}
} else {
if err != nil {
return err
}
rules[i].queryValueMap[p] = param{name: val[0], isPlaceholder: false}
}
}
}
}
return nil
}
// Translate the information given to the argument to action and resource
func (mr *MappingRules) Translate(domain, method, path, query string) (string, string, error) {
if mr.Rules == nil {
return method, path, nil
}
OUTER:
for _, rule := range mr.Rules[domain] {
if strings.ToLower(rule.Method) == strings.ToLower(method) {
requestedPaths := strings.Split(path, "/")
if len(requestedPaths) != len(rule.splitPaths) {
continue
}
requestedQuery, err := url.ParseQuery(query)
if err != nil {
return method, path, err
}
if len(requestedQuery) != len(rule.queryValueMap) {
continue
}
placeholderMap := make(map[string]string)
for i, reqPath := range requestedPaths {
if rule.splitPaths[i].isPlaceholder {
placeholderMap[rule.splitPaths[i].name] = reqPath
} else if reqPath != rule.splitPaths[i].name {
continue OUTER
}
}
for reqQuery, reqVal := range requestedQuery {
// query multiple values is not allowed
if len(reqVal) != 1 {
continue OUTER
}
if v, ok := rule.queryValueMap[reqQuery]; ok {
if v.isPlaceholder {
placeholderMap[v.name] = reqVal[0]
} else if reqVal[0] != v.name {
continue OUTER
}
} else {
continue OUTER
}
}
replacedRes := rule.Resource
for placeholder, v := range placeholderMap {
replacedRes = strings.ReplaceAll(replacedRes, placeholder, v)
}
return rule.Action, replacedRes, nil
}
}
return method, path, nil
}
func (r *Rule) isPlaceholder(s string) (bool, error) {
if s == placeholderPrefix+placeholderSuffix {
return false, errors.New("placeholder is empty")
}
if strings.HasPrefix(s, placeholderPrefix) && strings.HasSuffix(s, placeholderSuffix) {
if r.splitPaths != nil {
for _, p := range r.splitPaths {
if p.isPlaceholder && p.name == s {
return false, fmt.Errorf("placeholder(%s) is duplicated", s)
}
}
}
if r.queryValueMap != nil {
for _, p := range r.queryValueMap {
if p.isPlaceholder && p.name == s {
return false, fmt.Errorf("placeholder(%s) is duplicated", s)
}
}
}
return true, nil
}
return false, nil
}