Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open bbolt database with file mode 0600 instead of 0666 #545

Merged
merged 2 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ func TestBucket_Delete_Quick(t *testing.T) {

func ExampleBucket_Put() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1928,7 +1928,7 @@ func ExampleBucket_Put() {

func ExampleBucket_Delete() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1986,7 +1986,7 @@ func ExampleBucket_Delete() {

func ExampleBucket_ForEach() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (cmd *checkCommand) Run(args ...string) error {
}

// Open database.
db, err := bolt.Open(path, 0666, &bolt.Options{
db, err := bolt.Open(path, 0600, &bolt.Options{
ReadOnly: true,
PreLoadFreelist: true,
})
Expand Down Expand Up @@ -284,7 +284,7 @@ func (cmd *infoCommand) Run(args ...string) error {
}

// Open the database.
db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
Expand Down Expand Up @@ -644,7 +644,7 @@ func (cmd *pagesCommand) Run(args ...string) error {
}

// Open database.
db, err := bolt.Open(path, 0666, &bolt.Options{
db, err := bolt.Open(path, 0600, &bolt.Options{
ReadOnly: true,
PreLoadFreelist: true,
})
Expand Down Expand Up @@ -737,7 +737,7 @@ func (cmd *statsCommand) Run(args ...string) error {
}

// Open database.
db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
Expand Down Expand Up @@ -868,7 +868,7 @@ func (cmd *bucketsCommand) Run(args ...string) error {
}

// Open database.
db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
Expand Down Expand Up @@ -929,7 +929,7 @@ func (cmd *keysCommand) Run(args ...string) error {
}

// Open database.
db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
Expand Down Expand Up @@ -1020,7 +1020,7 @@ func (cmd *getCommand) Run(args ...string) error {
}

// Open database.
db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
Expand Down Expand Up @@ -1097,7 +1097,7 @@ func (cmd *benchCommand) Run(args ...string) error {
}

// Create database.
db, err := bolt.Open(options.Path, 0666, nil)
db, err := bolt.Open(options.Path, 0600, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -1652,7 +1652,7 @@ func (cmd *compactCommand) Run(args ...string) (err error) {
initialSize := fi.Size()

// Open source database.
src, err := bolt.Open(cmd.SrcPath, 0444, &bolt.Options{ReadOnly: true})
src, err := bolt.Open(cmd.SrcPath, 0400, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func fillBucket(b *bolt.Bucket, prefix []byte) error {
}

func chkdb(path string) ([]byte, error) {
db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion concurrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func mustOpenDB(t *testing.T, dbPath string, o *bolt.Options) *bolt.DB {

o.FreelistType = freelistType

db, err := bolt.Open(dbPath, 0666, o)
db, err := bolt.Open(dbPath, 0600, o)
require.NoError(t, err)

return db
Expand Down
4 changes: 2 additions & 2 deletions cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func TestCursor_QuickCheck_BucketsOnly_Reverse(t *testing.T) {

func ExampleCursor() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -798,7 +798,7 @@ func ExampleCursor() {

func ExampleCursor_reverse() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
34 changes: 17 additions & 17 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestOpen(t *testing.T) {
path := tempfile()
defer os.RemoveAll(path)

db, err := bolt.Open(path, 0666, nil)
db, err := bolt.Open(path, 0600, nil)
if err != nil {
t.Fatal(err)
} else if db == nil {
Expand Down Expand Up @@ -108,15 +108,15 @@ func TestOpen_MultipleGoroutines(t *testing.T) {

// Ensure that opening a database with a blank path returns an error.
func TestOpen_ErrPathRequired(t *testing.T) {
_, err := bolt.Open("", 0666, nil)
_, err := bolt.Open("", 0600, nil)
if err == nil {
t.Fatalf("expected error")
}
}

// Ensure that opening a database with a bad path returns an error.
func TestOpen_ErrNotExists(t *testing.T) {
_, err := bolt.Open(filepath.Join(tempfile(), "bad-path"), 0666, nil)
_, err := bolt.Open(filepath.Join(tempfile(), "bad-path"), 0600, nil)
if err == nil {
t.Fatal("expected error")
}
Expand All @@ -138,7 +138,7 @@ func TestOpen_ErrInvalid(t *testing.T) {
t.Fatal(err)
}

if _, err := bolt.Open(path, 0666, nil); err != berrors.ErrInvalid {
if _, err := bolt.Open(path, 0600, nil); err != berrors.ErrInvalid {
t.Fatalf("unexpected error: %s", err)
}
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestOpen_ErrVersionMismatch(t *testing.T) {
}

// Reopen data file.
if _, err := bolt.Open(path, 0666, nil); err != berrors.ErrVersionMismatch {
if _, err := bolt.Open(path, 0600, nil); err != berrors.ErrVersionMismatch {
t.Fatalf("unexpected error: %s", err)
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestOpen_ErrChecksum(t *testing.T) {
}

// Reopen data file.
if _, err := bolt.Open(path, 0666, nil); err != berrors.ErrChecksum {
if _, err := bolt.Open(path, 0600, nil); err != berrors.ErrChecksum {
t.Fatalf("unexpected error: %s", err)
}
}
Expand Down Expand Up @@ -366,7 +366,7 @@ func TestOpen_Size_Large(t *testing.T) {
}

// Reopen database, update, and check size again.
db0, err := bolt.Open(path, 0666, nil)
db0, err := bolt.Open(path, 0600, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -396,7 +396,7 @@ func TestOpen_Check(t *testing.T) {
path := tempfile()
defer os.RemoveAll(path)

db, err := bolt.Open(path, 0666, nil)
db, err := bolt.Open(path, 0600, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -407,7 +407,7 @@ func TestOpen_Check(t *testing.T) {
t.Fatal(err)
}

db, err = bolt.Open(path, 0666, nil)
db, err = bolt.Open(path, 0600, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -429,7 +429,7 @@ func TestOpen_FileTooSmall(t *testing.T) {
path := tempfile()
defer os.RemoveAll(path)

db, err := bolt.Open(path, 0666, nil)
db, err := bolt.Open(path, 0600, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -443,7 +443,7 @@ func TestOpen_FileTooSmall(t *testing.T) {
t.Fatal(err)
}

_, err = bolt.Open(path, 0666, nil)
_, err = bolt.Open(path, 0600, nil)
if err == nil || !strings.Contains(err.Error(), "file size too small") {
t.Fatalf("unexpected error: %s", err)
}
Expand All @@ -460,7 +460,7 @@ func TestDB_Open_InitialMmapSize(t *testing.T) {
initMmapSize := 1 << 30 // 1GB
testWriteSize := 1 << 27 // 134MB

db, err := bolt.Open(path, 0666, &bolt.Options{InitialMmapSize: initMmapSize})
db, err := bolt.Open(path, 0600, &bolt.Options{InitialMmapSize: initMmapSize})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -533,7 +533,7 @@ func TestDB_Open_ReadOnly(t *testing.T) {

f := db.Path()
o := &bolt.Options{ReadOnly: true}
readOnlyDB, err := bolt.Open(f, 0666, o)
readOnlyDB, err := bolt.Open(f, 0600, o)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -565,7 +565,7 @@ func TestDB_Open_ReadOnly(t *testing.T) {

func TestDB_Open_ReadOnly_NoCreate(t *testing.T) {
f := filepath.Join(t.TempDir(), "db")
_, err := bolt.Open(f, 0666, &bolt.Options{ReadOnly: true})
_, err := bolt.Open(f, 0600, &bolt.Options{ReadOnly: true})
require.ErrorIs(t, err, os.ErrNotExist)
}

Expand Down Expand Up @@ -1348,7 +1348,7 @@ func TestDBUnmap(t *testing.T) {

func ExampleDB_Update() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1388,7 +1388,7 @@ func ExampleDB_Update() {

func ExampleDB_View() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -1431,7 +1431,7 @@ func ExampleDB_View() {

func ExampleDB_Begin() {
// Open the database.
db, err := bolt.Open(tempfile(), 0666, nil)
db, err := bolt.Open(tempfile(), 0600, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/btesting/btesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func MustOpenDBWithOption(t testing.TB, f string, o *bolt.Options) *DB {

o.FreelistType = freelistType

db, err := bolt.Open(f, 0666, o)
db, err := bolt.Open(f, 0600, o)
require.NoError(t, err)
resDB := &DB{
DB: db,
Expand Down Expand Up @@ -115,7 +115,7 @@ func (db *DB) MustReopen() {
panic("Please call Close() before MustReopen()")
}
db.t.Logf("Reopening bbolt DB at: %s", db.f)
indb, err := bolt.Open(db.Path(), 0666, db.o)
indb, err := bolt.Open(db.Path(), 0600, db.o)
require.NoError(db.t, err)
db.DB = indb
db.strictModeEnabledDefault()
Expand Down
10 changes: 5 additions & 5 deletions tests/failpoint/db_failpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFailpoint_MapFail(t *testing.T) {
}()

f := filepath.Join(t.TempDir(), "db")
_, err = bolt.Open(f, 0666, nil)
_, err = bolt.Open(f, 0600, nil)
require.Error(t, err)
require.ErrorContains(t, err, "map somehow failed")
}
Expand All @@ -36,14 +36,14 @@ func TestFailpoint_UnmapFail_DbClose(t *testing.T) {

err := gofail.Enable("unmapError", `return("unmap somehow failed")`)
require.NoError(t, err)
_, err = bolt.Open(f, 0666, nil)
_, err = bolt.Open(f, 0600, nil)
require.Error(t, err)
require.ErrorContains(t, err, "unmap somehow failed")
//disable the error, and try to reopen the db
err = gofail.Disable("unmapError")
require.NoError(t, err)

db, err := bolt.Open(f, 0666, &bolt.Options{Timeout: 30 * time.Second})
db, err := bolt.Open(f, 0600, &bolt.Options{Timeout: 30 * time.Second})
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)
Expand All @@ -54,15 +54,15 @@ func TestFailpoint_mLockFail(t *testing.T) {
require.NoError(t, err)

f := filepath.Join(t.TempDir(), "db")
_, err = bolt.Open(f, 0666, &bolt.Options{Mlock: true})
_, err = bolt.Open(f, 0600, &bolt.Options{Mlock: true})
require.Error(t, err)
require.ErrorContains(t, err, "mlock somehow failed")

// It should work after disabling the failpoint.
err = gofail.Disable("mlockError")
require.NoError(t, err)

_, err = bolt.Open(f, 0666, &bolt.Options{Mlock: true})
_, err = bolt.Open(f, 0600, &bolt.Options{Mlock: true})
require.NoError(t, err)
}

Expand Down
Loading
Loading