-
Notifications
You must be signed in to change notification settings - Fork 76
Support getting encryption context on a message #526
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
Merged
BewareMyPower
merged 12 commits into
apache:main
from
BewareMyPower:bewaremypower/msg-enc-context
Dec 9, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e8e3e23
Initial commit
BewareMyPower 27e0092
Revert "Initial commit"
BewareMyPower 5775987
simplify the implementation
BewareMyPower aec2215
fix format
BewareMyPower dcaf920
Fix windows example
BewareMyPower 5e159b1
Fix encryption context not set for batched messages
BewareMyPower 832feda
Simplify header
BewareMyPower ee909be
Use MessageBatch to de-batch payload
BewareMyPower 99cf882
address copilot comments
BewareMyPower 9ea5a40
improve naming
BewareMyPower 70ab53f
Update include/pulsar/EncryptionContext.h
BewareMyPower 2fca472
fix format
BewareMyPower 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "CompressionType.h" | ||
| #include "defines.h" | ||
|
|
||
| namespace pulsar { | ||
|
|
||
| namespace proto { | ||
| class MessageMetadata; | ||
| } | ||
|
|
||
| struct PULSAR_PUBLIC EncryptionKey { | ||
| std::string key; | ||
| std::string value; | ||
| std::unordered_map<std::string, std::string> metadata; | ||
|
|
||
| EncryptionKey(const std::string& key, const std::string& value, | ||
| const decltype(EncryptionKey::metadata)& metadata) | ||
| : key(key), value(value), metadata(metadata) {} | ||
| }; | ||
|
|
||
| /** | ||
| * It contains encryption and compression information in it using which application can decrypt consumed | ||
| * message with encrypted-payload. | ||
| */ | ||
| class PULSAR_PUBLIC EncryptionContext { | ||
| public: | ||
| using KeysType = std::vector<EncryptionKey>; | ||
|
|
||
| /** | ||
| * @return the map of encryption keys used for the message | ||
| */ | ||
| const KeysType& keys() const noexcept { return keys_; } | ||
|
|
||
| /** | ||
| * @return the encryption parameter used for the message | ||
| */ | ||
| const std::string& param() const noexcept { return param_; } | ||
|
|
||
| /** | ||
| * @return the encryption algorithm used for the message | ||
| */ | ||
| const std::string& algorithm() const noexcept { return algorithm_; } | ||
|
|
||
| /** | ||
| * @return the compression type used for the message | ||
| */ | ||
| CompressionType compressionType() const noexcept { return compressionType_; } | ||
|
|
||
| /** | ||
| * @return the uncompressed message size if the message is compressed, 0 otherwise | ||
| */ | ||
| uint32_t uncompressedMessageSize() const noexcept { return uncompressedMessageSize_; } | ||
|
|
||
| /** | ||
| * @return the batch size if the message is part of a batch, -1 otherwise | ||
| */ | ||
| int32_t batchSize() const noexcept { return batchSize_; } | ||
|
|
||
| /** | ||
| * When the `ConsumerConfiguration#getCryptoFailureAction` is set to `CONSUME`, the message will still be | ||
| * returned even if the decryption failed. This method is provided to let users know whether the | ||
| * decryption failed. | ||
| * | ||
| * @return whether the decryption failed | ||
| */ | ||
| bool isDecryptionFailed() const noexcept { return isDecryptionFailed_; } | ||
|
|
||
| /** | ||
| * This constructor is public to allow in-place construction via std::optional | ||
| * (e.g., `std::optional<EncryptionContext>(std::in_place, metadata, false)`), | ||
| * but should not be used directly in application code. | ||
| */ | ||
| EncryptionContext(const proto::MessageMetadata&, bool); | ||
|
|
||
| private: | ||
| KeysType keys_; | ||
| std::string param_; | ||
| std::string algorithm_; | ||
| CompressionType compressionType_{CompressionNone}; | ||
| uint32_t uncompressedMessageSize_{0}; | ||
| int32_t batchSize_{-1}; | ||
| bool isDecryptionFailed_{false}; | ||
|
|
||
| void setDecryptionFailed(bool failed) noexcept { isDecryptionFailed_ = failed; } | ||
|
|
||
| friend class ConsumerImpl; | ||
| }; | ||
|
|
||
| } // namespace pulsar |
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
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,48 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| #include <pulsar/EncryptionContext.h> | ||
|
|
||
| #include "PulsarApi.pb.h" | ||
|
|
||
| namespace pulsar { | ||
|
|
||
| static EncryptionContext::KeysType encryptedKeysFromMetadata(const proto::MessageMetadata& msgMetadata) { | ||
| EncryptionContext::KeysType keys; | ||
| for (auto&& key : msgMetadata.encryption_keys()) { | ||
| decltype(EncryptionKey::metadata) metadata; | ||
| for (int i = 0; i < key.metadata_size(); i++) { | ||
| const auto& entry = key.metadata(i); | ||
| metadata[entry.key()] = entry.value(); | ||
| } | ||
| keys.emplace_back(key.key(), key.value(), std::move(metadata)); | ||
| } | ||
| return keys; | ||
| } | ||
|
|
||
| EncryptionContext::EncryptionContext(const proto::MessageMetadata& msgMetadata, bool isDecryptionFailed) | ||
|
|
||
| : keys_(encryptedKeysFromMetadata(msgMetadata)), | ||
| param_(msgMetadata.encryption_param()), | ||
| algorithm_(msgMetadata.encryption_algo()), | ||
| compressionType_(static_cast<CompressionType>(msgMetadata.compression())), | ||
| uncompressedMessageSize_(msgMetadata.uncompressed_size()), | ||
| batchSize_(msgMetadata.has_num_messages_in_batch() ? msgMetadata.num_messages_in_batch() : -1), | ||
| isDecryptionFailed_(isDecryptionFailed) {} | ||
|
|
||
| } // namespace pulsar |
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
Oops, something went wrong.
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.