2019-09-16 19:36:27 +00:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-09-24 12:42:46 +00:00
|
|
|
"github.com/volker-raschek/docker-hub-description-updater/pkg/types"
|
2019-09-24 13:38:34 +00:00
|
|
|
"github.com/volker-raschek/go-logger/pkg/logger"
|
2019-09-16 19:36:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dockerHubAPI = "https://hub.docker.com/v2"
|
|
|
|
flogger logger.Logger
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flogger = logger.NewSilentLogger()
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRepository(namespace string, name string, token *types.Token) (*types.Repository, error) {
|
|
|
|
|
|
|
|
if len(namespace) <= 0 {
|
|
|
|
return nil, errorNoNamespaceDefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) <= 0 {
|
|
|
|
return nil, errorNoRepositoryDefined
|
|
|
|
}
|
|
|
|
|
|
|
|
client := new(http.Client)
|
|
|
|
|
|
|
|
url, err := url.Parse(fmt.Sprintf("%v/repositories/%v/%v", dockerHubAPI, namespace, name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not prase URL: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not create request to get repository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if token != nil {
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("JWT %v", token.Token))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("An error has occured: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("Invalid HTTP-Statuscode: Get %v but expect 200", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
repository := new(types.Repository)
|
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
|
|
if err := jsonDecoder.Decode(repository); err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not encode JSON from Repository struct: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return repository, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetToken(loginCredentials *types.LoginCredentials) (*types.Token, error) {
|
|
|
|
|
|
|
|
if len(loginCredentials.User) <= 0 {
|
|
|
|
return nil, errorNoUserDefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(loginCredentials.Password) <= 0 {
|
|
|
|
return nil, errorNoPasswordDefined
|
|
|
|
}
|
|
|
|
|
|
|
|
client := new(http.Client)
|
|
|
|
|
|
|
|
loginBuffer := new(bytes.Buffer)
|
|
|
|
jsonEncoder := json.NewEncoder(loginBuffer)
|
|
|
|
if err := jsonEncoder.Encode(loginCredentials); err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not encode JSON from LoginCredential struct: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%v/users/login/", dockerHubAPI), loginBuffer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not create request to get token from %v: %v", dockerHubAPI, err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("An error has occured after sending the http request to get a JWT token from %v: %v", dockerHubAPI, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("Invalid HTTP-Statuscode while getting the JWT Token: Get %v but expect 200", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
token := new(types.Token)
|
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
|
|
if err := jsonDecoder.Decode(token); err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not decode token: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PatchRepository(repository *types.Repository, token *types.Token) (*types.Repository, error) {
|
|
|
|
|
|
|
|
if len(repository.Namespcace) <= 0 {
|
|
|
|
return nil, errorNoNamespaceDefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(repository.Name) <= 0 {
|
|
|
|
return nil, errorNoRepositoryDefined
|
|
|
|
}
|
|
|
|
|
2019-11-12 19:11:40 +00:00
|
|
|
// repositoryBuffer := new(bytes.Buffer)
|
|
|
|
// jsonEncoder := json.NewEncoder(repositoryBuffer)
|
|
|
|
// if err := jsonEncoder.Encode(repository); err != nil {
|
|
|
|
// return nil, fmt.Errorf("Can not encode JSON from Repository struct: %v", err)
|
|
|
|
// }
|
2019-09-16 19:36:27 +00:00
|
|
|
|
|
|
|
client := new(http.Client)
|
|
|
|
|
2019-11-12 19:11:40 +00:00
|
|
|
patchURL, err := url.Parse(fmt.Sprintf("%v/repositories/%v/%v", dockerHubAPI, repository.Namespcace, repository.Name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not prase URL: %v", err)
|
|
|
|
}
|
2019-09-16 19:36:27 +00:00
|
|
|
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("full_description", repository.FullDescription)
|
|
|
|
|
2019-11-12 19:11:40 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPatch, patchURL.String(), strings.NewReader(data.Encode()))
|
2019-09-16 19:36:27 +00:00
|
|
|
if err != nil {
|
2019-11-12 19:11:40 +00:00
|
|
|
return nil, fmt.Errorf("Can not create http request to update file: %v", err)
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("JWT %v", token.Token))
|
2019-11-12 19:11:40 +00:00
|
|
|
//req.Header.Add("Content-Type", "application/json")
|
2019-09-16 19:36:27 +00:00
|
|
|
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
|
2019-11-12 19:11:40 +00:00
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
2019-09-16 19:36:27 +00:00
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("An error has occured: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2019-11-12 19:11:40 +00:00
|
|
|
if resp.StatusCode != 200 {
|
2019-09-16 19:36:27 +00:00
|
|
|
bodyBytes, _ := ioutil.ReadAll(resp.Body)
|
2019-11-12 19:11:40 +00:00
|
|
|
return nil, fmt.Errorf("Invalid HTTP-Statuscode: Get %v but expect 200: %v", resp.StatusCode, string(bodyBytes))
|
2019-09-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
patchedRepository := new(types.Repository)
|
|
|
|
|
2019-11-12 19:11:40 +00:00
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
|
|
if err := jsonDecoder.Decode(patchedRepository); err != nil {
|
2019-09-16 19:36:27 +00:00
|
|
|
return nil, fmt.Errorf("Can not encode JSON from Repository struct: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return patchedRepository, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetLogger(l logger.Logger) {
|
|
|
|
flogger = l
|
|
|
|
}
|