Fix init file startup in community edition (#974)

* Fix init file startup in community edition

* Add possibility to build binary without MG_ENTERPRISE

* Added trace spdlog for when init file is not present

* Add gqlalchemy and unit tests

* Add init data files which correspond to the right directory by the github actions
This commit is contained in:
Josipmrden 2023-06-20 17:54:50 +02:00 committed by GitHub
parent eb22edfd35
commit df95775222
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 135 additions and 34 deletions

View File

@ -16,10 +16,7 @@ add_subdirectory(slk)
add_subdirectory(rpc)
add_subdirectory(license)
add_subdirectory(auth)
if(MG_ENTERPRISE)
add_subdirectory(audit)
endif()
add_subdirectory(audit)
string(TOLOWER ${CMAKE_BUILD_TYPE} lower_build_type)
@ -36,12 +33,7 @@ set(mg_single_node_v2_sources
)
set(mg_single_node_v2_libs stdc++fs Threads::Threads
mg-telemetry mg-query mg-communication mg-memory mg-utils mg-auth mg-license mg-settings mg-glue)
if(MG_ENTERPRISE)
# These are enterprise subsystems
set(mg_single_node_v2_libs ${mg_single_node_v2_libs} mg-audit)
endif()
mg-telemetry mg-query mg-communication mg-memory mg-utils mg-auth mg-license mg-settings mg-glue mg-audit)
# memgraph main executable
add_executable(memgraph ${mg_single_node_v2_sources})

View File

