Skip to content

Commit a32f52b

Browse files
committed
Fixed issue #2097
1 parent dc97104 commit a32f52b

6 files changed

Lines changed: 45 additions & 7 deletions

File tree

server/auth/database.go

Lines changed: 30 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,22 @@ 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+
fullAuthFileName, err := fileSystem.Abs(authFileName)
195+
if err != nil {
196+
return err
197+
}
198+
fullAuthDir := filepath.Dir(fullAuthFileName)
199+
if _, err := fileSystem.ReadFile(fullAuthDir); err != nil {
200+
if os.IsNotExist(err) {
201+
if err = fileSystem.MkDirs(fullAuthDir); err != nil {
202+
return err
203+
}
204+
} else {
205+
return err
206+
}
207+
}
208+
return nil
209+
}

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_
@@ -583,6 +585,7 @@ func DefaultServerConfig() *DoltgresConfig {
583585
DataDirStr: Ptr(dataDir),
584586
CfgDirStr: Ptr(filepath.Join(DefaultDataDir, DefaultCfgDir)),
585587
PrivilegeFile: Ptr(filepath.Join(DefaultDataDir, DefaultCfgDir, DefaultPrivilegeFilePath)),
588+
AuthFile: Ptr(filepath.Join(DefaultDataDir, DefaultCfgDir, DefaultAuthFilePath)),
586589
BranchControlFile: Ptr(filepath.Join(DefaultDataDir, DefaultCfgDir, DefaultBranchControlFilePath)),
587590
}
588591
}

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' {

0 commit comments

Comments
 (0)