|
| 1 | +package dockerhub |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "unicode/utf8" |
| 9 | + |
| 10 | + mclog "github.com/chryscloud/go-microkit-plugins/log" |
| 11 | + "github.com/go-resty/resty/v2" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + authURL = "https://auth.docker.io/token" |
| 16 | + serviceURL = "registry.docker.io" |
| 17 | + registryURL = "https://registry-1.docker.io" |
| 18 | +) |
| 19 | + |
| 20 | +// Options for digital ocean |
| 21 | +type Options struct { |
| 22 | + Log mclog.Logger |
| 23 | + Host string |
| 24 | + username string |
| 25 | + password string |
| 26 | +} |
| 27 | + |
| 28 | +// Option a single option |
| 29 | +type Option func(*Options) |
| 30 | + |
| 31 | +// Log - recommended but optional |
| 32 | +func Log(log mclog.Logger) Option { |
| 33 | + return func(args *Options) { |
| 34 | + args.Log = log |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Host - remote host, options, default = registry-1.docker.io |
| 39 | +func Host(remoteHost string) Option { |
| 40 | + return func(args *Options) { |
| 41 | + args.Host = remoteHost |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Credentials - optionsl |
| 46 | +func Credentials(username, password string) Option { |
| 47 | + return func(args *Options) { |
| 48 | + args.username = username |
| 49 | + args.password = password |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Client - dockerhub abstraction |
| 54 | +type Client struct { |
| 55 | + host string |
| 56 | + log mclog.Logger |
| 57 | + httpClient *resty.Client |
| 58 | + username string |
| 59 | + password string |
| 60 | + token string |
| 61 | + mutex *sync.Mutex |
| 62 | +} |
| 63 | + |
| 64 | +type authResponse struct { |
| 65 | + Token string `json:"token"` |
| 66 | + ExpiresIn int `json:"expires_in"` |
| 67 | + IssuedAt string `json:"issued_at"` |
| 68 | +} |
| 69 | + |
| 70 | +type tagsResponse struct { |
| 71 | + Name string `json:"name"` |
| 72 | + Tags []string `json:"tags"` |
| 73 | +} |
| 74 | + |
| 75 | +// NewClient new DockerHub client to interactions with private or public docker hub (currently only Tags method supported) |
| 76 | +func NewClient(opts ...Option) DockerHub { |
| 77 | + args := &Options{} |
| 78 | + for _, op := range opts { |
| 79 | + if op != nil { |
| 80 | + op(args) |
| 81 | + } |
| 82 | + } |
| 83 | + if args.Host == "" { |
| 84 | + args.Host = registryURL |
| 85 | + } |
| 86 | + cl := resty.New().SetHeader("Content-Type", "application/json").SetHeader("Docker-Distribution-Api-Version", "registry/2.0") |
| 87 | + cl.Debug = false |
| 88 | + |
| 89 | + outClient := &Client{ |
| 90 | + host: args.Host, |
| 91 | + log: args.Log, |
| 92 | + httpClient: cl, |
| 93 | + mutex: &sync.Mutex{}, |
| 94 | + } |
| 95 | + if args.username != "" { |
| 96 | + outClient.username = args.username |
| 97 | + } |
| 98 | + if args.password != "" { |
| 99 | + outClient.password = args.password |
| 100 | + } |
| 101 | + return outClient |
| 102 | +} |
| 103 | + |
| 104 | +// Tags - returns the list of tags from the dockerhub repository |
| 105 | +func (client *Client) Tags(repository string) ([]string, error) { |
| 106 | + // remove first slash if exists in repository |
| 107 | + if strings.HasPrefix(repository, "/") { |
| 108 | + _, i := utf8.DecodeRuneInString(repository) |
| 109 | + repository = repository[i:] |
| 110 | + } |
| 111 | + url := client.host + "/v2/" + repository + "/tags/list" |
| 112 | + |
| 113 | + var tagsResponse tagsResponse |
| 114 | + var tagsErr error |
| 115 | + var tagsGetResp *resty.Response |
| 116 | + |
| 117 | + var token authResponse |
| 118 | + if client.token == "" { |
| 119 | + t, err := client.retrieveAuthToken(repository) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + token = *t |
| 124 | + client.mutex.Lock() |
| 125 | + client.token = token.Token |
| 126 | + client.mutex.Unlock() |
| 127 | + } |
| 128 | + |
| 129 | + tagsGetResp, tagsErr = client.httpClient.R().SetHeader("Authorization", "Bearer "+client.token).SetResult(&tagsResponse).Get(url) |
| 130 | + if tagsGetResp.StatusCode() == http.StatusUnauthorized { |
| 131 | + t, err := client.retrieveAuthToken(repository) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + token = *t |
| 136 | + client.mutex.Lock() |
| 137 | + client.token = token.Token |
| 138 | + client.mutex.Unlock() |
| 139 | + |
| 140 | + tagsGetResp, tagsErr = client.httpClient.R().SetResult(&tagsResponse).SetHeader("Authorization", "Bearer "+client.token).Get(url) |
| 141 | + |
| 142 | + } |
| 143 | + if tagsErr != nil { |
| 144 | + if client.log != nil { |
| 145 | + client.log.Error("failed to retrieve tags", tagsErr, tagsGetResp) |
| 146 | + } |
| 147 | + return nil, tagsErr |
| 148 | + } |
| 149 | + if tagsGetResp.StatusCode() != http.StatusOK { |
| 150 | + if client.log != nil { |
| 151 | + client.log.Error("unexpected http code returned", tagsGetResp.StatusCode(), string(tagsGetResp.Body())) |
| 152 | + } |
| 153 | + return nil, errors.New("unexpected http code returned") |
| 154 | + } |
| 155 | + |
| 156 | + return tagsResponse.Tags, nil |
| 157 | +} |
| 158 | + |
| 159 | +func (client *Client) retrieveAuthToken(repository string) (*authResponse, error) { |
| 160 | + scope := getScope(repository) |
| 161 | + tokenURL := authURL + "?service=" + serviceURL + "&" + "scope=" + scope + "&offline_token=1&client_id=microkit-plugins-1.0" |
| 162 | + var authResponse authResponse |
| 163 | + request := client.httpClient.R() |
| 164 | + if client.username != "" && client.password != "" { |
| 165 | + request = request.SetBasicAuth(client.username, client.password) |
| 166 | + } |
| 167 | + request = request.SetResult(&authResponse) |
| 168 | + tokenResp, tokenErr := request.Get(tokenURL) |
| 169 | + if tokenErr != nil { |
| 170 | + if client.log != nil { |
| 171 | + client.log.Error("failed to get authentication token", tokenErr) |
| 172 | + return nil, errors.New("Unauthirized") |
| 173 | + } |
| 174 | + } |
| 175 | + if tokenResp.StatusCode() != http.StatusOK { |
| 176 | + if client.log != nil { |
| 177 | + client.log.Error("failed to retrieve auth token", tokenResp) |
| 178 | + return nil, errors.New("failed to retrieve auth token") |
| 179 | + } |
| 180 | + } |
| 181 | + return &authResponse, nil |
| 182 | +} |
| 183 | + |
| 184 | +// e.g. cocooncam/cc |
| 185 | +func getScope(repository string) string { |
| 186 | + return "repository:" + repository + ":pull" |
| 187 | +} |
| 188 | + |
| 189 | +func slashFirstSlash(repository string) string { |
| 190 | + // remove first slash if exists in repository |
| 191 | + if strings.HasPrefix(repository, "/") { |
| 192 | + _, i := utf8.DecodeRuneInString(repository) |
| 193 | + repository = repository[i:] |
| 194 | + } |
| 195 | + return repository |
| 196 | +} |
0 commit comments