Skip to content

Commit

Permalink
Merge pull request #69 from dedis/scenario-test
Browse files Browse the repository at this point in the history
Scenario test
  • Loading branch information
jbsv authored Mar 13, 2024
2 parents 997e115 + 2afce36 commit d7436d3
Show file tree
Hide file tree
Showing 37 changed files with 1,422 additions and 266 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

gradle.properties

chaincli
smccli
*/chaincli
*/smccli

profile.cov
report.json
Expand Down
9 changes: 8 additions & 1 deletion server/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all tidy generate lint vet test coverage pushdoc
.PHONY: all tidy build install lint vet test coverage

# Default "make" target to check locally that everything is ok, BEFORE pushing remotely
all: lint vet test build
Expand Down Expand Up @@ -26,6 +26,13 @@ build: tidy
# Building executable
@go build ./smc/smccli
@go build ./blockchain/chaincli
@go build ./registration

install: tidy
# Building executable
@go install ./smc/smccli
@go install ./blockchain/chaincli
@go install ./registration

coverage: tidy
# Test and generate a coverage output usable by sonarcloud
Expand Down
162 changes: 162 additions & 0 deletions server/blockchain/web/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package web

Check failure on line 1 in server/blockchain/web/action.go

View workflow job for this annotation

GitHub Actions / lint

: # go.dedis.ch/hbt/server/blockchain/web

import (
"encoding/json"
"fmt"
"net/http"

"github.com/gorilla/mux"
"go.dedis.ch/dela"
"go.dedis.ch/dela/cli/node"
"go.dedis.ch/dela/mino/proxy"
"go.dedis.ch/kyber/v3/suites"

"golang.org/x/xerrors"
)

// suite is the Kyber suite for Pedersen.
var suite = suites.MustFind("Ed25519")

const separator = ":"
const malformedEncoded = "malformed encoded: %s"

// RegisterAction is an action to register the HTTP handlers
//
// - implements node.ActionTemplate
type RegisterAction struct{}

// Execute implements node.ActionTemplate. It registers the handlers using the
// default proxy from the the injector.
func (a *RegisterAction) Execute(ctx node.Context) error {
var p proxy.Proxy
err := ctx.Injector.Resolve(&p)
if err != nil {
return xerrors.Errorf("failed to resolve proxy: %v", err)
}

router := mux.NewRouter()

s := &secretHandler{&ctx}
router.HandleFunc("/secret", s.addSecret).Methods("POST")
router.HandleFunc("/secret/list", s.listSecrets).Methods("GET")
router.HandleFunc("/secret", s.getSecret).Methods("GET")
router.HandleFunc("/secret/reveal", s.revealSecret).Methods("POST")

router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
router.MethodNotAllowedHandler = http.HandlerFunc(notAllowedHandler)

p.RegisterHandler("/secret/", router.ServeHTTP)

dela.Logger.Info().Msg("proxy handlers registered")

return nil
}

type DocID []byte

// a secretData is a struct to hold the secret data: the document ID and the
// encrypted key to access the document
type secretData struct {
docID DocID `json:"docid"`
encryptedKey string `json:"secret"`
}

type secretHandler struct {
ctx *node.Context
}

// addSecret adds a new secret in the blockchain
func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) {
ctx := *(s.ctx)

// Decode the request
var sec secretData
err := json.NewDecoder(r.Body).Decode(&sec)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

// add the secret to the blockchain
// the secret is added to the blockchain with the document ID as the key
// and the encrypted key as the value
// TODO add it to the blockchain
dela.Logger.Info().Msgf("secret added to the blockchain: ID=%v secret=%v", sec.docID,
sec.encryptedKey)
}

// listSecrets lists all secrets in the blockchain
func (s *secretHandler) listSecrets(w http.ResponseWriter, r *http.Request) {
ctx := *(s.ctx)

// list all secrets from the blockchain

}

