Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
1. SignedEntry.Index - returns (byte,error)
2. Manager.Remote - returns error-code
3. SignedEntry.Sign, SignedEntry.Signature - returns error-codes
4. Updated Makefile:
- compatibility with changes in skywire-cli
- `make build` and `make docker-volume` builds with -race flag
- `make release` for building wo -race flag
  • Loading branch information
ayuryshev committed Apr 4, 2019
1 parent ec1cc0c commit 8e90eb3
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 76 deletions.
50 changes: 33 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.PHONY : check lint install-linters dep test
.PHONY : build clean install format
.PHONY : host-apps bin
.PHONY : run stop
.PHONY : run stop config
.PHONY : docker-image docker-clean docker-network
.PHONY : docker-apps docker-bin docker-volume
.PHONY : docker-run docker-stop
Expand All @@ -17,12 +17,14 @@ check: lint test ## Run linters and tests

build: dep host-apps bin ## Install dependencies, build apps and binaries. `go build` with ${OPTS}

run: stop build ## Run skywire-node on host
run: stop build config ## Run skywire-node on host
./skywire-node

stop: ## Stop running skywire-node on host
-bash -c "kill $$(ps aux |grep '[s]kywire-node' |awk '{print $$2}')"

config: ## Generate skywire.json
-./skywire-cli gen-config -o ./skywire.json -r

clean: ## Clean project: remove created binaries and apps
-rm -rf ./apps
Expand Down Expand Up @@ -85,19 +87,33 @@ dep: ## Sorts dependencies

# Apps
host-apps: ## Build app
${OPTS} go build -o ./apps/chat.v1.0 ./cmd/apps/chat
${OPTS} go build -o ./apps/helloworld.v1.0 ./cmd/apps/helloworld
${OPTS} go build -o ./apps/therealproxy.v1.0 ./cmd/apps/therealproxy
${OPTS} go build -o ./apps/therealproxy-client.v1.0 ./cmd/apps/therealproxy-client
${OPTS} go build -o ./apps/therealssh.v1.0 ./cmd/apps/therealssh
${OPTS} go build -o ./apps/therealssh-client.v1.0 ./cmd/apps/therealssh-client
${OPTS} go build -race -o ./apps/chat.v1.0 ./cmd/apps/chat
${OPTS} go build -race -o ./apps/helloworld.v1.0 ./cmd/apps/helloworld
${OPTS} go build -race -o ./apps/therealproxy.v1.0 ./cmd/apps/therealproxy
${OPTS} go build -race -o ./apps/therealproxy-client.v1.0 ./cmd/apps/therealproxy-client
${OPTS} go build -race -o ./apps/therealssh.v1.0 ./cmd/apps/therealssh
${OPTS} go build -race -o ./apps/therealssh-client.v1.0 ./cmd/apps/therealssh-client

# Bin
bin: ## Build `skywire-node`, `skywire-cli`, `manager-node`, `therealssh-cli`
${OPTS} go build -race -o ./skywire-node ./cmd/skywire-node
${OPTS} go build -race -o ./skywire-cli ./cmd/skywire-cli
${OPTS} go build -race -o ./manager-node ./cmd/manager-node
${OPTS} go build -race -o ./therealssh-cli ./cmd/therealssh-cli

release: ## Build skywire-node`, skywire-cli, manager-node, therealssh-cli and apps without -race flag
${OPTS} go build -o ./skywire-node ./cmd/skywire-node
${OPTS} go build -o ./skywire-cli ./cmd/skywire-cli
${OPTS} go build -o ./manager-node ./cmd/manager-node
${OPTS} go build -o ./therealssh-cli ./cmd/therealssh-cli
${OPTS} go build -o ./apps/chat.v1.0 ./cmd/apps/chat
${OPTS} go build -o ./apps/helloworld.v1.0 ./cmd/apps/helloworld
${OPTS} go build -o ./apps/therealproxy.v1.0 ./cmd/apps/therealproxy
${OPTS} go build -o ./apps/therealproxy-client.v1.0 ./cmd/apps/therealproxy-client
${OPTS} go build -o ./apps/therealssh.v1.0 ./cmd/apps/therealssh
${OPTS} go build -o ./apps/therealssh-client.v1.0 ./cmd/apps/therealssh-client



# Dockerized skywire-node
docker-image: ## Build docker image `skywire-runner`
Expand All @@ -111,22 +127,22 @@ docker-network: ## Create docker network ${DOCKER_NETWORK}
-docker network create ${DOCKER_NETWORK}

