Skip to content

Commit 1024480

Browse files
authored
SQLite: Set 0640 permissions on SQLite database file (#26339)
* SQLite: Set 640 permissions on SQLite database file
1 parent 37aa35c commit 1024480

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

pkg/services/sqlstore/migrator/migrator.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
5151

5252
exists, err := mg.x.IsTableExist(new(MigrationLog))
5353
if err != nil {
54-
return nil, err
54+
return nil, errutil.Wrap("failed to check table existence", err)
5555
}
56-
5756
if !exists {
5857
return logMap, nil
5958
}
@@ -73,7 +72,7 @@ func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
7372
}
7473

7574
func (mg *Migrator) Start() error {
76-
mg.Logger.Info("Starting DB migration")
75+
mg.Logger.Info("Starting DB migrations")
7776

7877
logMap, err := mg.GetMigrationLog()
7978
if err != nil {
@@ -110,9 +109,8 @@ func (mg *Migrator) Start() error {
110109
_, err = sess.Insert(&record)
111110
return err
112111
})
113-
114112
if err != nil {
115-
return err
113+
return errutil.Wrap("migration failed", err)
116114
}
117115
}
118116

pkg/services/sqlstore/sqlstore.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/go-sql-driver/mysql"
1414
"github.com/grafana/grafana/pkg/bus"
15+
"github.com/grafana/grafana/pkg/infra/fs"
1516
"github.com/grafana/grafana/pkg/infra/localcache"
1617
"github.com/grafana/grafana/pkg/infra/log"
1718
"github.com/grafana/grafana/pkg/models"
@@ -265,6 +266,34 @@ func (ss *SqlStore) getEngine() (*xorm.Engine, error) {
265266
}
266267

267268
sqlog.Info("Connecting to DB", "dbtype", ss.dbCfg.Type)
269+
if ss.dbCfg.Type == migrator.SQLITE && strings.HasPrefix(connectionString, "file:") {
270+
exists, err := fs.Exists(ss.dbCfg.Path)
271+
if err != nil {
272+
return nil, errutil.Wrapf(err, "can't check for existence of %q", ss.dbCfg.Path)
273+
}
274+
275+
const perms = 0640
276+
if !exists {
277+
ss.log.Info("Creating SQLite database file", "path", ss.dbCfg.Path)
278+
f, err := os.OpenFile(ss.dbCfg.Path, os.O_CREATE|os.O_RDWR, perms)
279+
if err != nil {
280+
return nil, errutil.Wrapf(err, "failed to create SQLite database file %q", ss.dbCfg.Path)
281+
}
282+
if err := f.Close(); err != nil {
283+
return nil, errutil.Wrapf(err, "failed to create SQLite database file %q", ss.dbCfg.Path)
284+
}
285+
} else {
286+
fi, err := os.Lstat(ss.dbCfg.Path)
287+
if err != nil {
288+
return nil, errutil.Wrapf(err, "failed to stat SQLite database file %q", ss.dbCfg.Path)
289+
}
290+
m := fi.Mode() & os.ModePerm
291+
if m|perms != perms {
292+
ss.log.Warn("SQLite database file has broader permissions than it should",
293+
"path", ss.dbCfg.Path, "mode", m, "expected", os.FileMode(perms))
294+
}
295+
}
296+
}
268297
engine, err := xorm.NewEngine(ss.dbCfg.Type, connectionString)
269298
if err != nil {
270299
return nil, err

0 commit comments

Comments
 (0)