Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ common --enable_bzlmod
build --spawn_strategy=local

# Define versions
build --define=DAS_VERSION=1.2.3
build --define=DAS_VERSION=1.2.4

# Enable debugging symbols for development
build:debug --compilation_mode=dbg
Expand Down
1 change: 1 addition & 0 deletions src/atomdb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ cc_library(
":key_sensitive_atomdb",
"//atomdb/auth:auth_lib",
"//atomdb/inmemorydb:inmemorydb_api_types",
"//atomdb/redis_mongodb:redis_mongodb_api_types",
"//commons:commons_lib",
"//commons/atoms:atoms_lib",
"//hasher:hasher_lib",
Expand Down
76 changes: 60 additions & 16 deletions src/atomdb/ProtectedAtomDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Link.h"
#include "Logger.h"
#include "Node.h"
#include "RedisMongoDBAPITypes.h"
#include "Utils.h"

using namespace std;
Expand Down Expand Up @@ -42,6 +43,7 @@ shared_ptr<ProtectedAtomDB> ProtectedAtomDB::wrap(shared_ptr<AtomDB> atomdb) {
// Public methods

shared_ptr<Atom> ProtectedAtomDB::get_atom(const string& handle, shared_ptr<Keychain> keychain) {
if (!Hasher::is_atom_handle(handle)) return nullptr;
auto atom = this->backend->get_atom(handle);
if (this->authorize_read(atom, keychain)) {
return atom;
Expand Down Expand Up @@ -104,9 +106,51 @@ vector<shared_ptr<Atom>> ProtectedAtomDB::get_matching_atoms(bool is_toplevel,

shared_ptr<atomdb_api_types::HandleSet> ProtectedAtomDB::query_for_pattern(
const LinkSchema& link_schema, shared_ptr<Keychain> keychain) {
return this->filter_handle_set(this->try_get_public_key(keychain), [this, &link_schema] {
return this->backend->query_for_pattern(link_schema);
});
auto public_key = this->try_get_public_key(keychain);
if (!public_key) {
LOG_DEBUG("[ProtectedAtomDB(" << this->uid_ << ")] : skipped, no public key");
return make_shared<atomdb_api_types::HandleSetInMemory>();
}

auto candidate_handle_set = this->backend->query_for_pattern(link_schema);
if (candidate_handle_set == nullptr) {
LOG_DEBUG("[ProtectedAtomDB(" << this->uid_ << ")] : backend returned nullptr");
return make_shared<atomdb_api_types::HandleSetInMemory>();
}

if (this->manifest->is_unrestricted(*public_key)) {
LOG_DEBUG("[ProtectedAtomDB(" << this->uid_ << ")] : unrestricted, skipping authorization");
return candidate_handle_set;
}

auto it = candidate_handle_set->get_iterator();
vector<string> handles;
while (true) {
auto redis_it = dynamic_pointer_cast<atomdb_api_types::HandleSetRedisIterator>(it);
char* handle_cstr = redis_it ? redis_it->next_candidate() : it->next();
if (!handle_cstr) break;
string handle(handle_cstr);
handles.push_back(handle);
}

auto atoms = this->get_atoms(handles, keychain);
auto authorized_handle_set = make_shared<atomdb_api_types::HandleSetInMemory>();
for (auto atom : atoms) {
auto link = dynamic_pointer_cast<Link>(atom);
if (link == nullptr) continue;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Assignment assignment;
if (const_cast<LinkSchema&>(link_schema).match(*link, assignment, *this->backend)) {
authorized_handle_set->add_handle(
atom->handle(),
candidate_handle_set->get_metta_expressions_by_handle(atom->handle()),
assignment);
}
}

LOG_INFO("[ProtectedAtomDB(" << this->uid_ << ")] : " << candidate_handle_set->size()
<< " candidates -> " << authorized_handle_set->size() << " authorized");

return authorized_handle_set;
}

shared_ptr<atomdb_api_types::HandleList> ProtectedAtomDB::query_for_targets(
Expand Down Expand Up @@ -429,22 +473,22 @@ shared_ptr<atomdb_api_types::HandleSet> ProtectedAtomDB::filter_handle_set(
}

LOG_DEBUG("[ProtectedAtomDB(" << this->uid_ << ")] filter_handle_set: querying backend");
auto original_handle_set = query();
auto candidate_handle_set = query();
LOG_DEBUG("[ProtectedAtomDB(" << this->uid_ << ")] filter_handle_set: backend returned "
<< (original_handle_set ? original_handle_set->size() : 0)
<< (candidate_handle_set ? candidate_handle_set->size() : 0)
<< " handles");

if (original_handle_set == nullptr) {
if (candidate_handle_set == nullptr) {
return make_shared<atomdb_api_types::HandleSetInMemory>();
}

if (this->manifest->is_unrestricted(*public_key)) {
LOG_DEBUG("[ProtectedAtomDB(" << this->uid_
<< ")] filter_handle_set: unrestricted, skipping authorization");
return original_handle_set;
return candidate_handle_set;
}

auto it = original_handle_set->get_iterator();
auto it = candidate_handle_set->get_iterator();
auto authorized_handle_set = make_shared<atomdb_api_types::HandleSetInMemory>();

while (true) {
Expand All @@ -455,36 +499,36 @@ shared_ptr<atomdb_api_types::HandleSet> ProtectedAtomDB::filter_handle_set(
if (this->can_read(handle, *public_key)) {
authorized_handle_set->add_handle(
handle,
original_handle_set->get_metta_expressions_by_handle(handle),
original_handle_set->get_assignments_by_handle(handle));
candidate_handle_set->get_metta_expressions_by_handle(handle),
candidate_handle_set->get_assignments_by_handle(handle));
}
}

LOG_DEBUG("[ProtectedAtomDB(" << this->uid_
<< ")] filter_handle_set: " << original_handle_set->size() << " -> "
<< ")] filter_handle_set: " << candidate_handle_set->size() << " -> "
<< authorized_handle_set->size() << " authorized");

return authorized_handle_set;
}

set<string> ProtectedAtomDB::filter_handles(const set<string>& original_handles,
set<string> ProtectedAtomDB::filter_handles(const set<string>& candidate_handles,
const string& public_key) {
if (this->manifest->is_unrestricted(public_key)) {
return original_handles;
return candidate_handles;
}
set<string> authorized_handles;
for (const auto& handle : original_handles) {
for (const auto& handle : candidate_handles) {
if (this->can_read(handle, public_key)) {
authorized_handles.insert(handle);
}
}
return authorized_handles;
}

vector<shared_ptr<Atom>> ProtectedAtomDB::filter_atoms(const vector<shared_ptr<Atom>>& original_atoms,
vector<shared_ptr<Atom>> ProtectedAtomDB::filter_atoms(const vector<shared_ptr<Atom>>& candidate_atoms,
const string& public_key) {
vector<shared_ptr<Atom>> authorized_atoms;
for (const auto& atom : original_atoms) {
for (const auto& atom : candidate_atoms) {
if (this->can_read(atom, public_key)) {
authorized_atoms.push_back(atom);
}
Expand Down
4 changes: 2 additions & 2 deletions src/atomdb/redis_mongodb/RedisMongoDBAPITypes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ HandleSetRedisIterator::HandleSetRedisIterator(HandleSetRedis* handle_set) {

HandleSetRedisIterator::~HandleSetRedisIterator(){};

char* HandleSetRedisIterator::_next() {
char* HandleSetRedisIterator::next_candidate() {
if (this->outer_idx >= this->handle_set->replies.size()) {
// No more elements, return NULL.
return NULL;
Expand Down Expand Up @@ -110,7 +110,7 @@ char* HandleSetRedisIterator::next() {
RAISE_ERROR("Non-null link_schema requires a decoder to be matched against handles");
return NULL;
}
while ((candidate = _next()) != NULL) {
while ((candidate = next_candidate()) != NULL) {
handle = string(candidate);
if ((this->handle_set->link_schema == nullptr) ||
this->handle_set->link_schema->match(handle, assignment, *decoder)) {
Expand Down
7 changes: 6 additions & 1 deletion src/atomdb/redis_mongodb/RedisMongoDBAPITypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ class HandleSetRedisIterator : public HandleSetIterator {
HandleSetRedisIterator(HandleSetRedis* handle_set);
~HandleSetRedisIterator();

/**
* @brief Get the next handle in the set, whithout checking if it matches the link schema.
*
* @return The next handle in the set, or NULL if there are no more handles.
*/
char* next_candidate();
char* next();

private:
char* _next();
HandleSetRedis* handle_set;
unsigned int outer_idx;
unsigned int inner_idx;
Expand Down
7 changes: 7 additions & 0 deletions src/tests/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,13 @@ cc_test(
"-Iexternal/gtest/googletest/include",
"-Iexternal/gtest/googletest",
],
linkopts = [
"-L/usr/local/lib",
"-lhiredis_cluster",
"-lhiredis",
"-lmongocxx",
"-lbsoncxx",
],
linkstatic = 1,
deps = [
"//atomdb:protected_atomdb",
Expand Down
66 changes: 66 additions & 0 deletions src/tests/cpp/protected_atomdb_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,72 @@ TEST(ProtectedAtomDBTest, NestedRelatedGrantDoesNotImplyInnerSimilarity) {
.empty());
}

TEST(ProtectedAtomDBTest, QueryForPatternStoresSchemaAssignments) {
shared_ptr<ProtectedRedisMongo> protected_atomdb = make_shared<ProtectedRedisMongo>(true);
protected_atomdb->grant_link_template(PKSimilarityHuman, similarity_human_tokens());
auto similarity_human_keys = protected_atomdb->keys(PKSimilarityHuman);
Animals animals;

LinkSchema similarity_human_schema(similarity_human_tokens());
auto result =
protected_atomdb->db->query_for_pattern(similarity_human_schema, similarity_human_keys);

auto monkey = result->get_assignments_by_handle(animals.similarity_human_monkey);
auto chimp = result->get_assignments_by_handle(animals.similarity_human_chimp);
auto ent = result->get_assignments_by_handle(animals.similarity_human_ent);
EXPECT_EQ(monkey.variable_count(), 1u);
EXPECT_EQ(monkey.get("V"), animals.monkey);
EXPECT_EQ(chimp.variable_count(), 1u);
EXPECT_EQ(chimp.get("V"), animals.chimp);
EXPECT_EQ(ent.variable_count(), 1u);
EXPECT_EQ(ent.get("V"), animals.ent);
EXPECT_EQ(result->get_assignments_by_handle(animals.similarity_snake_vine).variable_count(), 0u);
}

TEST(ProtectedAtomDBTest, QueryForPatternDropsReadableCandidatesThatFailTheSchema) {
auto backend = make_shared<AccessDocumentBackend>("pk");
string similarity = backend->add_node(new Node("Symbol", "Similarity"));
string human = backend->add_node(new Node("Symbol", "\"human\""));
string monkey = backend->add_node(new Node("Symbol", "\"monkey\""));
string chimp = backend->add_node(new Node("Symbol", "\"chimp\""));
string matching = backend->add_link(new Link("Expression", {similarity, human, monkey}));
string non_matching = backend->add_link(new Link("Expression", {similarity, chimp, monkey}));

LinkSchema schema(similarity_human_tokens());
backend->add_pattern(schema.handle(), matching);
backend->add_pattern(schema.handle(), non_matching);

ProtectedAtomDB db(backend);
auto result = db.query_for_pattern(schema, make_keychain(db.get_uid(), "pk"));

EXPECT_EQ(handles_from_set(result), set<string>({matching}));
auto assignment = result->get_assignments_by_handle(matching);
EXPECT_EQ(assignment.variable_count(), 1u);
EXPECT_EQ(assignment.get("V"), monkey);
EXPECT_EQ(result->get_assignments_by_handle(non_matching).variable_count(), 0u);
}

TEST(ProtectedAtomDBTest, UnrestrictedQueryForPatternMatchesBackend) {
shared_ptr<ProtectedRedisMongo> protected_atomdb = make_shared<ProtectedRedisMongo>(true);
protected_atomdb->grant_full_access(PKAdmin);
auto admin_keys = protected_atomdb->keys(PKAdmin);
LinkSchema similarity_human_schema(similarity_human_tokens());

auto backend_result = protected_atomdb->backend->query_for_pattern(similarity_human_schema);
auto unrestricted_result =
protected_atomdb->db->query_for_pattern(similarity_human_schema, admin_keys);

auto backend_handles = handles_from_set(backend_result);
auto unrestricted_handles = handles_from_set(unrestricted_result);
ASSERT_FALSE(backend_handles.empty());
EXPECT_EQ(unrestricted_handles, backend_handles);

for (const auto& handle : backend_handles) {
EXPECT_EQ(unrestricted_result->get_assignments_by_handle(handle),
backend_result->get_assignments_by_handle(handle));
}
}

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
Utils::init_random(0);
Expand Down