mirror of
https://github.com/minio/minio.git
synced 2025-01-11 15:03:22 -05:00
pkg/wildcard: Simplify the wildcard logic further. (#2555)
This commit is contained in:
parent
b67c8970c9
commit
7270ca4157
@ -70,12 +70,12 @@ func bucketPolicyActionMatch(action string, statement policyStatement) bool {
|
|||||||
|
|
||||||
// Match function matches wild cards in 'pattern' for resource.
|
// Match function matches wild cards in 'pattern' for resource.
|
||||||
func resourceMatch(pattern, resource string) bool {
|
func resourceMatch(pattern, resource string) bool {
|
||||||
return wildcard.MatchExtended(pattern, resource)
|
return wildcard.Match(pattern, resource)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Match function matches wild cards in 'pattern' for action.
|
// Match function matches wild cards in 'pattern' for action.
|
||||||
func actionMatch(pattern, action string) bool {
|
func actionMatch(pattern, action string) bool {
|
||||||
return wildcard.Match(pattern, action)
|
return wildcard.MatchSimple(pattern, action)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify if given resource matches with policy statement.
|
// Verify if given resource matches with policy statement.
|
||||||
|
@ -114,7 +114,7 @@ func isElasticQueue(sqsArn arnSQS) bool {
|
|||||||
// Match function matches wild cards in 'pattern' for events.
|
// Match function matches wild cards in 'pattern' for events.
|
||||||
func eventMatch(eventType string, events []string) (ok bool) {
|
func eventMatch(eventType string, events []string) (ok bool) {
|
||||||
for _, event := range events {
|
for _, event := range events {
|
||||||
ok = wildcard.Match(event, eventType)
|
ok = wildcard.MatchSimple(event, eventType)
|
||||||
if ok {
|
if ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -16,123 +16,68 @@
|
|||||||
|
|
||||||
package wildcard
|
package wildcard
|
||||||
|
|
||||||
import (
|
// MatchSimple - finds whether the text matches/satisfies the pattern string.
|
||||||
"strings"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Match - finds whether the text matches/satisfies the pattern string.
|
|
||||||
// supports only '*' wildcard in the pattern.
|
// supports only '*' wildcard in the pattern.
|
||||||
// considers a file system path as a flat name space.
|
// considers a file system path as a flat name space.
|
||||||
func Match(pattern, text string) bool {
|
func MatchSimple(pattern, name string) bool {
|
||||||
if pattern == "" {
|
if pattern == "" {
|
||||||
return text == pattern
|
return name == pattern
|
||||||
}
|
}
|
||||||
if pattern == "*" {
|
if pattern == "*" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
parts := strings.Split(pattern, "*")
|
rname := make([]rune, 0, len(name))
|
||||||
if len(parts) == 1 {
|
rpattern := make([]rune, 0, len(pattern))
|
||||||
return text == pattern
|
for _, r := range name {
|
||||||
|
rname = append(rname, r)
|
||||||
}
|
}
|
||||||
tGlob := strings.HasSuffix(pattern, "*")
|
for _, r := range pattern {
|
||||||
end := len(parts) - 1
|
rpattern = append(rpattern, r)
|
||||||
if !strings.HasPrefix(text, parts[0]) {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
for i := 1; i < end; i++ {
|
simple := true // Does only wildcard '*' match.
|
||||||
if !strings.Contains(text, parts[i]) {
|
return deepMatchRune(rname, rpattern, simple)
|
||||||
return false
|
|
||||||
}
|
|
||||||
idx := strings.Index(text, parts[i]) + len(parts[i])
|
|
||||||
text = text[idx:]
|
|
||||||
}
|
|
||||||
return tGlob || strings.HasSuffix(text, parts[end])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatchExtended - finds whether the text matches/satisfies the pattern string.
|
// Match - finds whether the text matches/satisfies the pattern string.
|
||||||
// supports '*' and '?' wildcards in the pattern string.
|
// supports '*' and '?' wildcards in the pattern string.
|
||||||
// unlike path.Match(), considers a path as a flat name space while matching the pattern.
|
// unlike path.Match(), considers a path as a flat name space while matching the pattern.
|
||||||
// The difference is illustrated in the example here https://play.golang.org/p/Ega9qgD4Qz .
|
// The difference is illustrated in the example here https://play.golang.org/p/Ega9qgD4Qz .
|
||||||
func MatchExtended(pattern, name string) (matched bool) {
|
func Match(pattern, name string) (matched bool) {
|
||||||
Pattern:
|
if pattern == "" {
|
||||||
|
return name == pattern
|
||||||
|
}
|
||||||
|
if pattern == "*" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
rname := make([]rune, 0, len(name))
|
||||||
|
rpattern := make([]rune, 0, len(pattern))
|
||||||
|
for _, r := range name {
|
||||||
|
rname = append(rname, r)
|
||||||
|
}
|
||||||
|
for _, r := range pattern {
|
||||||
|
rpattern = append(rpattern, r)
|
||||||
|
}
|
||||||
|
simple := false // Does extended wildcard '*' and '?' match.
|
||||||
|
return deepMatchRune(rname, rpattern, simple)
|
||||||
|
}
|
||||||
|
|
||||||
|
func deepMatchRune(str, pattern []rune, simple bool) bool {
|
||||||
for len(pattern) > 0 {
|
for len(pattern) > 0 {
|
||||||
var star bool
|
switch pattern[0] {
|
||||||
var chunk string
|
|
||||||
star, chunk, pattern = scanChunk(pattern)
|
|
||||||
if star && chunk == "" {
|
|
||||||
// Trailing * matches rest of string.
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
// Look for match at current position.
|
|
||||||
t, ok := matchChunk(chunk, name)
|
|
||||||
// if we're the last chunk, make sure we've exhausted the name
|
|
||||||
// otherwise we'll give a false result even if we could still match
|
|
||||||
// using the star
|
|
||||||
if ok && (len(t) == 0 || len(pattern) > 0) {
|
|
||||||
name = t
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if star {
|
|
||||||
// Look for match skipping i+1 bytes.
|
|
||||||
for i := 0; i < len(name); i++ {
|
|
||||||
t, ok := matchChunk(chunk, name[i+1:])
|
|
||||||
if ok {
|
|
||||||
// if we're the last chunk, make sure we exhausted the name
|
|
||||||
if len(pattern) == 0 && len(t) > 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
name = t
|
|
||||||
continue Pattern
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return len(name) == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// scanChunk gets the next segment of pattern, which is a non-star string
|
|
||||||
// possibly preceded by a star.
|
|
||||||
func scanChunk(pattern string) (star bool, chunk, rest string) {
|
|
||||||
for len(pattern) > 0 && pattern[0] == '*' {
|
|
||||||
pattern = pattern[1:]
|
|
||||||
star = true
|
|
||||||
}
|
|
||||||
inrange := false
|
|
||||||
var i int
|
|
||||||
Scan:
|
|
||||||
for i = 0; i < len(pattern); i++ {
|
|
||||||
switch pattern[i] {
|
|
||||||
case '*':
|
|
||||||
if !inrange {
|
|
||||||
break Scan
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return star, pattern[0:i], pattern[i:]
|
|
||||||
}
|
|
||||||
|
|
||||||
// matchChunk checks whether chunk matches the beginning of s.
|
|
||||||
// If so, it returns the remainder of s (after the match).
|
|
||||||
// Chunk is all single-character operators: literals, char classes, and ?.
|
|
||||||
func matchChunk(chunk, s string) (rest string, ok bool) {
|
|
||||||
for len(chunk) > 0 {
|
|
||||||
if len(s) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch chunk[0] {
|
|
||||||
case '?':
|
|
||||||
_, n := utf8.DecodeRuneInString(s)
|
|
||||||
s = s[n:]
|
|
||||||
chunk = chunk[1:]
|
|
||||||
default:
|
default:
|
||||||
if chunk[0] != s[0] {
|
if len(str) == 0 || str[0] != pattern[0] {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
s = s[1:]
|
case '?':
|
||||||
chunk = chunk[1:]
|
if len(str) == 0 && !simple {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
case '*':
|
||||||
|
return deepMatchRune(str, pattern[1:], simple) ||
|
||||||
|
(len(str) > 0 && deepMatchRune(str[1:], pattern, simple))
|
||||||
}
|
}
|
||||||
|
str = str[1:]
|
||||||
|
pattern = pattern[1:]
|
||||||
}
|
}
|
||||||
return s, true
|
return len(str) == 0 && len(pattern) == 0
|
||||||
}
|
}
|
||||||
|
@ -14,16 +14,18 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package wildcard
|
package wildcard_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/minio/minio/pkg/wildcard"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestMatchExtended - Tests validate the logic of wild card matching.
|
// TestMatch - Tests validate the logic of wild card matching.
|
||||||
// `MatchExtended` supports '*' and '?' wildcards.
|
// `Match` supports '*' and '?' wildcards.
|
||||||
// Sample usage: In resource matching for bucket policy validation.
|
// Sample usage: In resource matching for bucket policy validation.
|
||||||
func TestMatchExtended(t *testing.T) {
|
func TestMatch(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
pattern string
|
pattern string
|
||||||
text string
|
text string
|
||||||
@ -131,7 +133,8 @@ func TestMatchExtended(t *testing.T) {
|
|||||||
},
|
},
|
||||||
// Test case - 15.
|
// Test case - 15.
|
||||||
// Test case where the keyname part of the pattern is expanded in the text.
|
// Test case where the keyname part of the pattern is expanded in the text.
|
||||||
{pattern: "my-bucket/In*/Ka*/Ban",
|
{
|
||||||
|
pattern: "my-bucket/In*/Ka*/Ban",
|
||||||
text: "my-bucket/India/Karnataka/Bangalore",
|
text: "my-bucket/India/Karnataka/Bangalore",
|
||||||
matched: false,
|
matched: false,
|
||||||
},
|
},
|
||||||
@ -144,7 +147,8 @@ func TestMatchExtended(t *testing.T) {
|
|||||||
},
|
},
|
||||||
// Test case - 17.
|
// Test case - 17.
|
||||||
// Test case with keyname part being a wildcard in the pattern.
|
// Test case with keyname part being a wildcard in the pattern.
|
||||||
{pattern: "my-bucket/*",
|
{
|
||||||
|
pattern: "my-bucket/*",
|
||||||
text: "my-bucket/India",
|
text: "my-bucket/India",
|
||||||
matched: true,
|
matched: true,
|
||||||
},
|
},
|
||||||
@ -367,16 +371,16 @@ func TestMatchExtended(t *testing.T) {
|
|||||||
}
|
}
|
||||||
// Iterating over the test cases, call the function under test and asert the output.
|
// Iterating over the test cases, call the function under test and asert the output.
|
||||||
for i, testCase := range testCases {
|
for i, testCase := range testCases {
|
||||||
actualResult := MatchExtended(testCase.pattern, testCase.text)
|
actualResult := wildcard.Match(testCase.pattern, testCase.text)
|
||||||
if testCase.matched != actualResult {
|
if testCase.matched != actualResult {
|
||||||
t.Errorf("Test %d: Expected the result to be `%v`, but instead found it to be `%v`", i+1, testCase.matched, actualResult)
|
t.Errorf("Test %d: Expected the result to be `%v`, but instead found it to be `%v`", i+1, testCase.matched, actualResult)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMatch - Tests validate the logic of wild card matching.
|
// TestMatchSimple - Tests validate the logic of wild card matching.
|
||||||
// `Match` supports matching for only '*' in the pattern string.
|
// `MatchSimple` supports matching for only '*' in the pattern string.
|
||||||
func TestMatch(t *testing.T) {
|
func TestMatchSimple(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
pattern string
|
pattern string
|
||||||
text string
|
text string
|
||||||
@ -484,7 +488,8 @@ func TestMatch(t *testing.T) {
|
|||||||
},
|
},
|
||||||
// Test case - 15.
|
// Test case - 15.
|
||||||
// Test case where the keyname part of the pattern is expanded in the text.
|
// Test case where the keyname part of the pattern is expanded in the text.
|
||||||
{pattern: "my-bucket/In*/Ka*/Ban",
|
{
|
||||||
|
pattern: "my-bucket/In*/Ka*/Ban",
|
||||||
text: "my-bucket/India/Karnataka/Bangalore",
|
text: "my-bucket/India/Karnataka/Bangalore",
|
||||||
matched: false,
|
matched: false,
|
||||||
},
|
},
|
||||||
@ -497,7 +502,8 @@ func TestMatch(t *testing.T) {
|
|||||||
},
|
},
|
||||||
// Test case - 17.
|
// Test case - 17.
|
||||||
// Test case with keyname part being a wildcard in the pattern.
|
// Test case with keyname part being a wildcard in the pattern.
|
||||||
{pattern: "my-bucket/*",
|
{
|
||||||
|
pattern: "my-bucket/*",
|
||||||
text: "my-bucket/India",
|
text: "my-bucket/India",
|
||||||
matched: true,
|
matched: true,
|
||||||
},
|
},
|
||||||
@ -507,10 +513,28 @@ func TestMatch(t *testing.T) {
|
|||||||
text: "my-bucket/odo",
|
text: "my-bucket/odo",
|
||||||
matched: false,
|
matched: false,
|
||||||
},
|
},
|
||||||
|
// Test case - 11.
|
||||||
|
{
|
||||||
|
pattern: "my-bucket/oo?*",
|
||||||
|
text: "my-bucket/oo???",
|
||||||
|
matched: true,
|
||||||
|
},
|
||||||
|
// Test case - 12:
|
||||||
|
{
|
||||||
|
pattern: "my-bucket/oo??*",
|
||||||
|
text: "my-bucket/odo",
|
||||||
|
matched: false,
|
||||||
|
},
|
||||||
|
// Test case - 13:
|
||||||
|
{
|
||||||
|
pattern: "?h?*",
|
||||||
|
text: "?h?hello",
|
||||||
|
matched: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
// Iterating over the test cases, call the function under test and asert the output.
|
// Iterating over the test cases, call the function under test and asert the output.
|
||||||
for i, testCase := range testCases {
|
for i, testCase := range testCases {
|
||||||
actualResult := Match(testCase.pattern, testCase.text)
|
actualResult := wildcard.MatchSimple(testCase.pattern, testCase.text)
|
||||||
if testCase.matched != actualResult {
|
if testCase.matched != actualResult {
|
||||||
t.Errorf("Test %d: Expected the result to be `%v`, but instead found it to be `%v`", i+1, testCase.matched, actualResult)
|
t.Errorf("Test %d: Expected the result to be `%v`, but instead found it to be `%v`", i+1, testCase.matched, actualResult)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user