Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UNG-67 backend response verification #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go/main/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"

log "github.com/sirupsen/logrus"
)

// post A http request to the backend service and return response code and body
Expand Down
104 changes: 83 additions & 21 deletions go/main/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,100 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

log "github.com/sirupsen/logrus"
)

const (
DEV_STAGE = "dev"
DEV_UUID = "9d3c78ff-22f3-4441-a5d1-85c636d486ff"
DEV_PUBKEY_ECDSA = "LnU8BkvGcZQPy5gWVUL+PHA0DP9dU61H8DBO8hZvTyI7lXIlG1/oruVMT7gS2nlZDK9QG+ugkRt/zTrdLrAYDA=="
DEV_PUBKEY_EdDSA = "okA7krya3TZbPNEv8SDQIGR/hOppg/mLxMh+D0vozWY="

DEMO_STAGE = "demo"
DEMO_UUID = "07104235-1892-4020-9042-00003c94b60b"
DEMO_PUBKEY_ECDSA = "xm+iIomBRjR3QdvLJrGE1OBs3bAf8EI49FfgBriRk36n4RUYX+0smrYK8tZkl6Lhrt9lzjiUGrXGijRoVE+UjA=="
DEMO_PUBKEY_EdDSA = "Of93YysDTQ66bSGcL/GS6fJJFsmgJnKstJ/QURiq0lE="

PROD_STAGE = "prod"
PROD_UUID = "10b2e1a4-56b3-4fff-9ada-cc8c20f93016"
PROD_PUBKEY_ECDSA = "pJdYoJN0N3QTFMBVjZVQie1hhgumQVTy2kX9I7kXjSyoIl40EOa9MX24SBAABBV7xV2IFi1KWMnC1aLOIvOQjQ=="
PROD_PUBKEY_EdDSA = "74BIrQbAKFrwF3AJOBgwxGzsAl0B2GCF51pPAEHC5pA="

defaultKeyURL = "https://key.%s.ubirch.com/api/keyService/v1/pubkey"
defaultDataURL = "https://data.%s.ubirch.com/v1"
defaultNiomonURL = "https://niomon.%s.ubirch.com/"
defaultVerifyURL = "https://verify.%s.ubirch.com/api/upp"
defaultBootstrapURL = "https://api.console.%s.ubirch.com/ubirch-web-ui/api/v1/devices/bootstrap"
)

// configuration file structure
type Config struct {
Password string `json:"password"` // password for the ubirch backend (mandatory)
Env string `json:"env"` // ubirch environment (optional)
KeyService string `json:"keyService"` // key service URL (optional)
Niomon string `json:"niomon"` // authentication service URL (optional)
DataService string `json:"data"` // data service URL (optional)
VerifyService string `json:"verify"` // verification service URL (optional)
BootstrapService string `json:"boot"` // bootstrap service URL (optional)
Debug bool `json:"debug"` // enable extended debug output (optional)
Uuid string `json:"uuid"` // the device uuid (set UUID here if you want to generate a new key pair on the SIM card)
Pin string `json:"pin"` // the SIM pin (set PIN here if bootstrapping is not possible)
Password string `json:"password"` // password for the ubirch backend (mandatory)
Env string `json:"env"` // ubirch environment (optional)
ServerIdentity identity `json:"serverIdentity"` // backend UUID and public keys (optional)
KeyService string `json:"keyService"` // key service URL (optional)
Niomon string `json:"niomon"` // authentication service URL (optional)
DataService string `json:"data"` // data service URL (optional)
VerifyService string `json:"verify"` // verification service URL (optional)
BootstrapService string `json:"boot"` // bootstrap service URL (optional)
Debug bool `json:"debug"` // enable extended debug output (optional)
Uuid string `json:"uuid"` // the device uuid (set UUID here if you want to generate a new key pair on the SIM card)
Pin string `json:"pin"` // the SIM pin (set PIN here if bootstrapping is not possible)
}

type identity struct {
UUID string
PubKey pubkey
}

type pubkey struct {
ECDSA string
EdDSA string
}

var defaultServerIdentities = map[string]identity{
DEV_STAGE: {UUID: DEV_UUID, PubKey: pubkey{ECDSA: DEV_PUBKEY_ECDSA, EdDSA: DEV_PUBKEY_EdDSA}},
DEMO_STAGE: {UUID: DEMO_UUID, PubKey: pubkey{ECDSA: DEMO_PUBKEY_ECDSA, EdDSA: DEMO_PUBKEY_EdDSA}},
PROD_STAGE: {UUID: PROD_UUID, PubKey: pubkey{ECDSA: PROD_PUBKEY_ECDSA, EdDSA: PROD_PUBKEY_EdDSA}},
}

