forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Victor Vieux <[email protected]>
- Loading branch information
Showing
9 changed files
with
187 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package filter | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
EQ = iota | ||
NOTEQ | ||
) | ||
|
||
var OPERATORS = []string{"==", "!="} | ||
|
||
type expr struct { | ||
key string | ||
operator int | ||
value string | ||
} | ||
|
||
func parseExprs(key string, env []string) ([]expr, error) { | ||
exprs := []expr{} | ||
for _, e := range env { | ||
if strings.HasPrefix(e, key+":") { | ||
entry := strings.TrimPrefix(e, key+":") | ||
found := false | ||
for i, op := range OPERATORS { | ||
if strings.Contains(entry, op) { | ||
// split with the op | ||
parts := strings.SplitN(entry, op, 2) | ||
|
||
// validate key | ||
// allow alpha-numeric | ||
matched, err := regexp.MatchString(`^(?i)[a-z_][a-z0-9\-_]+$`, parts[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if matched == false { | ||
return nil, fmt.Errorf("Key '%s' is invalid", parts[0]) | ||
} | ||
|
||
if len(parts) == 2 { | ||
|
||
// validate value | ||
// allow leading = in case of using == | ||
// allow * for globbing | ||
// allow regexp | ||
matched, err := regexp.MatchString(`^(?i)[=!\/]?[a-z0-9:\-_\.\*/\(\)\?\+\[\]\\\^\$]+$`, parts[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if matched == false { | ||
return nil, fmt.Errorf("Value '%s' is invalid", parts[1]) | ||
} | ||
exprs = append(exprs, expr{key: strings.ToLower(parts[0]), operator: i, value: parts[1]}) | ||
} else { | ||
exprs = append(exprs, expr{key: strings.ToLower(parts[0]), operator: i}) | ||
} | ||
|
||
found = true | ||
break // found an op, move to next entry | ||
} | ||
} | ||
if !found { | ||
return nil, fmt.Errorf("One of operator ==, != is expected") | ||
} | ||
} | ||
} | ||
return exprs, nil | ||
} | ||
|
||
func (e *expr) Match(whats ...string) bool { | ||
var ( | ||
pattern string | ||
match bool | ||
err error | ||
) | ||
|
||
if e.value[0] == '/' && e.value[len(e.value)-1] == '/' { | ||
// regexp | ||
pattern = e.value[1 : len(e.value)-1] | ||
} else { | ||
// simple match, create the regex for globbing (ex: ub*t* -> ^ub.*t.*$) and match. | ||
pattern = "^" + strings.Replace(e.value, "*", ".*", -1) + "$" | ||
} | ||
|
||
for _, what := range whats { | ||
if match, err = regexp.MatchString(pattern, what); match { | ||
break | ||
} else if err != nil { | ||
log.Error(err) | ||
} | ||
} | ||
|
||
switch e.operator { | ||
case EQ: | ||
return match | ||
case NOTEQ: | ||
return !match | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package filter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseExprs(t *testing.T) { | ||
// Cannot use the leading digit for key | ||
_, err := parseExprs("constraint", []string{"constraint:1node"}) | ||
assert.Error(t, err) | ||
|
||
// Cannot use space in key | ||
_, err = parseExprs("constraint", []string{"constraint:node ==node1"}) | ||
assert.Error(t, err) | ||
|
||
// Cannot use dot in key | ||
_, err = parseExprs("constraint", []string{"constraint:no.de==node1"}) | ||
assert.Error(t, err) | ||
|
||
// Cannot use * in key | ||
_, err = parseExprs("constraint", []string{"constraint:no*de==node1"}) | ||
assert.Error(t, err) | ||
|
||
// Allow leading underscore | ||
_, err = parseExprs("constraint", []string{"constraint:_node==_node1"}) | ||
assert.NoError(t, err) | ||
|
||
// Allow globbing | ||
_, err = parseExprs("constraint", []string{"constraint:node==*node*"}) | ||
assert.NoError(t, err) | ||
|
||
// Allow regexp in value | ||
_, err = parseExprs("constraint", []string{"constraint:node==/(?i)^[a-b]+c*$/"}) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestMatch(t *testing.T) { | ||
e := expr{operator: EQ, value: "foo"} | ||
assert.True(t, e.Match("foo")) | ||
assert.False(t, e.Match("bar")) | ||
assert.True(t, e.Match("foo", "bar")) | ||
|
||
e = expr{operator: NOTEQ, value: "foo"} | ||
assert.False(t, e.Match("foo")) | ||
assert.True(t, e.Match("bar")) | ||
assert.False(t, e.Match("foo", "bar")) | ||
|
||
e = expr{operator: EQ, value: "f*o"} | ||
assert.True(t, e.Match("foo")) | ||
assert.True(t, e.Match("fuo")) | ||
assert.True(t, e.Match("foo", "fuo", "bar")) | ||
|
||
e = expr{operator: NOTEQ, value: "f*o"} | ||
assert.False(t, e.Match("foo")) | ||
assert.False(t, e.Match("fuo")) | ||
assert.False(t, e.Match("foo", "fuo", "bar")) | ||
} |
Oops, something went wrong.