add user agent to registry client

This commit is contained in:
Wenkai Yin 2016-05-20 13:16:58 +08:00
parent cc8a4cbdab
commit 4d1d7799fd
3 changed files with 29 additions and 2 deletions

View File

@ -24,6 +24,7 @@ import (
"github.com/vmware/harbor/models"
svc_utils "github.com/vmware/harbor/service/utils"
"github.com/vmware/harbor/utils/log"
"github.com/vmware/harbor/utils/registry"
"github.com/astaxie/beego"
)
@ -54,7 +55,8 @@ func (n *NotificationHandler) Post() {
log.Errorf("Failed to match the media type against pattern, error: %v", err)
matched = false
}
if matched && strings.HasPrefix(e.Request.UserAgent, "docker") {
if matched && (strings.HasPrefix(e.Request.UserAgent, "docker") ||
strings.ToLower(strings.TrimSpace(e.Request.UserAgent)) == strings.ToLower(registry.UserAgent)) {
username = e.Actor.Name
action = e.Action
repo = e.Target.Repository

View File

@ -28,6 +28,10 @@ import (
"github.com/vmware/harbor/utils/registry/errors"
)
const (
UserAgent string = "registry-client"
)
// Registry holds information of a registry entity
type Registry struct {
Endpoint *url.URL
@ -149,8 +153,9 @@ func newClient(endpoint, username string, credential auth.Credential,
challenges := auth.ParseChallengeFromResponse(resp)
authorizer := auth.NewRequestAuthorizer(handlers, challenges)
headerModifier := NewHeaderModifier(map[string]string{http.CanonicalHeaderKey("User-Agent"): UserAgent})
transport := NewTransport(http.DefaultTransport, []RequestModifier{authorizer})
transport := NewTransport(http.DefaultTransport, []RequestModifier{authorizer, headerModifier})
return &http.Client{
Transport: transport,
}, nil

View File

@ -26,6 +26,26 @@ type RequestModifier interface {
ModifyRequest(*http.Request) error
}
// HeaderModifier adds headers to request
type HeaderModifier struct {
headers map[string]string
}
// NewHeaderModifier ...
func NewHeaderModifier(headers map[string]string) *HeaderModifier {
return &HeaderModifier{
headers: headers,
}
}
// ModifyRequest adds headers to the request
func (h *HeaderModifier) ModifyRequest(req *http.Request) error {
for key, value := range h.headers {
req.Header.Add(key, value)
}
return nil
}
// Transport holds information about base transport and modifiers
type Transport struct {
transport http.RoundTripper