2014-08-07 10:43:06 -04:00
package registry
import (
"bytes"
"crypto/sha256"
2014-10-06 15:34:39 -04:00
// this is required for some certificates
2014-08-07 10:43:06 -04:00
_ "crypto/sha512"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"
2015-03-26 18:22:04 -04:00
"github.com/Sirupsen/logrus"
2015-04-22 08:06:58 -04:00
"github.com/docker/docker/cliconfig"
2014-10-24 18:11:48 -04:00
"github.com/docker/docker/pkg/httputils"
2015-03-29 09:51:08 -04:00
"github.com/docker/docker/pkg/requestdecorator"
2014-08-07 10:43:06 -04:00
"github.com/docker/docker/pkg/tarsum"
)
type Session struct {
2015-04-22 08:06:58 -04:00
authConfig * cliconfig . AuthConfig
2015-03-29 09:51:08 -04:00
reqFactory * requestdecorator . RequestFactory
2014-08-26 19:21:04 -04:00
indexEndpoint * Endpoint
2014-08-07 10:43:06 -04:00
jar * cookiejar . Jar
timeout TimeoutType
}
2015-04-22 08:06:58 -04:00
func NewSession ( authConfig * cliconfig . AuthConfig , factory * requestdecorator . RequestFactory , endpoint * Endpoint , timeout bool ) ( r * Session , err error ) {
2014-08-07 10:43:06 -04:00
r = & Session {
authConfig : authConfig ,
2014-08-26 19:21:04 -04:00
indexEndpoint : endpoint ,
2014-08-07 10:43:06 -04:00
}
if timeout {
r . timeout = ReceiveTimeout
}
r . jar , err = cookiejar . New ( nil )
if err != nil {
return nil , err
}
// If we're working with a standalone private registry over HTTPS, send Basic Auth headers
// alongside our requests.
2014-10-01 21:26:06 -04:00
if r . indexEndpoint . VersionString ( 1 ) != IndexServerAddress ( ) && r . indexEndpoint . URL . Scheme == "https" {
2014-08-26 19:21:04 -04:00
info , err := r . indexEndpoint . Ping ( )
2014-08-07 10:43:06 -04:00
if err != nil {
return nil , err
}
2015-04-15 18:30:09 -04:00
if info . Standalone && authConfig != nil && factory != nil {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Endpoint %s is eligible for private registry. Enabling decorator." , r . indexEndpoint . String ( ) )
2015-03-29 09:51:08 -04:00
dec := requestdecorator . NewAuthDecorator ( authConfig . Username , authConfig . Password )
2014-08-07 10:43:06 -04:00
factory . AddDecorator ( dec )
}
}
r . reqFactory = factory
return r , nil
}
func ( r * Session ) doRequest ( req * http . Request ) ( * http . Response , * http . Client , error ) {
2014-12-11 20:55:15 -05:00
return doRequest ( req , r . jar , r . timeout , r . indexEndpoint . IsSecure )
2014-08-07 10:43:06 -04:00
}
// Retrieve the history of a given image from the Registry.
// Return a list of the parent's json (requested image included)
func ( r * Session ) GetRemoteHistory ( imgID , registry string , token [ ] string ) ( [ ] string , error ) {
req , err := r . reqFactory . NewRequest ( "GET" , registry + "images/" + imgID + "/ancestry" , nil )
if err != nil {
return nil , err
}
setTokenAuth ( req , token )
res , _ , err := r . doRequest ( req )
if err != nil {
return nil , err
}
defer res . Body . Close ( )
if res . StatusCode != 200 {
if res . StatusCode == 401 {
return nil , errLoginRequired
}
2015-03-29 17:17:23 -04:00
return nil , httputils . NewHTTPRequestError ( fmt . Sprintf ( "Server error: %d trying to fetch remote history for %s" , res . StatusCode , imgID ) , res )
2014-08-07 10:43:06 -04:00
}
jsonString , err := ioutil . ReadAll ( res . Body )
if err != nil {
return nil , fmt . Errorf ( "Error while reading the http response: %s" , err )
}
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Ancestry: %s" , jsonString )
2014-08-07 10:43:06 -04:00
history := new ( [ ] string )
if err := json . Unmarshal ( jsonString , history ) ; err != nil {
return nil , err
}
return * history , nil
}
// Check if an image exists in the Registry
2014-11-16 08:25:10 -05:00
func ( r * Session ) LookupRemoteImage ( imgID , registry string , token [ ] string ) error {
2014-08-07 10:43:06 -04:00
req , err := r . reqFactory . NewRequest ( "GET" , registry + "images/" + imgID + "/json" , nil )
if err != nil {
2014-11-16 08:25:10 -05:00
return err
2014-08-07 10:43:06 -04:00
}
setTokenAuth ( req , token )
res , _ , err := r . doRequest ( req )
if err != nil {
2014-11-16 08:25:10 -05:00
return err
2014-08-07 10:43:06 -04:00
}
res . Body . Close ( )
2014-11-16 08:25:10 -05:00
if res . StatusCode != 200 {
2015-03-29 17:17:23 -04:00
return httputils . NewHTTPRequestError ( fmt . Sprintf ( "HTTP code %d" , res . StatusCode ) , res )
2014-11-16 08:25:10 -05:00
}
return nil
2014-08-07 10:43:06 -04:00
}
// Retrieve an image from the Registry.
func ( r * Session ) GetRemoteImageJSON ( imgID , registry string , token [ ] string ) ( [ ] byte , int , error ) {
// Get the JSON
req , err := r . reqFactory . NewRequest ( "GET" , registry + "images/" + imgID + "/json" , nil )
if err != nil {
return nil , - 1 , fmt . Errorf ( "Failed to download json: %s" , err )
}
setTokenAuth ( req , token )
res , _ , err := r . doRequest ( req )
if err != nil {
return nil , - 1 , fmt . Errorf ( "Failed to download json: %s" , err )
}
defer res . Body . Close ( )
if res . StatusCode != 200 {
2015-03-29 17:17:23 -04:00
return nil , - 1 , httputils . NewHTTPRequestError ( fmt . Sprintf ( "HTTP code %d" , res . StatusCode ) , res )
2014-08-07 10:43:06 -04:00
}
// if the size header is not present, then set it to '-1'
imageSize := - 1
if hdr := res . Header . Get ( "X-Docker-Size" ) ; hdr != "" {
imageSize , err = strconv . Atoi ( hdr )
if err != nil {
return nil , - 1 , err
}
}
jsonString , err := ioutil . ReadAll ( res . Body )
if err != nil {
return nil , - 1 , fmt . Errorf ( "Failed to parse downloaded json: %s (%s)" , err , jsonString )
}
return jsonString , imageSize , nil
}
func ( r * Session ) GetRemoteImageLayer ( imgID , registry string , token [ ] string , imgSize int64 ) ( io . ReadCloser , error ) {
var (
2014-09-03 09:21:06 -04:00
retries = 5
statusCode = 0
client * http . Client
res * http . Response
imageURL = fmt . Sprintf ( "%simages/%s/layer" , registry , imgID )
2014-08-07 10:43:06 -04:00
)
req , err := r . reqFactory . NewRequest ( "GET" , imageURL , nil )
if err != nil {
return nil , fmt . Errorf ( "Error while getting from the server: %s\n" , err )
}
setTokenAuth ( req , token )
for i := 1 ; i <= retries ; i ++ {
2014-09-03 09:21:06 -04:00
statusCode = 0
2014-08-07 10:43:06 -04:00
res , client , err = r . doRequest ( req )
if err != nil {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Error contacting registry: %s" , err )
2014-09-03 09:21:06 -04:00
if res != nil {
if res . Body != nil {
res . Body . Close ( )
}
statusCode = res . StatusCode
2014-09-03 10:26:56 -04:00
}
2014-08-07 10:43:06 -04:00
if i == retries {
return nil , fmt . Errorf ( "Server error: Status %d while fetching image layer (%s)" ,
2014-09-03 09:21:06 -04:00
statusCode , imgID )
2014-08-07 10:43:06 -04:00
}
time . Sleep ( time . Duration ( i ) * 5 * time . Second )
continue
}
break
}
if res . StatusCode != 200 {
res . Body . Close ( )
return nil , fmt . Errorf ( "Server error: Status %d while fetching image layer (%s)" ,
res . StatusCode , imgID )
}
if res . Header . Get ( "Accept-Ranges" ) == "bytes" && imgSize > 0 {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "server supports resume" )
2014-08-07 10:43:06 -04:00
return httputils . ResumableRequestReaderWithInitialResponse ( client , req , 5 , imgSize , res ) , nil
}
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "server doesn't support resume" )
2014-08-07 10:43:06 -04:00
return res . Body , nil
}
func ( r * Session ) GetRemoteTags ( registries [ ] string , repository string , token [ ] string ) ( map [ string ] string , error ) {
if strings . Count ( repository , "/" ) == 0 {
// This will be removed once the Registry supports auto-resolution on
// the "library" namespace
repository = "library/" + repository
}
for _ , host := range registries {
endpoint := fmt . Sprintf ( "%srepositories/%s/tags" , host , repository )
req , err := r . reqFactory . NewRequest ( "GET" , endpoint , nil )
if err != nil {
return nil , err
}
setTokenAuth ( req , token )
res , _ , err := r . doRequest ( req )
if err != nil {
return nil , err
}
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Got status code %d from %s" , res . StatusCode , endpoint )
2014-08-07 10:43:06 -04:00
defer res . Body . Close ( )
2015-04-19 09:23:48 -04:00
if res . StatusCode == 404 {
2014-08-07 10:43:06 -04:00
return nil , fmt . Errorf ( "Repository not found" )
2015-04-19 17:36:58 -04:00
}
if res . StatusCode != 200 {
2015-04-19 09:23:48 -04:00
continue
2014-08-07 10:43:06 -04:00
}
result := make ( map [ string ] string )
2014-10-27 15:04:36 -04:00
if err := json . NewDecoder ( res . Body ) . Decode ( & result ) ; err != nil {
2014-08-07 10:43:06 -04:00
return nil , err
}
return result , nil
}
return nil , fmt . Errorf ( "Could not reach any registry endpoint" )
}
func buildEndpointsList ( headers [ ] string , indexEp string ) ( [ ] string , error ) {
var endpoints [ ] string
2014-10-06 15:34:39 -04:00
parsedURL , err := url . Parse ( indexEp )
2014-08-07 10:43:06 -04:00
if err != nil {
return nil , err
}
2014-10-06 15:34:39 -04:00
var urlScheme = parsedURL . Scheme
2014-08-07 10:43:06 -04:00
// The Registry's URL scheme has to match the Index'
for _ , ep := range headers {
epList := strings . Split ( ep , "," )
for _ , epListElement := range epList {
endpoints = append (
endpoints ,
fmt . Sprintf ( "%s://%s/v1/" , urlScheme , strings . TrimSpace ( epListElement ) ) )
}
}
return endpoints , nil
}
func ( r * Session ) GetRepositoryData ( remote string ) ( * RepositoryData , error ) {
2014-10-01 21:26:06 -04:00
repositoryTarget := fmt . Sprintf ( "%srepositories/%s/images" , r . indexEndpoint . VersionString ( 1 ) , remote )
2014-08-07 10:43:06 -04:00
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "[registry] Calling GET %s" , repositoryTarget )
2014-08-07 10:43:06 -04:00
req , err := r . reqFactory . NewRequest ( "GET" , repositoryTarget , nil )
if err != nil {
return nil , err
}
if r . authConfig != nil && len ( r . authConfig . Username ) > 0 {
req . SetBasicAuth ( r . authConfig . Username , r . authConfig . Password )
}
req . Header . Set ( "X-Docker-Token" , "true" )
res , _ , err := r . doRequest ( req )
if err != nil {
return nil , err
}
defer res . Body . Close ( )
if res . StatusCode == 401 {
return nil , errLoginRequired
}
// TODO: Right now we're ignoring checksums in the response body.
// In the future, we need to use them to check image validity.
2015-03-22 21:15:18 -04:00
if res . StatusCode == 404 {
2015-03-29 17:17:23 -04:00
return nil , httputils . NewHTTPRequestError ( fmt . Sprintf ( "HTTP code: %d" , res . StatusCode ) , res )
2015-03-22 21:15:18 -04:00
} else if res . StatusCode != 200 {
2015-03-14 04:31:35 -04:00
errBody , err := ioutil . ReadAll ( res . Body )
if err != nil {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Error reading response body: %s" , err )
2015-03-14 04:31:35 -04:00
}
2015-03-29 17:17:23 -04:00
return nil , httputils . NewHTTPRequestError ( fmt . Sprintf ( "Error: Status %d trying to pull repository %s: %q" , res . StatusCode , remote , errBody ) , res )
2014-08-07 10:43:06 -04:00
}
var tokens [ ] string
if res . Header . Get ( "X-Docker-Token" ) != "" {
tokens = res . Header [ "X-Docker-Token" ]
}
var endpoints [ ] string
if res . Header . Get ( "X-Docker-Endpoints" ) != "" {
2014-10-01 21:26:06 -04:00
endpoints , err = buildEndpointsList ( res . Header [ "X-Docker-Endpoints" ] , r . indexEndpoint . VersionString ( 1 ) )
2014-08-07 10:43:06 -04:00
if err != nil {
return nil , err
}
} else {
// Assume the endpoint is on the same host
2014-08-26 19:21:04 -04:00
endpoints = append ( endpoints , fmt . Sprintf ( "%s://%s/v1/" , r . indexEndpoint . URL . Scheme , req . URL . Host ) )
2014-08-07 10:43:06 -04:00
}
remoteChecksums := [ ] * ImgData { }
2014-10-27 15:04:36 -04:00
if err := json . NewDecoder ( res . Body ) . Decode ( & remoteChecksums ) ; err != nil {
2014-08-07 10:43:06 -04:00
return nil , err
}
// Forge a better object from the retrieved data
imgsData := make ( map [ string ] * ImgData )
for _ , elem := range remoteChecksums {
imgsData [ elem . ID ] = elem
}
return & RepositoryData {
ImgList : imgsData ,
Endpoints : endpoints ,
Tokens : tokens ,
} , nil
}
func ( r * Session ) PushImageChecksumRegistry ( imgData * ImgData , registry string , token [ ] string ) error {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "[registry] Calling PUT %s" , registry + "images/" + imgData . ID + "/checksum" )
2014-08-07 10:43:06 -04:00
req , err := r . reqFactory . NewRequest ( "PUT" , registry + "images/" + imgData . ID + "/checksum" , nil )
if err != nil {
return err
}
setTokenAuth ( req , token )
req . Header . Set ( "X-Docker-Checksum" , imgData . Checksum )
req . Header . Set ( "X-Docker-Checksum-Payload" , imgData . ChecksumPayload )
res , _ , err := r . doRequest ( req )
if err != nil {
return fmt . Errorf ( "Failed to upload metadata: %s" , err )
}
defer res . Body . Close ( )
if len ( res . Cookies ( ) ) > 0 {
r . jar . SetCookies ( req . URL , res . Cookies ( ) )
}
if res . StatusCode != 200 {
errBody , err := ioutil . ReadAll ( res . Body )
if err != nil {
return fmt . Errorf ( "HTTP code %d while uploading metadata and error when trying to parse response body: %s" , res . StatusCode , err )
}
var jsonBody map [ string ] string
if err := json . Unmarshal ( errBody , & jsonBody ) ; err != nil {
errBody = [ ] byte ( err . Error ( ) )
} else if jsonBody [ "error" ] == "Image already exists" {
return ErrAlreadyExists
}
2015-03-16 18:32:47 -04:00
return fmt . Errorf ( "HTTP code %d while uploading metadata: %q" , res . StatusCode , errBody )
2014-08-07 10:43:06 -04:00
}
return nil
}
// Push a local image to the registry
func ( r * Session ) PushImageJSONRegistry ( imgData * ImgData , jsonRaw [ ] byte , registry string , token [ ] string ) error {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "[registry] Calling PUT %s" , registry + "images/" + imgData . ID + "/json" )
2014-08-07 10:43:06 -04:00
req , err := r . reqFactory . NewRequest ( "PUT" , registry + "images/" + imgData . ID + "/json" , bytes . NewReader ( jsonRaw ) )
if err != nil {
return err
}
req . Header . Add ( "Content-type" , "application/json" )
setTokenAuth ( req , token )
res , _ , err := r . doRequest ( req )
if err != nil {
return fmt . Errorf ( "Failed to upload metadata: %s" , err )
}
defer res . Body . Close ( )
if res . StatusCode == 401 && strings . HasPrefix ( registry , "http://" ) {
2015-03-29 17:17:23 -04:00
return httputils . NewHTTPRequestError ( "HTTP code 401, Docker will not send auth headers over HTTP." , res )
2014-08-07 10:43:06 -04:00
}
if res . StatusCode != 200 {
errBody , err := ioutil . ReadAll ( res . Body )
if err != nil {
2015-03-29 17:17:23 -04:00
return httputils . NewHTTPRequestError ( fmt . Sprintf ( "HTTP code %d while uploading metadata and error when trying to parse response body: %s" , res . StatusCode , err ) , res )
2014-08-07 10:43:06 -04:00
}
var jsonBody map [ string ] string
if err := json . Unmarshal ( errBody , & jsonBody ) ; err != nil {
errBody = [ ] byte ( err . Error ( ) )
} else if jsonBody [ "error" ] == "Image already exists" {
return ErrAlreadyExists
}
2015-03-29 17:17:23 -04:00
return httputils . NewHTTPRequestError ( fmt . Sprintf ( "HTTP code %d while uploading metadata: %q" , res . StatusCode , errBody ) , res )
2014-08-07 10:43:06 -04:00
}
return nil
}
func ( r * Session ) PushImageLayerRegistry ( imgID string , layer io . Reader , registry string , token [ ] string , jsonRaw [ ] byte ) ( checksum string , checksumPayload string , err error ) {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "[registry] Calling PUT %s" , registry + "images/" + imgID + "/layer" )
2014-08-07 10:43:06 -04:00
2014-08-21 16:12:52 -04:00
tarsumLayer , err := tarsum . NewTarSum ( layer , false , tarsum . Version0 )
if err != nil {
return "" , "" , err
}
2014-08-07 10:43:06 -04:00
h := sha256 . New ( )
h . Write ( jsonRaw )
h . Write ( [ ] byte { '\n' } )
checksumLayer := io . TeeReader ( tarsumLayer , h )
req , err := r . reqFactory . NewRequest ( "PUT" , registry + "images/" + imgID + "/layer" , checksumLayer )
if err != nil {
return "" , "" , err
}
req . Header . Add ( "Content-Type" , "application/octet-stream" )
req . ContentLength = - 1
req . TransferEncoding = [ ] string { "chunked" }
setTokenAuth ( req , token )
res , _ , err := r . doRequest ( req )
if err != nil {
return "" , "" , fmt . Errorf ( "Failed to upload layer: %s" , err )
}
if rc , ok := layer . ( io . Closer ) ; ok {
if err := rc . Close ( ) ; err != nil {
return "" , "" , err
}
}
defer res . Body . Close ( )
if res . StatusCode != 200 {
errBody , err := ioutil . ReadAll ( res . Body )
if err != nil {
2015-03-29 17:17:23 -04:00
return "" , "" , httputils . NewHTTPRequestError ( fmt . Sprintf ( "HTTP code %d while uploading metadata and error when trying to parse response body: %s" , res . StatusCode , err ) , res )
2014-08-07 10:43:06 -04:00
}
2015-03-29 17:17:23 -04:00
return "" , "" , httputils . NewHTTPRequestError ( fmt . Sprintf ( "Received HTTP code %d while uploading layer: %q" , res . StatusCode , errBody ) , res )
2014-08-07 10:43:06 -04:00
}
checksumPayload = "sha256:" + hex . EncodeToString ( h . Sum ( nil ) )
return tarsumLayer . Sum ( jsonRaw ) , checksumPayload , nil
}
// push a tag on the registry.
// Remote has the format '<user>/<repo>
func ( r * Session ) PushRegistryTag ( remote , revision , tag , registry string , token [ ] string ) error {
// "jsonify" the string
revision = "\"" + revision + "\""
path := fmt . Sprintf ( "repositories/%s/tags/%s" , remote , tag )
req , err := r . reqFactory . NewRequest ( "PUT" , registry + path , strings . NewReader ( revision ) )
if err != nil {
return err
}
req . Header . Add ( "Content-type" , "application/json" )
setTokenAuth ( req , token )
req . ContentLength = int64 ( len ( revision ) )
res , _ , err := r . doRequest ( req )
if err != nil {
return err
}
res . Body . Close ( )
if res . StatusCode != 200 && res . StatusCode != 201 {
2015-03-29 17:17:23 -04:00
return httputils . NewHTTPRequestError ( fmt . Sprintf ( "Internal server error: %d trying to push tag %s on %s" , res . StatusCode , tag , remote ) , res )
2014-08-07 10:43:06 -04:00
}
return nil
}
func ( r * Session ) PushImageJSONIndex ( remote string , imgList [ ] * ImgData , validate bool , regs [ ] string ) ( * RepositoryData , error ) {
cleanImgList := [ ] * ImgData { }
if validate {
for _ , elem := range imgList {
if elem . Checksum != "" {
cleanImgList = append ( cleanImgList , elem )
}
}
} else {
cleanImgList = imgList
}
imgListJSON , err := json . Marshal ( cleanImgList )
if err != nil {
return nil , err
}
var suffix string
if validate {
suffix = "images"
}
2014-10-01 21:26:06 -04:00
u := fmt . Sprintf ( "%srepositories/%s/%s" , r . indexEndpoint . VersionString ( 1 ) , remote , suffix )
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "[registry] PUT %s" , u )
logrus . Debugf ( "Image list pushed to index:\n%s" , imgListJSON )
2014-12-10 21:08:40 -05:00
headers := map [ string ] [ ] string {
"Content-type" : { "application/json" } ,
"X-Docker-Token" : { "true" } ,
2014-08-07 10:43:06 -04:00
}
if validate {
2014-12-10 21:08:40 -05:00
headers [ "X-Docker-Endpoints" ] = regs
2014-08-07 10:43:06 -04:00
}
// Redirect if necessary
2014-12-10 21:08:40 -05:00
var res * http . Response
for {
if res , err = r . putImageRequest ( u , headers , imgListJSON ) ; err != nil {
2014-08-07 10:43:06 -04:00
return nil , err
}
2014-12-10 21:08:40 -05:00
if ! shouldRedirect ( res ) {
break
2014-08-07 10:43:06 -04:00
}
2014-12-10 21:08:40 -05:00
res . Body . Close ( )
u = res . Header . Get ( "Location" )
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Redirected to %s" , u )
2014-08-07 10:43:06 -04:00
}
2014-12-10 21:08:40 -05:00
defer res . Body . Close ( )
2014-08-07 10:43:06 -04:00
2015-01-12 14:56:01 -05:00
if res . StatusCode == 401 {
return nil , errLoginRequired
}
2014-08-07 10:43:06 -04:00
var tokens , endpoints [ ] string
if ! validate {
if res . StatusCode != 200 && res . StatusCode != 201 {
errBody , err := ioutil . ReadAll ( res . Body )
if err != nil {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Error reading response body: %s" , err )
2014-08-07 10:43:06 -04:00
}
2015-03-29 17:17:23 -04:00
return nil , httputils . NewHTTPRequestError ( fmt . Sprintf ( "Error: Status %d trying to push repository %s: %q" , res . StatusCode , remote , errBody ) , res )
2014-08-07 10:43:06 -04:00
}
2015-04-19 09:23:48 -04:00
if res . Header . Get ( "X-Docker-Token" ) == "" {
2014-08-07 10:43:06 -04:00
return nil , fmt . Errorf ( "Index response didn't contain an access token" )
}
2015-04-19 09:23:48 -04:00
tokens = res . Header [ "X-Docker-Token" ]
logrus . Debugf ( "Auth token: %v" , tokens )
2014-08-07 10:43:06 -04:00
2015-04-19 09:23:48 -04:00
if res . Header . Get ( "X-Docker-Endpoints" ) == "" {
2014-08-07 10:43:06 -04:00
return nil , fmt . Errorf ( "Index response didn't contain any endpoints" )
}
2015-04-19 09:23:48 -04:00
endpoints , err = buildEndpointsList ( res . Header [ "X-Docker-Endpoints" ] , r . indexEndpoint . VersionString ( 1 ) )
if err != nil {
return nil , err
}
2014-08-07 10:43:06 -04:00
}
if validate {
if res . StatusCode != 204 {
errBody , err := ioutil . ReadAll ( res . Body )
if err != nil {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Error reading response body: %s" , err )
2014-08-07 10:43:06 -04:00
}
2015-03-29 17:17:23 -04:00
return nil , httputils . NewHTTPRequestError ( fmt . Sprintf ( "Error: Status %d trying to push checksums %s: %q" , res . StatusCode , remote , errBody ) , res )
2014-08-07 10:43:06 -04:00
}
}
return & RepositoryData {
Tokens : tokens ,
Endpoints : endpoints ,
} , nil
}
2014-12-10 21:08:40 -05:00
func ( r * Session ) putImageRequest ( u string , headers map [ string ] [ ] string , body [ ] byte ) ( * http . Response , error ) {
req , err := r . reqFactory . NewRequest ( "PUT" , u , bytes . NewReader ( body ) )
if err != nil {
return nil , err
}
req . SetBasicAuth ( r . authConfig . Username , r . authConfig . Password )
req . ContentLength = int64 ( len ( body ) )
for k , v := range headers {
req . Header [ k ] = v
}
response , _ , err := r . doRequest ( req )
if err != nil {
return nil , err
}
return response , nil
}
func shouldRedirect ( response * http . Response ) bool {
return response . StatusCode >= 300 && response . StatusCode < 400
}
2014-08-07 10:43:06 -04:00
func ( r * Session ) SearchRepositories ( term string ) ( * SearchResults , error ) {
2015-03-26 18:22:04 -04:00
logrus . Debugf ( "Index server: %s" , r . indexEndpoint )
2014-10-01 21:26:06 -04:00
u := r . indexEndpoint . VersionString ( 1 ) + "search?q=" + url . QueryEscape ( term )
2014-08-07 10:43:06 -04:00
req , err := r . reqFactory . NewRequest ( "GET" , u , nil )
if err != nil {
return nil , err
}
if r . authConfig != nil && len ( r . authConfig . Username ) > 0 {
req . SetBasicAuth ( r . authConfig . Username , r . authConfig . Password )
}
req . Header . Set ( "X-Docker-Token" , "true" )
res , _ , err := r . doRequest ( req )
if err != nil {
return nil , err
}
defer res . Body . Close ( )
if res . StatusCode != 200 {
2015-03-29 17:17:23 -04:00
return nil , httputils . NewHTTPRequestError ( fmt . Sprintf ( "Unexpected status code %d" , res . StatusCode ) , res )
2014-08-07 10:43:06 -04:00
}
result := new ( SearchResults )
2015-04-26 12:50:25 -04:00
return result , json . NewDecoder ( res . Body ) . Decode ( result )
2014-08-07 10:43:06 -04:00
}
2015-04-22 08:06:58 -04:00
func ( r * Session ) GetAuthConfig ( withPasswd bool ) * cliconfig . AuthConfig {
2014-08-07 10:43:06 -04:00
password := ""
if withPasswd {
password = r . authConfig . Password
}
2015-04-22 08:06:58 -04:00
return & cliconfig . AuthConfig {
2014-08-07 10:43:06 -04:00
Username : r . authConfig . Username ,
Password : password ,
Email : r . authConfig . Email ,
}
}
func setTokenAuth ( req * http . Request , token [ ] string ) {
if req . Header . Get ( "Authorization" ) == "" { // Don't override
req . Header . Set ( "Authorization" , "Token " + strings . Join ( token , "," ) )
}
}