docker-apps: ## Build apps binaries for dockerized skywire-node. `go build` with ${DOCKER_OPTS}
-${DOCKER_OPTS} go build -o ./node/apps/chat.v1.0 ./cmd/apps/chat
-${DOCKER_OPTS} go build -o ./node/apps/helloworld.v1.0 ./cmd/apps/helloworld
-${DOCKER_OPTS} go build -o ./node/apps/therealproxy.v1.0 ./cmd/apps/therealproxy
-${DOCKER_OPTS} go build -o ./node/apps/therealproxy-client.v1.0 ./cmd/apps/therealproxy-client
-${DOCKER_OPTS} go build -o ./node/apps/therealssh.v1.0 ./cmd/apps/therealssh
-${DOCKER_OPTS} go build -o ./node/apps/therealssh-client.v1.0 ./cmd/apps/therealssh-client
-${DOCKER_OPTS} go build -race -o ./node/apps/chat.v1.0 ./cmd/apps/chat
-${DOCKER_OPTS} go build -race -o ./node/apps/helloworld.v1.0 ./cmd/apps/helloworld
-${DOCKER_OPTS} go build -race -o ./node/apps/therealproxy.v1.0 ./cmd/apps/therealproxy
-${DOCKER_OPTS} go build -race -o ./node/apps/therealproxy-client.v1.0 ./cmd/apps/therealproxy-client
-${DOCKER_OPTS} go build -race -o ./node/apps/therealssh.v1.0 ./cmd/apps/therealssh
-${DOCKER_OPTS} go build -race -o ./node/apps/therealssh-client.v1.0 ./cmd/apps/therealssh-client

docker-bin: ## Build `skywire-node`, `skywire-cli`, `manager-node`, `therealssh-cli`. `go build` with ${DOCKER_OPTS}
${DOCKER_OPTS} go build -o ./node/skywire-node ./cmd/skywire-node
${DOCKER_OPTS} go build -race -o ./node/skywire-node ./cmd/skywire-node

docker-volume: docker-apps docker-bin bin ## Prepare docker volume for dockerized skywire-node
./skywire-cli config ./node/skywire.json
-./skywire-cli gen-config -o ./node/skywire.json

docker-run: docker-clean docker-image docker-network docker-volume ## Run dockerized skywire-node ${DOCKER_NODE} in image ${DOCKER_IMAGE} with network ${DOCKER_NETWORK}
docker run -it -v $(shell pwd)/node:/sky --network=${DOCKER_NETWORK} \
--name=${DOCKER_NODE} ${DOCKER_IMAGE} bash -c "cd /sky && ./skywire-node"
--name=${DOCKER_NODE} ${DOCKER_IMAGE} bash -c "cd /sky && ./skywire-node skywire.json"

docker-stop: ## Stop running dockerized skywire-node ${DOCKER_NODE}
-docker container stop ${DOCKER_NODE}
Expand Down
3 changes: 2 additions & 1 deletion pkg/messaging/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/skycoin/skywire/internal/ioutil"
"github.com/skycoin/skywire/internal/noise"
"github.com/skycoin/skywire/pkg/cipher"
"github.com/skycoin/skywire/pkg/transport"
)

