mirror of
https://github.com/goharbor/harbor.git
synced 2024-10-27 13:49:34 +01:00
add user agent to registry client
This commit is contained in:
parent
cc8a4cbdab
commit
4d1d7799fd
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user