// getSecret gets a secret from the blockchain
func (s *secretHandler) getSecret(w http.ResponseWriter, r *http.Request) {
ctx := *(s.ctx)

// Decode the request
var id DocID
err := json.NewDecoder(r.Body).Decode(&id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

// get the secret from the blockchain
// TODO

}

// -----------------------------------------------------------------------------
// Helper functions

// HTTPError defines the standard error format
type HTTPError struct {
Title string
Code uint
Message string
Args map[string]interface{}
}

// notFoundHandler defines a generic handler for 404
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
err := HTTPError{
Title: "Not found",
Code: http.StatusNotFound,
Message: "The requested endpoint was not found",
Args: map[string]interface{}{
"url": r.URL.String(),
"method": r.Method,
},
}

buf, _ := json.MarshalIndent(&err, "", " ")

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, string(buf))
}

// notAllowedHandler degines a generic handler for 405
func notAllowedHandler(w http.ResponseWriter, r *http.Request) {
err := HTTPError{
Title: "Not allowed",
Code: http.StatusMethodNotAllowed,
Message: "The requested endpoint was not allowed",
Args: map[string]interface{}{
"url": r.URL.String(),
"method": r.Method,
},
}

buf, _ := json.MarshalIndent(&err, "", " ")

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintln(w, string(buf))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sproxy
package web

import (
"os"
Expand All @@ -15,7 +15,7 @@ import (
var defaultRetry = 10
var proxyFac func(string) proxy.Proxy = http.NewHTTP

const defaultProxyAddr = "127.0.0.1:0"
const defaultProxyAddr = "127.0.0.1:3003"

// NewController returns a new controller initializer
func NewController() node.Initializer {
Expand All @@ -41,7 +41,7 @@ func (m controller) SetCommands(builder node.Builder) {

// OnStart implements node.Initializer. It creates and registers a pedersen DKG.
func (m controller) OnStart(ctx cli.Flags, inj node.Injector) error {
dela.Logger.Info().Msg("Installing SMC proxy")
dela.Logger.Info().Msg("Installing Blockchain proxy")

proxyAddr := ctx.String("proxyaddr")

Expand Down
Binary file added server/passport.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion server/registration/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"admin_name": "admin",
"admin_password": "admin",
"user_port": 3000,
"admin_port": 4000
"admin_port": 3001
}
3 changes: 1 addition & 2 deletions server/registration/database/mongodb/dbtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ package mongodb
type Document struct {
Name string `bson:"name"`
Passport string `bson:"passport"`
Role uint `bson:"role"`
Role uint64 `bson:"role"`
Picture []byte `bson:"picture"`
Hash []byte `bson:"hash"`
Registered bool `bson:"registered"`
}
16 changes: 0 additions & 16 deletions server/registration/database/mongodb/mongodb.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mongodb

import (
"bytes"
"context"
"errors"

Expand Down Expand Up @@ -42,7 +41,6 @@ func (d dbAccess) Create(data *registry.RegistrationData) (*registry.Registratio
Passport: data.Passport,
Role: data.Role,
Picture: data.Picture,
Hash: data.Hash,
Registered: false,
}

Expand Down Expand Up @@ -86,12 +84,6 @@ func (d dbAccess) Read(id registry.RegistrationID, hash []byte) (
return nil, err
}

if hash != nil {
if !bytes.Equal(hash, doc.Hash) {
return nil, errors.New("hashes do not match")
}
}

data := registry.RegistrationData{
Name: doc.Name,
Passport: doc.Passport,
Expand All @@ -115,10 +107,6 @@ func (d dbAccess) Update(
return err
}

if !bytes.Equal(reg.Hash, doc.Hash) {
return errors.New("hashes do not match")
}

result, err := d.client.Database("registration").Collection("documents").UpdateOne(context.Background(),
id, reg)
if err != nil {
Expand All @@ -142,10 +130,6 @@ func (d dbAccess) Delete(id registry.RegistrationID, hash []byte) error {
return err
}

if !bytes.Equal(hash, doc.Hash) {
return errors.New("hashes do not match")
}

result, err := d.client.Database("registration").Collection("documents").DeleteOne(context.Background(),
id)
if err != nil {
Expand Down
18 changes: 5 additions & 13 deletions server/registration/registry/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, db database.Database
return
}

log.Println(fileHeader)

picData := make([]byte, fileHeader.Size)
_, err = picture.Read(picData)
if err != nil {
Expand All @@ -48,14 +46,11 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, db database.Database
return
}

hash := r.FormValue("hash")

regData := &registry.RegistrationData{
Name: name,
Passport: passport,
Role: uint(role),
Role: role,
Picture: picData,
Hash: []byte(hash),
Registered: false,
}

Expand Down Expand Up @@ -151,7 +146,6 @@ func UpdateDocument(w http.ResponseWriter, r *http.Request, db database.Database
}
return
}
hash := r.FormValue("hash")

log.Println(fileHeader)

Expand All @@ -168,12 +162,10 @@ func UpdateDocument(w http.ResponseWriter, r *http.Request, db database.Database
}

regData := &registry.RegistrationData{
Name: name,
Passport: passport,
Role: uint(role),
Picture: picData,
Hash: []byte(hash),
Registered: registered,
Name: name,
Passport: passport,
Role: role,
Picture: picData,
}

err = db.Update(registrationID, regData)
Expand Down
15 changes: 11 additions & 4 deletions server/registration/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ package registry
type RegistrationData struct {
Name string `json:"name"`
Passport string `json:"passport"`
Role uint `json:"role"`
Picture []byte `json:"picture"`
Hash []byte `json:"hash"`
Role uint64 `json:"role"`
Registered bool `json:"registered"`
}

type DocID []byte
// EncryptedData contains the above encrypted data for a registration
// and a flag to indicate if the data has been successfully registered
type EncryptedData struct {
Name []byte `json:"name"`
Passport []byte `json:"passport"`
Picture []byte `json:"picture"`
Role []byte `json:"role"`
Registered []byte `json:"registered"`
}

// RegistrationID contains the reference to the document in the database
type RegistrationID struct {
ID DocID `json:"doc_id"`
ID []byte `json:"doc_id"`
}
11 changes: 7 additions & 4 deletions server/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ command -v tmux >/dev/null 2>&1 || { echo >&2 "tmux is not on your PATH!"; exit
tmux list-sessions 2>/dev/null | rg "^${S}" && { echo -e ${RED}"A session with the same name (${S}) already exists and will be destroyed${NC}"; tmux kill-session -t ${S};}

echo -e "Create a tmux detached session: ${S} with a window 'chain'"
tmux new-session -s ${S} -n chain -d
tmux new-session -s ${S} -n blockchain -d
echo -e "Create a tmux window 'registry' in the session ${S}"
tmux new-window -t ${S} -n registry
echo -e "Create a tmux window 'smc' in the session ${S}"
tmux new-window -t ${S} -n smc

./start_chain.sh -t ${L} -s ${S} -w chain
./start_smc.sh -t ${L} -s ${S} -w smc
./start_chain.sh -t ${L}
./start_registry.sh -t ${L}
./start_smc.sh -t ${L}
tmux send-keys -t ${S}:smc.0 "./publish_roster.sh" C-m

# attach to session
tmux select-pane -t ${S}:smc.0
tmux send-keys -t ${S}:smc.0 "# TMUX MINI CHEAT SHEET" C-m
tmux send-keys -t ${S}:smc.0 "# Use 'tmux lscm' to list tmux commands" C-m
tmux send-keys -t ${S}:smc.0 "# Use 'Ctrl+B N (or P)' for next (previous) window" C-m
tmux send-keys -t ${S}:smc.0 "# Use 'Ctrl+B N (or P)' for next (or previous) window" C-m
tmux send-keys -t ${S}:smc.0 "# Use 'Ctrl+B <arrow>' to select pane" C-m
tmux send-keys -t ${S}:smc.0 "# './teardown.sh' to clean this tmux session" C-m
tmux attach -t ${S}
Loading

0 comments on commit d7436d3

Please sign in to comment.