mirror of https://github.com/docker/docs.git
using roundtripper in notary client
Signed-off-by: David Lawrence <david.lawrence@docker.com> (github: endophage)
This commit is contained in:
parent
436e02d390
commit
d1b09962f1
|
@ -47,7 +47,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "github.com/endophage/gotuf",
|
||||
"Rev": "66da486b58ef378c96433af965f61ca0efaccb9a"
|
||||
"Rev": "88765abdd5ec33be6be1efa18c71d7f43c7c4983"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/go-sql-driver/mysql",
|
||||
|
|
|
@ -12,7 +12,6 @@ import (
|
|||
"path"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/endophage/gotuf/utils"
|
||||
)
|
||||
|
||||
// HTTPStore manages pulling and pushing metadata from and to a remote
|
||||
|
@ -30,9 +29,10 @@ type HTTPStore struct {
|
|||
metaExtension string
|
||||
targetsPrefix string
|
||||
keyExtension string
|
||||
roundTrip http.RoundTripper
|
||||
}
|
||||
|
||||
func NewHTTPStore(baseURL, metaPrefix, metaExtension, targetsPrefix, keyExtension string) (*HTTPStore, error) {
|
||||
func NewHTTPStore(baseURL, metaPrefix, metaExtension, targetsPrefix, keyExtension string, roundTrip http.RoundTripper) (*HTTPStore, error) {
|
||||
base, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -46,6 +46,7 @@ func NewHTTPStore(baseURL, metaPrefix, metaExtension, targetsPrefix, keyExtensio
|
|||
metaExtension: metaExtension,
|
||||
targetsPrefix: targetsPrefix,
|
||||
keyExtension: keyExtension,
|
||||
roundTrip: roundTrip,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -57,7 +58,11 @@ func (s HTTPStore) GetMeta(name string, size int64) (json.RawMessage, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := utils.Download(*url)
|
||||
req, err := http.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := s.roundTrip.RoundTrip(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -79,7 +84,11 @@ func (s HTTPStore) SetMeta(name string, blob json.RawMessage) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = utils.Upload(url.String(), bytes.NewReader(blob))
|
||||
req, err := http.NewRequest("POST", url.String(), bytes.NewReader(blob))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.roundTrip.RoundTrip(req)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -116,7 +125,11 @@ func (s HTTPStore) GetTarget(path string) (io.ReadCloser, error) {
|
|||
return nil, err
|
||||
}
|
||||
logrus.Debug("Attempting to download target: ", url.String())
|
||||
resp, err := utils.Download(*url)
|
||||
req, err := http.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := s.roundTrip.RoundTrip(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -128,7 +141,11 @@ func (s HTTPStore) GetKey(role string) ([]byte, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := utils.Download(*url)
|
||||
req, err := http.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := s.roundTrip.RoundTrip(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
@ -13,12 +14,20 @@ import (
|
|||
"github.com/endophage/gotuf/signed"
|
||||
)
|
||||
|
||||
type TestRoundTripper struct{}
|
||||
|
||||
func (rt *TestRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
func TestGetMeta(t *testing.T) {
|
||||
store, err := NewHTTPStore(
|
||||
"http://mirror1.poly.edu/test-pypi/",
|
||||
"metadata",
|
||||
"txt",
|
||||
"targets",
|
||||
"key",
|
||||
&TestRoundTripper{},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
@ -72,6 +73,7 @@ type NotaryRepository struct {
|
|||
privKeyStore *trustmanager.KeyFileStore
|
||||
rootKeyStore *trustmanager.KeyFileStore
|
||||
rootSigner *UnlockedSigner
|
||||
roundTrip http.RoundTripper
|
||||
}
|
||||
|
||||
// Target represents a simplified version of the data TUF operates on, so external
|
||||
|
@ -100,7 +102,7 @@ func NewTarget(targetName string, targetPath string) (*Target, error) {
|
|||
// NewNotaryRepository is a helper method that returns a new notary repository.
|
||||
// It takes the base directory under where all the trust files will be stored
|
||||
// (usually ~/.docker/trust/).
|
||||
func NewNotaryRepository(baseDir, gun, baseURL string) (*NotaryRepository, error) {
|
||||
func NewNotaryRepository(baseDir, gun, baseURL string, rt http.RoundTripper) (*NotaryRepository, error) {
|
||||
trustDir := filepath.Join(baseDir, trustDir)
|
||||
rootKeysDir := filepath.Join(baseDir, rootKeysDir)
|
||||
|
||||
|
@ -118,6 +120,7 @@ func NewNotaryRepository(baseDir, gun, baseURL string) (*NotaryRepository, error
|
|||
tufRepoPath: filepath.Join(baseDir, tufDir, gun),
|
||||
signer: signer,
|
||||
privKeyStore: privKeyStore,
|
||||
roundTrip: rt,
|
||||
}
|
||||
|
||||
if err := nRepo.loadKeys(trustDir, rootKeysDir); err != nil {
|
||||
|
@ -141,7 +144,7 @@ func (r *NotaryRepository) Initialize(uSigner *UnlockedSigner) error {
|
|||
return err
|
||||
}
|
||||
|
||||
remote, err := getRemoteStore(r.baseURL, r.Gun)
|
||||
remote, err := getRemoteStore(r.baseURL, r.Gun, r.roundTrip)
|
||||
rawTSKey, err := remote.GetKey("timestamp")
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -374,7 +377,7 @@ func (r *NotaryRepository) Publish(getPass passwordRetriever) error {
|
|||
return err
|
||||
}
|
||||
|
||||
remote, err := getRemoteStore(r.baseURL, r.Gun)
|
||||
remote, err := getRemoteStore(r.baseURL, r.Gun, r.roundTrip)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -574,7 +577,7 @@ func (r *NotaryRepository) validateRoot(root *data.Signed) error {
|
|||
}
|
||||
|
||||
func (r *NotaryRepository) bootstrapClient() (*tufclient.Client, error) {
|
||||
remote, err := getRemoteStore(r.baseURL, r.Gun)
|
||||
remote, err := getRemoteStore(r.baseURL, r.Gun, r.roundTrip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ func TestInitRepo(t *testing.T) {
|
|||
ts := createTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
repo, err := NewNotaryRepository(tempBaseDir, gun, ts.URL)
|
||||
repo, err := NewNotaryRepository(tempBaseDir, gun, ts.URL, http.DefaultTransport)
|
||||
assert.NoError(t, err, "error creating repo: %s", err)
|
||||
|
||||
rootKeyID, err := repo.GenRootKey("passphrase")
|
||||
|
@ -172,7 +172,7 @@ func TestAddTarget(t *testing.T) {
|
|||
ts := createTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
repo, err := NewNotaryRepository(tempBaseDir, gun, ts.URL)
|
||||
repo, err := NewNotaryRepository(tempBaseDir, gun, ts.URL, http.DefaultTransport)
|
||||
assert.NoError(t, err, "error creating repository: %s", err)
|
||||
|
||||
rootKeyID, err := repo.GenRootKey("passphrase")
|
||||
|
@ -276,7 +276,7 @@ func TestValidateRootKey(t *testing.T) {
|
|||
ts := createTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
repo, err := NewNotaryRepository(tempBaseDir, gun, ts.URL)
|
||||
repo, err := NewNotaryRepository(tempBaseDir, gun, ts.URL, http.DefaultTransport)
|
||||
assert.NoError(t, err, "error creating repository: %s", err)
|
||||
|
||||
rootKeyID, err := repo.GenRootKey("passphrase")
|
||||
|
|
|
@ -2,6 +2,7 @@ package client
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/docker/notary/client/changelist"
|
||||
|
@ -11,13 +12,14 @@ import (
|
|||
)
|
||||
|
||||
// Use this to initialize remote HTTPStores from the config settings
|
||||
func getRemoteStore(baseURL, gun string) (store.RemoteStore, error) {
|
||||
func getRemoteStore(baseURL, gun string, rt http.RoundTripper) (store.RemoteStore, error) {
|
||||
return store.NewHTTPStore(
|
||||
baseURL+"/v2/"+gun+"/_trust/tuf/",
|
||||
"",
|
||||
"json",
|
||||
"",
|
||||
"key",
|
||||
rt,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
|
@ -77,7 +78,7 @@ func tufAdd(cmd *cobra.Command, args []string) {
|
|||
targetName := args[1]
|
||||
targetPath := args[2]
|
||||
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL)
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL, http.DefaultTransport)
|
||||
if err != nil {
|
||||
fatalf(err.Error())
|
||||
}
|
||||
|
@ -101,7 +102,7 @@ func tufInit(cmd *cobra.Command, args []string) {
|
|||
|
||||
gun := args[0]
|
||||
|
||||
nRepo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL)
|
||||
nRepo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL, http.DefaultTransport)
|
||||
if err != nil {
|
||||
fatalf(err.Error())
|
||||
}
|
||||
|
@ -147,7 +148,7 @@ func tufList(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
gun := args[0]
|
||||
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL)
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL, http.DefaultTransport)
|
||||
if err != nil {
|
||||
fatalf(err.Error())
|
||||
}
|
||||
|
@ -172,7 +173,7 @@ func tufLookup(cmd *cobra.Command, args []string) {
|
|||
gun := args[0]
|
||||
targetName := args[1]
|
||||
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL)
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL, http.DefaultTransport)
|
||||
if err != nil {
|
||||
fatalf(err.Error())
|
||||
}
|
||||
|
@ -196,7 +197,7 @@ func tufPublish(cmd *cobra.Command, args []string) {
|
|||
|
||||
fmt.Println("Pushing changes to ", gun, ".")
|
||||
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL)
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL, http.DefaultTransport)
|
||||
if err != nil {
|
||||
fatalf(err.Error())
|
||||
}
|
||||
|
@ -241,7 +242,7 @@ func verify(cmd *cobra.Command, args []string) {
|
|||
//TODO (diogo): This code is copy/pasted from lookup.
|
||||
gun := args[0]
|
||||
targetName := args[1]
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL)
|
||||
repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL, http.DefaultTransport)
|
||||
if err != nil {
|
||||
fatalf(err.Error())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue