-
Notifications
You must be signed in to change notification settings - Fork 10
[#1177] Add authorization system with in-memory manifest, authorizer, and MongoDB persistence #1229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marcocapozzoli
wants to merge
23
commits into
masc/1177-atomdb-auth-b
Choose a base branch
from
masc/1177-atomdb-auth-c
base: masc/1177-atomdb-auth-b
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
31181e1
WIP
marcocapozzoli c18e3d8
WIP
marcocapozzoli 7a711f2
Merge branch 'masc/1177-atomdb-auth-b' into masc/1177-atomdb-auth-c
marcocapozzoli 2a83069
WIP
marcocapozzoli ab301fe
Merge branch 'masc/1177-atomdb-auth-b' into masc/1177-atomdb-auth-c
marcocapozzoli f96661f
WIP
marcocapozzoli 1bc9afb
WIP
marcocapozzoli 3309604
WIP
marcocapozzoli 06b1e32
Merge branch 'masc/1177-atomdb-auth-b' into masc/1177-atomdb-auth-c
marcocapozzoli dd4e0f5
Refactor MongoAuthorizationPersistence
marcocapozzoli 2104e95
Change public_key from object string
marcocapozzoli c558e6a
Refactor auth
marcocapozzoli 20cca56
Merge branch 'masc/1177-atomdb-auth-b' into masc/1177-atomdb-auth-c
marcocapozzoli f04b5cc
WIP
marcocapozzoli b0ee315
Re-design
marcocapozzoli e9a6d5b
Add explicit
marcocapozzoli 006c3a0
Add destructor and fix test
marcocapozzoli 396d89f
Update src/atomdb/auth/AuthorizationManager.cc
marcocapozzoli 46c7627
Add namespace
marcocapozzoli d96143b
Merge branch 'masc/1177-atomdb-auth-b' into masc/1177-atomdb-auth-c
marcocapozzoli c7f1394
Merge branch 'masc/1177-atomdb-auth-b' into masc/1177-atomdb-auth-c
marcocapozzoli 2a4439d
Add includes; Reject null manifest in ManifestAuthorizer()
marcocapozzoli 49c6891
Merge branch 'masc/1177-atomdb-auth-b' into masc/1177-atomdb-auth-c
marcocapozzoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
|
|
||
| #include "AuthorizationManager.h" | ||
|
|
||
| using namespace std; | ||
| using namespace atomdb; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // -------------------------------------------------------------------------------- | ||
| // Constructor | ||
|
|
||
| AuthorizationManager::AuthorizationManager(shared_ptr<AuthorizationPersistence> persistence) | ||
| : persistence(persistence) { | ||
| if (!this->persistence) { | ||
| RAISE_ERROR("Authorization persistence is required"); | ||
| } | ||
| } | ||
|
|
||
| // -------------------------------------------------------------------------------- | ||
| // Public methods | ||
|
|
||
| vector<atomdb_api_types::AccessPermissionEntry> atomdb::AuthorizationManager::list( | ||
| const string& public_key) { | ||
| return this->persistence->list(public_key); | ||
| } | ||
|
|
||
| void AuthorizationManager::authorize(const string& public_key, | ||
| const atomdb_api_types::AccessPermissionEntry& entry) { | ||
| this->persistence->save(public_key, entry); | ||
| } | ||
|
|
||
| void AuthorizationManager::revoke(const string& public_key, | ||
| const atomdb_api_types::AccessPermissionEntry& entry) { | ||
| this->persistence->remove(public_key, entry); | ||
| } | ||
|
|
||
| void AuthorizationManager::revoke_all(const string& public_key) { | ||
| this->persistence->remove_all(public_key); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "AtomDBAPITypes.h" | ||
| #include "AuthorizationPersistence.h" | ||
|
|
||
| using namespace std; | ||
| using namespace atoms; | ||
|
|
||
| namespace atomdb { | ||
|
|
||
| /** | ||
| * @brief Manages authorization permissions through persistent storage. | ||
| */ | ||
| class AuthorizationManager { | ||
| public: | ||
| /** | ||
| * @param persistence Storage used to manage authorization permissions. | ||
| */ | ||
| AuthorizationManager(shared_ptr<AuthorizationPersistence> persistence); | ||
|
|
||
| ~AuthorizationManager() = default; | ||
|
|
||
| /** | ||
| * @brief Lists all permissions granted to public_key. | ||
| */ | ||
| vector<atomdb_api_types::AccessPermissionEntry> list(const string& public_key); | ||
|
|
||
| /** | ||
| * @brief Grants an authorization entry to public_key. | ||
| */ | ||
| void authorize(const string& public_key, const atomdb_api_types::AccessPermissionEntry& entry); | ||
|
|
||
| /** | ||
| * @brief Revokes an authorization entry from public_key. | ||
| */ | ||
| void revoke(const string& public_key, const atomdb_api_types::AccessPermissionEntry& entry); | ||
|
|
||
| /** | ||
| * @brief Revokes all authorization entries from public_key. | ||
| */ | ||
| void revoke_all(const string& public_key); | ||
|
|
||
| private: | ||
| shared_ptr<AuthorizationPersistence> persistence; | ||
| }; | ||
|
|
||
| } // namespace atomdb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #include "AuthorizationManifest.h" | ||
|
|
||
| #include <algorithm> | ||
|
|
||
| #define LOG_LEVEL INFO_LEVEL | ||
| #include "Logger.h" | ||
| #include "Utils.h" | ||
|
|
||
| using namespace std; | ||
| using namespace atomdb; | ||
|
|
||
| // -------------------------------------------------------------------------------- | ||
| // Public methods | ||
|
|
||
| void AuthorizationManifest::set(const atomdb_api_types::AccessPermissionDocument& document) { | ||
| this->documents.insert_or_assign(document.access_key, document); | ||
| } | ||
|
|
||
| void AuthorizationManifest::add(const string& public_key, | ||
| const atomdb_api_types::AccessPermissionEntry& entry) { | ||
| auto document = this->get_document(public_key); | ||
|
|
||
| if (document == nullptr) { | ||
| this->documents.emplace(public_key, | ||
| atomdb_api_types::AccessPermissionDocument(public_key, false, {entry})); | ||
| return; | ||
| } | ||
|
|
||
| vector<atomdb_api_types::AccessPermissionEntry>& entries = document->entries; | ||
|
|
||
| for (auto& existing : entries) { | ||
| if (existing.schema.handle() == entry.schema.handle()) { | ||
| existing = entry; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| entries.push_back(entry); | ||
| } | ||
|
|
||
| void AuthorizationManifest::remove(const string& public_key, | ||
| const atomdb_api_types::AccessPermissionEntry& entry) { | ||
| auto document = this->get_document(public_key); | ||
|
|
||
| if (document == nullptr) return; | ||
|
|
||
| vector<atomdb_api_types::AccessPermissionEntry>& entries = document->entries; | ||
|
|
||
| for (auto it = entries.begin(); it != entries.end(); ++it) { | ||
| if (it->schema.handle() == entry.schema.handle()) { | ||
| entries.erase(it); | ||
| return; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| void AuthorizationManifest::remove_all(const string& public_key) { this->documents.erase(public_key); } | ||
|
|
||
| bool AuthorizationManifest::is_registered(const string& public_key) const { | ||
| return this->documents.find(public_key) != this->documents.end(); | ||
| } | ||
|
|
||
| bool AuthorizationManifest::full_access(const string& public_key) { | ||
| auto document = this->get_document(public_key); | ||
| if (document == nullptr) return false; | ||
| return document->full_access; | ||
| } | ||
|
|
||
| atomdb_api_types::AccessPermissionDocument* AuthorizationManifest::get_document( | ||
| const string& public_key) { | ||
| auto it = this->documents.find(public_key); | ||
|
|
||
| if (it == this->documents.end()) { | ||
| return nullptr; | ||
| } | ||
| return &it->second; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #pragma once | ||
|
|
||
| #include <map> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "AtomDBAPITypes.h" | ||
|
|
||
| using namespace std; | ||
|
|
||
| namespace atomdb { | ||
|
|
||
| /** | ||
| * @brief In-memory representation of the authorization state. | ||
| * | ||
| * Stores the authorization documents used by the authorization checks. | ||
| * The manifest is independent of the underlying persistence mechanism. | ||
| */ | ||
| class AuthorizationManifest { | ||
| public: | ||
| AuthorizationManifest() = default; | ||
| ~AuthorizationManifest() = default; | ||
|
|
||
| /** | ||
| * @brief Replaces or inserts the authorization document. | ||
| */ | ||
| void set(const atomdb_api_types::AccessPermissionDocument& document); | ||
|
|
||
| /** | ||
| * @brief Adds an authorization entry for public_key. | ||
| * | ||
| * Creates the authorization document if public_key is not registered. | ||
| */ | ||
| void add(const string& public_key, const atomdb_api_types::AccessPermissionEntry& entry); | ||
|
|
||
| /** | ||
| * @brief Removes an authorization entry from public_key. | ||
| * | ||
| * Does nothing if public_key or the specified entry is not present. | ||
| */ | ||
| void remove(const string& public_key, const atomdb_api_types::AccessPermissionEntry& entry); | ||
|
|
||
| /** | ||
| * @brief Removes all authorization entries for public_key. | ||
| * | ||
| * Does nothing if public_key is not registered. | ||
| */ | ||
| void remove_all(const string& public_key); | ||
|
|
||
| /** | ||
| * @brief Returns whether public_key has an authorization document. | ||
| */ | ||
| bool is_registered(const string& public_key) const; | ||
|
|
||
| /** | ||
| * @brief Returns whether public_key has full access. | ||
| */ | ||
| bool full_access(const string& public_key); | ||
|
|
||
| /** | ||
| * @brief Returns a document from public_key. | ||
| */ | ||
| atomdb_api_types::AccessPermissionDocument* get_document(const string& public_key); | ||
|
|
||
| private: | ||
| map<string, atomdb_api_types::AccessPermissionDocument> documents; | ||
| }; | ||
|
|
||
| } // namespace atomdb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "AtomDBAPITypes.h" | ||
|
|
||
| using namespace std; | ||
|
|
||
| namespace atomdb { | ||
|
|
||
| /** | ||
| * @brief Persistence interface for authorization data. | ||
| */ | ||
| class AuthorizationPersistence { | ||
| public: | ||
| virtual ~AuthorizationPersistence() = default; | ||
|
|
||
| /** | ||
| * @brief Lists all authorization entries for public_key. | ||
| */ | ||
| virtual vector<atomdb_api_types::AccessPermissionEntry> list(const string& public_key) = 0; | ||
|
|
||
| /** | ||
| * @brief Persists an authorization entry for public_key. | ||
| */ | ||
| virtual void save(const string& public_key, | ||
| const atomdb_api_types::AccessPermissionEntry& entry) = 0; | ||
|
|
||
| /** | ||
| * @brief Removes an authorization entry from public_key. | ||
| */ | ||
| virtual void remove(const string& public_key, | ||
| const atomdb_api_types::AccessPermissionEntry& entry) = 0; | ||
|
|
||
| /** | ||
| * @brief Removes all authorization entries for public_key. | ||
| */ | ||
| virtual void remove_all(const string& public_key) = 0; | ||
| }; | ||
| } // namespace atomdb |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.