Skip to content

Commit b4c5c8e

Browse files
andreazevedofacebook-github-bot
authored andcommitted
Add connection time acl check to standalone mcrouter
Summary: Add per-connection acl checker to mcrouter server. Reviewed By: callenrain Differential Revision: D13355499 fbshipit-source-id: 8ac8c5b68b325ce2315b86eef26ea00cadc86d9d
1 parent 4d98639 commit b4c5c8e

File tree

8 files changed

+160
-37
lines changed

8 files changed

+160
-37
lines changed

mcrouter/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ mcrouter_SOURCES = \
199199
Server-inl.h \
200200
Server.h \
201201
ServerOnRequest.h \
202+
StandaloneConfig.cpp \
203+
StandaloneConfig.h \
202204
StandaloneUtils.cpp \
203205
StandaloneUtils.h \
204206
standalone_options.h \

mcrouter/Server-inl.h

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/*
2-
* Copyright (c) Facebook, Inc.
3-
*
4-
* This source code is licensed under the MIT license found in the LICENSE
5-
* file in the root directory of this source tree.
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
63
*
4+
* This source code is licensed under the MIT license found in the LICENSE
5+
* file in the root directory of this source tree.
76
*/
87
#include <signal.h>
98

@@ -16,6 +15,7 @@
1615
#include "mcrouter/Proxy.h"
1716
#include "mcrouter/ProxyThread.h"
1817
#include "mcrouter/ServerOnRequest.h"
18+
#include "mcrouter/StandaloneConfig.h"
1919
#include "mcrouter/config.h"
2020
#include "mcrouter/lib/network/AsyncMcServer.h"
2121
#include "mcrouter/lib/network/AsyncMcServerWorker.h"
@@ -27,6 +27,26 @@ namespace mcrouter {
2727

2828
namespace detail {
2929

30+
inline std::function<void(McServerSession&)> getAclChecker(
31+
const McrouterOptions& opts,
32+
const McrouterStandaloneOptions& standaloneOpts) {
33+
if (standaloneOpts.acl_checker_enable) {
34+
try {
35+
return getConnectionAclChecker(
36+
standaloneOpts.server_ssl_service_identity,
37+
standaloneOpts.acl_checker_enforce);
38+
} catch (const std::exception& ex) {
39+
MC_LOG_FAILURE(
40+
opts,
41+
failure::Category::kSystemError,
42+
"Error creating acl checker: {}",
43+
ex.what());
44+
LOG(WARNING) << "Disabling acl checker on all threads.";
45+
}
46+
}
47+
return [](McServerSession&) {};
48+
}
49+
3050
template <class RouterInfo, template <class> class RequestHandler>
3151
void serverLoop(
3252
CarbonRouterInstance<RouterInfo>& router,
@@ -47,9 +67,24 @@ void serverLoop(
4767
*routerClient,
4868
standaloneOpts.retain_source_ip,
4969
standaloneOpts.enable_pass_through_mode));
50-
worker.setOnConnectionAccepted([proxy](McServerSession&) {
51-
proxy->stats().increment(num_client_connections_stat);
52-
});
70+
71+
worker.setOnConnectionAccepted(
72+
[proxy,
73+
aclChecker = getAclChecker(proxy->router().opts(), standaloneOpts)](
74+
McServerSession& session) mutable {
75+
proxy->stats().increment(num_client_connections_stat);
76+
try {
77+
aclChecker(session);
78+
} catch (const std::exception& ex) {
79+
MC_LOG_FAILURE(
80+
proxy->router().opts(),
81+
failure::Category::kSystemError,
82+
"Error running acl checker: {}",
83+
ex.what());
84+
LOG(WARNING) << "Disabling acl checker on this thread.";
85+
aclChecker = [](McServerSession&) {};
86+
}
87+
});
5388
worker.setOnConnectionCloseFinish(
5489
[proxy](McServerSession&, bool onAcceptedCalled) {
5590
if (onAcceptedCalled) {

mcrouter/StandaloneConfig.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the LICENSE
5+
* file in the root directory of this source tree.
6+
*/
7+
#include "StandaloneConfig.h"
8+
9+
#include <functional>
10+
#include <unordered_map>
11+
12+
namespace facebook {
13+
namespace memcache {
14+
namespace mcrouter {
15+
16+
void standalonePreInitFromCommandLineOpts(
17+
const std::unordered_map<std::string, std::string>& standaloneOptionsDict) {
18+
}
19+
20+
void standaloneInit(
21+
const McrouterOptions& opts,
22+
const McrouterStandaloneOptions& standaloneOpts) {}
23+
24+
void initStandaloneSSL() {}
25+
26+
void finalizeStandaloneOptions(McrouterStandaloneOptions& opts) {}
27+
28+
std::function<void(McServerSession&)> getConnectionAclChecker(
29+
const std::string& /* serviceIdentity */,
30+
bool /* enforce */) {
31+
return [](McServerSession&) {};
32+
}
33+
34+
} // namespace mcrouter
35+
} // namespace memcache
36+
} // namespace facebook

mcrouter/StandaloneConfig.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the LICENSE
5+
* file in the root directory of this source tree.
6+
*/
7+
#pragma once
8+
9+
#include <functional>
10+
#include <unordered_map>
11+
12+
namespace facebook {
13+
namespace memcache {
14+
15+
// forward declarations
16+
class McrouterOptions;
17+
class McServerSession;
18+
19+
namespace mcrouter {
20+
// forward declarations
21+
class McrouterStandaloneOptions;
22+
23+
void standalonePreInitFromCommandLineOpts(
24+
const std::unordered_map<std::string, std::string>& standaloneOptionsDict);
25+
26+
void standaloneInit(
27+
const McrouterOptions& opts,
28+
const McrouterStandaloneOptions& standaloneOpts);
29+
30+
void initStandaloneSSL();
31+
32+
void finalizeStandaloneOptions(McrouterStandaloneOptions& opts);
33+
34+
std::function<void(McServerSession&)> getConnectionAclChecker(
35+
const std::string& serviceIdentity,
36+
bool enforce);
37+
38+
} // namespace mcrouter
39+
} // namespace memcache
40+
} // namespace facebook

mcrouter/StandaloneUtils.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/*
2-
* Copyright (c) Facebook, Inc.
3-
*
4-
* This source code is licensed under the MIT license found in the LICENSE
5-
* file in the root directory of this source tree.
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
63
*
4+
* This source code is licensed under the MIT license found in the LICENSE
5+
* file in the root directory of this source tree.
76
*/
87
#include "StandaloneUtils.h"
98

@@ -24,6 +23,7 @@
2423
#include "mcrouter/McrouterLogFailure.h"
2524
#include "mcrouter/RouterRegistry.h"
2625
#include "mcrouter/Server.h"
26+
#include "mcrouter/StandaloneConfig.h"
2727
#include "mcrouter/config.h"
2828
#include "mcrouter/options.h"
2929
#include "mcrouter/standalone_options.h"
@@ -430,6 +430,9 @@ void setupStandaloneMcrouter(
430430
option);
431431
}
432432

433+
// finialize standalone options
434+
finalizeStandaloneOptions(standaloneOptions);
435+
433436
// init a few things.
434437
initStandaloneSSL();
435438
srand(time(nullptr) + getpid());

mcrouter/mcrouter_config.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/*
2-
* Copyright (c) 2014-present, Facebook, Inc.
3-
*
4-
* This source code is licensed under the MIT license found in the LICENSE
5-
* file in the root directory of this source tree.
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
63
*
4+
* This source code is licensed under the MIT license found in the LICENSE
5+
* file in the root directory of this source tree.
76
*/
87
#include <memory>
98

@@ -133,8 +132,6 @@ folly::dynamic readStaticJsonFile(folly::StringPiece file) {
133132
return folly::parseJson(contents);
134133
}
135134

136-
void initStandaloneSSL() {}
137-
138135
} // namespace mcrouter
139136
} // namespace memcache
140137
} // namespace facebook

mcrouter/mcrouter_config.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/*
2-
* Copyright (c) Facebook, Inc.
3-
*
4-
* This source code is licensed under the MIT license found in the LICENSE
5-
* file in the root directory of this source tree.
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
63
*
4+
* This source code is licensed under the MIT license found in the LICENSE
5+
* file in the root directory of this source tree.
76
*/
87
#pragma once
98

@@ -50,7 +49,6 @@ namespace mcrouter {
5049
class CarbonRouterInstanceBase;
5150
class ConfigApi;
5251
class McrouterLogger;
53-
class McrouterStandaloneOptions;
5452
struct FailoverContext;
5553
class ProxyBase;
5654
struct RequestLoggerContext;
@@ -107,13 +105,6 @@ std::unique_ptr<ConfigApi> createConfigApi(const McrouterOptions& opts);
107105

108106
std::string performOptionSubstitution(std::string str);
109107

110-
inline void standalonePreInitFromCommandLineOpts(
111-
const std::unordered_map<std::string, std::string>& st_option_dict) {}
112-
113-
inline void standaloneInit(
114-
const McrouterOptions& opts,
115-
const McrouterStandaloneOptions& standaloneOpts) {}
116-
117108
std::unique_ptr<McrouterLogger> createMcrouterLogger(
118109
CarbonRouterInstanceBase& router);
119110

@@ -153,8 +144,6 @@ std::string getBinPath(folly::StringPiece name);
153144

154145
void finalizeOptions(McrouterOptions& options);
155146

156-
void initStandaloneSSL();
157-
158147
/**
159148
* Reads a static json file. Do not monitor for changes.
160149
* May throw if there's an error while parsing file contents.

mcrouter/standalone_options_list.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ MCROUTER_OPTION_TOGGLE(
6767

6868
MCROUTER_OPTION_STRING(
6969
server_pem_cert_path,
70-
"",
70+
"", // this may get overwritten by finalizeOptions
7171
"server-pem-cert-path",
7272
no_short,
7373
"Path of pem-style server certificate for ssl.")
7474

7575
MCROUTER_OPTION_STRING(
7676
server_pem_key_path,
77-
"",
77+
"", // this may get overwritten by finalizeOptions
7878
"server-pem-key-path",
7979
no_short,
8080
"Path of pem-style server key for ssl.")
@@ -195,6 +195,27 @@ MCROUTER_OPTION_INTEGER(
195195
"use the zero copy optimization on TX."
196196
"If 0, the tcp zero copy optimization will not be applied.")
197197

198+
MCROUTER_OPTION_TOGGLE(
199+
acl_checker_enable,
200+
false,
201+
"acl-checker-enable",
202+
no_short,
203+
"If true, incoming requests are checked against the ACL.")
204+
205+
MCROUTER_OPTION_TOGGLE(
206+
acl_checker_enforce,
207+
false,
208+
"acl-checker-enforce",
209+
no_short,
210+
"If true, enforces the result of the ACL check.")
211+
212+
MCROUTER_OPTION_STRING(
213+
server_ssl_service_identity,
214+
"memcache",
215+
"server-ssl-service-identity",
216+
no_short,
217+
"If true, enforces the result of the ACL check.")
218+
198219
#ifdef ADDITIONAL_STANDALONE_OPTIONS_FILE
199220
#include ADDITIONAL_STANDALONE_OPTIONS_FILE
200221
#endif

0 commit comments

Comments
 (0)