Skip to content

Commit

Permalink
Merge pull request #72 from dedis/integration-test
Browse files Browse the repository at this point in the history
Improve integration test
  • Loading branch information
jbsv authored Apr 15, 2024
2 parents a8fd536 + 8773370 commit 94afa8e
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/server_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of Sonar analysis

Expand Down
102 changes: 98 additions & 4 deletions server/blockchain/web/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ func (a *RegisterAction) Execute(ctx node.Context) error {
router := mux.NewRouter()

s := &secretHandler{ctx}
router.HandleFunc("/secret/smc", s.advertiseSmc).Methods("POST")

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/admin/list", s.listSecrets).Methods("GET")
router.HandleFunc("/secret/admin", s.getSecret).Methods("GET")

router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
router.MethodNotAllowedHandler = http.HandlerFunc(notAllowedHandler)
Expand All @@ -57,13 +60,66 @@ type secretHandler struct {
ctx node.Context
}

// advertiseSmc advertises the SMC public key and its roster to the blockchain
func (s *secretHandler) advertiseSmc(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
log.Fatal().Err(err)
}

smckey := r.FormValue("smckey")
roster := r.FormValue("roster")
dela.Logger.Info().Msgf("received SMC pubkey %v from SMC roster %v", smckey, roster)

// get the calypso contract
var c calypso.Contract
err = s.ctx.Injector.Resolve(&c)
if err != nil {
dela.Logger.Error().Err(err).Msg("failed to resolve calypso contract")
http.Error(w, fmt.Sprintf("failed to resolve calypso contract: %v", err),
http.StatusInternalServerError)
return
}

var db purbkv.DB
err = s.ctx.Injector.Resolve(&db)
if err != nil {
dela.Logger.Error().Err(err).Msg("failed to resolve PURB database")
http.Error(w, fmt.Sprintf("failed to resolve database: %v", err),
http.StatusInternalServerError)
return
}

err = db.Update(func(txn purbkv.WritableTx) error {
b, err := txn.GetBucketOrCreate([]byte("bucket:secret"))
if err != nil {
return err
}

err = c.Execute(b, makeStep(calypso.CmdArg, string(calypso.CmdAdvertiseSmc),
calypso.SmcPublicKeyArg, smckey, calypso.RosterArg, roster))

return err
})

if err != nil {
dela.Logger.Error().Err(err).Msg("failed to advertise SMC to the blockchain")
http.Error(w, fmt.Sprintf("failed to advertise SMC to the blockchain: %v", err),
http.StatusInternalServerError)
return
}

dela.Logger.Info().Msg("SMC advertised to the blockchain")
}

// addSecret adds a new secret in the blockchain
func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
log.Fatal().Err(err)
}

smckey := r.FormValue("smckey")
secret := r.FormValue("secret")
id := r.FormValue("id")
dela.Logger.Info().Msgf("received doc ID=%v with secret=%v", id, secret)
Expand All @@ -82,7 +138,7 @@ func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) {
err = s.ctx.Injector.Resolve(&db)
if err != nil {
dela.Logger.Error().Err(err).Msg("failed to resolve database")
http.Error(w, fmt.Sprintf("failed to resolve database: %v", err),
http.Error(w, fmt.Sprintf("failed to resolve PURB database: %v", err),
http.StatusInternalServerError)
return
}
Expand All @@ -98,6 +154,7 @@ func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) {
}

err = c.Execute(b, makeStep(calypso.CmdArg, string(calypso.CmdCreateSecret),
calypso.SmcPublicKeyArg, smckey,
calypso.SecretNameArg, id, calypso.SecretArg, secret))

return err
Expand All @@ -114,8 +171,45 @@ func (s *secretHandler) addSecret(w http.ResponseWriter, r *http.Request) {
}

// listSecrets lists all secrets in the blockchain
func (s *secretHandler) listSecrets(_ http.ResponseWriter, _ *http.Request) {
func (s *secretHandler) listSecrets(w http.ResponseWriter, r *http.Request) {
// list all secrets from the blockchain
err := r.ParseForm()
if err != nil {
dela.Logger.Error().Err(err).Msg("failed to parse form")
http.Error(w, fmt.Sprintf("failed to parse form: %v", err),
http.StatusInternalServerError)
return
}

pubkey := r.Form.Get("pubkey")
dela.Logger.Info().Msgf("received request from %v to list the secrets", pubkey)

// get the calypso contract
var c calypso.Contract
err = s.ctx.Injector.Resolve(&c)
if err != nil {
dela.Logger.Error().Err(err).Msg("failed to resolve calypso contract")
http.Error(w, fmt.Sprintf("failed to resolve calypso contract: %v", err),
http.StatusInternalServerError)
return
}

var db purbkv.DB
err = s.ctx.Injector.Resolve(&db)
if err != nil {
dela.Logger.Error().Err(err).Msg("failed to resolve database")
http.Error(w, fmt.Sprintf("failed to resolve database: %v", err),
http.StatusInternalServerError)
return
}

err = db.View(func(txn purbkv.ReadableTx) error {
b := txn.GetBucket([]byte("bucket:secret"))

err = c.Execute(b, makeStep(calypso.CmdArg, string(calypso.CmdListSecrets)))

return err
})

}

Expand Down
35 changes: 17 additions & 18 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.21

require (
github.com/gorilla/mux v1.8.1
github.com/rs/zerolog v1.31.0
github.com/rs/zerolog v1.32.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
go.dedis.ch/dela v0.0.0-20240330091609-33fd2a361d2d
github.com/stretchr/testify v1.9.0
go.dedis.ch/dela v0.1.0
go.dedis.ch/kyber/v3 v3.1.1-0.20231024084410-31ea167adbbb
go.dedis.ch/purb-db v0.0.1
go.mongodb.org/mongo-driver v1.13.2
go.mongodb.org/mongo-driver v1.14.0
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
)

Expand All @@ -19,20 +19,19 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dedis/debugtools v0.0.0-20221206213939-0bc3bacd3042 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
Expand All @@ -47,30 +46,30 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.dedis.ch/debugtools v0.1.1 // indirect
go.dedis.ch/fixbuf v1.0.3 // indirect
go.dedis.ch/libpurb v0.0.0-20231108133532-c70e1b84b632 // indirect
go.dedis.ch/protobuf v1.0.11 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.etcd.io/bbolt v1.3.9 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 94afa8e

Please sign in to comment.