From 4c145b45d0fc94833eafc62290e02fead049f629 Mon Sep 17 00:00:00 2001 From: Jason Carey Date: Thu, 11 Apr 2019 11:38:09 -0400 Subject: SERVER-40580 add NonAuthTaskExecutor Spin up a non-authing task executor for the service context for use in local communication to mongodb wire protocol speaking services --- src/mongo/executor/SConscript | 27 ++++++++ src/mongo/executor/non_auth_task_executor.cpp | 75 ++++++++++++++++++++++ src/mongo/executor/non_auth_task_executor.h | 50 +++++++++++++++ .../non_auth_task_executor_integration_test.cpp | 71 ++++++++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 src/mongo/executor/non_auth_task_executor.cpp create mode 100644 src/mongo/executor/non_auth_task_executor.h create mode 100644 src/mongo/executor/non_auth_task_executor_integration_test.cpp (limited to 'src/mongo') diff --git a/src/mongo/executor/SConscript b/src/mongo/executor/SConscript index 364ff4772a4..2da4b4e18a6 100644 --- a/src/mongo/executor/SConscript +++ b/src/mongo/executor/SConscript @@ -300,3 +300,30 @@ env.CppUnitTest( 'thread_pool_task_executor_test_fixture', ], ) + +env.Library( + target='non_auth_task_executor', + source=[ + 'non_auth_task_executor.cpp', + ], + LIBDEPS=[ + 'task_executor_interface', + '$BUILD_DIR/mongo/executor/network_interface_factory', + '$BUILD_DIR/mongo/executor/network_interface_thread_pool', + '$BUILD_DIR/mongo/executor/thread_pool_task_executor', + '$BUILD_DIR/mongo/util/concurrency/thread_pool', + ], +) + +env.CppIntegrationTest( + target='non_auth_task_executor_integration_test', + source=[ + 'non_auth_task_executor_integration_test.cpp', + ], + LIBDEPS=[ + 'non_auth_task_executor', + '$BUILD_DIR/mongo/db/wire_version', + '$BUILD_DIR/mongo/transport/transport_layer_egress_init', + '$BUILD_DIR/mongo/util/version_impl', + ], +) diff --git a/src/mongo/executor/non_auth_task_executor.cpp b/src/mongo/executor/non_auth_task_executor.cpp new file mode 100644 index 00000000000..60471994a8d --- /dev/null +++ b/src/mongo/executor/non_auth_task_executor.cpp @@ -0,0 +1,75 @@ +/** + * Copyright (C) 2019-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * . + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kExecutor + +#include "mongo/platform/basic.h" + +#include "mongo/executor/non_auth_task_executor.h" + +#include "mongo/executor/network_interface_factory.h" +#include "mongo/executor/network_interface_thread_pool.h" +#include "mongo/executor/thread_pool_task_executor.h" + +namespace mongo { +namespace executor { + +namespace { + +const auto getExecutor = ServiceContext::declareDecoration>(); + +ServiceContext::ConstructorActionRegisterer nonAuthExecutorCAR{ + "NonAuthTaskExecutor", + [](ServiceContext* service) { + std::shared_ptr ni = + makeNetworkInterface("NonAuthExecutor", nullptr, nullptr, [] { + ConnectionPool::Options options; + options.skipAuthentication = true; + return options; + }()); + auto tp = std::make_unique(ni.get()); + auto exec = std::make_unique(std::move(tp), std::move(ni)); + + exec->startup(); + + getExecutor(service) = std::move(exec); + }, + [](ServiceContext* service) { + // Destruction implicitly performs the needed shutdown and join() + getExecutor(service).reset(); + }}; + +} // namespace + +TaskExecutor* getNonAuthTaskExecutor(ServiceContext* svc) { + return getExecutor(svc).get(); +} + +} // namespace executor +} // namespace mongo diff --git a/src/mongo/executor/non_auth_task_executor.h b/src/mongo/executor/non_auth_task_executor.h new file mode 100644 index 00000000000..30329dd6740 --- /dev/null +++ b/src/mongo/executor/non_auth_task_executor.h @@ -0,0 +1,50 @@ +/** + * Copyright (C) 2019-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * . + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +#include "mongo/executor/task_executor.h" + +namespace mongo { + +class ServiceContext; + +namespace executor { + +/** + * Provides access to a service context scoped task executor which does not perform authentication + * in its connection pool. + * + * This executor has a bounded thread pool and otherwise unspecified hooks. It should only be used + * for internal communication to local mongodb protocol speaking services. + */ +TaskExecutor* getNonAuthTaskExecutor(ServiceContext* svc); + +} // namespace executor +} // namespace mongo diff --git a/src/mongo/executor/non_auth_task_executor_integration_test.cpp b/src/mongo/executor/non_auth_task_executor_integration_test.cpp new file mode 100644 index 00000000000..b1d29190a04 --- /dev/null +++ b/src/mongo/executor/non_auth_task_executor_integration_test.cpp @@ -0,0 +1,71 @@ +/** + * Copyright (C) 2019-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * . + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/basic.h" + +#include "mongo/executor/non_auth_task_executor.h" + +#include "mongo/db/service_context.h" +#include "mongo/unittest/integration_test.h" +#include "mongo/unittest/unittest.h" +#include "mongo/util/future.h" + +namespace mongo { +namespace executor { +namespace { + +// Basic test that the non auth task executor is actually set up and works +TEST(NonAuthTaskExecutor, Basic) { + ServiceContext::UniqueServiceContext svc = ServiceContext::make(); + auto exec = getNonAuthTaskExecutor(svc.get()); + + RemoteCommandRequest rcr(unittest::getFixtureConnectionString().getServers().front(), + "admin", + BSON("isMaster" << 1), + BSONObj(), + nullptr); + + auto pf = makePromiseFuture(); + + ASSERT(exec->scheduleRemoteCommand(std::move(rcr), + [&](const TaskExecutor::RemoteCommandCallbackArgs& args) { + if (args.response.isOK()) { + pf.promise.emplaceValue(); + } else { + pf.promise.setError(args.response.status); + } + }) + .isOK()); + + ASSERT_OK(pf.future.getNoThrow()); +} + +} // namespace +} // namespace executor +} // namespace mongo -- cgit v1.2.1