Skip to content

Commit

Permalink
SYSDB: only monitor (and tests) should create cache files
Browse files Browse the repository at this point in the history
Everything else (providers, responders, tools) should only connect to.
  • Loading branch information
alexey-tikhonov committed Sep 13, 2024
1 parent cfffd6c commit c40f7c4
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/db/sysdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ struct sysdb_upgrade_ctx {

int sysdb_init_ext(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domains,
bool create_missing_cache,
struct sysdb_upgrade_ctx *upgrade_ctx);

/* used to initialize only one domain database.
Expand Down
14 changes: 11 additions & 3 deletions src/db/sysdb_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,12 @@ static int sysdb_timestamp_cache_connect(struct sysdb_ctx *sysdb,
int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domain,
const char *db_path,
bool create_missing_cache,
struct sysdb_dom_upgrade_ctx *upgrade_ctx,
struct sysdb_ctx **_ctx)
{
TALLOC_CTX *tmp_ctx = NULL;
bool ldb_file_missing;
struct sysdb_ctx *sysdb;
int ret;

Expand All @@ -799,6 +801,11 @@ int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
if (ret != EOK) {
goto done;
}
ldb_file_missing = (access(sysdb->ldb_file, F_OK) == -1 && errno == ENOENT);
if (ldb_file_missing && !create_missing_cache) {
ret = ENOENT;
goto done;
}
DEBUG(SSSDBG_FUNC_DATA,
"DB File for %s: %s\n", domain->name, sysdb->ldb_file);
if (sysdb->ldb_ts_file) {
Expand Down Expand Up @@ -833,11 +840,12 @@ int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
int sysdb_init(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domains)
{
return sysdb_init_ext(mem_ctx, domains, NULL);
return sysdb_init_ext(mem_ctx, domains, false, NULL);
}

int sysdb_init_ext(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domains,
bool create_missing_cache,
struct sysdb_upgrade_ctx *upgrade_ctx)
{
struct sss_domain_info *dom;
Expand Down Expand Up @@ -868,7 +876,7 @@ int sysdb_init_ext(TALLOC_CTX *mem_ctx,
dom_upgrade_ctx = NULL;
}

ret = sysdb_domain_init_internal(tmp_ctx, dom, DB_PATH,
ret = sysdb_domain_init_internal(tmp_ctx, dom, DB_PATH, create_missing_cache,
dom_upgrade_ctx, &sysdb);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE,
Expand All @@ -892,5 +900,5 @@ int sysdb_domain_init(TALLOC_CTX *mem_ctx,
struct sysdb_ctx **_ctx)
{
return sysdb_domain_init_internal(mem_ctx, domain,
db_path, NULL, _ctx);
db_path, false, NULL, _ctx);
}
1 change: 1 addition & 0 deletions src/db/sysdb_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ struct sysdb_dom_upgrade_ctx {
int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
struct sss_domain_info *domain,
const char *db_path,
bool create_missing_cache,
struct sysdb_dom_upgrade_ctx *upgrade_ctx,
struct sysdb_ctx **_ctx);

Expand Down
2 changes: 1 addition & 1 deletion src/monitor/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ static int monitor_process_init(struct mt_ctx *ctx)
}

db_up_ctx.cdb = ctx->cdb;
ret = sysdb_init_ext(tmp_ctx, ctx->domains, &db_up_ctx);
ret = sysdb_init_ext(tmp_ctx, ctx->domains, true, &db_up_ctx);
if (ret != EOK) {
DEBUG(SSSDBG_TRACE_FUNC,
"sysdb_init_ext() failed: '%s'\n", sss_strerror(ret));
Expand Down
2 changes: 1 addition & 1 deletion src/tests/auth-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static int setup_sysdb_tests(struct sysdb_test_ctx **ctx)
return ret;
}

ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES",
ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES",
TESTS_PATH, &test_ctx->domain);
if (ret != EOK) {
ck_abort_msg("Could not initialize connection to the sysdb (%d)", ret);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/cmocka/test_ldap_id_cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
"config/domain/FILES", "cache_credentials", val);
assert_int_equal(ret, EOK);

ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES",
ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES",
TESTS_PATH, &test_ctx->domain);
assert_int_equal(ret, EOK);

Expand Down
2 changes: 1 addition & 1 deletion src/tests/cmocka/test_sysdb_views.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
"config/domain/FILES", "cache_credentials", val);
assert_int_equal(ret, EOK);

ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES",
ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES",
TESTS_PATH, &test_ctx->domain);
assert_int_equal(ret, EOK);

Expand Down
6 changes: 6 additions & 0 deletions src/tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ struct sss_test_ctx *create_ev_test_ctx(TALLOC_CTX *mem_ctx);

void reset_ldb_errstrings(struct sss_domain_info *dom);

errno_t test_domain_init(TALLOC_CTX *mem_ctx,
struct confdb_ctx *cdb,
const char *domain_name,
const char *db_path,
struct sss_domain_info **_domain);

struct sss_test_ctx *
create_multidom_test_ctx(TALLOC_CTX *mem_ctx,
const char *tests_path,
Expand Down
32 changes: 30 additions & 2 deletions src/tests/common_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,34 @@ void reset_ldb_errstrings(struct sss_domain_info *dom)
}
}

errno_t test_domain_init(TALLOC_CTX *mem_ctx,
struct confdb_ctx *cdb,
const char *domain_name,
const char *db_path,
struct sss_domain_info **_domain)
{
/* This is a replacement of `sssd_domain_init()`
* that uses `sysdb_domain_init_internal()` instead of `sysdb_domain_init()`
* under the hood to let tests create sysdb cache files
*/
int ret;

ret = confdb_get_domain(cdb, domain_name, _domain);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "Error retrieving domain configuration.\n");
return ret;
}

ret = sysdb_domain_init_internal(mem_ctx, *_domain,
db_path, true, NULL, &(*_domain)->sysdb);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, "Error opening cache database.\n");
return ret;
}

return EOK;
}

static errno_t
mock_domain(TALLOC_CTX *mem_ctx,
struct confdb_ctx *cdb,
Expand All @@ -192,9 +220,9 @@ mock_domain(TALLOC_CTX *mem_ctx,
errno_t ret;

/* initialize sysdb */
ret = sssd_domain_init(mem_ctx, cdb, name, db_path, &domain);
ret = test_domain_init(mem_ctx, cdb, name, db_path, &domain);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "sssd_domain_init() of %s failed "
DEBUG(SSSDBG_CRIT_FAILURE, "test_domain_init() of %s failed "
"[%d]: %s\n", name, ret, sss_strerror(ret));
goto done;
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests/sysdb-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
return ret;
}

ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES",
ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES",
TESTS_PATH, &test_ctx->domain);
if (ret != EOK) {
ck_abort_msg("Could not initialize connection to the sysdb (%d)", ret);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/sysdb_ssh-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static int setup_sysdb_tests(struct sysdb_test_ctx **ctx)
return ret;
}

ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES",
ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES",
TESTS_PATH, &test_ctx->domain);
if (ret != EOK) {
ck_abort_msg("Could not initialize connection to the sysdb (%d)", ret);
Expand Down

0 comments on commit c40f7c4

Please sign in to comment.