diff --git a/tuf/client/client.go b/tuf/client/client.go index 0a0d2ddcbc..88734bf9e0 100644 --- a/tuf/client/client.go +++ b/tuf/client/client.go @@ -6,7 +6,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io" "github.com/Sirupsen/logrus" "github.com/docker/notary" @@ -541,18 +540,3 @@ func (c Client) TargetMeta(role, path string, excludeRoles ...string) (*data.Fil } return meta, "" } - -// DownloadTarget downloads the target to dst from the remote -func (c Client) DownloadTarget(dst io.Writer, path string, meta *data.FileMeta) error { - reader, err := c.remote.GetTarget(path) - if err != nil { - return err - } - defer reader.Close() - r := io.TeeReader( - io.LimitReader(reader, meta.Length), - dst, - ) - err = utils.ValidateTarget(r, meta) - return err -} diff --git a/tuf/store/interfaces.go b/tuf/store/interfaces.go index 6d73da8a96..a373110d58 100644 --- a/tuf/store/interfaces.go +++ b/tuf/store/interfaces.go @@ -1,8 +1,6 @@ package store import ( - "io" - "github.com/docker/notary/tuf/data" ) @@ -23,17 +21,9 @@ type PublicKeyStore interface { GetKey(role string) ([]byte, error) } -// TargetStore represents a collection of targets that can be walked similarly -// to walking a directory, passing a callback that receives the path and meta -// for each target -type TargetStore interface { - WalkStagedTargets(paths []string, targetsFn targetsWalkFunc) error -} - // LocalStore represents a local TUF sture type LocalStore interface { MetadataStore - TargetStore } // RemoteStore is similar to LocalStore with the added expectation that it should @@ -41,5 +31,4 @@ type LocalStore interface { type RemoteStore interface { MetadataStore PublicKeyStore - GetTarget(path string) (io.ReadCloser, error) } diff --git a/tuf/store/memorystore.go b/tuf/store/memorystore.go index ca24ef3d9d..b0a630ad4f 100644 --- a/tuf/store/memorystore.go +++ b/tuf/store/memorystore.go @@ -1,10 +1,8 @@ package store import ( - "bytes" "crypto/sha256" "fmt" - "io" "github.com/docker/notary" "github.com/docker/notary/tuf/data" @@ -45,7 +43,10 @@ type MemoryStore struct { keys map[string][]data.PrivateKey } +// GetMeta returns up to size bytes of data references by name. // If size is -1, this corresponds to "infinite," but we cut off at 100MB +// as we will always know the size for everything but a timestamp and +// sometimes a root, neither of which should be exceptionally large func (m *MemoryStore) GetMeta(name string, size int64) ([]byte, error) { d, ok := m.meta[name] if ok { @@ -67,6 +68,7 @@ func (m *MemoryStore) GetMeta(name string, size int64) ([]byte, error) { return nil, ErrMetaNotFound{Resource: name} } +// SetMeta sets the metadata value for the given name func (m *MemoryStore) SetMeta(name string, meta []byte) error { m.meta[name] = meta @@ -76,6 +78,8 @@ func (m *MemoryStore) SetMeta(name string, meta []byte) error { return nil } +// SetMultiMeta sets multiple pieces of metadata for multiple names +// in a single operation. func (m *MemoryStore) SetMultiMeta(metas map[string][]byte) error { for role, blob := range metas { m.SetMeta(role, blob) @@ -86,56 +90,22 @@ func (m *MemoryStore) SetMultiMeta(metas map[string][]byte) error { // RemoveMeta removes the metadata for a single role - if the metadata doesn't // exist, no error is returned func (m *MemoryStore) RemoveMeta(name string) error { - delete(m.meta, name) - return nil -} - -func (m *MemoryStore) GetTarget(path string) (io.ReadCloser, error) { - return &utils.NoopCloser{Reader: bytes.NewReader(m.files[path])}, nil -} - -func (m *MemoryStore) WalkStagedTargets(paths []string, targetsFn targetsWalkFunc) error { - if len(paths) == 0 { - for path, dat := range m.files { - meta, err := data.NewFileMeta(bytes.NewReader(dat), "sha256") - if err != nil { - return err - } - if err = targetsFn(path, meta); err != nil { - return err - } - } - return nil - } - - for _, path := range paths { - dat, ok := m.files[path] - if !ok { - return ErrMetaNotFound{Resource: path} - } - meta, err := data.NewFileMeta(bytes.NewReader(dat), "sha256") - if err != nil { - return err - } - if err = targetsFn(path, meta); err != nil { - return err - } + if meta, ok := m.meta[name]; ok { + checksum := sha256.Sum256(meta) + path := utils.URLFilePath(name, checksum[:], true) + delete(m.meta, name) + delete(m.consistent, path) } return nil } -func (m *MemoryStore) Commit(map[string][]byte, bool, map[string]data.Hashes) error { - return nil -} - +// GetKey returns the public key for the given role func (m *MemoryStore) GetKey(role string) ([]byte, error) { return nil, fmt.Errorf("GetKey is not implemented for the MemoryStore") } -// Clear this existing memory store by setting this store as new empty one +// RemoveAll clears the existing memory store by setting this store as new empty one func (m *MemoryStore) RemoveAll() error { - m.meta = make(map[string][]byte) - m.files = make(map[string][]byte) - m.keys = make(map[string][]data.PrivateKey) + *m = *NewMemoryStore(nil, nil) return nil } diff --git a/tuf/testutils/corrupt_memorystore.go b/tuf/testutils/corrupt_memorystore.go index fb3ed495f1..52b2b70680 100644 --- a/tuf/testutils/corrupt_memorystore.go +++ b/tuf/testutils/corrupt_memorystore.go @@ -9,11 +9,15 @@ type CorruptingMemoryStore struct { store.MemoryStore } +// NewCorruptingMemoryStore returns a new instance of memory store that +// corrupts all data requested from it. func NewCorruptingMemoryStore(meta map[string][]byte, files map[string][]byte) *CorruptingMemoryStore { s := store.NewMemoryStore(meta, files) return &CorruptingMemoryStore{MemoryStore: *s} } +// GetMeta returns up to size bytes of meta identified by string. It will +// always be corrupted by setting the first character to } func (cm CorruptingMemoryStore) GetMeta(name string, size int64) ([]byte, error) { d, err := cm.MemoryStore.GetMeta(name, size) if err != nil {