Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit a72d008

Browse files
authored
Ensure config files/directories for sessions & KMS (#6)
1 parent 8b8e95f commit a72d008

File tree

7 files changed

+131
-4
lines changed

7 files changed

+131
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.vscode
33
!.vscode/extensions.json
44
bin/
5+
tmp/*
56
config/*.yml
67
!config/*example.yml
78
web/redoc/**/*.json

internal/db/connection/bolt/bolt.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bolt
22

33
import (
4+
"github.com/oxygenpay/oxygen/internal/util"
45
"github.com/pkg/errors"
56
"github.com/rs/zerolog"
67
"go.etcd.io/bbolt"
@@ -17,9 +18,15 @@ type Connection struct {
1718

1819
const WalletsBucket = "wallets"
1920

21+
const chmodReadWrite = 0660
22+
2023
func Open(cfg Config, logger *zerolog.Logger) (*Connection, error) {
2124
log := logger.With().Str("channel", "bolt").Logger()
2225

26+
if err := util.EnsureFile(cfg.DataSource, chmodReadWrite); err != nil {
27+
return nil, errors.Wrap(err, "unable to ensure KMS database file")
28+
}
29+
2330
log.Info().Str("db_path", cfg.DataSource).Msg("connecting to bolt")
2431
db, err := bbolt.Open(cfg.DataSource, 0660, nil)
2532
if err != nil {

internal/server/http/middleware/middleware.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package middleware
22

33
import (
4+
"fmt"
45
"net/http"
56

67
"github.com/gorilla/sessions"
@@ -9,6 +10,7 @@ import (
910
mw "github.com/labstack/echo/v4/middleware"
1011
"github.com/labstack/gommon/log"
1112
"github.com/oxygenpay/oxygen/internal/server/http/common"
13+
"github.com/oxygenpay/oxygen/internal/util"
1214
"github.com/rs/zerolog"
1315
)
1416

@@ -25,7 +27,13 @@ type SessionConfig struct {
2527

2628
const sessionOptionsKey = "session_options"
2729

30+
const chmodReadWrite = 0660
31+
2832
func Session(cfg SessionConfig) echo.MiddlewareFunc {
33+
if err := util.EnsureDirectory(cfg.FilesystemPath, chmodReadWrite); err != nil {
34+
panic(fmt.Sprintf("unable to ensure session directory %q", cfg.FilesystemPath))
35+
}
36+
2937
sessionOptions := sessions.Options{
3038
Path: cfg.CookiePath,
3139
Domain: cfg.CookieDomain,

internal/util/fs.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"io/fs"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/pkg/errors"
10+
)
11+
12+
// EnsureFile checks that file exists. If not, creates an empty file.
13+
func EnsureFile(path string, mode fs.FileMode) error {
14+
stat, err := os.Stat(path)
15+
16+
switch {
17+
case os.IsNotExist(err):
18+
if errDir := EnsureDirectory(filepath.Dir(path), mode); errDir != nil {
19+
return errDir
20+
}
21+
22+
return createFile(path, mode)
23+
case err != nil:
24+
return err
25+
case stat.IsDir():
26+
return fmt.Errorf("path %q is a directory", path)
27+
}
28+
29+
return nil
30+
}
31+
32+
// EnsureDirectory checks that file exists. If not, creates an empty directory.
33+
func EnsureDirectory(path string, mode fs.FileMode) error {
34+
if err := os.MkdirAll(path, mode); err != nil {
35+
return errors.Wrapf(err, "unable to ensure directory %q", path)
36+
}
37+
38+
return nil
39+
}
40+
41+
func createFile(path string, mode fs.FileMode) error {
42+
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, mode)
43+
if err != nil {
44+
return err
45+
}
46+
47+
_ = file.Close()
48+
49+
return nil
50+
}

internal/util/fs_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package util
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFS(t *testing.T) {
10+
// RW
11+
const mode = 0770
12+
13+
tempRoot := t.TempDir()
14+
wrap := func(path string) string {
15+
return tempRoot + path
16+
}
17+
18+
for _, tt := range []struct {
19+
name string
20+
path string
21+
isFile bool
22+
}{
23+
{
24+
name: "ensures env file",
25+
path: "/app/oxygen.env",
26+
isFile: true,
27+
},
28+
{
29+
name: "ensures env file one more time",
30+
path: "/app/oxygen.env",
31+
isFile: true,
32+
},
33+
{
34+
name: "creates sessions directory",
35+
path: "/app/sessions",
36+
},
37+
{
38+
name: "creates bolt.db file",
39+
path: "/app/kms/kms.db",
40+
isFile: true,
41+
},
42+
} {
43+
t.Run(tt.name, func(t *testing.T) {
44+
// ARRANGE
45+
p := wrap(tt.path)
46+
47+
// ACT
48+
var err error
49+
if tt.isFile {
50+
err = EnsureFile(p, mode)
51+
} else {
52+
err = EnsureDirectory(p, mode)
53+
}
54+
55+
// ASSERT
56+
assert.NoError(t, err)
57+
58+
if tt.isFile {
59+
assert.FileExists(t, p)
60+
} else {
61+
assert.DirExists(t, p)
62+
}
63+
})
64+
}
65+
}

tmp/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

tmp/sessions/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)