Skip to content

Commit

Permalink
Minor improvements and QA adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
bcessa committed Jul 2, 2019
1 parent b19f608 commit 872358b
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 83 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ vendor/**

# Build
bryk-did
bryk-did-*
bryk-did-*

# Default agent data store
data
17 changes: 4 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
.PHONY: proto
.DEFAULT_GOAL := help
FILES_LIST=`find . -iname '*.go' | grep -v 'vendor'`
GO_PKG_LIST=`go list ./... | grep -v 'vendor'`
BINARY_NAME=bryk-did
VERSION_TAG=0.1.0
VERSION_TAG=0.1.1

# Custom compilation tags
LD_FLAGS="\
Expand All @@ -13,19 +12,12 @@ LD_FLAGS="\

test: ## Run all tests excluding the vendor dependencies
# Formatting
go vet $(GO_PKG_LIST)
gofmt -s -w $(FILES_LIST)
golint -set_exit_status $(GO_PKG_LIST)
misspell $(FILES_LIST)

# Static analysis
ineffassign $(FILES_LIST)
GO111MODULE=off gosec ./...
gocyclo -over 15 `find . -iname '*.go' | grep -v 'vendor' | grep -v '_test.go' | grep -v 'pb.go' | grep -v 'pb.gw.go'`
golangci-lint run ./...
go-consistent -v ./...

# Unit tests
go test -race -cover -v $(GO_PKG_LIST)
go test -race -cover -v -failfast ./...

build: ## Build for the current architecture in use, intended for devevelopment
go build -v -ldflags $(LD_FLAGS) -o $(BINARY_NAME) github.com/bryk-io/did-method/client/cli
Expand Down Expand Up @@ -57,7 +49,6 @@ clean: ## Download and compile all dependencies and intermediary products
go mod tidy
go mod verify

.PHONY: proto
proto: ## Compile protocol buffers and RPC services
prototool lint
prototool generate
Expand Down
11 changes: 9 additions & 2 deletions agent/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,19 @@ func runMethodServer(_ *cobra.Command, _ []string) error {
Port: port,
EmitDefaults: false,
}))

// Start server and wait for it to be ready
server, err := handler.GetServer(opts...)
if err != nil {
return fmt.Errorf("failed to start node: %s", err)
}
go server.Start()
ready := make(chan bool)
go func() {
_ = server.Start(ready)
}()
<-ready

// Wait for system signals
handler.Log("waiting for incoming requests")
<-cli.SignalsHandler([]os.Signal{
syscall.SIGHUP,
Expand All @@ -82,7 +89,7 @@ func runMethodServer(_ *cobra.Command, _ []string) error {
})
handler.Log("preparing to exit")
err = handler.Close()
if !strings.Contains(err.Error(), "closed network connection") {
if err != nil && !strings.Contains(err.Error(), "closed network connection") {
return err
}
return nil
Expand Down
27 changes: 16 additions & 11 deletions agent/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ func (h *Handler) Retrieve(subject string) (*did.Identifier, error) {

// Process an incoming request ticket
func (h *Handler) Process(req *proto.Request) error {
// Validate ticket
if err := req.Ticket.Verify(nil); err != nil {
h.output.WithField("error", err.Error()).Error("invalid ticket")
return err
}

// Load DID document
id, err := req.Ticket.LoadDID()
if err != nil {
h.output.WithField("error", err.Error()).Error("invalid DID contents")
Expand Down Expand Up @@ -120,20 +123,23 @@ func (h *Handler) GetServer(opts ...rpc.ServerOption) (*rpc.Server, error) {
}

// Add RPC service handler
opts = append(opts, rpc.WithService(func(s *grpc.Server) {
proto.RegisterAgentServer(s, &rpcHandler{handler: h})
}, proto.RegisterAgentHandlerFromEndpoint))
opts = append(opts, rpc.WithService(&rpc.Service{
GatewaySetup: proto.RegisterAgentHandlerFromEndpoint,
Setup: func(s *grpc.Server) {
proto.RegisterAgentServer(s, &rpcHandler{handler: h})
},
}))

// Custom HTTP handler method for data retrieval, the response should be the JSON-LD encoded
// document of the requested DID instance
opts = append(opts, rpc.WithHandlerFunc("/v1/retrieve", h.queryHTTP))

// Create server instance
srv, err := rpc.NewServer(opts...)
if err != nil {
return nil, err
}

// Custom HTTP handler method for data retrieval, the response should be the JSON-LD encoded
// document of the requested DID instance
srv.HandleFunc("/v1/retrieve", h.queryHTTP)

h.server = srv
return h.server, nil
}
Expand Down Expand Up @@ -162,7 +168,7 @@ func (h *Handler) queryHTTP(writer http.ResponseWriter, request *http.Request) {
if err != nil {
writer.WriteHeader(400)
eh["error"] = err.Error()
json.NewEncoder(writer).Encode(eh)
_ = json.NewEncoder(writer).Encode(eh)
return
}

Expand All @@ -171,14 +177,13 @@ func (h *Handler) queryHTTP(writer http.ResponseWriter, request *http.Request) {
if err != nil {
writer.WriteHeader(400)
eh["error"] = err.Error()
json.NewEncoder(writer).Encode(eh)
_ = json.NewEncoder(writer).Encode(eh)
return
}

// Send response
writer.WriteHeader(200)
fmt.Fprintf(writer, "%s", output)
return
_, _ = fmt.Fprintf(writer, "%s", output)
}

// Verify the provided path exists and is a directory
Expand Down
11 changes: 7 additions & 4 deletions client/cli/cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func runSignCmd(_ *cobra.Command, args []string) error {
}

// Get input, CLI takes precedence, from standard input otherwise
var input []byte
input = []byte(viper.GetString("sign.input"))
input := []byte(viper.GetString("sign.input"))
if len(input) == 0 {
input, _ = cli.ReadPipedInput(maxPipeInputSize)
}
Expand All @@ -63,7 +62,9 @@ func runSignCmd(_ *cobra.Command, args []string) error {
}
if len(input) > 32 {
digest := sha3.New256()
digest.Write(input)
if _, err := digest.Write(input); err != nil {
return err
}
input = digest.Sum(nil)
}

Expand All @@ -72,7 +73,9 @@ func runSignCmd(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
defer st.Close()
defer func() {
_ = st.Close()
}()

// Retrieve identifier
name := sanitize.Name(args[0])
Expand Down
17 changes: 9 additions & 8 deletions client/cli/cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func runSyncCmd(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
defer st.Close()
defer func() {
_ = st.Close()
}()

// Retrieve identifier
name := sanitize.Name(args[0])
Expand Down Expand Up @@ -100,7 +102,9 @@ func runSyncCmd(_ *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("failed to establish connection: %s", err)
}
defer conn.Close()
defer func() {
_ = conn.Close()
}()

// Build request
req := &proto.Request{
Expand Down Expand Up @@ -135,17 +139,14 @@ func getRequestTicket(contents []byte, key *did.PublicKey, ll *log.Logger) (*pro
ll.Info("generating request ticket")
ticket := proto.NewTicket(contents, key.ID)
start := time.Now()
challenge, err := ticket.Solve(context.TODO())
if err != nil {
return nil, fmt.Errorf("failed to generate request ticket: %s", err)
}
challenge := ticket.Solve(context.TODO())
ll.Debugf("ticket obtained: %s", challenge)
ll.Debugf("time: %s (rounds completed %d)", time.Since(start), ticket.Nonce())
ch, _ := hex.DecodeString(challenge)

// Sign ticket
ticket.Signature, err = key.Sign(ch)
if err != nil {
var err error
if ticket.Signature, err = key.Sign(ch); err != nil {
return nil, fmt.Errorf("failed to generate request ticket: %s", err)
}

Expand Down
7 changes: 4 additions & 3 deletions client/cli/cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func runVerifyCmd(_ *cobra.Command, args []string) error {
}

// Get input, CLI takes precedence, from standard input otherwise
var input []byte
input = []byte(viper.GetString("verify.input"))
input := []byte(viper.GetString("verify.input"))
if len(input) == 0 {
input, _ = cli.ReadPipedInput(maxPipeInputSize)
}
Expand All @@ -51,7 +50,9 @@ func runVerifyCmd(_ *cobra.Command, args []string) error {
}
if len(input) > 32 {
digest := sha3.New256()
digest.Write(input)
if _, err := digest.Write(input); err != nil {
return err
}
input = digest.Sum(nil)
}

Expand Down
24 changes: 18 additions & 6 deletions client/store/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ func TestLocalStore(t *testing.T) {
if err != nil {
t.Fatal("failed to create local store:", err)
}
defer st.Close()
defer func() {
_ = st.Close()
}()

// Sample entry
id, _ := did.NewIdentifierWithMode("bryk", "", did.ModeUUID)
id.AddNewKey("master", did.KeyTypeEd, did.EncodingHex)
id.AddAuthenticationKey("master")
id.AddProof("master", "sample.acme.com")
if err := id.AddNewKey("master", did.KeyTypeEd, did.EncodingHex); err != nil {
t.Error(err)
}
if err := id.AddAuthenticationKey("master"); err != nil {
t.Error(err)
}
if err := id.AddProof("master", "sample.acme.com"); err != nil {
t.Error(err)
}
contents, _ := id.Encode()
entry := &Entry{
Name: id.Subject(),
Expand Down Expand Up @@ -52,8 +60,12 @@ func TestLocalStore(t *testing.T) {
}

// Update
id.AddNewKey("iadb-provider", did.KeyTypeEd, did.EncodingHex)
id.AddProof("master", "sample.acme.com")
if err := id.AddNewKey("iadb-provider", did.KeyTypeEd, did.EncodingHex); err != nil {
t.Error(err)
}
if err := id.AddProof("master", "sample.acme.com"); err != nil {
t.Error(err)
}
newContents, _ := id.Encode()
if err = st.Update("invalid-entry", newContents); err == nil {
t.Fatal("failed to catch invalid entry to update")
Expand Down
22 changes: 10 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
module github.com/bryk-io/did-method

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/bryk-io/x v0.0.0-20190321181458-cb94ffbf19af
github.com/bryk-io/x v0.0.0-20190701133613-135e915a55a2
github.com/gogo/googleapis v1.1.0
github.com/gogo/protobuf v1.2.0
github.com/golang/protobuf v1.3.0
github.com/grpc-ecosystem/grpc-gateway v1.7.0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/gogo/protobuf v1.2.1
github.com/golang/protobuf v1.3.1
github.com/grpc-ecosystem/grpc-gateway v1.9.2
github.com/kennygrant/sanitize v1.2.4
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.4.3 // indirect
github.com/sirupsen/logrus v1.3.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.2
github.com/vmihailenco/msgpack v4.0.2+incompatible
github.com/vmihailenco/msgpack v4.0.4+incompatible
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613
golang.org/x/net v0.0.0-20190301231341-16b79f2e4e95
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f
golang.org/x/net v0.0.0-20190522155817-f3200d17e092
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect
google.golang.org/grpc v1.18.0
google.golang.org/grpc v1.21.1
)

replace (
github.com/dgraph-io/badger v1.5.5 => github.com/bryk-io/badger v1.5.5
github.com/grpc-ecosystem/go-grpc-middleware => github.com/bryk-io/go-grpc-middleware v1.0.1-0.20190202210917-0105da141832
github.com/grpc-ecosystem/go-grpc-middleware => github.com/bryk-io/go-grpc-middleware v1.0.1-0.20190615102816-71cc94c54bbc
)
Loading

0 comments on commit 872358b

Please sign in to comment.