Просмотр исходного кода

Merge pull request #827 from porter-dev/0.5.0-fix-private-dockerhub

[0.5.0] Load image tags properly from private Docker Hub repository
abelanger5 4 лет назад
Родитель
Сommit
0c4ec11f26
1 измененных файлов с 47 добавлено и 3 удалено
  1. 47 3
      internal/registry/registry.go

+ 47 - 3
internal/registry/registry.go

@@ -9,7 +9,7 @@ import (
 	"net/url"
 	"strings"
 	"time"
-
+	
 	"github.com/aws/aws-sdk-go/aws/awserr"
 	"github.com/aws/aws-sdk-go/service/ecr"
 	"github.com/porter-dev/porter/internal/models"
@@ -697,6 +697,15 @@ type dockerHubImageResp struct {
 	Results []dockerHubImageResult `json:"results"`
 }
 
+type dockerHubLoginReq struct {
+	Username string `json:"username"`
+	Password string `json:"password"`
+}
+
+type dockerHubLoginResp struct {
+	Token string `json:"token"`
+}
+
 func (r *Registry) listDockerHubImages(repoName string, repo repository.Repository) ([]*Image, error) {
 	basic, err := repo.BasicIntegration.ReadBasicIntegration(
 		r.BasicIntegrationID,
@@ -708,7 +717,42 @@ func (r *Registry) listDockerHubImages(repoName string, repo repository.Reposito
 
 	client := &http.Client{}
 
+	// first, make a request for the access token
+
+	data, err := json.Marshal(&dockerHubLoginReq{
+		Username: string(basic.Username), 
+		Password: string(basic.Password),
+	})
+
+	if err != nil {
+		return nil, err
+	}
+
 	req, err := http.NewRequest(
+		"POST",
+		"https://hub.docker.com/v2/users/login",
+		strings.NewReader(string(data)),
+	)
+
+	if err != nil {
+		return nil, err
+	}
+
+	req.Header.Add("Content-Type", "application/json")
+
+	resp, err := client.Do(req)
+
+	if err != nil {
+		return nil, err
+	}
+
+	tokenObj := dockerHubLoginResp{}
+
+	if err := json.NewDecoder(resp.Body).Decode(&tokenObj); err != nil {
+		return nil, fmt.Errorf("Could not decode Dockerhub token from response: %v", err)
+	}
+
+	req, err = http.NewRequest(
 		"GET",
 		fmt.Sprintf("https://hub.docker.com/v2/repositories/%s/tags", strings.Split(r.URL, "docker.io/")[1]),
 		nil,
@@ -718,9 +762,9 @@ func (r *Registry) listDockerHubImages(repoName string, repo repository.Reposito
 		return nil, err
 	}
 
-	req.SetBasicAuth(string(basic.Username), string(basic.Password))
+	req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tokenObj.Token))
 
-	resp, err := client.Do(req)
+	resp, err = client.Do(req)
 
 	if err != nil {
 		return nil, err