From 64b7838c05ec6c883e4b01235c67544dc3ad55bb Mon Sep 17 00:00:00 2001 From: Luke Hinds <7058938+lukehinds@users.noreply.github.com> Date: Thu, 1 Oct 2020 20:06:34 +0100 Subject: [PATCH] Improve Status Handling (#23) --- cmd/add.go | 46 ++++++++++------------------------------------ cmd/get.go | 36 +++++++++++++++++++----------------- cmd/getleaf.go | 13 ++++++++----- cmd/update.go | 13 +++++++++---- 4 files changed, 46 insertions(+), 62 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 8f6a6ce..6e1ef31 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -17,7 +17,7 @@ package cmd import ( "context" - "fmt" + "encoding/json" "io/ioutil" "net/http" "time" @@ -27,40 +27,6 @@ import ( "github.com/spf13/viper" ) -//type LeafData struct { -// File string `json:"file"` -// Hash string `json:"hash"` -//} - -//type LeafData struct { -// Signatures []struct { -// Keyid string `json:"keyid"` -// Sig string `json:"sig"` -// } `json:"signatures"` -// Signed struct { -// Type string `json:"_type"` -// Byproducts struct { -// ReturnValue int `json:"return-value"` -// Stderr string `json:"stderr"` -// Stdout string `json:"stdout"` -// } `json:"byproducts"` -// Command []string `json:"command"` -// Environment struct { -// } `json:"environment"` -// Materials struct { -// FooPy struct { -// Sha256 string `json:"sha256"` -// } `json:"foo.py"` -// } `json:"materials"` -// Name string `json:"name"` -// Products struct { -// FooTarGz struct { -// Sha256 string `json:"sha256"` -// } `json:"foo.tar.gz"` -// } `json:"products"` -// } `json:"signed"` -//} - // addCmd represents the add command var addCmd = &cobra.Command{ Use: "add", @@ -98,7 +64,15 @@ then hash the file into the transparency log`, if err != nil { log.Fatal(err) } - fmt.Println(string(content)) + + resp := getLeafResponse{} + + if err := json.Unmarshal(content, &resp); err != nil { + log.Fatal(err) + } + + log.Info("Status: ", resp.Status) + }, } diff --git a/cmd/get.go b/cmd/get.go index 23a4de0..40ab3b7 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -20,7 +20,6 @@ import ( "crypto" "crypto/x509" "encoding/json" - "fmt" "io/ioutil" "net/http" "time" @@ -39,8 +38,9 @@ import ( ) type getProofResponse struct { - Proof *trillian.GetInclusionProofByHashResponse - Key []byte + Status string + Proof *trillian.GetInclusionProofByHashResponse + Key []byte } // getCmd represents the get command @@ -82,8 +82,6 @@ exists within the transparency log`, log.Fatal(err) } - fmt.Println(string(content)) - resp := getProofResponse{} if err := json.Unmarshal(content, &resp); err != nil { log.Fatal(err) @@ -99,19 +97,23 @@ exists within the transparency log`, log.Fatal(err) } - leafHash := rfc6962.DefaultHasher.HashLeaf(f) - verifier := tclient.NewLogVerifier(rfc6962.DefaultHasher, pub, crypto.SHA256) - root, err := tcrypto.VerifySignedLogRoot(verifier.PubKey, verifier.SigHash, resp.Proof.SignedLogRoot) - if err != nil { - log.Fatal(err) - } - - v := merkle.NewLogVerifier(rfc6962.DefaultHasher) - proof := resp.Proof.Proof[0] - if err := v.VerifyInclusionProof(proof.LeafIndex, int64(root.TreeSize), proof.Hashes, root.RootHash, leafHash); err != nil { - log.Fatal(err) + if resp.Proof != nil { + leafHash := rfc6962.DefaultHasher.HashLeaf(f) + verifier := tclient.NewLogVerifier(rfc6962.DefaultHasher, pub, crypto.SHA256) + root, err := tcrypto.VerifySignedLogRoot(verifier.PubKey, verifier.SigHash, resp.Proof.SignedLogRoot) + if err != nil { + log.Fatal(err) + } + + v := merkle.NewLogVerifier(rfc6962.DefaultHasher) + proof := resp.Proof.Proof[0] + if err := v.VerifyInclusionProof(proof.LeafIndex, int64(root.TreeSize), proof.Hashes, root.RootHash, leafHash); err != nil { + log.Fatal(err) + } + log.Info("Proof correct!") + } else { + log.Info(resp.Status) } - log.Info("proof correct!") }, } diff --git a/cmd/getleaf.go b/cmd/getleaf.go index 71ce6b6..d850a44 100644 --- a/cmd/getleaf.go +++ b/cmd/getleaf.go @@ -36,6 +36,12 @@ import ( "github.com/spf13/viper" ) +type getLeafResponse struct { + Status RespStatusCode + Leaf *trillian.GetLeavesByIndexResponse + Key []byte +} + func GenerateRand(length int) string { b := make([]byte, length) if _, err := rand.Read(b); err != nil { @@ -44,11 +50,6 @@ func GenerateRand(length int) string { return hex.EncodeToString(b) } -type getLeafResponse struct { - Leaf *trillian.GetLeavesByIndexResponse - Key []byte -} - var getleafCmd = &cobra.Command{ Use: "getleaf", Short: "Rekor Get Leaf Command", @@ -88,6 +89,8 @@ var getleafCmd = &cobra.Command{ log.Fatal(err) } + log.Info("Status: ", resp.Status) + pub, err := x509.ParsePKIXPublicKey(resp.Key) if err != nil { log.Fatal(err) diff --git a/cmd/update.go b/cmd/update.go index d399316..f1a7479 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -43,9 +43,14 @@ import ( "github.com/spf13/cobra" ) +type RespStatusCode struct { + Code string `json:"file_recieved"` +} + type latestResponse struct { - Proof *trillian.GetLatestSignedLogRootResponse - Key []byte + Status RespStatusCode + Proof *trillian.GetLatestSignedLogRootResponse + Key []byte } type state struct { @@ -150,13 +155,13 @@ var updateCmd = &cobra.Command{ log.Fatal(err) } - fmt.Println(string(content)) - resp := latestResponse{} if err := json.Unmarshal(content, &resp); err != nil { log.Fatal(err) } + log.Info("Status: ", resp.Status) + pub, err := x509.ParsePKIXPublicKey(resp.Key) if err != nil { log.Fatal(err) -- GitLab