Skip to content
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

[feat] Support end-to-end encryption in C Reader API #262

Merged
merged 5 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions include/pulsar/c/reader_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <pulsar/c/reader.h>
#include <pulsar/defines.h>

#include "consumer_configuration.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -89,6 +91,16 @@ PULSAR_PUBLIC void pulsar_reader_configuration_set_read_compacted(

PULSAR_PUBLIC int pulsar_reader_configuration_is_read_compacted(pulsar_reader_configuration_t *configuration);

PULSAR_PUBLIC void pulsar_reader_configuration_set_default_crypto_key_reader(
pulsar_reader_configuration_t *configuration, const char *public_key_path, const char *private_key_path);

PULSAR_PUBLIC pulsar_consumer_crypto_failure_action
pulsar_reader_configuration_get_crypto_failure_action(pulsar_reader_configuration_t *configuration);

PULSAR_PUBLIC void pulsar_reader_configuration_set_crypto_failure_action(
pulsar_reader_configuration_t *configuration,
pulsar_consumer_crypto_failure_action crypto_failure_action);

#ifdef __cplusplus
}
#endif
19 changes: 19 additions & 0 deletions lib/c/c_ReaderConfiguration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,22 @@ void pulsar_reader_configuration_set_read_compacted(pulsar_reader_configuration_
int pulsar_reader_configuration_is_read_compacted(pulsar_reader_configuration_t *configuration) {
return configuration->conf.isReadCompacted();
}

void pulsar_reader_configuration_set_default_crypto_key_reader(pulsar_reader_configuration_t *configuration,
const char *public_key_path,
const char *private_key_path) {
std::shared_ptr<pulsar::DefaultCryptoKeyReader> keyReader =
std::make_shared<pulsar::DefaultCryptoKeyReader>(public_key_path, private_key_path);
configuration->conf.setCryptoKeyReader(keyReader);
}

pulsar_consumer_crypto_failure_action pulsar_reader_configuration_get_crypto_failure_action(
pulsar_reader_configuration_t *configuration) {
return (pulsar_consumer_crypto_failure_action)configuration->conf.getCryptoFailureAction();
}

void pulsar_reader_configuration_set_crypto_failure_action(
pulsar_reader_configuration_t *configuration,
pulsar_consumer_crypto_failure_action crypto_failure_action) {
configuration->conf.setCryptoFailureAction((pulsar::ConsumerCryptoFailureAction)crypto_failure_action);
}
66 changes: 66 additions & 0 deletions tests/c/c_ReaderConfigurationTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 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 <gtest/gtest.h>
#include <pulsar/c/reader_configuration.h>

#include <climits>

/*
ASSERT_EQ(consumerConf.getConsumerType(), ConsumerExclusive);
shibd marked this conversation as resolved.
Show resolved Hide resolved
ASSERT_EQ(consumerConf.getReceiverQueueSize(), 1000);
ASSERT_EQ(consumerConf.isReadCompacted(), false);
ASSERT_EQ(consumerConf.getSchema().getName(), "BYTES");
ASSERT_EQ(consumerConf.getUnAckedMessagesTimeoutMs(), 0);
ASSERT_EQ(consumerConf.getTickDurationInMs(), 1000);
ASSERT_EQ(consumerConf.getAckGroupingTimeMs(), 100);
ASSERT_EQ(consumerConf.getAckGroupingMaxSize(), 1000);
ASSERT_EQ(consumerConf.getCryptoKeyReader().get(), nullptr);
ASSERT_EQ(consumerConf.getCryptoFailureAction(), ConsumerCryptoFailureAction::FAIL);
ASSERT_TRUE(consumerConf.getProperties().empty());
ASSERT_TRUE(consumerConf.getConsumerName().empty());
ASSERT_FALSE(consumerConf.hasMessageListener());
*/

TEST(C_ReaderConfigurationTest, testCApiConfig) {
pulsar_reader_configuration_t *reader_conf = pulsar_reader_configuration_create();

ASSERT_FALSE(pulsar_reader_configuration_has_reader_listener(reader_conf));

ASSERT_EQ(pulsar_reader_configuration_get_receiver_queue_size(reader_conf), 1000);
pulsar_reader_configuration_set_receiver_queue_size(reader_conf, 1729);
ASSERT_EQ(pulsar_reader_configuration_get_receiver_queue_size(reader_conf), 1729);

ASSERT_STREQ(pulsar_reader_configuration_get_subscription_role_prefix(reader_conf), "");
pulsar_reader_configuration_set_subscription_role_prefix(reader_conf, "prefix");
ASSERT_STREQ(pulsar_reader_configuration_get_subscription_role_prefix(reader_conf), "prefix");

ASSERT_STREQ(pulsar_reader_configuration_get_reader_name(reader_conf), "");
pulsar_reader_configuration_set_reader_name(reader_conf, "reader");
ASSERT_STREQ(pulsar_reader_configuration_get_reader_name(reader_conf), "reader");

ASSERT_FALSE(pulsar_reader_configuration_is_read_compacted(reader_conf));
pulsar_reader_configuration_set_read_compacted(reader_conf, true);
ASSERT_TRUE(pulsar_reader_configuration_is_read_compacted(reader_conf));

ASSERT_EQ(pulsar_reader_configuration_get_crypto_failure_action(reader_conf), pulsar_ConsumerFail);
pulsar_reader_configuration_set_crypto_failure_action(reader_conf, pulsar_ConsumerDiscard);
ASSERT_EQ(pulsar_reader_configuration_get_crypto_failure_action(reader_conf), pulsar_ConsumerDiscard);

pulsar_reader_configuration_free(reader_conf);
}