Skip to content

Commit

Permalink
chmod the storage to 600 after opening it.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Nov 7, 2023
1 parent bc8a5f7 commit 02ec57f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ops/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pickle
import shutil
import sqlite3
import stat
import subprocess
from datetime import timedelta
from pathlib import Path
Expand Down Expand Up @@ -62,6 +63,8 @@ def __init__(self, filename: Union['Path', str]):
self._db = sqlite3.connect(str(filename),
isolation_level=None,
timeout=self.DB_LOCK_TIMEOUT.total_seconds())
if filename != ":memory:":
os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR)
self._setup()

def _setup(self):
Expand Down
11 changes: 11 additions & 0 deletions test/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
import pathlib
import sys
import stat
import tempfile
import typing
import unittest
Expand Down Expand Up @@ -218,6 +219,16 @@ class TestSQLiteStorage(StoragePermutations, BaseTestCase):
def create_storage(self):
return ops.storage.SQLiteStorage(':memory:')

def test_permissions(self):
fd, filename = tempfile.mkstemp()
try:
os.close(fd)
os.remove(filename)
storage = ops.storage.SQLiteStorage(filename)
self.assertEqual(stat.S_IMODE(os.stat(filename).st_mode), stat.S_IREAD | stat.S_IWRITE)
storage.close()
finally:
os.remove(filename)

def setup_juju_backend(test_case: unittest.TestCase, state_file: pathlib.Path):
"""Create fake scripts for pretending to be state-set and state-get."""
Expand Down

0 comments on commit 02ec57f

Please sign in to comment.