Skip to content

Commit 0ac2d96

Browse files
Add flag --no-access-check to ServerMain (#2436)
When calling `ServerMain` with this new flag, privileged operations (like updates), can be issued without access token. This is useful for scenarios where security is not an issue or where access control is guaranteed by other means. In particular, this solves #2402
1 parent bb232e5 commit 0ac2d96

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

src/ServerMain.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int main(int argc, char** argv) {
4646

4747
std::string indexBasename;
4848
std::string accessToken;
49+
bool noAccessCheck = false;
4950
bool text = false;
5051
unsigned short port;
5152
NonNegative numSimultaneousQueries = 1;
@@ -70,6 +71,10 @@ int main(int argc, char** argv) {
7071
"The port on which HTTP requests are served (required).");
7172
add("access-token,a", po::value<std::string>(&accessToken)->default_value(""),
7273
"Access token for restricted API calls (default: no access).");
74+
add("no-access-check,n",
75+
po::bool_switch(&noAccessCheck)->default_value(false),
76+
"If set to true, no access-token check is performed for restricted API "
77+
"calls (default: false).");
7378
add("num-simultaneous-queries,j",
7479
po::value<NonNegative>(&numSimultaneousQueries)->default_value(1),
7580
"The number of queries that can be processed simultaneously.");
@@ -186,7 +191,7 @@ int main(int argc, char** argv) {
186191

187192
try {
188193
Server server(port, numSimultaneousQueries, memoryMaxSize,
189-
std::move(accessToken), !noPatterns);
194+
std::move(accessToken), noAccessCheck, !noPatterns);
190195
server.run(indexBasename, text, !noPatterns, !onlyPsoAndPosPermutations,
191196
persistUpdates);
192197
} catch (const std::exception& e) {

src/engine/Server.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ using ad_utility::MediaType;
4747
// __________________________________________________________________________
4848
Server::Server(unsigned short port, size_t numThreads,
4949
ad_utility::MemorySize maxMem, std::string accessToken,
50-
bool usePatternTrick)
50+
bool noAccessCheck, bool usePatternTrick)
5151
: numThreads_(numThreads),
5252
port_(port),
5353
accessToken_(std::move(accessToken)),
54+
noAccessCheck_(noAccessCheck),
5455
allocator_{ad_utility::makeAllocationMemoryLeftThreadsafeObject(maxMem),
5556
[this](ad_utility::MemorySize numMemoryToAllocate) {
5657
cache_.makeRoomAsMuchAsPossible(MAKE_ROOM_SLACK_FACTOR *
@@ -92,8 +93,13 @@ void Server::initialize(const std::string& indexBaseName, bool useText,
9293
allocator_, index_.numTriples().normalAndInternal_() *
9394
PERCENTAGE_OF_TRIPLES_FOR_SORT_ESTIMATE / 100);
9495

95-
AD_LOG_INFO << "Access token for restricted API calls is \"" << accessToken_
96-
<< "\"" << std::endl;
96+
if (noAccessCheck_) {
97+
AD_LOG_INFO << "No access token required for restricted API calls"
98+
<< std::endl;
99+
} else {
100+
AD_LOG_INFO << "Access token for restricted API calls is \"" << accessToken_
101+
<< "\"" << std::endl;
102+
}
97103
}
98104

99105
// _____________________________________________________________________________
@@ -1227,6 +1233,10 @@ CPP_template_def(typename Function,
12271233
// _____________________________________________________________________________
12281234
bool Server::checkAccessToken(
12291235
std::optional<std::string_view> accessToken) const {
1236+
if (noAccessCheck_) {
1237+
AD_LOG_DEBUG << "Skipping access check" << std::endl;
1238+
return true;
1239+
}
12301240
if (!accessToken) {
12311241
return false;
12321242
}

src/engine/Server.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Server {
4646
public:
4747
explicit Server(unsigned short port, size_t numThreads,
4848
ad_utility::MemorySize maxMem, std::string accessToken,
49-
bool usePatternTrick = true);
49+
bool noAccessCheck = false, bool usePatternTrick = true);
5050

5151
virtual ~Server() = default;
5252

@@ -80,6 +80,7 @@ class Server {
8080
const size_t numThreads_;
8181
unsigned short port_;
8282
std::string accessToken_;
83+
bool noAccessCheck_;
8384
QueryResultCache cache_;
8485
NamedResultCache namedResultCache_;
8586
ad_utility::AllocatorWithLimit<Id> allocator_;
@@ -299,6 +300,7 @@ class Server {
299300
/// formulated towards end users, it can be sent directly as the text of an
300301
/// HTTP error response.
301302
bool checkAccessToken(std::optional<std::string_view> accessToken) const;
303+
FRIEND_TEST(ServerTest, checkAccessToken);
302304

303305
/// Check if user-provided timeout is authorized with a valid access-token or
304306
/// lower than the server default. Return an empty optional and send a 403

test/ServerTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,15 @@ TEST(ServerTest, configurePinnedResultWithName) {
346346
// Verify qec was not modified when exception was thrown
347347
EXPECT_FALSE(qec->pinResultWithName().has_value());
348348
}
349+
350+
TEST(ServerTest, checkAccessToken) {
351+
Server server{4321, 1, ad_utility::MemorySize::megabytes(1), "accessToken"};
352+
EXPECT_TRUE(server.checkAccessToken("accessToken"));
353+
354+
AD_EXPECT_THROW_WITH_MESSAGE(
355+
server.checkAccessToken("invalidAccessToken"),
356+
testing::HasSubstr("Access token was provided but it was invalid"));
357+
358+
Server server2{1234, 1, ad_utility::MemorySize::megabytes(1), "", true};
359+
EXPECT_TRUE(server2.checkAccessToken(std::nullopt));
360+
}

0 commit comments

Comments
 (0)