fix: log in correctly

See #139.
This commit is contained in:
decentral1se 2021-12-26 03:44:29 +01:00
parent 86d87253c5
commit b0834925a3
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
1 changed files with 13 additions and 7 deletions

View File

@ -1,6 +1,7 @@
package client
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
@ -35,25 +36,30 @@ func GetRegistryTags(image string) (RawTags, error) {
return tags, nil
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
// getRegv2Token retrieves a registry v2 authentication token.
func getRegv2Token(cl *client.Client, image reference.Named) (string, error) {
img := reference.Path(image)
tokenURL := "https://auth.docker.io/token"
values := fmt.Sprintf("service=registry.docker.io&scope=repository:%s:pull", img)
username, userOk := os.LookupEnv("DOCKER_USERNAME")
password, passOk := os.LookupEnv("DOCKER_PASSWORD")
if userOk && passOk {
logrus.Debugf("using docker log in credentials for registry token request")
values = fmt.Sprintf("%s&grant_type=password&client_id=coopcloud.tech&username=%s&password=%s", values, username, password)
}
fullURL := fmt.Sprintf("%s?%s", tokenURL, values)
req, err := retryablehttp.NewRequest("GET", fullURL, nil)
if err != nil {
return "", err
}
username, userOk := os.LookupEnv("DOCKER_USERNAME")
password, passOk := os.LookupEnv("DOCKER_PASSWORD")
if userOk && passOk {
logrus.Debugf("using docker log in credentials for registry token request")
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", basicAuth(username, password)))
}
client := web.NewHTTPRetryClient()
res, err := client.Do(req)
if err != nil {