list works

This commit is contained in:
David Lawrence 2015-06-17 23:45:51 -07:00
parent 322f60b1ba
commit e434232709
9 changed files with 82 additions and 36 deletions

2
Godeps/Godeps.json generated
View File

@ -47,7 +47,7 @@
}, },
{ {
"ImportPath": "github.com/endophage/gotuf", "ImportPath": "github.com/endophage/gotuf",
"Rev": "4e1cdf8615f2039032f44b575cb48842a523919f" "Rev": "de9ae3ede560ae61c3653aa98a5c07587bbb04a7"
}, },
{ {
"ImportPath": "github.com/go-sql-driver/mysql", "ImportPath": "github.com/go-sql-driver/mysql",

View File

@ -57,12 +57,12 @@ func (c *Client) Update() error {
func (c *Client) update() error { func (c *Client) update() error {
err := c.downloadTimestamp() err := c.downloadTimestamp()
if err != nil { if err != nil {
logrus.Errorf("Client Update (Timestamp):", err) logrus.Errorf("Client Update (Timestamp): ", err.Error())
return err return err
} }
err = c.downloadSnapshot() err = c.downloadSnapshot()
if err != nil { if err != nil {
logrus.Errorf("Client Update (Snapshot):", err) logrus.Errorf("Client Update (Snapshot): ", err.Error())
return err return err
} }
err = c.checkRoot() err = c.checkRoot()
@ -71,7 +71,7 @@ func (c *Client) update() error {
} }
err = c.downloadTargets("targets") err = c.downloadTargets("targets")
if err != nil { if err != nil {
logrus.Errorf("Client Update (Targets):", err) logrus.Errorf("Client Update (Targets): ", err.Error())
return err return err
} }
return nil return nil
@ -130,7 +130,7 @@ func (c *Client) downloadTimestamp() error {
// downloadSnapshot is responsible for downloading the snapshot.json // downloadSnapshot is responsible for downloading the snapshot.json
func (c *Client) downloadSnapshot() error { func (c *Client) downloadSnapshot() error {
role := data.RoleName("snapshot") role := data.RoleName("snapshot")
size := c.local.Timestamp.Signed.Meta[role+".txt"].Length size := c.local.Timestamp.Signed.Meta[role].Length
raw, err := c.remote.GetMeta(role, size) raw, err := c.remote.GetMeta(role, size)
if err != nil { if err != nil {
return err return err
@ -185,7 +185,7 @@ func (c Client) GetTargetsFile(roleName string, keyIDs []string, snapshotMeta da
if err != nil { if err != nil {
return nil, err return nil, err
} }
r, err := c.remote.GetMeta(rolePath, snapshotMeta[roleName+".txt"].Length) r, err := c.remote.GetMeta(rolePath, snapshotMeta[roleName].Length)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -36,7 +36,7 @@ func NewTimestamp(snapshot *Signed) (*SignedTimestamp, error) {
Version: 0, Version: 0,
Expires: DefaultExpires("timestamp").String(), Expires: DefaultExpires("timestamp").String(),
Meta: Files{ Meta: Files{
ValidRoles["timestamp"]: snapshotMeta, ValidRoles["snapshot"]: snapshotMeta,
}, },
}, },
}, nil }, nil

View File

@ -22,7 +22,7 @@ import (
var Verifiers = map[string]Verifier{ var Verifiers = map[string]Verifier{
"ed25519": Ed25519Verifier{}, "ed25519": Ed25519Verifier{},
"rsa": RSAVerifier{}, "rsa": RSAVerifier{},
"rsassa-pkcs1-v1_5-sign": RSAPemVerifier{}, "rsassa-pkcs1-v1_5-sign": RSAPemVerifier{}, // RSASSA-PKCS1-V1_5-SIGN
"pycrypto-pkcs#1 pss": RSAPSSVerifier{}, "pycrypto-pkcs#1 pss": RSAPSSVerifier{},
} }
@ -95,20 +95,20 @@ func (v RSAPemVerifier) Verify(key data.Key, sig []byte, msg []byte) error {
digest := sha256.Sum256(msg) digest := sha256.Sum256(msg)
k, _ := pem.Decode([]byte(key.Public())) k, _ := pem.Decode([]byte(key.Public()))
pub, err := x509.ParsePKIXPublicKey(k.Bytes) cert, err := x509.ParseCertificate(k.Bytes)
if err != nil { if err != nil {
logrus.Infof("Failed to parse public key: %s\n", err) logrus.Errorf("Failed to parse public key: %s\n", err.Error())
return ErrInvalid return ErrInvalid
} }
rsaPub, ok := pub.(*rsa.PublicKey) rsaPub, ok := cert.PublicKey.(*rsa.PublicKey)
if !ok { if !ok {
logrus.Infof("Value returned from ParsePKIXPublicKey was not an RSA public key") logrus.Infof("Value returned from ParsePKIXPublicKey was not an RSA public key")
return ErrInvalid return ErrInvalid
} }
if err = rsa.VerifyPKCS1v15(rsaPub, crypto.SHA256, digest[:], sig); err != nil { if err = rsa.VerifyPKCS1v15(rsaPub, crypto.SHA256, digest[:], sig); err != nil {
logrus.Infof("Failed verification: %s", err) logrus.Errorf("Failed verification: %s", err.Error())
return ErrInvalid return ErrInvalid
} }
return nil return nil

View File

@ -89,24 +89,24 @@ func VerifySignatures(s *data.Signed, role string, db *keys.KeyDB) error {
valid := make(map[string]struct{}) valid := make(map[string]struct{})
for _, sig := range s.Signatures { for _, sig := range s.Signatures {
if !roleData.ValidKey(sig.KeyID) { if !roleData.ValidKey(sig.KeyID) {
logrus.Infof("continuing b/c keyid was invalid: %s for roledata %s\n", sig.KeyID, roleData) logrus.Debugf("continuing b/c keyid was invalid: %s for roledata %s\n", sig.KeyID, roleData)
continue continue
} }
key := db.GetKey(sig.KeyID) key := db.GetKey(sig.KeyID)
if key == nil { if key == nil {
logrus.Infof("continuing b/c keyid lookup was nil: %s\n", sig.KeyID) logrus.Debugf("continuing b/c keyid lookup was nil: %s\n", sig.KeyID)
continue continue
} }
// make method lookup consistent with case uniformity. // make method lookup consistent with case uniformity.
method := strings.ToLower(sig.Method) method := strings.ToLower(sig.Method)
verifier, ok := Verifiers[method] verifier, ok := Verifiers[method]
if !ok { if !ok {
logrus.Infof("continuing b/c signing method is not supported: %s\n", sig.Method) logrus.Debugf("continuing b/c signing method is not supported: %s\n", sig.Method)
continue continue
} }
if err := verifier.Verify(key, sig.Signature, msg); err != nil { if err := verifier.Verify(key, sig.Signature, msg); err != nil {
logrus.Infof("continuing b/c signature was invalid\n") logrus.Debugf("continuing b/c signature was invalid\n")
continue continue
} }
valid[sig.KeyID] = struct{}{} valid[sig.KeyID] = struct{}{}

View File

@ -8,7 +8,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
@ -244,8 +243,7 @@ func (tr *TufRepo) SetRoot(s *data.Signed) error {
logrus.Debug("Given Key ID:", kid, "\nGenerated Key ID:", key.ID()) logrus.Debug("Given Key ID:", kid, "\nGenerated Key ID:", key.ID())
} }
for roleName, role := range r.Signed.Roles { for roleName, role := range r.Signed.Roles {
roleName = strings.TrimSuffix(roleName, ".txt") baseRole, err := data.NewRole(
rol, err := data.NewRole(
roleName, roleName,
role.Threshold, role.Threshold,
role.KeyIDs, role.KeyIDs,
@ -255,7 +253,7 @@ func (tr *TufRepo) SetRoot(s *data.Signed) error {
if err != nil { if err != nil {
return err return err
} }
err = tr.keysDB.AddRole(rol) err = tr.keysDB.AddRole(baseRole)
if err != nil { if err != nil {
return err return err
} }

View File

@ -15,7 +15,11 @@ import (
) )
func Download(url url.URL) (*http.Response, error) { func Download(url url.URL) (*http.Response, error) {
return http.Get(url.String()) tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
return client.Get(url.String())
} }
func Upload(url string, body io.Reader) (*http.Response, error) { func Upload(url string, body io.Reader) (*http.Response, error) {

View File

@ -40,42 +40,42 @@ func init() {
} }
var cmdTufAdd = &cobra.Command{ var cmdTufAdd = &cobra.Command{
Use: "add [ QDN ] <target> <file path>", Use: "add [ GUN ] <target> <file path>",
Short: "pushes local updates.", Short: "pushes local updates.",
Long: "pushes all local updates within a specific TUF repo to remote trust server.", Long: "pushes all local updates within a specific TUF repo to remote trust server.",
Run: tufAdd, Run: tufAdd,
} }
var cmdTufRemove = &cobra.Command{ var cmdTufRemove = &cobra.Command{
Use: "remove [ QDN ] <target>", Use: "remove [ GUN ] <target>",
Short: "Removes a target from the TUF repo.", Short: "Removes a target from the TUF repo.",
Long: "removes a target from the local TUF repo identified by a Qualified Docker Name.", Long: "removes a target from the local TUF repo identified by a Qualified Docker Name.",
Run: tufRemove, Run: tufRemove,
} }
var cmdTufInit = &cobra.Command{ var cmdTufInit = &cobra.Command{
Use: "init [ QDN ]", Use: "init [ GUN ]",
Short: "initializes the local TUF repository.", Short: "initializes the local TUF repository.",
Long: "creates locally the initial set of TUF metadata for the Qualified Docker Name.", Long: "creates locally the initial set of TUF metadata for the Qualified Docker Name.",
Run: tufInit, Run: tufInit,
} }
var cmdTufList = &cobra.Command{ var cmdTufList = &cobra.Command{
Use: "list [ QDN ]", Use: "list [ GUN ]",
Short: "Lists all targets in a TUF repository.", Short: "Lists all targets in a TUF repository.",
Long: "lists all the targets in the TUF repository identified by the Qualified Docker Name.", Long: "lists all the targets in the TUF repository identified by the Qualified Docker Name.",
Run: tufList, Run: tufList,
} }
var cmdTufLookup = &cobra.Command{ var cmdTufLookup = &cobra.Command{
Use: "lookup [ QDN ] <target name>", Use: "lookup [ GUN ] <target name>",
Short: "Looks up a specific TUF target in a repository.", Short: "Looks up a specific TUF target in a repository.",
Long: "looks up a TUF target in a repository given a Qualified Docker Name.", Long: "looks up a TUF target in a repository given a Qualified Docker Name.",
Run: tufLookup, Run: tufLookup,
} }
var cmdTufPush = &cobra.Command{ var cmdTufPush = &cobra.Command{
Use: "push [ QDN ]", Use: "push [ GUN ]",
Short: "initializes the local TUF repository.", Short: "initializes the local TUF repository.",
Long: "creates locally the initial set of TUF metadata for the Qualified Docker Name.", Long: "creates locally the initial set of TUF metadata for the Qualified Docker Name.",
Run: tufPush, Run: tufPush,
@ -84,7 +84,7 @@ var cmdTufPush = &cobra.Command{
func tufAdd(cmd *cobra.Command, args []string) { func tufAdd(cmd *cobra.Command, args []string) {
if len(args) < 3 { if len(args) < 3 {
cmd.Usage() cmd.Usage()
fatalf("must specify a QDN, target name, and local path to target data") fatalf("must specify a GUN, target name, and local path to target data")
} }
gun := args[0] gun := args[0]
@ -212,24 +212,65 @@ func tufInit(cmd *cobra.Command, args []string) {
func tufList(cmd *cobra.Command, args []string) { func tufList(cmd *cobra.Command, args []string) {
if len(args) < 1 { if len(args) < 1 {
cmd.Usage() cmd.Usage()
fatalf("must specify a QDN") fatalf("must specify a GUN")
}
gun := args[0]
kdb := keys.NewDB()
repo := tuf.NewTufRepo(kdb, nil)
remote, err := store.NewHTTPStore(
"https://vetinari:4443/v2/"+gun+"/_trust/tuf/",
"",
"json",
"",
)
rootJSON, err := remote.GetMeta("root", 5<<20)
if err != nil {
fmt.Println("Couldn't get initial root")
fatalf(err.Error())
}
root := &data.Signed{}
err = json.Unmarshal(rootJSON, root)
if err != nil {
fmt.Println("Couldn't parse initial root")
fatalf(err.Error())
}
// TODO: Validate the root file against the key store
err = repo.SetRoot(root)
if err != nil {
fmt.Println("Error setting root")
fatalf(err.Error())
}
c := client.NewClient(
repo,
remote,
kdb,
)
err = c.Update()
if err != nil {
fmt.Println("Update failed")
fatalf(err.Error())
}
for name, meta := range repo.Targets["targets"].Signed.Targets {
fmt.Println(name, " ", meta.Hashes["sha256"], " ", meta.Length)
} }
} }
func tufLookup(cmd *cobra.Command, args []string) { func tufLookup(cmd *cobra.Command, args []string) {
if len(args) < 2 { if len(args) < 2 {
cmd.Usage() cmd.Usage()
fatalf("must specify a QDN and target path to look up.") fatalf("must specify a GUN and target path to look up.")
} }
fmt.Println("Remote trust server configured: " + remoteTrustServer) gun := args[0]
qdn := args[0]
targetName := args[1] targetName := args[1]
kdb := keys.NewDB() kdb := keys.NewDB()
repo := tuf.NewTufRepo(kdb, nil) repo := tuf.NewTufRepo(kdb, nil)
remote, err := store.NewHTTPStore( remote, err := store.NewHTTPStore(
"https://localhost:4443/v2"+qdn+"/_trust/tuf/", "https://localhost:4443/v2"+gun+"/_trust/tuf/",
"", "",
"json", "json",
"", "",
@ -262,7 +303,7 @@ func tufLookup(cmd *cobra.Command, args []string) {
func tufPush(cmd *cobra.Command, args []string) { func tufPush(cmd *cobra.Command, args []string) {
if len(args) < 1 { if len(args) < 1 {
cmd.Usage() cmd.Usage()
fatalf("must specify a QDN") fatalf("must specify a GUN")
} }
gun := args[0] gun := args[0]
@ -318,7 +359,7 @@ func tufPush(cmd *cobra.Command, args []string) {
func tufRemove(cmd *cobra.Command, args []string) { func tufRemove(cmd *cobra.Command, args []string) {
if len(args) < 1 { if len(args) < 1 {
cmd.Usage() cmd.Usage()
fatalf("must specify a QDN") fatalf("must specify a GUN")
} }
} }

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/Sirupsen/logrus"
"github.com/endophage/gotuf/data" "github.com/endophage/gotuf/data"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -98,6 +99,7 @@ func GetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *er
qdn := vars["imageName"] qdn := vars["imageName"]
tufRole := vars["tufRole"] tufRole := vars["tufRole"]
data, err := store.GetCurrent(qdn, tufRole) data, err := store.GetCurrent(qdn, tufRole)
logrus.Debug("JSON: ", string(data))
if err != nil { if err != nil {
return &errors.HTTPError{ return &errors.HTTPError{
HTTPStatus: http.StatusInternalServerError, HTTPStatus: http.StatusInternalServerError,
@ -112,6 +114,7 @@ func GetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *er
Err: err, Err: err,
} }
} }
logrus.Debug("Writing data")
w.Write(data)
return nil return nil
} }