type channel struct {
Expand All @@ -30,7 +31,7 @@ type channel struct {
}

func (c *channel) Edges() [2]cipher.PubKey {
return [2]cipher.PubKey{c.link.Local(), c.remotePK}
return transport.SortPubKeys(c.link.Local(), c.remotePK)
}

func newChannel(initiator bool, secKey cipher.SecKey, remote cipher.PubKey, link *Link) (*channel, error) {
Expand Down
30 changes: 18 additions & 12 deletions pkg/node/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,19 @@ type TransportSummary struct {
}

func newTransportSummary(tm *transport.Manager, tp *transport.ManagedTransport, includeLogs bool) *TransportSummary {
summary := TransportSummary{
ID: tp.ID,
Local: tm.Local(),
Remote: tm.Remote(tp.Edges()),
Type: tp.Type(),
}
if includeLogs {
summary.Log = tp.LogEntry
if remote, Ok := tm.Remote(tp.Edges()); Ok == nil {
summary := TransportSummary{
ID: tp.ID,
Local: tm.Local(),
Remote: remote,
Type: tp.Type(),
}
if includeLogs {
summary.Log = tp.LogEntry
}
return &summary
}
return &summary
return &TransportSummary{}
}

// Summary provides a summary of an AppNode.
Expand Down Expand Up @@ -179,10 +182,13 @@ func (r *RPC) Transports(in *TransportsIn, out *[]*TransportSummary) error {
return true
}
r.node.tm.WalkTransports(func(tp *transport.ManagedTransport) bool {
if typeIncluded(tp.Type()) && pkIncluded(r.node.tm.Local(), r.node.tm.Remote(tp.Edges())) {
*out = append(*out, newTransportSummary(r.node.tm, tp, in.ShowLogs))
if remote, Ok := r.node.tm.Remote(tp.Edges()); Ok == nil {
if typeIncluded(tp.Type()) && pkIncluded(r.node.tm.Local(), remote) {
*out = append(*out, newTransportSummary(r.node.tm, tp, in.ShowLogs))
}
return true
}
return true
return false
})
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,11 @@ func (r *Router) advanceNoiseHandshake(addr *app.LoopAddr, noiseMsg []byte) (ni

func (r *Router) isSetupTransport(tr transport.Transport) bool {
for _, pk := range r.config.SetupNodes {
if r.tm.Remote(tr.Edges()) == pk {
return true
remote, Ok := r.tm.Remote(tr.Edges())
if Ok == nil {
if remote == pk {
return true
}
}
}

Expand Down
32 changes: 22 additions & 10 deletions pkg/transport/entry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transport

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -96,29 +97,40 @@ type SignedEntry struct {
}

// Index returns position of a given pk in edges
func (se *SignedEntry) Index(pk cipher.PubKey) byte {
func (se *SignedEntry) Index(pk cipher.PubKey) (byte, error) {
if pk == se.Entry.Edges()[1] {
return 1
return 1, nil
}
return 0
if pk == se.Entry.Edges()[0] {
return 0, nil
}
return 0xff, errors.New("invalid pubkey")
}

// Sign sets Signature for a given PubKey in correct position
func (se *SignedEntry) Sign(pk cipher.PubKey, secKey cipher.SecKey) {
idx := se.Index(pk)
se.Signatures[idx] = se.Entry.Signature(secKey)
func (se *SignedEntry) Sign(pk cipher.PubKey, secKey cipher.SecKey) error {
idx, Ok := se.Index(pk)
if Ok == nil {
se.Signatures[idx] = se.Entry.Signature(secKey)
}
return Ok
}

// Signature gets Signature for a given PubKey from correct position
func (se *SignedEntry) Signature(pk cipher.PubKey) cipher.Sig {
idx := se.Index(pk)
return se.Signatures[idx]
func (se *SignedEntry) Signature(pk cipher.PubKey) (cipher.Sig, error) {
idx, Ok := se.Index(pk)
if Ok != nil {
return cipher.Sig{}, Ok
}
return se.Signatures[idx], nil
}

// NewSignedEntry creates a SignedEntry with first signature
func NewSignedEntry(entry *Entry, pk cipher.PubKey, secKey cipher.SecKey) *SignedEntry {
se := &SignedEntry{Entry: entry}
se.Sign(pk, secKey)
if err := se.Sign(pk, secKey); err != nil {
return &SignedEntry{}
}
return se
}

Expand Down
47 changes: 41 additions & 6 deletions pkg/transport/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,38 +92,73 @@ func ExampleSignedEntry_Sign() {
fmt.Println("No signatures set")
}

sEntry.Sign(pkA, skA)
if errA := sEntry.Sign(pkA, skA); errA != nil {
fmt.Println(errA.Error())
}
if (!sEntry.Signatures[0].Null() && sEntry.Signatures[1].Null()) ||
(!sEntry.Signatures[1].Null() && sEntry.Signatures[0].Null()) {
fmt.Println("One signature set")
}

sEntry.Sign(pkB, skB)
if errB := sEntry.Sign(pkB, skB); errB != nil {
fmt.Println(errB.Error())
}

if !sEntry.Signatures[0].Null() && !sEntry.Signatures[1].Null() {
fmt.Println("Both signatures set")
} else {
fmt.Printf("sEntry.Signatures:\n%v\n", sEntry.Signatures)
}

// Output: No signatures set
// One signature set
// Both signatures set
}

func errorsPrint(errs ...error) {
for _, err := range errs {
if err != nil {
fmt.Println(err.Error())
}
}
}

func ExampleSignedEntry_Signature() {
pkA, skA := cipher.GenerateKeyPair()
pkB, skB := cipher.GenerateKeyPair()

entry := NewEntry(pkA, pkB, "mock", true)
sEntry := &SignedEntry{Entry: entry}
sEntry.Sign(pkA, skA)
sEntry.Sign(pkB, skB)
if errA := sEntry.Sign(pkA, skA); errA != nil {
fmt.Println(errA.Error())
}
if errB := sEntry.Sign(pkB, skB); errB != nil {
fmt.Println(errB.Error())
}

idxA, errIdxA := sEntry.Index(pkA)
idxB, errIdxB := sEntry.Index(pkB)

sigA, errSigA := sEntry.Signature(pkA)
sigB, errSigB := sEntry.Signature(pkB)

if sEntry.Signature(pkA) == sEntry.Signatures[sEntry.Index(pkA)] {
if sigA == sEntry.Signatures[idxA] {
fmt.Println("SignatureA got")
}
if sEntry.Signature(pkB) == sEntry.Signatures[sEntry.Index(pkB)] {

if sigB == sEntry.Signatures[idxB] {
fmt.Println("SignatureB got")
}

// Incorrect case
pkC, _ := cipher.GenerateKeyPair()
_, errSigC := sEntry.Signature(pkC)
if errSigC != nil {
fmt.Printf("SignatureC got error: %v\n", errSigC.Error())
}

errorsPrint(errIdxA, errIdxB, errSigA, errSigB)
// Output: SignatureA got
// SignatureB got
// SignatureC got error: invalid pubkey
}
33 changes: 24 additions & 9 deletions pkg/transport/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,34 @@ func settlementInitiatorHandshake(public bool) settlementHandshake {

sEntry := NewSignedEntry(entry, tm.config.PubKey, tm.config.SecKey)
if err := validateSignedEntry(sEntry, tr, tm.config.PubKey); err != nil {
return nil, fmt.Errorf("NewSignedEntry: %s", err)

return nil, fmt.Errorf("settlementInitiatorHandshake NewSignedEntry: %s\n sEntry: %v\n", err, sEntry)
}

if err := json.NewEncoder(tr).Encode(sEntry); err != nil {
return nil, fmt.Errorf("write: %s", err)
}

if err := json.NewDecoder(tr).Decode(sEntry); err != nil {
rcvdSEntry := &SignedEntry{}
if err := json.NewDecoder(tr).Decode(rcvdSEntry); err != nil {
return nil, fmt.Errorf("read: %s", err)
}

// Verifying remote signature
if err := verifySig(sEntry, tm.Remote(tr.Edges())); err != nil {
remote, err := tm.Remote(tr.Edges())
if err != nil {
return nil, err
}
if err := verifySig(rcvdSEntry, remote); err != nil {
return nil, err
}

newEntry := tm.walkEntries(func(e *Entry) bool { return *e == *sEntry.Entry }) == nil
newEntry := tm.walkEntries(func(e *Entry) bool { return *e == *rcvdSEntry.Entry }) == nil
if newEntry {
tm.addEntry(entry)
}

return sEntry.Entry, nil
return rcvdSEntry.Entry, nil
}
}

Expand All @@ -72,14 +78,19 @@ func settlementResponderHandshake(tm *Manager, tr Transport) (*Entry, error) {
return nil, fmt.Errorf("read: %s", err)
}

// it must be tm.Local() ?
if err := validateSignedEntry(sEntry, tr, tm.Remote(tr.Edges())); err != nil {
remote, errRemote := tm.Remote(tr.Edges())
if errRemote != nil {
return nil, errRemote
}
if err := validateSignedEntry(sEntry, tr, remote); err != nil {
return nil, err
}

// Write second signature
// sEntry.Signatures[1] = sEntry.Entry.Signature(tm.config.SecKey)
sEntry.Sign(tm.Local(), tm.config.SecKey)
if err := sEntry.Sign(tm.Local(), tm.config.SecKey); err != nil {
return nil, err
}

newEntry := tm.walkEntries(func(e *Entry) bool { return *e == *sEntry.Entry }) == nil

Expand Down Expand Up @@ -126,5 +137,9 @@ func validateSignedEntry(sEntry *SignedEntry, tr Transport, pk cipher.PubKey) er
}

func verifySig(sEntry *SignedEntry, pk cipher.PubKey) error {
return cipher.VerifyPubKeySignedPayload(pk, sEntry.Signature(pk), sEntry.Entry.ToBinary())
sig, err := sEntry.Signature(pk)
if err != nil {
return err
}
return cipher.VerifyPubKeySignedPayload(pk, sig, sEntry.Entry.ToBinary())
}
Loading

0 comments on commit 8e90eb3

Please sign in to comment.