-
Notifications
You must be signed in to change notification settings - Fork 0
Wiring up encryption metadata to the DBPA remote and local agents. #167
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
Changes from 2 commits
9aded37
58fc43d
53926fc
02023d2
3d793f0
dc9a956
f9f1740
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,10 @@ using namespace dbps::enum_utils; | |
|
|
||
| // LocalEncryptionResult implementation | ||
|
|
||
| LocalEncryptionResult::LocalEncryptionResult(std::vector<uint8_t> ciphertext) | ||
| : ciphertext_(std::move(ciphertext)), success_(true) { | ||
| LocalEncryptionResult::LocalEncryptionResult(std::vector<uint8_t> ciphertext, const std::map<std::string, std::string>& encryption_metadata) | ||
| : ciphertext_(std::move(ciphertext)), | ||
| success_(true), | ||
| encryption_metadata_(encryption_metadata) { | ||
| } | ||
|
|
||
| LocalEncryptionResult::LocalEncryptionResult(const std::string& error_stage, const std::string& error_message) | ||
|
|
@@ -38,6 +40,10 @@ bool LocalEncryptionResult::success() const { | |
| return success_; | ||
| } | ||
|
|
||
| const std::optional<std::map<std::string, std::string>> LocalEncryptionResult::encryption_metadata() const { | ||
| return encryption_metadata_.empty() ? std::nullopt : std::optional{encryption_metadata_}; | ||
| } | ||
|
|
||
| const std::string& LocalEncryptionResult::error_message() const { | ||
| return error_message_; | ||
| } | ||
|
|
@@ -174,7 +180,8 @@ std::unique_ptr<EncryptionResult> LocalDataBatchProtectionAgent::Encrypt( | |
| compression_type_, | ||
| column_key_id_, | ||
| user_id_, | ||
| app_context_ | ||
| app_context_, | ||
| {} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add // encryption_metadata comment
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| ); | ||
|
|
||
| // Convert plaintext span to vector for the sequencer | ||
|
|
@@ -188,8 +195,8 @@ std::unique_ptr<EncryptionResult> LocalDataBatchProtectionAgent::Encrypt( | |
| return std::make_unique<LocalEncryptionResult>(sequencer.error_stage_, sequencer.error_message_); | ||
| } | ||
|
|
||
| // Return successful result with encrypted data | ||
| return std::make_unique<LocalEncryptionResult>(std::move(sequencer.encrypted_result_)); | ||
| // Return successful result with encrypted data and encryption_metadata | ||
| return std::make_unique<LocalEncryptionResult>(std::move(sequencer.encrypted_result_), sequencer.encryption_metadata_); | ||
| } | ||
|
|
||
| std::unique_ptr<DecryptionResult> LocalDataBatchProtectionAgent::Decrypt( | ||
|
|
@@ -224,7 +231,8 @@ std::unique_ptr<DecryptionResult> LocalDataBatchProtectionAgent::Decrypt( | |
| compression_type_, | ||
| column_key_id_, | ||
| user_id_, | ||
| app_context_ | ||
| app_context_, | ||
| column_encryption_metadata_.value_or(std::map<std::string, std::string>{}) | ||
| ); | ||
|
|
||
| // Convert ciphertext span to vector for the sequencer | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ namespace dbps::external { | |
| class DBPS_EXPORT LocalEncryptionResult : public EncryptionResult { | ||
| public: | ||
| // Constructor for successful encryption | ||
| LocalEncryptionResult(std::vector<uint8_t> ciphertext); | ||
| LocalEncryptionResult(std::vector<uint8_t> ciphertext, const std::map<std::string, std::string>& encryption_metadata = {}); | ||
|
|
||
| // Constructor for failed encryption | ||
| LocalEncryptionResult(const std::string& error_stage, const std::string& error_message); | ||
|
|
@@ -33,6 +33,7 @@ class DBPS_EXPORT LocalEncryptionResult : public EncryptionResult { | |
| span<const uint8_t> ciphertext() const override; | ||
| std::size_t size() const override; | ||
| bool success() const override; | ||
| const std::optional<std::map<std::string, std::string>> encryption_metadata() const override; | ||
| const std::string& error_message() const override; | ||
| const std::map<std::string, std::string>& error_fields() const override; | ||
|
|
||
|
|
@@ -41,6 +42,7 @@ class DBPS_EXPORT LocalEncryptionResult : public EncryptionResult { | |
| private: | ||
| std::vector<uint8_t> ciphertext_; | ||
| bool success_; | ||
| std::map<std::string, std::string> encryption_metadata_; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it should be in this header file or in another, but can you add a comment with an example of what may be contained in here?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added comments on the dbpa_interface.h file. |
||
| std::string error_message_; | ||
| std::map<std::string, std::string> error_fields_; | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,18 @@ bool RemoteEncryptionResult::success() const { | |
| return response_ && response_->Success(); | ||
| } | ||
|
|
||
| const std::optional<std::map<std::string, std::string>> RemoteEncryptionResult::encryption_metadata() const { | ||
| if (!cached_encryption_metadata_.has_value() && response_ && response_->Success()) { | ||
| const auto& response_attrs = response_->GetResponseAttributes(); | ||
| if (!response_attrs.encryption_metadata_.empty()) { | ||
| cached_encryption_metadata_ = response_attrs.encryption_metadata_; | ||
| } else { | ||
| cached_encryption_metadata_ = std::nullopt; | ||
| } | ||
|
Comment on lines
+52
to
+60
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's document what's going on here - it's a bit hard to grasp
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. You can let me know if the comment is sufficient and I can expand more if needed.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a clearer, thanks - but the whole concept of 'cached metadata' is a bit confusing. maybe let's add a short note on that? (in particular, why is it 'cached' ?)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you! it's much clearer now. |
||
| } | ||
| return cached_encryption_metadata_; | ||
| } | ||
|
|
||
| const std::string& RemoteEncryptionResult::error_message() const { | ||
| if (cached_error_message_.empty() && response_) { | ||
| if (!response_->Success()) { | ||
|
|
@@ -301,7 +313,9 @@ std::unique_ptr<DecryptionResult> RemoteDataBatchProtectionAgent::Decrypt(span<c | |
| compression_type_, | ||
| column_key_id_, | ||
| user_id_, | ||
| app_context_ | ||
| app_context_, | ||
| // For the Decrypt use case, column_encryption_metadata_ will be set on the constructor, however adding a default empty map for type safety. | ||
| column_encryption_metadata_.value_or(std::map<std::string, std::string>{}) | ||
| ); | ||
|
|
||
| // Validate that response fields match request fields | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ class DBPS_EXPORT RemoteEncryptionResult : public EncryptionResult { | |
| span<const uint8_t> ciphertext() const override; | ||
| std::size_t size() const override; | ||
| bool success() const override; | ||
| const std::optional<std::map<std::string, std::string>> encryption_metadata() const override; | ||
| const std::string& error_message() const override; | ||
| const std::map<std::string, std::string>& error_fields() const override; | ||
|
|
||
|
|
@@ -40,11 +41,12 @@ class DBPS_EXPORT RemoteEncryptionResult : public EncryptionResult { | |
| private: | ||
| std::unique_ptr<EncryptApiResponse> response_; | ||
|
|
||
| // Cache error message/fields to avoid repeated parsing of API response. | ||
| // Cache error message/fields/encryption_metadata to avoid repeated parsing of API response. | ||
| // Defined as mutable because it has lazy evaluation (updated only once on the getter methods) | ||
| // The caching of this is possible because the API response `response_` doesn't change after construction. | ||
| mutable std::string cached_error_message_; | ||
| mutable std::map<std::string, std::string> cached_error_fields_; | ||
| mutable std::optional<std::map<std::string, std::string>> cached_encryption_metadata_; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add a note for the future, where we state that this is cacheable only because the metadata is identical across requests (but that may change)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this is the RemoteEncryptionResult, not the RemoteDPBA. A new instance is returned on each call to RemoteDBPA.Encrypt(). This is cached separately on each RemoteEncryptionResult instance, to avoid parsing the Json with every call to But let me know if something is unclear, and we can discuss. |
||
| }; | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.