Skip to content

Commit

Permalink
[iam] Implement CertReceiver subscription in ProvisionManager
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Kobets <[email protected]>
  • Loading branch information
mykola-kobets-epam committed Sep 24, 2024
1 parent f2e576d commit 0e89bcb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
32 changes: 24 additions & 8 deletions include/aos/iam/provisionmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,26 @@ class ProvisionManagerItf {
virtual Error ApplyCert(const String& certType, const String& pemCert, certhandler::CertInfo& certInfo) = 0;

/**
* Returns certificate info.
* Subscribes certificates receiver.
*
* @param certType certificate type.
* @param issuer issuer name.
* @param serial serial number.
* @param[out] resCert result certificate.
* @param certReceiver certificate receiver.
* @returns Error.
*/
virtual Error GetCert(const String& certType, const Array<uint8_t>& issuer, const Array<uint8_t>& serial,
certhandler::CertInfo& resCert)
virtual Error SubscribeCertReceiver(const String& certType, const Array<uint8_t>& issuer,
const Array<uint8_t>& serial, certhandler::CertReceiverItf& certReceiver)
= 0;

/**
* Unsubscribes certificate receiver.
*
* @param certReceiver certificate receiver.
* @returns Error.
*/
virtual Error UnsubscribeCertReceiver(certhandler::CertReceiverItf& certReceiver) = 0;

/**
* Finishes provisioning.
*
Expand Down Expand Up @@ -193,16 +201,24 @@ class ProvisionManager : public ProvisionManagerItf {
Error ApplyCert(const String& certType, const String& pemCert, certhandler::CertInfo& certInfo) override;

/**
* Returns certificate info.
* Subscribes certificates receiver.
*
* @param certType certificate type.
* @param issuer issuer name.
* @param serial serial number.
* @param[out] resCert result certificate.
* @param certReceiver certificate receiver.
* @returns Error.
*/
Error SubscribeCertReceiver(const String& certType, const Array<uint8_t>& issuer, const Array<uint8_t>& serial,
certhandler::CertReceiverItf& certReceiver) override;

/**
* Unsubscribes certificate receiver.
*
* @param certReceiver certificate receiver.
* @returns Error.
*/
Error GetCert(const String& certType, const Array<uint8_t>& issuer, const Array<uint8_t>& serial,
certhandler::CertInfo& resCert) override;
Error UnsubscribeCertReceiver(certhandler::CertReceiverItf& certReceiver) override;

/**
* Finishes provisioning.
Expand Down
15 changes: 11 additions & 4 deletions src/iam/provisionmanager/provisionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,19 @@ Error ProvisionManager::ApplyCert(const String& certType, const String& pemCert,
return AOS_ERROR_WRAP(mCertHandler->ApplyCertificate(certType, pemCert, certInfo));
}

Error ProvisionManager::GetCert(
const String& certType, const Array<uint8_t>& issuer, const Array<uint8_t>& serial, certhandler::CertInfo& resCert)
Error ProvisionManager::SubscribeCertReceiver(const String& certType, const Array<uint8_t>& issuer,
const Array<uint8_t>& serial, certhandler::CertReceiverItf& certReceiver)
{
LOG_DBG() << "Get cert: type = " << certType;
LOG_DBG() << "Subscribe cert receiver: type = " << certType;

return AOS_ERROR_WRAP(mCertHandler->GetCertificate(certType, issuer, serial, resCert));
return AOS_ERROR_WRAP(mCertHandler->SubscribeCertReceiver(certType, issuer, serial, certReceiver));
}

Error ProvisionManager::UnsubscribeCertReceiver(certhandler::CertReceiverItf& certReceiver)
{
LOG_DBG() << "Unsubscribe cert receiver";

return AOS_ERROR_WRAP(mCertHandler->UnsubscribeCertReceiver(certReceiver));
}

} // namespace aos::iam::provisionmanager
25 changes: 11 additions & 14 deletions tests/iam/provisionmanager/provisionmanager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <aos/iam/provisionmanager.hpp>

#include "mocks/certhandlermock.hpp"
#include "mocks/certreceivermock.hpp"
#include "mocks/provisioningcallbackmock.hpp"

using namespace testing;
Expand Down Expand Up @@ -269,28 +270,24 @@ TEST_F(ProvisionManagerTest, ApplyCert)
EXPECT_EQ(certInfo, generatedCertInfo);
}

TEST_F(ProvisionManagerTest, GetCert)
TEST_F(ProvisionManagerTest, SubscribeCertReceiver)
{
auto convertByteArrayToAosArray = [](const char* data, size_t size) -> aos::Array<uint8_t> {
return {reinterpret_cast<const uint8_t*>(data), size};
};

int64_t nowSec = static_cast<int64_t>(time(nullptr));
int64_t nowNSec = 0;
aos::iam::certhandler::CertInfo certInfo;
const aos::String certType = "iam";
const auto issuer = convertByteArrayToAosArray("issuer", strlen("issuer"));
const auto serial = convertByteArrayToAosArray("serial", strlen("serial"));

certInfo.mIssuer = convertByteArrayToAosArray("issuer", strlen("issuer"));
certInfo.mSerial = convertByteArrayToAosArray("serial", strlen("serial"));
certInfo.mCertURL = "certURL";
certInfo.mKeyURL = "keyURL";
certInfo.mNotAfter = aos::Time::Unix(nowSec, nowNSec).Add(aos::Time::cYear);
CertReceiverItfMock certReceiver;

EXPECT_CALL(mCertHandler, GetCertificate)
.WillOnce(DoAll(SetArgReferee<3>(certInfo), Return(aos::ErrorEnum::eNone)));
EXPECT_CALL(mCertHandler, SubscribeCertReceiver(certType, issuer, serial, _))
.WillOnce(Return(aos::ErrorEnum::eNone));
ASSERT_TRUE(mProvisionManager.SubscribeCertReceiver(certType, issuer, serial, certReceiver).IsNone());

aos::iam::certhandler::CertInfo result;
ASSERT_TRUE(mProvisionManager.GetCert("certType", certInfo.mIssuer, certInfo.mSerial, result).IsNone());
EXPECT_EQ(result, certInfo);
EXPECT_CALL(mCertHandler, UnsubscribeCertReceiver(Ref(certReceiver))).WillOnce(Return(aos::ErrorEnum::eNone));
ASSERT_TRUE(mProvisionManager.UnsubscribeCertReceiver(certReceiver).IsNone());
}

TEST_F(ProvisionManagerTest, Deprovision)
Expand Down

0 comments on commit 0e89bcb

Please sign in to comment.