@ -34,6 +34,7 @@
#include <spdlog/sinks/dist_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include "audit/log.hpp"
#include "auth/models.hpp"
#include "communication/bolt/v1/constants.hpp"
#include "communication/http/server.hpp"
@ -98,10 +99,6 @@
#include "auth/auth.hpp"
#include "glue/auth.hpp"
#ifdef MG_ENTERPRISE
#include "audit/log.hpp"
#endif
constexpr const char *kMgUser = "MEMGRAPH_USER";
constexpr const char *kMgPassword = "MEMGRAPH_PASSWORD";
constexpr const char *kMgPassfile = "MEMGRAPH_PASSFILE";
@ -473,31 +470,30 @@ struct SessionData {
DEFINE_string(auth_user_or_role_name_regex, memgraph::glue::kDefaultUserRoleRegex.data(),
"Set to the regular expression that each user or role name must fulfill.");
void InitFromCypherlFile(memgraph::query::InterpreterContext &ctx, std::string cypherl_file_path
#ifdef MG_ENTERPRISE
,
memgraph::audit::Log *audit_log
#endif
) {
void InitFromCypherlFile(memgraph::query::InterpreterContext &ctx, std::string cypherl_file_path,
memgraph::audit::Log *audit_log = nullptr) {
memgraph::query::Interpreter interpreter(&ctx);
std::ifstream file(cypherl_file_path);
if (file.is_open()) {
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);
#ifdef MG_ENTERPRISE
if (memgraph::license::global_license_checker.IsEnterpriseValidFast()) {
audit_log->Record("", "", line, {});
}
#endif
if (!file.is_open()) {
spdlog::trace("Could not find init file {}", cypherl_file_path);
return;
}
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);
if (audit_log) {
audit_log->Record("", "", line, {});
}
}
file.close();
}
file.close();
}
namespace memgraph::metrics {
@ -945,10 +941,12 @@ int main(int argc, char **argv) {
interpreter_context.auth_checker = &auth_checker;
if (!FLAGS_init_file.empty()) {
spdlog::info("Running init file.");
spdlog::info("Running init file...");
#ifdef MG_ENTERPRISE
if (memgraph::license::global_license_checker.IsEnterpriseValidFast()) {
InitFromCypherlFile(interpreter_context, FLAGS_init_file, &audit_log);
} else {
InitFromCypherlFile(interpreter_context, FLAGS_init_file);
}
#else
InitFromCypherlFile(interpreter_context, FLAGS_init_file);
@ -1095,6 +1093,8 @@ int main(int argc, char **argv) {
#ifdef MG_ENTERPRISE
if (memgraph::license::global_license_checker.IsEnterpriseValidFast()) {
InitFromCypherlFile(interpreter_context, FLAGS_init_data_file, &audit_log);
} else {
InitFromCypherlFile(interpreter_context, FLAGS_init_data_file);
}
#else
InitFromCypherlFile(interpreter_context, FLAGS_init_data_file);

View File

@ -29,6 +29,14 @@ function(copy_e2e_cpp_files TARGET_PREFIX FILE_NAME)
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE_NAME})
endfunction()
function(copy_e2e_files TARGET_PREFIX FILE_NAME)
add_custom_target(memgraph__e2e__${TARGET_PREFIX}__${FILE_NAME} ALL
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/${FILE_NAME}
${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE_NAME})
endfunction()
add_subdirectory(fine_grained_access)
add_subdirectory(server)
add_subdirectory(replication)
@ -47,6 +55,7 @@ add_subdirectory(python_query_modules_reloading)
add_subdirectory(analyze_graph)
add_subdirectory(transaction_queue)
add_subdirectory(mock_api)
add_subdirectory(init_file_flags)
copy_e2e_python_files(pytest_runner pytest_runner.sh "")
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/memgraph-selfsigned.crt DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

View File

@ -0,0 +1,12 @@
function(copy_init_file_flags_e2e_python_files FILE_NAME)
copy_e2e_files(init_file_flags ${FILE_NAME})
endfunction()
function(copy_init_file_flags_e2e_files FILE_NAME)
copy_e2e_files(init_file_flags ${FILE_NAME})
endfunction()
copy_init_file_flags_e2e_python_files(init_file_setup.py)
copy_init_file_flags_e2e_python_files(init_data_file_setup.py)
copy_init_file_flags_e2e_files(init_file.cypherl)

View File

@ -0,0 +1,27 @@
# 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.
import sys
import pytest
from gqlalchemy import Memgraph
def test_given_init_data_file_when_memgraph_started_then_node_is_created():
mg = Memgraph("localhost", 7687)
result = next(mg.execute_and_fetch("MATCH (n) RETURN count(n) AS cnt"))["cnt"]
assert result == 1
if __name__ == "__main__":
sys.exit(pytest.main([__file__, "-rA"]))

View File

@ -0,0 +1 @@
CREATE (n);

View File

@ -0,0 +1,27 @@
# 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.
import sys
import pytest
from gqlalchemy import Memgraph
def test_given_init_file_when_memgraph_started_then_node_is_created():
mg = Memgraph("localhost", 7687)
result = next(mg.execute_and_fetch("MATCH (n) RETURN count(n) AS cnt"))["cnt"]
assert result == 1
if __name__ == "__main__":
sys.exit(pytest.main([__file__, "-rA"]))

View File

@ -0,0 +1,32 @@
init_file_cluster: &init_file_cluster
cluster:
main:
args: [
"--bolt-port", "7687",
"--log-level=TRACE",
"--init-file=init_file_flags/init_file.cypherl"
]
log_file: "init-file-flags-e2e.log"
validation_queries: []
init_data_file_cluster: &init_data_file_cluster
cluster:
main:
args: [
"--bolt-port", "7687",
"--log-level=TRACE",
"--init-data-file=init_file_flags/init_file.cypherl"
]
log_file: "init-data-file-flags-e2e.log"
validation_queries: []
workloads:
- name: "Init file flags"
binary: "tests/e2e/pytest_runner.sh"
args: ["init_file_flags/init_file_setup.py"]
<<: *init_file_cluster
- name: "Init data file flags"
binary: "tests/e2e/pytest_runner.sh"
args: ["init_file_flags/init_data_file_setup.py"]
<<: *init_data_file_cluster

View File

@ -16,6 +16,7 @@ PIP_DEPS=(
"pyyaml==5.4.1"
"six==1.15.0"
"networkx==2.4"
"gqlalchemy==1.3.3"
)
cd "$DIR"