From f160027c17def743dee4499266f18fd95223dcdc Mon Sep 17 00:00:00 2001 From: Benjamin Kuen Date: Tue, 26 Dec 2023 17:25:58 +0100 Subject: [PATCH] Add application lib dir and ssh keypair generation --- cmd/bwizard/main.go | 17 +++++++++++ go.mod | 5 +++- go.sum | 4 +-- internal/pkg/ssh/keypair.go | 34 +++++++++++++++++++++ internal/pkg/ssh/keypair_test.go | 51 ++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 internal/pkg/ssh/keypair.go create mode 100644 internal/pkg/ssh/keypair_test.go diff --git a/cmd/bwizard/main.go b/cmd/bwizard/main.go index 49bef9b..e84afe5 100644 --- a/cmd/bwizard/main.go +++ b/cmd/bwizard/main.go @@ -1,6 +1,7 @@ package main import ( + "bwizard/internal/pkg/ssh" "bwizard/internal/pkg/wizard/application" "bwizard/internal/pkg/wizard/infrastructure/mysql" "bwizard/internal/pkg/wizardapi" @@ -9,6 +10,10 @@ import ( "time" ) +const ( + ApplicationDir = "/var/lib/bwizard" +) + func main() { logger := zerolog.New( zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}, @@ -18,6 +23,18 @@ func main() { Caller(). Logger() + if err := os.Mkdir(ApplicationDir, 0755); err != nil { + if !os.IsExist(err) { + logger.Error().Msg(err.Error()) + os.Exit(1) + } + } + + if err := ssh.CreateSSHKeyPairIfNotExists(ApplicationDir); err != nil { + logger.Error().Msg(err.Error()) + os.Exit(1) + } + db, err := mysql.Connect(&logger) if err != nil { logger.Error().Msg(err.Error()) diff --git a/go.mod b/go.mod index 813ac39..9c14956 100644 --- a/go.mod +++ b/go.mod @@ -9,20 +9,23 @@ require ( github.com/labstack/echo/v4 v4.11.4 github.com/oapi-codegen/runtime v1.1.0 github.com/rs/zerolog v1.31.0 + github.com/stretchr/testify v1.8.4 golang.org/x/crypto v0.17.0 ) require ( github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/stretchr/objx v0.5.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.5.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c5bb7cc..8dc22bc 100644 --- a/go.sum +++ b/go.sum @@ -43,13 +43,10 @@ github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -78,6 +75,7 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/internal/pkg/ssh/keypair.go b/internal/pkg/ssh/keypair.go new file mode 100644 index 0000000..1d38a9a --- /dev/null +++ b/internal/pkg/ssh/keypair.go @@ -0,0 +1,34 @@ +package ssh + +import ( + "golang.org/x/crypto/ed25519" + "os" + "path" +) + +// CreateSSHKeyPairIfNotExists creates a new ed25519 key pair inside dir only if no key exists +func CreateSSHKeyPairIfNotExists(dir string) error { + if _, err := os.Stat(path.Join(dir, "id_ed25519")); os.IsNotExist(err) { + return CreateSSHKeyPair(dir) + } + + return nil +} + +// CreateSSHKeyPair creates a new ed25519 key pair inside dir +func CreateSSHKeyPair(dir string) error { + privateKey, publicKey, err := ed25519.GenerateKey(nil) + if err != nil { + return err + } + + if err := os.WriteFile(path.Join(dir, "id_ed25519"), privateKey, 0600); err != nil { + return err + } + + if err := os.WriteFile(path.Join(dir, "id_ed25519.pub"), publicKey, 0600); err != nil { + return err + } + + return nil +} diff --git a/internal/pkg/ssh/keypair_test.go b/internal/pkg/ssh/keypair_test.go new file mode 100644 index 0000000..9175531 --- /dev/null +++ b/internal/pkg/ssh/keypair_test.go @@ -0,0 +1,51 @@ +package ssh_test + +import ( + "bwizard/internal/pkg/ssh" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "os" + "path" + "testing" +) + +func TestCreateSSHKeyPair(t *testing.T) { + dir := t.TempDir() + + err := ssh.CreateSSHKeyPair(dir) + require.NoError(t, err) + + privateKey, err := os.ReadFile(path.Join(dir, "id_ed25519")) + require.NoError(t, err) + + publicKey, err := os.ReadFile(path.Join(dir, "id_ed25519.pub")) + require.NoError(t, err) + + assert.True(t, len(privateKey) > 0) + assert.True(t, len(publicKey) > 0) +} + +func TestCreateSSHKeyPairIfNotExists(t *testing.T) { + dir := t.TempDir() + + err := ssh.CreateSSHKeyPair(dir) + require.NoError(t, err) + + oldPrivateKey, err := os.ReadFile(path.Join(dir, "id_ed25519")) + require.NoError(t, err) + + oldPublicKey, err := os.ReadFile(path.Join(dir, "id_ed25519.pub")) + require.NoError(t, err) + + err = ssh.CreateSSHKeyPairIfNotExists(dir) + require.NoError(t, err) + + newPrivateKey, err := os.ReadFile(path.Join(dir, "id_ed25519")) + require.NoError(t, err) + + newPublicKey, err := os.ReadFile(path.Join(dir, "id_ed25519.pub")) + require.NoError(t, err) + + assert.Equal(t, oldPrivateKey, newPrivateKey) + assert.Equal(t, oldPublicKey, newPublicKey) +}