MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / manageClickHouseUser

Function manageClickHouseUser

programs/docker-init/docker-init.cpp:366–508  ·  view source on GitHub ↗

Write the user management XML to `/etc/clickhouse-server/users.d/default-user.xml`. Returns false if a user-requested setup (CLICKHOUSE_USER/PASSWORD/ACCESS_MANAGEMENT) is invalid — caller should treat this as fatal.

Source from the content-addressed store, hash-verified

364/// Returns false if a user-requested setup (CLICKHOUSE_USER/PASSWORD/ACCESS_MANAGEMENT)
365/// is invalid — caller should treat this as fatal.
366bool manageClickHouseUser(
367 const std::string & config_file,
368 const std::string & clickhouse_user,
369 const std::string & clickhouse_password,
370 const std::string & access_management,
371 bool skip_user_setup)
372{
373 if (skip_user_setup)
374 {
375 std::cerr << "docker-init: explicitly skip changing user 'default'\n";
376 return true;
377 }
378
379 const std::string users_d_dir = "/etc/clickhouse-server/users.d";
380 const std::string default_user_xml = users_d_dir + "/default-user.xml";
381
382 std::error_code ec;
383 fs::create_directories(users_d_dir, ec);
384
385 /// Detect whether the default user was customised via a mounted config file.
386 bool clickhouse_default_changed = false;
387 std::string users_xml_path = extractConfigValue(config_file, "user_directories.users_xml.path");
388 if (!users_xml_path.empty())
389 {
390 std::string abs_users_xml = (users_xml_path[0] == '/')
391 ? users_xml_path
392 : (fs::path(config_file).parent_path() / users_xml_path).string();
393
394 auto join = [](const std::vector<std::string> & v)
395 {
396 std::string s;
397 for (const auto & line : v)
398 s += line + "\n";
399 return s;
400 };
401
402 auto [c1, original] = captureCommand({
403 g_clickhouse_binary, "extract-from-config",
404 "--config-file", abs_users_xml,
405 "--key", "users.default", "--try",
406 });
407 auto [c2, processed] = captureCommand({
408 g_clickhouse_binary, "extract-from-config",
409 "--config-file", config_file,
410 "--users", "--key", "users.default", "--try",
411 });
412
413 if (c1 == 0 && c2 == 0 && join(original) != join(processed))
414 clickhouse_default_changed = true;
415 }
416
417 bool has_custom_user = !clickhouse_user.empty() && clickhouse_user != "default";
418 bool has_password = !clickhouse_password.empty();
419 bool has_access_mgmt = access_management != "0";
420
421 if (has_custom_user || has_password || has_access_mgmt)
422 {
423 if (access_management != "0" && access_management != "1")

Callers 1

Calls 10

extractConfigValueFunction · 0.85
captureCommandFunction · 0.85
parent_pathMethod · 0.80
isValidIdentifierFunction · 0.70
emptyMethod · 0.45
stringMethod · 0.45
findMethod · 0.45
appendMethod · 0.45
sizeMethod · 0.45
goodMethod · 0.45

Tested by

no test coverage detected