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"
|
"github.com/vmware/harbor/models"
|
||||||
svc_utils "github.com/vmware/harbor/service/utils"
|
svc_utils "github.com/vmware/harbor/service/utils"
|
||||||
"github.com/vmware/harbor/utils/log"
|
"github.com/vmware/harbor/utils/log"
|
||||||
|
"github.com/vmware/harbor/utils/registry"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"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)
|
log.Errorf("Failed to match the media type against pattern, error: %v", err)
|
||||||
matched = false
|
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
|
username = e.Actor.Name
|
||||||
action = e.Action
|
action = e.Action
|
||||||
repo = e.Target.Repository
|
repo = e.Target.Repository
|
||||||
|
@ -28,6 +28,10 @@ import (
|
|||||||
"github.com/vmware/harbor/utils/registry/errors"
|
"github.com/vmware/harbor/utils/registry/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
UserAgent string = "registry-client"
|
||||||
|
)
|
||||||
|
|
||||||
// Registry holds information of a registry entity
|
// Registry holds information of a registry entity
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
Endpoint *url.URL
|
Endpoint *url.URL
|
||||||
@ -149,8 +153,9 @@ func newClient(endpoint, username string, credential auth.Credential,
|
|||||||
|
|
||||||
challenges := auth.ParseChallengeFromResponse(resp)
|
challenges := auth.ParseChallengeFromResponse(resp)
|
||||||
authorizer := auth.NewRequestAuthorizer(handlers, challenges)
|
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{
|
return &http.Client{
|
||||||
Transport: transport,
|
Transport: transport,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -26,6 +26,26 @@ type RequestModifier interface {
|
|||||||
ModifyRequest(*http.Request) error
|
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
|
// Transport holds information about base transport and modifiers
|
||||||
type Transport struct {
|
type Transport struct {
|
||||||
transport http.RoundTripper
|
transport http.RoundTripper
|
||||||
|
Loading…
Reference in New Issue
Block a user