Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use old version docker and auth to dockerhub (#3)
* add dockerhuboption * fix dependency * fix docker auth * update go1.13
- Loading branch information
1 parent
9894f20
commit 1389279
Showing
9 changed files
with
240 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package dockerhub | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/docker/docker/api/types" | ||
) | ||
|
||
func getJSON(ctx context.Context, url string, auth types.AuthConfig, timeout time.Duration, response interface{}) (http.Header, error) { | ||
cli, err := new(auth, timeout) | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resp, err := cli.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(response); err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp.Header, nil | ||
} | ||
|
||
func new(auth types.AuthConfig, timeout time.Duration) (*http.Client, error) { | ||
transport := http.DefaultTransport | ||
tokenTransport := &DockerhubTokenTransport{ | ||
Transport: transport, | ||
Username: auth.Username, | ||
Password: auth.Password, | ||
} | ||
|
||
registry := &http.Client{ | ||
Timeout: timeout, | ||
Transport: tokenTransport, | ||
} | ||
return registry, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package dockerhub | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
const authURL = "https://hub.docker.com/v2/users/login/" | ||
|
||
type authToken struct { | ||
Token string `json:"token"` | ||
} | ||
|
||
type DockerhubTokenTransport struct { | ||
Transport http.RoundTripper | ||
Username string | ||
Password string | ||
} | ||
|
||
// RoundTrip defines the round tripper for token transport. | ||
func (t *DockerhubTokenTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
resp, err := t.Transport.RoundTrip(req) | ||
if err != nil { | ||
return resp, err | ||
} | ||
if t.Username == "" || t.Password == "" { | ||
return resp, nil | ||
} | ||
if !isTokenDemand(resp) { | ||
return resp, nil | ||
} | ||
resp.Body.Close() | ||
return t.authAndRetry(req) | ||
} | ||
|
||
func isTokenDemand(resp *http.Response) bool { | ||
if resp == nil { | ||
return false | ||
} | ||
if resp.StatusCode == http.StatusNotFound { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (t *DockerhubTokenTransport) authAndRetry(req *http.Request) (*http.Response, error) { | ||
token, authResp, err := t.auth(req.Context()) | ||
if err != nil { | ||
return authResp, err | ||
} | ||
|
||
response, err := t.retry(req, token) | ||
if response != nil { | ||
response.Header.Set("request-token", token) | ||
} | ||
return response, err | ||
} | ||
|
||
func (t *DockerhubTokenTransport) retry(req *http.Request, token string) (*http.Response, error) { | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
return t.Transport.RoundTrip(req) | ||
} | ||
|
||
func (t *DockerhubTokenTransport) auth(ctx context.Context) (string, *http.Response, error) { | ||
jsonStr := []byte(fmt.Sprintf(`{"username": "%s","password": "%s"}`, t.Username, t.Password)) | ||
resp, err := http.Post(authURL, "application/json", bytes.NewBuffer(jsonStr)) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
return "", resp, err | ||
} | ||
var authToken authToken | ||
if err := json.NewDecoder(resp.Body).Decode(&authToken); err != nil { | ||
return "", nil, err | ||
} | ||
token := authToken.Token | ||
return token, nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters