Skip to content

Commit

Permalink
changes to revoke method
Browse files Browse the repository at this point in the history
  • Loading branch information
bpfarmer committed Sep 15, 2017
1 parent 44188d9 commit 73bf0ed
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 33 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified dist/centos/connector
Binary file not shown.
Binary file added dist/ubuntu/transparency
Binary file not shown.
2 changes: 1 addition & 1 deletion merkle/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (n *Node) Save(s *Store) {
} else {
q = `UPDATE nodes SET val=$2, l_val=$3, r_val=$4, deleted=$5 WHERE id = $1;`
}
s.Save(func(tx *sql.Tx) {
s.Exec(func(tx *sql.Tx) {
stmt, err := tx.Prepare(q)
if err != nil {
log.Fatal(err)
Expand Down
4 changes: 0 additions & 4 deletions merkle/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ func (n *Node) HashVal() string {
if n.IsLeaf() {
return n.Val
}
if n.Deleted {
h.Write([]byte("EMPTY NODE"))
return hex.EncodeToString(h.Sum(nil))
}
if len(n.LVal) == 0 && n.L != nil {
n.LVal = n.L.HashVal()
}
Expand Down
4 changes: 2 additions & 2 deletions merkle/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (s Store) DropTables() {
s.DB.Exec("DROP TABLE nodes;")
}

// Save comment
func (s Store) Save(op func(tx *sql.Tx)) {
// Exec comment
func (s Store) Exec(op func(tx *sql.Tx)) {
tx, err := s.DB.Begin()
if err != nil {
log.Fatal(err)
Expand Down
52 changes: 47 additions & 5 deletions merkle/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package merkle

import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"io"
"log"
"math"
"strconv"
"transparency/merkle"
"strings"
)

// Tree comment
Expand Down Expand Up @@ -194,6 +195,36 @@ func FindNode(s *Store, val string) *Node {
return nil
}

// FindNodes comment
func FindNodes(s *Store, val string) []*Node {
q := "select * from nodes where val = $1"
rows, err := s.DB.Query(q, val)
if err != nil {
log.Fatal(err)
}
return MapToNodes(rows)
}

// RemoveLeaves comment
func RemoveLeaves(nodes []Node, s *Store) {
var vals []string
for _, n := range nodes {
vals = append(vals, n.Val)
}
q := `UPDATE nodes SET deleted=true WHERE VAL IN ($1);`
s.Exec(func(tx *sql.Tx) {
stmt, err := tx.Prepare(q)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(strings.Join(vals, ","))
if err != nil {
log.Fatal(err)
}
})
}

// AddLeaf comment
func (t *Tree) AddLeaf(n *Node, s *Store) {
if t.Root == nil {
Expand All @@ -203,7 +234,7 @@ func (t *Tree) AddLeaf(n *Node, s *Store) {
return
}

var o = merkle.FindNode(store, n.Val)
var o = FindNode(s, n.Val)
if o != nil {
o.Deleted = false
n = o
Expand Down Expand Up @@ -239,6 +270,8 @@ func (t *Tree) RemoveLeaf(n *Node, s *Store) {
// walkSave comment
func walkSave(n *Node, s *Store) {
// Save the current node
log.Print("walkSave(): about to save: ")
log.Println(n)
n.Save(s)
// Look for a parent node in memory
if n.P != nil {
Expand Down Expand Up @@ -271,10 +304,19 @@ func walkHash(n *Node, s *Store) {
} else {
n.RVal = ""
}
// Hash left and write values for parent
// Hash left and right values for parent
h := sha256.New()
io.WriteString(h, hashEmpty(n.LVal))
io.WriteString(h, hashEmpty(n.RVal))
if !n.LEntry(s).Deleted {
io.WriteString(h, hashEmpty(n.LVal))
} else {
io.WriteString(h, "DELETED NODE")
}
if !n.REntry(s).Deleted {
io.WriteString(h, hashEmpty(n.RVal))
} else {
io.WriteString(h, "DELETED NODE")
}

n.Val = hex.EncodeToString(h.Sum(nil))

// Recursively traverse the path of the current node
Expand Down
32 changes: 11 additions & 21 deletions transparency.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func main() {
store.AddTables()
//pub, priv, err = ed25519.GenerateKey(rand.Reader)

leaves := loadLeaves()
addLoadedLeaves(leaves, store)
//leaves := loadLeaves()
//addLoadedLeaves(leaves, store)

fs := http.FileServer(http.Dir("static"))
http.HandleFunc("/verify/", verifyReq)
http.HandleFunc("/add/", addReq)
http.HandleFunc("/remove/", removeReq)
http.HandleFunc("/reset/", resetReq)
//http.HandleFunc("/reset/", resetReq)
http.Handle("/", fs)
http.ListenAndServe(port, nil)
}
Expand All @@ -62,7 +62,7 @@ func verifyReq(w http.ResponseWriter, r *http.Request) {
res := make(map[string][]string)
if len(addr) > 0 && len(val) > 0 {
n := merkle.FindNode(store, val)
if n == nil {
if n == nil || n.Deleted {
res["error"] = []string{"Invalid"}
} else {
log.Println("Transparency.verifyReq():node.Val=" + n.Val)
Expand Down Expand Up @@ -122,14 +122,12 @@ func addReq(w http.ResponseWriter, r *http.Request) {
tree := &merkle.Tree{Root: merkle.RootEntry(store)}
for _, n := range nodes {
log.Println("addReq():trying to add node with val=" + n.Val)
if merkle.FindNode(store, n.Val) == nil {
tree.AddLeaf(&n, store)
}
tree.AddLeaf(&n, store)
}
}

func removeReq(w http.ResponseWriter, r *http.Request) {
log.Print("addReq():received request to add leaves=")
log.Print("removeReq():received request to remove leaves=")
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
log.Println(err)
Expand All @@ -138,37 +136,29 @@ func removeReq(w http.ResponseWriter, r *http.Request) {

// TODO naive authentication, redo this before production-ready
if r.Header.Get("X-Access-Token") != authToken {
log.Println("addReq():failed authentication check")
log.Println("removeReq():failed authentication check")
http.Error(w, "Authentication Failed", http.StatusInternalServerError)
return
}

log.Println("addReq():passed authentication")
log.Println("removeReq():passed authentication")
if r.Body == nil {
log.Println("addReq():no body found")
log.Println("removeReq():no body found")
http.Error(w, "Please send a request body", 400)
return
}
var nodes []merkle.Node
err = json.NewDecoder(r.Body).Decode(&nodes)
log.Println(err)
log.Println(nodes)
if err != nil {
log.Fatal(err)
}
tree := &merkle.Tree{Root: merkle.RootEntry(store)}
for _, n := range nodes {
log.Println("addReq():trying to add node with val=" + n.Val)
if merkle.FindNode(store, n.Val) != nil {
tree.RemoveLeaf(&n, store)
}
}
merkle.RemoveLeaves(nodes, store)
}

// TODO may not want this in the db
func resetReq(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Access-Token") != authToken {
log.Println("addReq():failed authentication check")
log.Println("resetReq():failed authentication check")
http.Error(w, "Authentication Failed", http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit 73bf0ed

Please sign in to comment.