Skip to content

Commit

Permalink
Add application lib dir and ssh keypair generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bkuen committed Dec 26, 2023
1 parent 4ebdf01 commit f160027
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
17 changes: 17 additions & 0 deletions cmd/bwizard/main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -9,6 +10,10 @@ import (
"time"
)

const (
ApplicationDir = "/var/lib/bwizard"
)

func main() {
logger := zerolog.New(
zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339},
Expand All @@ -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())
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 1 addition & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
34 changes: 34 additions & 0 deletions internal/pkg/ssh/keypair.go
Original file line number Diff line number Diff line change
@@ -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
}
51 changes: 51 additions & 0 deletions internal/pkg/ssh/keypair_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit f160027

Please sign in to comment.