Skip to content

Commit

Permalink
Rename more occurences of "cache" to "db" (or "database")
Browse files Browse the repository at this point in the history
  • Loading branch information
adombeck committed Feb 4, 2025
1 parent b2b08d2 commit ed87b40
Show file tree
Hide file tree
Showing 28 changed files with 266 additions and 226 deletions.
16 changes: 8 additions & 8 deletions cmd/authd/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type App struct {
// only overriable for tests.
type systemPaths struct {
BrokersConf string
Cache string
Database string
Socket string
}

Expand Down Expand Up @@ -65,7 +65,7 @@ func New() *App {
a.config = daemonConfig{
Paths: systemPaths{
BrokersConf: consts.DefaultBrokersConfPath,
Cache: consts.DefaultCacheDir,
Database: consts.DefaultDatabaseDir,
Socket: "",
},
UsersConfig: users.DefaultConfig,
Expand All @@ -82,7 +82,7 @@ func New() *App {
setVerboseMode(a.config.Verbosity)
log.Debugf(context.Background(), "Verbosity: %d", a.config.Verbosity)

if err := migrateOldCacheDir(consts.OldCacheDir, a.config.Paths.Cache); err != nil {
if err := migrateOlddbDir(consts.OlddbDir, a.config.Paths.Database); err != nil {
return err
}

Expand Down Expand Up @@ -111,18 +111,18 @@ func New() *App {
func (a *App) serve(config daemonConfig) error {
ctx := context.Background()

cacheDir := config.Paths.Cache
if err := ensureDirWithPerms(cacheDir, 0700); err != nil {
dbDir := config.Paths.Database
if err := ensureDirWithPerms(dbDir, 0700); err != nil {
close(a.ready)
return fmt.Errorf("error initializing cache directory at %q: %v", cacheDir, err)
return fmt.Errorf("error initializing database directory at %q: %v", dbDir, err)
}

m, err := services.NewManager(ctx, cacheDir, config.Paths.BrokersConf, config.Brokers, config.UsersConfig)
m, err := services.NewManager(ctx, dbDir, config.Paths.BrokersConf, config.Brokers, config.UsersConfig)
if err != nil {
close(a.ready)
return err
}
// We are closing the cache on exit.
// We are closing the database on exit.
defer func() { _ = m.Stop() }()

socketPath := config.Paths.Socket
Expand Down
34 changes: 17 additions & 17 deletions cmd/authd/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestAppCanQuitWithoutExecute(t *testing.T) {

func TestAppRunFailsOnComponentsCreationAndQuit(t *testing.T) {
t.Parallel()
// Trigger the error with a cache directory that cannot be created over an
// Trigger the error with a database directory that cannot be created over an
// existing file

const (
Expand All @@ -124,17 +124,17 @@ func TestAppRunFailsOnComponentsCreationAndQuit(t *testing.T) {
)

testCases := map[string]struct {
cacheDBBehavior int
cachePathBehavior int
dbBehavior int
dbPathBehavior int
socketPathBehavior int
}{
"Error_on_existing_cache_path_not_being_a_directory": {cachePathBehavior: dirIsFile},
"Error_on_existing_cache_path_with_invalid_permissions": {cachePathBehavior: hasWrongPermission},
"Error_on_missing_parent_cache_directory": {cachePathBehavior: parentDirDoesNotExists},
"Error_on_existing_db_path_not_being_a_directory": {dbPathBehavior: dirIsFile},
"Error_on_existing_db_path_with_invalid_permissions": {dbPathBehavior: hasWrongPermission},
"Error_on_missing_parent_db_directory": {dbPathBehavior: parentDirDoesNotExists},

"Error_on_grpc_daemon_creation_failure": {socketPathBehavior: dirIsFile},

"Error_on_manager_creationg_failure": {cacheDBBehavior: hasWrongPermission},
"Error_on_manager_creationg_failure": {dbBehavior: hasWrongPermission},
}

for name, tc := range testCases {
Expand All @@ -156,27 +156,27 @@ func TestAppRunFailsOnComponentsCreationAndQuit(t *testing.T) {
require.NoError(t, err, "Setup: failed to write file")

var config daemon.DaemonConfig
switch tc.cachePathBehavior {
switch tc.dbPathBehavior {
case dirIsFile:
config.Paths.Cache = filePath
config.Paths.Database = filePath
case hasWrongPermission:
config.Paths.Cache = worldAccessDir
config.Paths.Database = worldAccessDir
case parentDirDoesNotExists:
config.Paths.Cache = filepath.Join(shortTmp, "not-exists", "cache")
config.Paths.Database = filepath.Join(shortTmp, "not-exists", "db")
}
switch tc.socketPathBehavior {
case dirIsFile:
config.Paths.Socket = filepath.Join(filePath, "mysocket")
default:
config.Paths.Socket = filepath.Join(shortTmp, "mysocket")
}
switch tc.cacheDBBehavior {
switch tc.dbBehavior {
case hasWrongPermission:
config.Paths.Cache = filepath.Join(shortTmp, "cache")
err := os.MkdirAll(config.Paths.Cache, 0700)
require.NoError(t, err, "Setup: could not create cache directory")
config.Paths.Database = filepath.Join(shortTmp, "db")
err := os.MkdirAll(config.Paths.Database, 0700)
require.NoError(t, err, "Setup: could not create database directory")
//nolint: gosec // This is a file with invalid permission for tests.
err = os.WriteFile(filepath.Join(config.Paths.Cache, db.Z_ForTests_DBName()), nil, 0644)
err = os.WriteFile(filepath.Join(config.Paths.Database, db.Z_ForTests_DBName()), nil, 0644)
require.NoError(t, err, "Setup: could not create database with invalid permissions")
}

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

require.Equal(t, 0, a.Config().Verbosity, "Default Verbosity")
require.Equal(t, consts.DefaultBrokersConfPath, a.Config().Paths.BrokersConf, "Default brokers configuration path")
require.Equal(t, consts.DefaultCacheDir, a.Config().Paths.Cache, "Default cache directory")
require.Equal(t, consts.DefaultDatabaseDir, a.Config().Paths.Database, "Default database directory")
require.Equal(t, "", a.Config().Paths.Socket, "No socket address as default")
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/authd/daemon/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func GenerateTestConfig(t *testing.T, origConf *daemonConfig) string {
if conf.Verbosity == 0 {
conf.Verbosity = 2
}
if conf.Paths.Cache == "" {
conf.Paths.Cache = t.TempDir()
if conf.Paths.Database == "" {
conf.Paths.Database = t.TempDir()
//nolint: gosec // This is a directory owned only by the current user for tests.
err := os.Chmod(conf.Paths.Cache, 0700)
require.NoError(t, err, "Setup: could not change permission on cache directory for tests")
err := os.Chmod(conf.Paths.Database, 0700)
require.NoError(t, err, "Setup: could not change permission on database directory for tests")
}
if conf.Paths.Socket == "" {
conf.Paths.Socket = filepath.Join(t.TempDir(), "authd.socket")
Expand Down
2 changes: 1 addition & 1 deletion cmd/authd/daemon/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ubuntu/authd/log"
)

func migrateOldCacheDir(oldPath, newPath string) error {
func migrateOlddbDir(oldPath, newPath string) error {
exists, err := fileutils.FileExists(oldPath)
if err != nil {
// Let's not fail if we can't access the old database dir, but log a warning
Expand Down
8 changes: 4 additions & 4 deletions cmd/authd/daemon/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ubuntu/authd/internal/fileutils"
)

func TestMigrateOldCacheDir(t *testing.T) {
func TestMigrateOlddbDir(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
Expand Down Expand Up @@ -56,8 +56,8 @@ func TestMigrateOldCacheDir(t *testing.T) {

oldParentDir := t.TempDir()
newParentDir := t.TempDir()
oldDir := filepath.Join(oldParentDir, "cache")
newDir := filepath.Join(newParentDir, "cache")
oldDir := filepath.Join(oldParentDir, "db")
newDir := filepath.Join(newParentDir, "db")
dbFilename := "authd.db"

if tc.oldDirExists {
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestMigrateOldCacheDir(t *testing.T) {
}()
}

err := migrateOldCacheDir(oldDir, newDir)
err := migrateOlddbDir(oldDir, newDir)
require.ErrorIs(t, err, tc.wantedErr)

if tc.wantOldDirExists {
Expand Down
8 changes: 4 additions & 4 deletions internal/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const (
// DefaultBrokersConfPath is the default configuration directory for the brokers.
DefaultBrokersConfPath = "/etc/authd/brokers.d/"

// OldCacheDir is the directory where the database was stored by default before 0.3.7.
OldCacheDir = "/var/cache/authd/"
// OlddbDir is the directory where the database was stored by default before 0.3.7.
OlddbDir = "/var/cache/authd/"

// DefaultCacheDir is the default directory for the database.
DefaultCacheDir = "/var/lib/authd/"
// DefaultDatabaseDir is the default directory for the database.
DefaultDatabaseDir = "/var/lib/authd/"

// ServiceName is the authd service name for health check purposes.
ServiceName = "com.ubuntu.authd"
Expand Down
8 changes: 4 additions & 4 deletions internal/services/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Manager struct {
}

// NewManager returns a new manager after creating all necessary items for our business logic.
func NewManager(ctx context.Context, cacheDir, brokersConfPath string, configuredBrokers []string, usersConfig users.Config) (m Manager, err error) {
func NewManager(ctx context.Context, dbDir, brokersConfPath string, configuredBrokers []string, usersConfig users.Config) (m Manager, err error) {
defer decorate.OnError(&err /*i18n.G(*/, "can't create authd object") //)

log.Debug(ctx, "Building authd object")
Expand All @@ -39,7 +39,7 @@ func NewManager(ctx context.Context, cacheDir, brokersConfPath string, configure
return m, err
}

userManager, err := users.NewManager(usersConfig, cacheDir)
userManager, err := users.NewManager(usersConfig, dbDir)
if err != nil {
return m, err
}
Expand Down Expand Up @@ -78,9 +78,9 @@ func (m Manager) RegisterGRPCServices(ctx context.Context) *grpc.Server {
return grpcServer
}

// stop stops the underlying cache.
// stop stops the underlying database.
func (m *Manager) stop() error {
log.Debug(context.TODO(), "Closing gRPC manager and cache")
log.Debug(context.TODO(), "Closing gRPC manager and database")

return m.userManager.Stop()
}
10 changes: 5 additions & 5 deletions internal/services/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ import (

func TestNewManager(t *testing.T) {
tests := map[string]struct {
cacheDir string
dbDir string

systemBusSocket string

wantErr bool
}{
"Successfully_create_the_manager": {},

"Error_when_can_not_create_cache": {cacheDir: "doesnotexist", wantErr: true},
"Error_when_can_not_create_db": {dbDir: "doesnotexist", wantErr: true},
"Error_when_can_not_create_broker_manager": {systemBusSocket: "doesnotexist", wantErr: true},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
if tc.cacheDir == "" {
tc.cacheDir = t.TempDir()
if tc.dbDir == "" {
tc.dbDir = t.TempDir()
}
if tc.systemBusSocket != "" {
t.Setenv("DBUS_SYSTEM_BUS_ADDRESS", tc.systemBusSocket)
}

m, err := services.NewManager(context.Background(), tc.cacheDir, t.TempDir(), nil, users.DefaultConfig)
m, err := services.NewManager(context.Background(), tc.dbDir, t.TempDir(), nil, users.DefaultConfig)
if tc.wantErr {
require.Error(t, err, "NewManager should have returned an error, but did not")
return
Expand Down
2 changes: 1 addition & 1 deletion internal/services/nss/nss.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s Service) GetPasswdByName(ctx context.Context, req *authd.GetPasswdByName
return nil, noDataFoundErrorToGRPCError(err)
}

// If the user is not found in the local cache, we check if it exists in at least one broker.
// If the user is not found in the database, we check if it exists in at least one broker.
pwent, err := s.userPreCheck(ctx, req.GetName())
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
Expand Down
34 changes: 17 additions & 17 deletions internal/services/nss/nss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ func TestGetPasswdByName(t *testing.T) {
}{
"Return_existing_user": {username: "user1"},

"Precheck_user_if_not_in_cache": {username: "user-pre-check", shouldPreCheck: true},
"Precheck_user_if_not_in_db": {username: "user-pre-check", shouldPreCheck: true},
"Prechecked_user_with_upper_cases_in_username_has_same_id_as_lower_case": {username: "User-Pre-Check", shouldPreCheck: true},

"Error_in_database_fetched_content": {username: "user1", sourceDB: "invalid.db.yaml", wantErr: true},
"Error_with_typed_GRPC_notfound_code_on_unexisting_user": {username: "does-not-exists", wantErr: true, wantErrNotExists: true},
"Error_on_missing_name": {wantErr: true},

"Error_in_database_fetched_content_does_not_trigger_precheck": {username: "user1", sourceDB: "invalid.db.yaml", shouldPreCheck: true, wantErr: true},
"Error_if_user_not_in_cache_and_precheck_is_disabled": {username: "user-pre-check", wantErr: true, wantErrNotExists: true},
"Error_if_user_not_in_cache_and_precheck_fails": {username: "does-not-exist", sourceDB: "empty.db.yaml", shouldPreCheck: true, wantErr: true, wantErrNotExists: true},
"Error_if_user_not_in_db_and_precheck_is_disabled": {username: "user-pre-check", wantErr: true, wantErrNotExists: true},
"Error_if_user_not_in_db_and_precheck_fails": {username: "does-not-exist", sourceDB: "empty.db.yaml", shouldPreCheck: true, wantErr: true, wantErrNotExists: true},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about gpasswd output here as it's already covered in the cache unit tests.
// We don't care about gpasswd output here as it's already covered in the db unit tests.
_ = localgroupstestutils.SetupGPasswdMock(t, filepath.Join("testdata", "empty.group"))

client := newNSSClient(t, tc.sourceDB, false)
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestGetPasswdByUID(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about gpasswd output here as it's already covered in the cache unit tests.
// We don't care about gpasswd output here as it's already covered in the db unit tests.
_ = localgroupstestutils.SetupGPasswdMock(t, filepath.Join("testdata", "empty.group"))

client := newNSSClient(t, tc.sourceDB, false)
Expand All @@ -120,7 +120,7 @@ func TestGetPasswdEntries(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about gpasswd output here as it's already covered in the cache unit tests.
// We don't care about gpasswd output here as it's already covered in the db unit tests.
_ = localgroupstestutils.SetupGPasswdMock(t, filepath.Join("testdata", "empty.group"))

client := newNSSClient(t, tc.sourceDB, false)
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestGetGroupByName(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about gpasswd output here as it's already covered in the cache unit tests.
// We don't care about gpasswd output here as it's already covered in the db unit tests.
_ = localgroupstestutils.SetupGPasswdMock(t, filepath.Join("testdata", "empty.group"))

client := newNSSClient(t, tc.sourceDB, false)
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestGetGroupByGID(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about gpasswd output here as it's already covered in the cache unit tests.
// We don't care about gpasswd output here as it's already covered in the db unit tests.
_ = localgroupstestutils.SetupGPasswdMock(t, filepath.Join("testdata", "empty.group"))

client := newNSSClient(t, tc.sourceDB, false)
Expand All @@ -200,7 +200,7 @@ func TestGetGroupEntries(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about gpasswd output here as it's already covered in the cache unit tests.
// We don't care about gpasswd output here as it's already covered in the db unit tests.
_ = localgroupstestutils.SetupGPasswdMock(t, filepath.Join("testdata", "empty.group"))

client := newNSSClient(t, tc.sourceDB, false)
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestGetShadowByName(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about gpasswd output here as it's already covered in the cache unit tests.
// We don't care about gpasswd output here as it's already covered in the db unit tests.
_ = localgroupstestutils.SetupGPasswdMock(t, filepath.Join("testdata", "empty.group"))

client := newNSSClient(t, tc.sourceDB, tc.currentUserNotRoot)
Expand All @@ -256,7 +256,7 @@ func TestGetShadowEntries(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// We don't care about gpasswd output here as it's already covered in the cache unit tests.
// We don't care about gpasswd output here as it's already covered in the db unit tests.
_ = localgroupstestutils.SetupGPasswdMock(t, filepath.Join("testdata", "empty.group"))

client := newNSSClient(t, tc.sourceDB, tc.currentUserNotRoot)
Expand All @@ -271,7 +271,7 @@ func TestMockgpasswd(t *testing.T) {
localgroupstestutils.Mockgpasswd(t)
}

// newNSSClient returns a new GRPC PAM client for tests with the provided sourceDB as its initial cache.
// newNSSClient returns a new GRPC PAM client for tests with the provided sourceDB as its initial database.
func newNSSClient(t *testing.T, sourceDB string, currentUserNotRoot bool) (client authd.NSSClient) {
t.Helper()

Expand Down Expand Up @@ -322,15 +322,15 @@ func enableCheckGlobalAccess(s nss.Service) grpc.UnaryServerInterceptor {
}
}

// newUserManagerForTests returns a cache object cleaned up with the test ends.
// newUserManagerForTests returns a user manager object cleaned up with the test ends.
func newUserManagerForTests(t *testing.T, sourceDB string) *users.Manager {
t.Helper()

cacheDir := t.TempDir()
dbDir := t.TempDir()
if sourceDB == "" {
sourceDB = "cache.db.yaml"
sourceDB = "default.db.yaml"
}
db.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", sourceDB), cacheDir)
db.Z_ForTests_CreateDBFromYAML(t, filepath.Join("testdata", sourceDB), dbDir)

managerOpts := []users.Option{
users.WithIDGenerator(&idgenerator.IDGeneratorMock{
Expand All @@ -339,7 +339,7 @@ func newUserManagerForTests(t *testing.T, sourceDB string) *users.Manager {
}),
}

m, err := users.NewManager(users.DefaultConfig, cacheDir, managerOpts...)
m, err := users.NewManager(users.DefaultConfig, dbDir, managerOpts...)
require.NoError(t, err, "Setup: could not create user manager")

t.Cleanup(func() { _ = m.Stop() })
Expand Down
Loading

0 comments on commit ed87b40

Please sign in to comment.