Fix restarts when using init-file flag (#1465)

This commit is contained in:
Andi 2023-11-24 13:11:47 +01:00 committed by GitHub
parent 70db2fca56
commit 7f5a55f1b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 129 additions and 30 deletions

View File

@ -65,10 +65,13 @@ void InitFromCypherlFile(memgraph::query::InterpreterContext &ctx, memgraph::dbm
std::string line;
while (std::getline(file, line)) {
if (!line.empty()) {
auto results = interpreter.Prepare(line, {}, {});
memgraph::query::DiscardValueResultStream stream;
interpreter.Pull(&stream, {}, results.qid);
try {
auto results = interpreter.Prepare(line, {}, {});
memgraph::query::DiscardValueResultStream stream;
interpreter.Pull(&stream, {}, results.qid);
} catch (const memgraph::query::UserAlreadyExistsException &e) {
spdlog::warn("{} The rest of the init-file will be run.", e.what());
}
if (audit_log) {
audit_log->Record("", "", line, {}, memgraph::dbms::kDefaultDB);
}

View File

@ -126,6 +126,12 @@ class InfoInMulticommandTxException : public QueryException {
SPECIALIZE_GET_EXCEPTION_NAME(InfoInMulticommandTxException)
};
class UserAlreadyExistsException : public QueryException {
public:
using QueryException::QueryException;
SPECIALIZE_GET_EXCEPTION_NAME(UserAlreadyExistsException)
};
/**
* An exception for an illegal operation that can not be detected
* before the query starts executing over data.

View File

@ -478,7 +478,7 @@ Callback HandleAuthQuery(AuthQuery *auth_query, InterpreterContext *interpreter_
MG_ASSERT(password.IsString() || password.IsNull());
if (!auth->CreateUser(username, password.IsString() ? std::make_optional(std::string(password.ValueString()))
: std::nullopt)) {
throw QueryRuntimeException("User '{}' already exists.", username);
throw UserAlreadyExistsException("User '{}' already exists.", username);
}
// If the license is not valid we create users with admin access

View File

@ -1,38 +1,14 @@
# telemetry test binaries
add_subdirectory(telemetry)
# ssl test binaries
add_subdirectory(ssl)
# transactions test binaries
add_subdirectory(transactions)
# auth test binaries
add_subdirectory(auth)
# lba test binaries
add_subdirectory(fine_grained_access)
# audit test binaries
add_subdirectory(audit)
# ldap test binaries
add_subdirectory(ldap)
# mg_import_csv test binaries
add_subdirectory(mg_import_csv)
# license_check test binaries
add_subdirectory(license_info)
#environment variable check binaries
add_subdirectory(env_variable_check)
#flag check binaries
add_subdirectory(flag_check)
#storage mode binaries
add_subdirectory(storage_mode)
#run time settings binaries
add_subdirectory(run_time_settings)
add_subdirectory(init_file)

View File

@ -0,0 +1,6 @@
set(target_name memgraph__integration__init_file)
set(tester_target_name ${target_name}__tester)
add_executable(${tester_target_name} tester.cpp)
set_target_properties(${tester_target_name} PROPERTIES OUTPUT_NAME tester)
target_link_libraries(${tester_target_name} mg-communication)

View File

@ -0,0 +1 @@
CREATE USER memgraph1 IDENTIFIED BY '1234';

View File

@ -0,0 +1,60 @@
import argparse
import os
import subprocess
import sys
import tempfile
import time
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
PROJECT_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "..", "..", ".."))
BUILD_DIR = os.path.join(PROJECT_DIR, "build")
INIT_FILE = os.path.join(SCRIPT_DIR, "auth.cypherl")
SIGNAL_SIGTERM = 15
def wait_for_server(port, delay=0.1):
cmd = ["nc", "-z", "-w", "1", "127.0.0.1", str(port)]
while subprocess.call(cmd) != 0:
time.sleep(0.01)
time.sleep(delay)
def prepare_memgraph(memgraph_args):
memgraph = subprocess.Popen(list(map(str, memgraph_args)))
time.sleep(0.1)
assert memgraph.poll() is None, "Memgraph process died prematurely!"
wait_for_server(7687)
return memgraph
def terminate_memgraph(memgraph):
pid = memgraph.pid
try:
os.kill(pid, SIGNAL_SIGTERM)
except os.OSError:
assert False, "Memgraph process didn't exit cleanly!"
time.sleep(1)
def execute_test_restart_memgraph_with_init_file(memgraph_binary: str, tester_binary: str) -> None:
storage_directory = tempfile.TemporaryDirectory()
tester_args = [tester_binary, "--username", "memgraph1", "--password", "1234"]
memgraph = prepare_memgraph([memgraph_binary, "--data-directory", storage_directory.name, "--init-file", INIT_FILE])
subprocess.run(tester_args, stdout=subprocess.PIPE, check=True).check_returncode()
terminate_memgraph(memgraph)
memgraph = prepare_memgraph([memgraph_binary, "--data-directory", storage_directory.name, "--init-file", INIT_FILE])
subprocess.run(tester_args, stdout=subprocess.PIPE, check=True).check_returncode()
terminate_memgraph(memgraph)
if __name__ == "__main__":
memgraph_binary = os.path.join(PROJECT_DIR, "build", "memgraph")
tester_binary = os.path.join(BUILD_DIR, "tests", "integration", "init_file", "tester")
parser = argparse.ArgumentParser()
parser.add_argument("--memgraph", default=memgraph_binary)
parser.add_argument("--tester", default=tester_binary)
args = parser.parse_args()
execute_test_restart_memgraph_with_init_file(args.memgraph, args.tester)
sys.exit(0)

View File

@ -0,0 +1,47 @@
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <gflags/gflags.h>
#include "communication/bolt/client.hpp"
#include "io/network/endpoint.hpp"
#include "io/network/utils.hpp"
#include "utils/logging.hpp"
DEFINE_string(address, "127.0.0.1", "Server address");
DEFINE_int32(port, 7687, "Server port");
DEFINE_string(username, "", "Username for the database");
DEFINE_string(password, "", "Password for the database");
DEFINE_bool(use_ssl, false, "Set to true to connect with SSL to the server.");
// NOLINTNEXTLINE(bugprone-exception-escape)
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
memgraph::logging::RedirectToStderr();
memgraph::communication::SSLInit sslInit;
memgraph::io::network::Endpoint endpoint(memgraph::io::network::ResolveHostname(FLAGS_address), FLAGS_port);
memgraph::communication::ClientContext context(FLAGS_use_ssl);
memgraph::communication::bolt::Client client(context);
client.Connect(endpoint, FLAGS_username, FLAGS_password);
auto ret = client.Execute("SHOW USERS", {});
auto size = ret.records.size();
MG_ASSERT(size == 1, "Too much users returned for SHOW USERA (got {}, expected 1)!", size);
auto row0_size = ret.records[0].size();
MG_ASSERT(row0_size == 1, "Too much entries in query dump row (got {}, expected 1)!", row0_size);
auto user = ret.records[0][0].ValueString();
MG_ASSERT(user == "memgraph1", "Unexpected user returned for SHOW USERS (got {}, expected memgraph)!", user);
return 0;
}