Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
Refine client (#203)
Browse files Browse the repository at this point in the history
* Refine client domain
* Refine user domain entity
* Depend on lint to bail CI early
* Refine app
* Cleanup app ifaces
* Refine error handling on application
* Cleanup enclaverepo
* Refine error handling on enclaverepo
* Fix dependency between enclaverepo and remote
* Move remote-related encryption to remote module
* Refine error handling on remote adapter
* Partially refine rest http server port
* Fix sync bug
* Fix test suites
* Improve golangci-lint
* Refine rest api returned errors
* Organize client rest api file structure & cleanup middleware
* Parallelize all tests
* Add some error handling on keystores/invitations
* Remove metrics endpoint
* Fix lint
* Refine applications startup
* Remove config pkg
* Fix shutdown error
* Refine shared secrets calculations
* Move keypair generation to enclave
* Fix invitations count on sidebar
* Refine invitations page statuses
* Cleanup routers
* Some more error handling
* Silent authentication cronjob
  • Loading branch information
rdnt authored Jun 17, 2023
1 parent b0a3d32 commit 7ac7cf4
Show file tree
Hide file tree
Showing 83 changed files with 2,166 additions and 2,616 deletions.
38 changes: 0 additions & 38 deletions .devcontainer/devcontainer.json

This file was deleted.

5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run:
timeout: 3m
concurrency: 32
skip-dirs:
- ui
97 changes: 58 additions & 39 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (
"os/signal"
"time"

"github.com/namsral/flag"
"github.com/pkg/errors"

"myst/pkg/config"
"myst/pkg/logger"
"myst/src/client/application"
"myst/src/client/enclaverepo"
"myst/src/client/remote"
"myst/src/client/rest"
"myst/src/client/scheduler"

"github.com/namsral/flag"
)

//go:embed static/*
var static embed.FS

var log = logger.New("client", logger.Red)

type Config struct {
RemoteAddress string
Port int
Expand All @@ -44,80 +44,99 @@ func parseFlags() Config {
return cfg
}

var log = logger.New("client", logger.Red)

func createDataDir(dir string) error {
var create bool
_, err := os.Stat(dir)
if errors.Is(err, os.ErrNotExist) {
create = true
} else if err != nil {
return err
func main() {
cleanup, err := run()
if err != nil {
log.Error(err)
os.Exit(1)
return
}

if !create {
return nil
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

return os.Mkdir(dir, os.ModePerm)
<-c

err = cleanup()
if err != nil {
log.Error(err)
os.Exit(1)
return
}
}

func main() {
func run() (cleanup func() error, err error) {
cfg := parseFlags()

if cfg.Slow {
time.Sleep(500 * time.Millisecond)
}

logger.EnableDebug = config.Debug
logger.EnableDebug = true

err := createDataDir(cfg.DataDir)
err = createDataDir(cfg.DataDir)
if err != nil {
panic(err)
return nil, errors.WithMessage(err, "unable to create data directory")
}

enc, err := enclaverepo.New(cfg.DataDir)
if err != nil {
panic(err)
}
enc := enclaverepo.New(cfg.DataDir)

rem, err := remote.New(
remote.WithAddress(cfg.RemoteAddress),
)
if err != nil {
panic(err)
return nil, errors.WithMessage(err, "unable to create remote repository")
}

app, err := application.New(
app := application.New(
application.WithEnclave(enc),
application.WithRemote(rem),
)

sched, err := scheduler.New(app, rem)
if err != nil {
panic(err)
return nil, errors.WithMessage(err, "unable to create scheduler")
}

server := rest.NewServer(app, static)

sched, err := scheduler.New(app)
if err != nil {
panic(err)
}

err = sched.Start()
if err != nil {
panic(err)
return nil, errors.WithMessage(err, "unable to start scheduler")
}

err = server.Start(fmt.Sprintf(":%d", cfg.Port))
if err != nil {
log.Error(err)
return nil, errors.WithMessage(err, "unable to start server")
}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
return func() error {
err = server.Stop()
if err != nil {
return errors.WithMessage(err, "unable to stop server")
}

<-c
err = sched.Stop()
if err != nil {
return errors.WithMessage(err, "unable to stop scheduler")
}

return nil
}, nil
}

_ = server.Stop()
_ = sched.Stop()
func createDataDir(dir string) error {
var create bool
_, err := os.Stat(dir)
if errors.Is(err, os.ErrNotExist) {
create = true
} else if err != nil {
return err
}

if !create {
return nil
}

return os.Mkdir(dir, os.ModePerm)
}
55 changes: 34 additions & 21 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package main

import (
"encoding/base64"
"fmt"
"os"
"os/signal"
"time"

"github.com/namsral/flag"
"github.com/pkg/errors"

"myst/pkg/config"
"myst/pkg/logger"
"myst/src/server/application"
"myst/src/server/mongorepo"
"myst/src/server/rest"
)

var log = logger.New("app", logger.Red)

type Config struct {
Port int
Slow bool
Expand All @@ -41,52 +42,64 @@ func parseFlags() Config {
return cfg
}

var log = logger.New("app", logger.Red)

func main() {
cleanup, err := run()
if err != nil {
log.Error(err)
os.Exit(1)
return
}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

<-c

err = cleanup()
if err != nil {
log.Error(err)
os.Exit(1)
}
}

func run() (cleanup func() error, err error) {
cfg := parseFlags()

jwtSigningKey, err := base64.StdEncoding.DecodeString(cfg.JWTSigningKey)
if err != nil {
panic(err)
return nil, errors.Wrap(err, "could not decode jwt signing key")
}

if cfg.Slow {
time.Sleep(500 * time.Millisecond)
}

logger.EnableDebug = config.Debug
logger.EnableDebug = true

repo, err := mongorepo.New(cfg.MongoAddress, cfg.MongoDatabase)
if err != nil {
panic(err)
return nil, errors.WithMessage(err, "could not create mongo repository")
}

app := application.New(
application.WithKeystoreRepository(repo),
application.WithUserRepository(repo),
application.WithInvitationRepository(repo),
)
if err != nil {
panic(err)
}

server := rest.NewServer(app, jwtSigningKey)

err = server.Run(fmt.Sprintf(":%d", cfg.Port))
if err != nil {
log.Error(err)
}

err = server.Start(":8080")
if err != nil {
log.Error(err)
return nil, errors.WithMessage(err, "could not start server")
}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

<-c
return func() error {
err := server.Stop()
if err != nil {
return errors.WithMessage(err, "could not stop server")
}

_ = server.Stop()
return nil
}, nil
}
2 changes: 1 addition & 1 deletion pkg/builder/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Run(command ...string) {
return os.Getenv(s)
})
if err != nil {
panic(err)
runtime.Panic(fmt.Errorf("failed to expand command: %s\n", err))
}

args, err := shellquote.Split(expanded)
Expand Down
19 changes: 0 additions & 19 deletions pkg/config/config.go

This file was deleted.

2 changes: 2 additions & 0 deletions pkg/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestPKCS7PadUnpad(t *testing.T) {
t.Parallel()

s := ""

for i := 0; i < 100; i++ {
Expand Down
13 changes: 3 additions & 10 deletions pkg/crypto/curve25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ import (
"myst/pkg/crypto"
)

func TestKex(t *testing.T) {
func TestKeyExchange(t *testing.T) {
t.Parallel()

pub, key, err := crypto.NewCurve25519Keypair()
assert.NilError(t, err)

t.Logf("Alice Public key: \t%s\n", b64(pub))
t.Logf("Alice Private key:\t%s\n", b64(key))

pub2, key2, err := crypto.NewCurve25519Keypair()
assert.NilError(t, err)

t.Logf("Bob Public key: \t%s\n", b64(pub2))
t.Logf("Bob Private key:\t%s\n", b64(key2))

// exchange pub keys...

out, err := curve25519.X25519(key, pub2)
Expand All @@ -31,9 +27,6 @@ func TestKex(t *testing.T) {
out2, err := curve25519.X25519(key2, pub)
assert.NilError(t, err)

t.Logf("Shared key (Alice):\t%s\n", b64(out))
t.Logf("Shared key (Bob):\t%s\n", b64(out2))

assert.Equal(t, b64(out), b64(out2))
}

Expand Down
Loading

0 comments on commit 7ac7cf4

Please sign in to comment.