forked from cesanta/docker_auth
-
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.
Auth{n,z} refactor; fix token generation for 1.8
Since 1.8 Docker will always request push and pull actions (scopes), and we should return the subset that is allowed.
- Loading branch information
Showing
12 changed files
with
280 additions
and
163 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,83 @@ | ||
package authz | ||
|
||
import ( | ||
"encoding/json" | ||
"path" | ||
"regexp" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
type ACL []ACLEntry | ||
|
||
type ACLEntry struct { | ||
Match *MatchConditions `yaml:"match"` | ||
Actions *[]string `yaml:"actions,flow"` | ||
} | ||
|
||
type MatchConditions struct { | ||
Account *string `yaml:"account,omitempty" json:"account,omitempty"` | ||
Type *string `yaml:"type,omitempty" json:"type,omitempty"` | ||
Name *string `yaml:"name,omitempty" json:"name,omitempty"` | ||
} | ||
|
||
type aclAuthorizer struct { | ||
acl ACL | ||
} | ||
|
||
func NewACLAuthorizer(acl ACL) Authorizer { | ||
return &aclAuthorizer{acl: acl} | ||
} | ||
|
||
func (aa *aclAuthorizer) Authorize(ai *AuthRequestInfo) ([]string, error) { | ||
for _, e := range aa.acl { | ||
matched := e.Matches(ai) | ||
if matched { | ||
glog.V(2).Infof("%s matched %s", ai, e) | ||
if len(*e.Actions) == 1 && (*e.Actions)[0] == "*" { | ||
return ai.Actions, nil | ||
} | ||
return StringSetIntersection(ai.Actions, *e.Actions), nil | ||
} | ||
} | ||
return nil, NoMatch | ||
} | ||
|
||
func (aa *aclAuthorizer) Stop() { | ||
// Nothing to do. | ||
} | ||
|
||
func (aa *aclAuthorizer) Name() string { | ||
return "static ACL" | ||
} | ||
|
||
type aclEntryJSON *ACLEntry | ||
|
||
func (e ACLEntry) String() string { | ||
b, _ := json.Marshal(e) | ||
return string(b) | ||
} | ||
|
||
func matchString(pp *string, s string) bool { | ||
if pp == nil { | ||
return true | ||
} | ||
p := *pp | ||
var matched bool | ||
var err error | ||
if len(p) > 2 && p[0] == '/' && p[len(p)-1] == '/' { | ||
matched, err = regexp.Match(p[1:len(p)-1], []byte(s)) | ||
} else { | ||
matched, err = path.Match(p, s) | ||
} | ||
return err == nil && matched | ||
} | ||
|
||
func (e *ACLEntry) Matches(ai *AuthRequestInfo) bool { | ||
if matchString(e.Match.Account, ai.Account) && | ||
matchString(e.Match.Type, ai.Type) && | ||
matchString(e.Match.Name, ai.Name) { | ||
return true | ||
} | ||
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,44 @@ | ||
package authz | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Authorizer interface performs authorization of the request. | ||
// It is invoked after authentication so it can be assumed that the requestor has | ||
// presented satisfactory credentials for Account. | ||
// Principally, it answers the question: is this Account allowed to perform these Actions | ||
// on this Type.Name subject in the give Service? | ||
type Authorizer interface { | ||
// Authorize performs authorization given the request information. | ||
// It returns a set of authorized actions (of the set requested), which can be empty/nil. | ||
// Error should only be reported if request could not be serviced, not if it should be denied. | ||
// A special NoMatch error is returned if the authorizer could not reach a decision, | ||
// e.g. none of the rules matched. | ||
// Implementations must be goroutine-safe. | ||
Authorize(ai *AuthRequestInfo) ([]string, error) | ||
|
||
// Finalize resources in preparation for shutdown. | ||
// When this call is made there are guaranteed to be no Authenticate requests in flight | ||
// and there will be no more calls made to this instance. | ||
Stop() | ||
|
||
// Human-readable name of the authenticator. | ||
Name() string | ||
} | ||
|
||
var NoMatch = errors.New("did not match any rule") | ||
|
||
type AuthRequestInfo struct { | ||
Account string | ||
Type string | ||
Name string | ||
Service string | ||
Actions []string | ||
} | ||
|
||
func (ai AuthRequestInfo) String() string { | ||
return fmt.Sprintf("{%s %s %s %s}", ai.Account, strings.Join(ai.Actions, ","), ai.Type, ai.Name) | ||
} |
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,26 @@ | ||
package authz | ||
|
||
import ( | ||
"sort" | ||
|
||
mapset "github.com/deckarep/golang-set" | ||
) | ||
|
||
func makeSet(ss []string) mapset.Set { | ||
set := mapset.NewSet() | ||
for _, s := range ss { | ||
set.Add(s) | ||
} | ||
return set | ||
} | ||
|
||
func StringSetIntersection(a, b []string) []string { | ||
as := makeSet(a) | ||
bs := makeSet(b) | ||
d := []string{} | ||
for s := range as.Intersect(bs).Iter() { | ||
d = append(d, s.(string)) | ||
} | ||
sort.Strings(d) | ||
return d | ||
} |
Oops, something went wrong.