@@ -160,7 +160,9 @@ func (r *Registry) GetRemoteHistory(imgID, registry string, token []string) ([]s
160160 if err != nil {
161161 return nil , err
162162 }
163- req .Header .Set ("Authorization" , "Token " + strings .Join (token , ", " ))
163+ if req .Header .Get ("Authorization" ) == "" { // Don't override
164+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
165+ }
164166 res , err := doWithCookies (r .client , req )
165167 if err != nil {
166168 return nil , err
@@ -193,7 +195,9 @@ func (r *Registry) LookupRemoteImage(imgID, registry string, token []string) boo
193195 if err != nil {
194196 return false
195197 }
196- req .Header .Set ("Authorization" , "Token " + strings .Join (token , ", " ))
198+ if req .Header .Get ("Authorization" ) == "" { // Don't override
199+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
200+ }
197201 res , err := doWithCookies (r .client , req )
198202 if err != nil {
199203 return false
@@ -209,7 +213,9 @@ func (r *Registry) GetRemoteImageJSON(imgID, registry string, token []string) ([
209213 if err != nil {
210214 return nil , - 1 , fmt .Errorf ("Failed to download json: %s" , err )
211215 }
212- req .Header .Set ("Authorization" , "Token " + strings .Join (token , ", " ))
216+ if req .Header .Get ("Authorization" ) == "" { // Don't override
217+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
218+ }
213219 res , err := doWithCookies (r .client , req )
214220 if err != nil {
215221 return nil , - 1 , fmt .Errorf ("Failed to download json: %s" , err )
@@ -236,7 +242,9 @@ func (r *Registry) GetRemoteImageLayer(imgID, registry string, token []string) (
236242 if err != nil {
237243 return nil , fmt .Errorf ("Error while getting from the server: %s\n " , err )
238244 }
239- req .Header .Set ("Authorization" , "Token " + strings .Join (token , ", " ))
245+ if req .Header .Get ("Authorization" ) == "" { // Don't override
246+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
247+ }
240248 res , err := doWithCookies (r .client , req )
241249 if err != nil {
242250 return nil , err
@@ -262,7 +270,9 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
262270 if err != nil {
263271 return nil , err
264272 }
265- req .Header .Set ("Authorization" , "Token " + strings .Join (token , ", " ))
273+ if req .Header .Get ("Authorization" ) == "" { // Don't override
274+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
275+ }
266276 res , err := doWithCookies (r .client , req )
267277 if err != nil {
268278 return nil , err
@@ -290,7 +300,8 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
290300 return nil , fmt .Errorf ("Could not reach any registry endpoint" )
291301}
292302
293- func (r * Registry ) GetRepositoryData (indexEp , remote string ) (* RepositoryData , error ) {
303+ func (r * Registry ) GetRepositoryData (remote string ) (* RepositoryData , error ) {
304+ indexEp := r .indexEndpoint
294305 repositoryTarget := fmt .Sprintf ("%srepositories/%s/images" , indexEp , remote )
295306
296307 utils .Debugf ("[registry] Calling GET %s" , repositoryTarget )
@@ -364,7 +375,9 @@ func (r *Registry) PushImageChecksumRegistry(imgData *ImgData, registry string,
364375 if err != nil {
365376 return err
366377 }
367- req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
378+ if req .Header .Get ("Authorization" ) == "" { // Don't override
379+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
380+ }
368381 req .Header .Set ("X-Docker-Checksum" , imgData .Checksum )
369382
370383 res , err := doWithCookies (r .client , req )
@@ -401,7 +414,9 @@ func (r *Registry) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, regis
401414 return err
402415 }
403416 req .Header .Add ("Content-type" , "application/json" )
404- req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
417+ if req .Header .Get ("Authorization" ) == "" { // Don't override
418+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
419+ }
405420
406421 res , err := doWithCookies (r .client , req )
407422 if err != nil {
@@ -436,7 +451,9 @@ func (r *Registry) PushImageLayerRegistry(imgID string, layer io.Reader, registr
436451 }
437452 req .ContentLength = - 1
438453 req .TransferEncoding = []string {"chunked" }
439- req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
454+ if req .Header .Get ("Authorization" ) == "" { // Don't override
455+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
456+ }
440457 res , err := doWithCookies (r .client , req )
441458 if err != nil {
442459 return "" , fmt .Errorf ("Failed to upload layer: %s" , err )
@@ -465,7 +482,9 @@ func (r *Registry) PushRegistryTag(remote, revision, tag, registry string, token
465482 return err
466483 }
467484 req .Header .Add ("Content-type" , "application/json" )
468- req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
485+ if req .Header .Get ("Authorization" ) == "" { // Don't override
486+ req .Header .Set ("Authorization" , "Token " + strings .Join (token , "," ))
487+ }
469488 req .ContentLength = int64 (len (revision ))
470489 res , err := doWithCookies (r .client , req )
471490 if err != nil {
@@ -478,8 +497,9 @@ func (r *Registry) PushRegistryTag(remote, revision, tag, registry string, token
478497 return nil
479498}
480499
481- func (r * Registry ) PushImageJSONIndex (indexEp , remote string , imgList []* ImgData , validate bool , regs []string ) (* RepositoryData , error ) {
500+ func (r * Registry ) PushImageJSONIndex (remote string , imgList []* ImgData , validate bool , regs []string ) (* RepositoryData , error ) {
482501 cleanImgList := []* ImgData {}
502+ indexEp := r .indexEndpoint
483503
484504 if validate {
485505 for _ , elem := range imgList {
@@ -583,6 +603,7 @@ func (r *Registry) PushImageJSONIndex(indexEp, remote string, imgList []*ImgData
583603}
584604
585605func (r * Registry ) SearchRepositories (term string ) (* SearchResults , error ) {
606+ utils .Debugf ("Index server: %s" , r .indexEndpoint )
586607 u := auth .IndexServerAddress () + "search?q=" + url .QueryEscape (term )
587608 req , err := r .reqFactory .NewRequest ("GET" , u , nil )
588609 if err != nil {
@@ -644,12 +665,13 @@ type ImgData struct {
644665}
645666
646667type Registry struct {
647- client * http.Client
648- authConfig * auth.AuthConfig
649- reqFactory * utils.HTTPRequestFactory
668+ client * http.Client
669+ authConfig * auth.AuthConfig
670+ reqFactory * utils.HTTPRequestFactory
671+ indexEndpoint string
650672}
651673
652- func NewRegistry (root string , authConfig * auth.AuthConfig , factory * utils.HTTPRequestFactory ) (r * Registry , err error ) {
674+ func NewRegistry (authConfig * auth.AuthConfig , factory * utils.HTTPRequestFactory , indexEndpoint string ) (r * Registry , err error ) {
653675 httpTransport := & http.Transport {
654676 DisableKeepAlives : true ,
655677 Proxy : http .ProxyFromEnvironment ,
@@ -660,12 +682,22 @@ func NewRegistry(root string, authConfig *auth.AuthConfig, factory *utils.HTTPRe
660682 client : & http.Client {
661683 Transport : httpTransport ,
662684 },
685+ indexEndpoint : indexEndpoint ,
663686 }
664687 r .client .Jar , err = cookiejar .New (nil )
665688 if err != nil {
666689 return nil , err
667690 }
668691
692+ // If we're working with a private registry over HTTPS, send Basic Auth headers
693+ // alongside our requests.
694+ if indexEndpoint != auth .IndexServerAddress () && strings .HasPrefix (indexEndpoint , "https://" ) {
695+ utils .Debugf ("Endpoint %s is eligible for private registry auth. Enabling decorator." , indexEndpoint )
696+ dec := utils .NewHTTPAuthDecorator (authConfig .Username , authConfig .Password )
697+ factory .AddDecorator (dec )
698+ }
699+
669700 r .reqFactory = factory
670701 return r , nil
671702}
703+
0 commit comments