// Load the config file
func (c *Config) Load(fn string) error {
contextBytes, err := ioutil.ReadFile(fn)
fileHandle, err := os.Open(fn)
if err != nil {
return err
}
defer fileHandle.Close()

err = json.Unmarshal(contextBytes, c)
err = json.NewDecoder(fileHandle).Decode(c)
if err != nil {
log.Fatalf("unable to read configuration %v", err)
return err
}

if c.Debug {
log.SetLevel(log.DebugLevel)
}

if c.Password == "" {
log.Printf("password not set in config. will skip backend communication.")
}

if c.Env == "" {
c.Env = "prod"
c.Env = PROD_STAGE
}

// assert Env variable value is a valid UBIRCH backend environment
if !(c.Env == DEV_STAGE || c.Env == DEMO_STAGE || c.Env == PROD_STAGE) {
return fmt.Errorf("invalid UBIRCH backend environment: \"%s\"", c.Env)
}

log.Debugf("UBIRCH backend \"%s\" environment", c.Env)

if c.KeyService == "" {
c.KeyService = fmt.Sprintf("https://key.%s.ubirch.com/api/keyService/v1/pubkey", c.Env)
c.KeyService = fmt.Sprintf(defaultKeyURL, c.Env)
} else {
c.KeyService = strings.TrimSuffix(c.KeyService, "/mpack")
}
Expand All @@ -53,18 +105,28 @@ func (c *Config) Load(fn string) error {
c.Env = strings.Split(c.KeyService, ".")[1]

if c.Niomon == "" {
c.Niomon = fmt.Sprintf("https://niomon.%s.ubirch.com/", c.Env)
c.Niomon = fmt.Sprintf(defaultNiomonURL, c.Env)
}
if c.DataService == "" {
c.DataService = fmt.Sprintf("https://data.%s.ubirch.com/v1", c.Env)
c.DataService = fmt.Sprintf(defaultDataURL, c.Env)
} else {
c.DataService = strings.TrimSuffix(c.DataService, "/msgPack")
}
if c.VerifyService == "" {
c.VerifyService = fmt.Sprintf("https://verify.%s.ubirch.com/api/upp", c.Env)
c.VerifyService = fmt.Sprintf(defaultVerifyURL, c.Env)
}
if c.BootstrapService == "" {
c.BootstrapService = fmt.Sprintf("https://api.console.%s.ubirch.com/ubirch-web-ui/api/v1/devices/bootstrap", c.Env)
c.BootstrapService = fmt.Sprintf(defaultBootstrapURL, c.Env)
}

log.Debugf(" - Key Service: %s", c.KeyService)
log.Debugf(" - Authentication Service: %s", c.Niomon)
log.Debugf(" - Data Service: %s", c.DataService)
log.Debugf(" - Verification Service: %s", c.VerifyService)
log.Debugf(" - Bootstrapping Service: %s", c.BootstrapService)

if c.ServerIdentity == (identity{}) {
c.ServerIdentity = defaultServerIdentities[c.Env]
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion go/main/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.12

require (
github.com/google/uuid v1.1.1
github.com/ubirch/ubirch-protocol-go/ubirch/v2 v2.1.3
github.com/sirupsen/logrus v1.7.0
github.com/ubirch/ubirch-protocol-sim/go/ubirch v0.0.0
go.bug.st/serial v1.1.0
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go/main/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ github.com/creack/goselect v0.1.1 h1:tiSSgKE1eJtxs1h/VgGQWuXUP0YS4CDIFMp6vaI1ls0
github.com/creack/goselect v0.1.1/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/paypal/go.crypto v0.1.0/go.mod h1:SkWGgUAivahcWkhrh1+u2TdpofifG/A2vNpmnhT4oHY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/ubirch/go.crypto v0.1.2 h1:IYMOx19UgWt4+k7PydcOVtEWFiU10O8dHoof00j8IKw=
github.com/ubirch/go.crypto v0.1.2/go.mod h1:aiZQ37CxSBS7cBLsFbTnDxGeg5RkH7Z5LKBYeNasIrw=
github.com/ubirch/ubirch-protocol-go/ubirch v1.0.1 h1:N5pGaZ/sW4bK2m4CUraVyrzfuQpnacXoQTmh7f6sRzQ=
github.com/ubirch/ubirch-protocol-go/ubirch v1.0.1/go.mod h1:FNnB4hBmDXOLmdf4ytPSab+9HUdtgK10AY/0eg1RSOs=
github.com/ubirch/ubirch-protocol-go/ubirch/v2 v2.1.2 h1:oyvbyF4XfKa8tAwveNXxnaliFUbocELEFsXfyp4atag=
github.com/ubirch/ubirch-protocol-go/ubirch/v2 v2.1.2/go.mod h1:nTqN+niHQNN5XLxMgd5MOD+yW9vBi9trYIj4vBuVi2Y=
github.com/ubirch/ubirch-protocol-go/ubirch/v2 v2.1.3 h1:jz8Fu27w2VtwPTfooQ0k5evfkaRe4EboZxK6c1tkg0c=
github.com/ubirch/ubirch-protocol-go/ubirch/v2 v2.1.3/go.mod h1:nTqN+niHQNN5XLxMgd5MOD+yW9vBi9trYIj4vBuVi2Y=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
Expand All @@ -25,6 +28,7 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
go.bug.st/serial v1.1.0 h1:O0EHZw8ZdhmTAikak5ZY/8vyKCpFxZYgqZw1bGegxU8=
go.bug.st/serial v1.1.0/go.mod h1:rpXPISGjuNjPTRTcMlxi9lN6LoIPxd1ixVjBd8aSk/Q=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
Loading