Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
btget: move db, validate files
Browse files Browse the repository at this point in the history
- move db to ~/.config/btget/btget.db
- validate file sum to log sum
  • Loading branch information
philips committed May 5, 2020
1 parent e710f21 commit b62be44
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions btget/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@ import (
"bytes"
"crypto/sha256"
"database/sql"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/cavaliercoder/grab"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/mod/sumdb/note"
_ "rsc.io/sqlite"

"go.transparencylog.net/btget/sumdb"
)

var cfgFile string

var cacheFile string
var serverAddr string

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -72,7 +73,7 @@ func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.btget.yaml)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.config/btget/config.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand All @@ -81,22 +82,26 @@ func init() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".btget" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".btget")
btgetDir := filepath.Join(home, ".config", "btget")
err = os.MkdirAll(btgetDir, 0700)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

cacheFile = filepath.Join(btgetDir, "btget.db")

// Search config in home directory with name ".btget" (without extension).
viper.AddConfigPath(btgetDir)
viper.SetConfigName("config")

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
Expand All @@ -111,8 +116,8 @@ type clientCache struct {

func NewClientCache() *clientCache {
client := &clientCache{}
// TODO: make filename configurable
sdb, err := sql.Open("sqlite3", "btget.db")

sdb, err := sql.Open("sqlite3", cacheFile)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -186,34 +191,22 @@ func get(cmd *cobra.Command, args []string) {
key := u.Host + u.Path

// Step 0: Initialize cache if needed
vkey := "sum.golang.org+033de0ae+Ac4zctda0e5eza+HJyk9SxEdh+s3Ux18htTTAD8OuAn8"
verifier, err := note.NewVerifier(vkey)
if err != nil {
log.Fatalf("invalid verifier key: %v", err)
}
verifiers := note.VerifierList(verifier)

vkey := "log+998cdb6b+AUDa+aCu48rSILe2yaFwjrL5p3h5jUi4x4tTX0wSpeXU"
cache := NewClientCache()
_, err = cache.ReadConfig("key")
if err == nil {
if err != nil {
if err := cache.WriteConfig("key", nil, []byte(vkey)); err != nil {
log.Fatal(err)
}
}

// Step 1: Download the tlog entry for the URL
client := sumdb.NewClient(cache)
log.Print("looking up key: ", key)
_, data, err := client.Lookup(key)
if err != nil {
log.Fatal(err)
}
n, err := note.Open(data, verifiers)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s (%08x):\n%s", n.Sigs[0].Name, n.Sigs[0].Hash, n.Text)
logDigest := strings.Trim(string(data), "\n")

// create download request
req, err := grab.NewRequest("", durl)
Expand Down Expand Up @@ -241,7 +234,14 @@ func get(cmd *cobra.Command, args []string) {

fileSum := h.Sum(nil)

// TODO VALIDATE SUM FILE HERE
logSum, err := hex.DecodeString(logDigest)
if err != nil {
panic(err)
}

if !bytes.Equal(fileSum, logSum) {
log.Fatalf("file digest %x != log digest %x", fileSum, logSum)
}

fmt.Printf("validated file sum: %x\n", fileSum)

Expand Down

0 comments on commit b62be44

Please sign in to comment.