Skip to content

Commit b4a352e

Browse files
authored
Merge pull request #2106 from dolthub/daylon/fix-2097
Fixed issue #2097
2 parents c257644 + 02261a3 commit b4a352e

10 files changed

Lines changed: 50 additions & 10 deletions

File tree

server/auth/database.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ package auth
1616

1717
import (
1818
"os"
19+
"path/filepath"
1920
"sync"
2021
"sync/atomic"
2122

2223
"github.com/dolthub/dolt/go/libraries/doltcore/env"
2324
"github.com/dolthub/dolt/go/libraries/utils/filesys"
25+
26+
doltgresservercfg "github.com/dolthub/doltgresql/servercfg"
2427
)
2528

2629
// authFileName is the name of the file that contains all authorization-related data.
27-
const authFileName = "auth.db"
30+
var authFileName = "auth.db"
2831

2932
var (
3033
globalDatabase Database
@@ -128,7 +131,7 @@ func LockWrite(f func()) {
128131

129132
// dbInit handle the global database initialization. Panics if an error occurs, since it points to something going
130133
// terribly wrong.
131-
func dbInit(dEnv *env.DoltEnv) {
134+
func dbInit(dEnv *env.DoltEnv, cfg *doltgresservercfg.DoltgresConfig) {
132135
globalDatabase = Database{
133136
rolesByName: make(map[string]RoleID),
134137
rolesByID: make(map[RoleID]Role),
@@ -140,10 +143,16 @@ func dbInit(dEnv *env.DoltEnv) {
140143
globalLock = &sync.RWMutex{}
141144
if dEnv != nil {
142145
if _, ok := dEnv.FS.(*filesys.InMemFS); !ok {
146+
if cfg != nil && cfg.AuthFile != nil && len(*cfg.AuthFile) > 0 {
147+
authFileName = *cfg.AuthFile
148+
}
143149
fileSystem = dEnv.FS
144150
authData, err := fileSystem.ReadFile(authFileName)
145151
if os.IsNotExist(err) {
146152
dbInitDefault()
153+
if err = dbInitCreateAuthDirectory(authFileName); err != nil {
154+
panic(err)
155+
}
147156
if err = fileSystem.WriteFile(authFileName, globalDatabase.serialize(), 0644); err != nil {
148157
panic(err)
149158
}
@@ -179,3 +188,20 @@ func dbInitDefault() {
179188
}
180189
SetRole(superUser)
181190
}
191+
192+
// dbInitCreateAuthDirectory creates the directory structure pointed to by the auth file if it does not already exist.
193+
func dbInitCreateAuthDirectory(authFileName string) error {
194+
dir, _ := filepath.Split(authFileName)
195+
// If there's no directory, then we have nothing to create
196+
if len(dir) == 0 {
197+
return nil
198+
}
199+
fullDir, err := fileSystem.Abs(dir)
200+
if err != nil {
201+
return err
202+
}
203+
if exists, _ := fileSystem.Exists(fullDir); !exists {
204+
return fileSystem.MkDirs(fullDir)
205+
}
206+
return nil
207+
}

server/auth/init.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919

2020
"github.com/dolthub/dolt/go/libraries/doltcore/env"
2121
"github.com/dolthub/go-mysql-server/sql"
22+
23+
doltgresservercfg "github.com/dolthub/doltgresql/servercfg"
2224
)
2325

2426
// doltgresPasswordEnvVar is the name of the environment variable that can be used to set the password for the
@@ -30,8 +32,8 @@ const doltgresPasswordEnvVar = "DOLTGRES_PASSWORD"
3032
const doltgresUserEnvVar = "DOLTGRES_USER"
3133

3234
// Init handles all initialization needs in this package.
33-
func Init(dEnv *env.DoltEnv) {
34-
dbInit(dEnv)
35+
func Init(dEnv *env.DoltEnv, cfg *doltgresservercfg.DoltgresConfig) {
36+
dbInit(dEnv, cfg)
3537
sql.SetAuthorizationHandlerFactory(AuthorizationHandlerFactory{})
3638
}
3739

server/initialization/initialization.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ import (
4343
var once = &sync.Once{}
4444

4545
// Initialize initializes each package across the project. This function should be used instead of an init() function.
46-
func Initialize(dEnv *env.DoltEnv) {
46+
func Initialize(dEnv *env.DoltEnv, cfg *doltgresservercfg.DoltgresConfig) {
4747
once.Do(func() {
4848
core.Init()
4949
rootobject.Init()
50-
auth.Init(dEnv)
50+
auth.Init(dEnv, cfg)
5151
analyzer.Init()
5252
config.Init()
5353
binary.Init()

server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func RunInMemory(cfg *servercfg.DoltgresConfig, protocolListenerFactory server.P
8888
// runServer starts the server based on the given args, using the provided file system as the backing store.
8989
// The returned WaitGroup may be used to wait for the server to close.
9090
func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.DoltEnv, protocolListenerFactory server.ProtocolListenerFunc) (*svcs.Controller, error) {
91-
initialization.Initialize(dEnv)
91+
initialization.Initialize(dEnv, cfg)
9292

9393
if dEnv.HasDoltDataDir() {
9494
cwd, _ := dEnv.FS.Abs(".")

servercfg/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const (
6161
DefaultDataDir = "."
6262
DefaultCfgDir = ".doltcfg"
6363
DefaultPrivilegeFilePath = "privileges.db"
64+
DefaultAuthFilePath = "auth.db"
6465
DefaultBranchControlFilePath = "branch_control.db"
6566
DefaultMetricsHost = ""
6667
DefaultMetricsPort = -1
@@ -175,6 +176,7 @@ type DoltgresConfig struct {
175176
MetricsConfig *DoltgesMetricsConfig `yaml:"metrics,omitempty" minver:"0.7.4"`
176177
RemotesapiConfig *DoltgresRemotesapiConfig `yaml:"remotesapi,omitempty" minver:"0.7.4"`
177178
PrivilegeFile *string `yaml:"privilege_file,omitempty" minver:"0.7.4"`
179+
AuthFile *string `yaml:"auth_file,omitempty" minver:"0.54.4"`
178180
BranchControlFile *string `yaml:"branch_control_file,omitempty" minver:"0.7.4"`
179181

180182
// TODO: Rename to UserVars_
@@ -587,6 +589,7 @@ func DefaultServerConfig() *DoltgresConfig {
587589
DataDirStr: Ptr(dataDir),
588590
CfgDirStr: Ptr(filepath.Join(DefaultDataDir, DefaultCfgDir)),
589591
PrivilegeFile: Ptr(filepath.Join(DefaultDataDir, DefaultCfgDir, DefaultPrivilegeFilePath)),
592+
AuthFile: Ptr(filepath.Join(DefaultDataDir, DefaultCfgDir, DefaultAuthFilePath)),
590593
BranchControlFile: Ptr(filepath.Join(DefaultDataDir, DefaultCfgDir, DefaultBranchControlFilePath)),
591594
}
592595
}

servercfg/testdata/minver_validation.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ RemotesapiConfig *servercfg.DoltgresRemotesapiConfig 0.7.4 remotesapi,omitempty
3636
-Port *int 0.7.4 port,omitempty
3737
-ReadOnly *bool 0.7.4 read_only,omitempty
3838
PrivilegeFile *string 0.7.4 privilege_file,omitempty
39+
AuthFile *string 0.54.4 auth_file,omitempty
3940
BranchControlFile *string 0.7.4 branch_control_file,omitempty
4041
Vars []servercfg.DoltgresUserSessionVars 0.7.4 user_session_vars,omitempty
4142
-Name string 0.0.0 name

testing/bats/doltgres.bats

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ remotesapi: {}
315315
316316
privilege_file: .doltcfg/privileges.db
317317
318+
auth_file: .doltcfg/auth.db
319+
318320
branch_control_file: .doltcfg/branch_control.db
319321
320322
user_session_vars: []
@@ -332,6 +334,9 @@ EOF
332334
run query_server -c "select * from t1" -t
333335
[ "$status" -eq 0 ]
334336
[[ "$output" =~ "1 | 2" ]] || false
337+
338+
run test -f ".doltcfg/auth.db"
339+
[ "$status" -eq 0 ]
335340
}
336341

337342
@test 'doltgres: DOLTGRES_DATA_DIR set to current dir' {

testing/dataloader/csvdataloader_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ import (
2929
"github.com/dolthub/doltgresql/core/dataloader"
3030
"github.com/dolthub/doltgresql/server/initialization"
3131
"github.com/dolthub/doltgresql/server/types"
32+
doltgresservercfg "github.com/dolthub/doltgresql/servercfg"
3233
)
3334

3435
// TestCsvDataLoader tests the CsvDataLoader implementation.
3536
func TestCsvDataLoader(t *testing.T) {
3637
db := memory.NewDatabase("mydb")
3738
provider := memory.NewDBProvider(db)
38-
initialization.Initialize(nil)
39+
initialization.Initialize(nil, doltgresservercfg.DefaultServerConfig())
3940

4041
ctx := &sql.Context{
4142
Context: context.Background(),

testing/dataloader/tabdataloader_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ import (
2828
"github.com/dolthub/doltgresql/core/dataloader"
2929
"github.com/dolthub/doltgresql/server/initialization"
3030
"github.com/dolthub/doltgresql/server/types"
31+
doltgresservercfg "github.com/dolthub/doltgresql/servercfg"
3132
)
3233

3334
func TestTabDataLoader(t *testing.T) {
3435
db := memory.NewDatabase("mydb")
3536
provider := memory.NewDBProvider(db)
36-
initialization.Initialize(nil)
37+
initialization.Initialize(nil, doltgresservercfg.DefaultServerConfig())
3738

3839
ctx := &sql.Context{
3940
Context: context.Background(),

testing/generation/function_coverage/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/dolthub/doltgresql/server/functions/framework"
2424
"github.com/dolthub/doltgresql/server/initialization"
25+
doltgresservercfg "github.com/dolthub/doltgresql/servercfg"
2526
"github.com/dolthub/doltgresql/testing/generation/utils"
2627
)
2728

@@ -33,7 +34,7 @@ type Assertion struct {
3334
}
3435

3536
func main() {
36-
initialization.Initialize(nil)
37+
initialization.Initialize(nil, doltgresservercfg.DefaultServerConfig())
3738
rootFolder, err := utils.GetRootFolder()
3839
if err != nil {
3940
fmt.Printf("%s\n", err.Error())

0 commit comments

Comments
 (0)