Skip to content

Commit

Permalink
fix #627 issue, correct username to principal name
Browse files Browse the repository at this point in the history
when registry repo name can customized, the username is same with
repository name. so it can't query with email. fix it.
  • Loading branch information
xiaods committed Jan 22, 2017
1 parent d927494 commit 5e1c975
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/plugins/registry/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

"github.com/Dataman-Cloud/crane/src/plugins/auth"

log "github.com/Sirupsen/logrus"
"github.com/docker/distribution/registry/auth/token"
"github.com/docker/libtrust"
)
Expand Down Expand Up @@ -52,6 +52,7 @@ func (registry *Registry) FilterAccess(username string, authenticated bool, a *t
namespace := strings.Split(a.Name, "/")[0]
image := strings.Split(a.Name, "/")[1]

log.Infof("username: %s, namespace: %s, image: %s", username, namespace, image)
permission := registry.GetPermission(username, namespace, image)
if strings.Contains(permission, "W") {
a.Actions = append(a.Actions, "push")
Expand Down Expand Up @@ -151,31 +152,31 @@ func base64UrlEncode(b []byte) string {
return strings.TrimRight(base64.URLEncoding.EncodeToString(b), "=")
}

func (registry *Registry) GetPermission(username, namespace, image string) string {
if namespace == "library" && len(registry.Authenticator.GetDefaultAccounts()) > 0 &&
registry.Authenticator.GetDefaultAccounts()[0].Email == username {
func (registry *Registry) GetPermission(principal, namespace, image string) string {
if namespace == "library" && len(registry.Authenticator.GetDefaultAccounts()) > 0 {
return "RW"
}

if namespace == "library" {
return "R"
}

if registry.registryNamespaceForEmail(username) == namespace { // for user access himself's repository
return "RWM"
// for user access himself's repository
var namespaceEmail NamespaceEmail
if err := registry.DbClient.Where("namespace = ?", principal).Find(&namespaceEmail).Error; err != nil {
return ""
}

var modelImage Image
err := registry.DbClient.Where("namespace = ? AND image = ?", namespace, image).Find(&modelImage)
if err != nil {
if err := registry.DbClient.Where("namespace = ? AND image = ?", namespace, image).Find(&modelImage).Error; err != nil {
return ""
}

if modelImage.Publicity == 1 { // for public repository
return "R"
}

return ""
return "RWM"
}

func (registry *Registry) GenTokenForUI(username, service, scope string) (string, error) {
Expand Down

0 comments on commit 5e1c975

Please sign in to comment.