Skip to content

Commit

Permalink
Merge pull request #8 from dmcgowan/wip_provenance
Browse files Browse the repository at this point in the history
Update pull to use manifest data from registry
  • Loading branch information
vbatts committed Sep 18, 2014
2 parents 3726654 + a9007c9 commit 7b011b2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 32 deletions.
2 changes: 1 addition & 1 deletion api/client/commands.go
Expand Up @@ -1169,7 +1169,7 @@ func (cli *DockerCli) CmdPush(args ...string) error {
return err
}

signedBody, err := js.PrettySignature("buildSignatures")
signedBody, err := js.PrettySignature("signatures")
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions graph/manifest.go
Expand Up @@ -46,10 +46,13 @@ func (s *TagStore) CmdManifest(job *engine.Job) engine.Status {
layersSeen := make(map[string]bool)

layer, err := s.graph.Get(layerId)
if err != nil {
return job.Error(err)
}
manifest.Architecture = layer.Architecture
var metadata runconfig.Config
metadata = *layer.Config
history := make(map[string]string)
history := make([]string, 0, cap(tarsums))

for ; layer != nil; layer, err = layer.GetParent() {
if err != nil {
Expand Down Expand Up @@ -83,7 +86,7 @@ func (s *TagStore) CmdManifest(job *engine.Job) engine.Status {
if err != nil {
return job.Error(fmt.Errorf("Cannot retrieve the path for {%s}: %s", layer.ID, err))
}
history[tarId] = string(jsonData)
history = append(history, string(jsonData))
}

manifest.BlobSums = tarsums
Expand Down
80 changes: 58 additions & 22 deletions graph/pull.go
Expand Up @@ -15,8 +15,33 @@ import (
"github.com/docker/docker/pkg/log"
"github.com/docker/docker/registry"
"github.com/docker/docker/utils"
"github.com/docker/libtrust"
)

func (s *TagStore) verifyManifest(manifestBytes []byte) (*registry.ManifestData, error) {
sig, err := libtrust.ParsePrettySignature(manifestBytes, "signatures")
if err != nil {
return nil, fmt.Errorf("error parsing payload: %s", err)
}
_, err = sig.Verify()
if err != nil {
return nil, fmt.Errorf("error verifying payload: %s", err)
}

payload, err := sig.Payload()
if err != nil {
return nil, fmt.Errorf("error retrieving payload: %s", err)
}

var manifest registry.ManifestData
err = json.Unmarshal(payload, &manifest)
if err != nil {
return nil, fmt.Errorf("error unmarshalling manifest: %s", err)
}

return &manifest, nil
}

func (s *TagStore) CmdPull(job *engine.Job) engine.Status {
if n := len(job.Args); n != 1 && n != 2 {
return job.Errorf("Usage: %s IMAGE [TAG]", job.Name)
Expand Down Expand Up @@ -84,42 +109,52 @@ func (s *TagStore) CmdPull(job *engine.Job) engine.Status {
if err != nil {
return job.Error(err)
}
manifest := map[string]interface{}{}
err = json.Unmarshal(manifestBytes, &manifest)

manifest, err := s.verifyManifest(manifestBytes)
if err != nil {
return job.Error(err)
return job.Errorf("error verifying manifest: %s", err)
}
log.Debugf("%#v", manifest["history"])
h, ok := manifest["history"].(map[string]interface{})
if !ok {
return job.Error(fmt.Errorf("manifest 'history' is not a map[string]string"))
}
log.Debugf("%#v", manifest["tarsum"])
sums, ok := manifest["tarsum"].([]interface{})
if !ok {
return job.Error(fmt.Errorf("manifest 'tarsum' is not a []string"))

if len(manifest.BlobSums) != len(manifest.History) {
return job.Errorf("length of history not equal to number of layers")
}
for _, sumInterface := range sums {
sumStr := sumInterface.(string)
jsonBytes := h[sumStr]
//
_ = jsonBytes.(string)

for i := len(manifest.BlobSums) - 1; i >= 0; i-- {
sumStr := manifest.BlobSums[i]
imgJSON := []byte(manifest.History[i])

img, err := image.NewImgJSON(imgJSON)
if err != nil {
return job.Error(fmt.Errorf("failed to parse json: %s", err))
}

chunks := strings.SplitN(sumStr, ":", 2)
if len(chunks) < 2 {
return job.Error(fmt.Errorf("expected 2 parts in the sumStr, got %#v", chunks))
}
sumType, checksum := chunks[0], chunks[1]

log.Infof("pulling blob %q to V1 img %s", sumStr, img.ID)

tmpFile, err := ioutil.TempFile("", "GetV2ImageBlob")
if err != nil {
job.Error(err)
return job.Error(err)
}
if err = r.GetV2ImageBlob(remoteName, chunks[0], chunks[1], tmpFile, nil); err != nil {
job.Error(err)
if err = r.GetV2ImageBlob(remoteName, sumType, checksum, tmpFile, nil); err != nil {
return job.Error(err)
}
fmt.Println(tmpFile)
}
tmpFile.Seek(0, 0)

log.Debugf("%#v", manifest["history"])
err = s.graph.Register([]byte(imgJSON), tmpFile, img)
if err != nil {
return job.Error(err)
}

if err = s.Set(localName, tag, img.ID, true); err != nil {
return job.Error(err)
}
}

return engine.StatusOK // return from this pull, so we don't do a v1 pull
}
Expand Down Expand Up @@ -307,6 +342,7 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint
continue
}
img, err = image.NewImgJSON(imgJSON)
// _RETURN HERE after getting image fom json
if err != nil && j == retries {
out.Write(sf.FormatProgress(utils.TruncateID(id), "Error pulling dependent layers", nil))
return fmt.Errorf("Failed to parse json: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions registry/session_prov.go
Expand Up @@ -198,11 +198,11 @@ func (r *Session) GetV2ImageBlob(imageName, sumType, sum string, blobWrtr io.Wri
return err
}
defer res.Body.Close()
if res.StatusCode != 201 {
if res.StatusCode != 200 {
if res.StatusCode == 401 {
return errLoginRequired
}
return utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to push %s blob", res.StatusCode, imageName), res)
return utils.NewHTTPRequestError(fmt.Sprintf("Server error: %d trying to pull %s blob", res.StatusCode, imageName), res)
}

_, err = io.Copy(blobWrtr, res.Body)
Expand Down
10 changes: 5 additions & 5 deletions registry/types.go
Expand Up @@ -33,11 +33,11 @@ type RegistryInfo struct {
}

type ManifestData struct {
Name string `json:"name"`
Tag string `json:"tag"`
Architecture string `json:"architecture"`
BlobSums []string `json:"blobSums"`
History map[string]string `json:"history"`
Name string `json:"name"`
Tag string `json:"tag"`
Architecture string `json:"architecture"`
BlobSums []string `json:"blobSums"`
History []string `json:"history"`
}

type APIVersion int
Expand Down
1 change: 1 addition & 0 deletions vendor/src/github.com/docker/libtrust
Submodule libtrust added at 540d8f

0 comments on commit 7b011b2

Please sign in to comment.