mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Merge CertificateManager and CredentialManager
Refactoring change. Merge CertificateManager and CredentialManager into a single interface. Hide AdvertisementFactory in implementation/. The client app will not be using that class directly. Clean up build targets. PiperOrigin-RevId: 466748133
This commit is contained in:
committed by
Copybara-Service
parent
63b4f0760c
commit
07119e80de
@@ -176,7 +176,7 @@ cc_library(
|
||||
"//connections/implementation:__subpackages__",
|
||||
"//internal/platform:__pkg__",
|
||||
"//internal/platform/implementation:__subpackages__",
|
||||
"//presence:__pkg__",
|
||||
"//presence:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"//internal/platform/implementation:types",
|
||||
|
||||
+1
-53
@@ -74,7 +74,6 @@ cc_library(
|
||||
deps = [
|
||||
":credential",
|
||||
":encryption",
|
||||
":types",
|
||||
"//internal/platform:logging",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/types:variant",
|
||||
@@ -96,35 +95,6 @@ cc_library(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "certificate_manager",
|
||||
hdrs = ["certificate_manager.h"],
|
||||
deps = [
|
||||
":credential",
|
||||
":types",
|
||||
"@com_google_absl//absl/status:statusor",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "advertisement_factory",
|
||||
srcs = ["advertisement_factory.cc"],
|
||||
hdrs = ["advertisement_factory.h"],
|
||||
deps = [
|
||||
":broadcast_request",
|
||||
":certificate_manager",
|
||||
":credential",
|
||||
":types",
|
||||
"//internal/platform:logging",
|
||||
"//internal/platform:uuid",
|
||||
"//internal/platform/implementation:comm",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/status:statusor",
|
||||
"@com_google_absl//absl/strings:str_format",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "action_factory",
|
||||
srcs = ["action_factory.cc"],
|
||||
@@ -146,28 +116,6 @@ cc_library(
|
||||
visibility = [
|
||||
"//third_party/nearby:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"//presence/proto:credential_cc_proto",
|
||||
"//presence/proto:device_metadata_cc_proto",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "advertisement_factory_test",
|
||||
size = "small",
|
||||
srcs = ["advertisement_factory_test.cc"],
|
||||
deps = [
|
||||
":action_factory",
|
||||
":advertisement_factory",
|
||||
":certificate_manager",
|
||||
":credential",
|
||||
":types",
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
@@ -263,7 +211,7 @@ cc_test(
|
||||
shard_count = 6,
|
||||
deps = [
|
||||
":presence",
|
||||
"//internal/platform/implementation/g3",
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
// Copyright 2022 Google LLC
|
||||
//
|
||||
// Licensed 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
|
||||
//
|
||||
// https://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.
|
||||
|
||||
#ifndef THIRD_PARTY_NEARBY_PRESENCE_CERTIFICATE_MANAGER_H_
|
||||
#define THIRD_PARTY_NEARBY_PRESENCE_CERTIFICATE_MANAGER_H_
|
||||
#include <string>
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "presence/presence_identity.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace presence {
|
||||
|
||||
/** Stores and manages credentials/certificates on local device, and uses the
|
||||
* certificates for encryption and deceryption. */
|
||||
class CertificateManager {
|
||||
public:
|
||||
virtual ~CertificateManager() = default;
|
||||
|
||||
/** Returns encrypted metadata key associated with `identity` for Base NP
|
||||
* advertisement */
|
||||
virtual absl::StatusOr<std::string> GetBaseEncryptedMetadataKey(
|
||||
const PresenceIdentity& identity) = 0;
|
||||
/** Encrypts `data_elements` using certificate associated with `identity` and
|
||||
* `salt` */
|
||||
virtual absl::StatusOr<std::string> EncryptDataElements(
|
||||
const PresenceIdentity& identity, absl::string_view salt,
|
||||
absl::string_view data_elements) = 0;
|
||||
};
|
||||
|
||||
} // namespace presence
|
||||
} // namespace nearby
|
||||
|
||||
#endif // THIRD_PARTY_NEARBY_PRESENCE_CERTIFICATE_MANAGER_H_
|
||||
@@ -17,10 +17,12 @@ cc_library(
|
||||
name = "internal",
|
||||
srcs = [
|
||||
"advertisement_decoder.cc",
|
||||
"advertisement_factory.cc",
|
||||
"credential_manager_impl.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"advertisement_decoder.h",
|
||||
"advertisement_factory.h",
|
||||
"broadcast_manager.h",
|
||||
"credential_manager.h",
|
||||
"credential_manager_impl.h",
|
||||
@@ -37,10 +39,11 @@ cc_library(
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:comm",
|
||||
"//internal/platform:logging",
|
||||
"//internal/platform:uuid",
|
||||
"//internal/platform/implementation:comm",
|
||||
"//internal/platform/implementation:types",
|
||||
"//presence:action_factory",
|
||||
"//presence:advertisement_factory",
|
||||
"//presence:broadcast_request",
|
||||
"//presence:credential",
|
||||
"//presence:encryption",
|
||||
"//presence:types",
|
||||
@@ -67,6 +70,22 @@ cc_test(
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "advertisement_factory_test",
|
||||
size = "small",
|
||||
srcs = ["advertisement_factory_test.cc"],
|
||||
deps = [
|
||||
":internal",
|
||||
"//internal/platform/implementation/g3", # build_cleaner: keep
|
||||
"//presence:action_factory",
|
||||
"//presence:types",
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "credential_manager_impl_test",
|
||||
size = "small",
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "presence/advertisement_factory.h"
|
||||
#include "presence/data_element.h"
|
||||
#include "presence/implementation/advertisement_factory.h"
|
||||
#include "presence/implementation/credential_manager.h"
|
||||
#include "presence/presence_identity.h"
|
||||
|
||||
|
||||
+3
-3
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "presence/advertisement_factory.h"
|
||||
#include "presence/implementation/advertisement_factory.h"
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
@@ -96,7 +96,7 @@ AdvertisementFactory::CreateBaseNpAdvertisement(
|
||||
}
|
||||
}
|
||||
absl::StatusOr<std::string> identity =
|
||||
certificate_manager_.GetBaseEncryptedMetadataKey(presence.identity);
|
||||
credential_manager_.GetBaseEncryptedMetadataKey(presence.identity);
|
||||
if (!identity.ok()) {
|
||||
return identity.status();
|
||||
}
|
||||
@@ -122,7 +122,7 @@ AdvertisementFactory::CreateBaseNpAdvertisement(
|
||||
return result;
|
||||
}
|
||||
if (!identity->empty()) {
|
||||
auto encrypted = certificate_manager_.EncryptDataElements(
|
||||
auto encrypted = credential_manager_.EncryptDataElements(
|
||||
presence.identity, request.salt, data_elements);
|
||||
if (!encrypted.ok()) {
|
||||
return encrypted.status();
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "absl/status/statusor.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "presence/broadcast_request.h"
|
||||
#include "presence/certificate_manager.h"
|
||||
#include "presence/implementation/credential_manager.h"
|
||||
#include "presence/presence_identity.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -33,8 +33,8 @@ using ::location::nearby::api::ble_v2::BleAdvertisementData;
|
||||
/** Builds BLE advertisements from broadcast requests. */
|
||||
class AdvertisementFactory {
|
||||
public:
|
||||
explicit AdvertisementFactory(CertificateManager* certificate_manager)
|
||||
: certificate_manager_(*certificate_manager) {}
|
||||
explicit AdvertisementFactory(CredentialManager* credential_manager)
|
||||
: credential_manager_(*credential_manager) {}
|
||||
|
||||
/** Returns a BLE advertisement for given `request` */
|
||||
absl::StatusOr<BleAdvertisementData> CreateAdvertisement(
|
||||
@@ -44,7 +44,7 @@ class AdvertisementFactory {
|
||||
absl::StatusOr<BleAdvertisementData> CreateBaseNpAdvertisement(
|
||||
const BroadcastRequest& request) const;
|
||||
|
||||
CertificateManager& certificate_manager_;
|
||||
CredentialManager& credential_manager_;
|
||||
};
|
||||
|
||||
} // namespace presence
|
||||
+11
-12
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "presence/advertisement_factory.h"
|
||||
#include "presence/implementation/advertisement_factory.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -23,8 +23,8 @@
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "presence/action_factory.h"
|
||||
#include "presence/certificate_manager.h"
|
||||
#include "presence/data_element.h"
|
||||
#include "presence/implementation/credential_manager_impl.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace presence {
|
||||
@@ -35,7 +35,7 @@ using ::testing::NiceMock;
|
||||
using ::testing::Return;
|
||||
using ::testing::status::StatusIs;
|
||||
|
||||
class MockCertificateManager : public CertificateManager {
|
||||
class MockCredentialManager : public CredentialManagerImpl {
|
||||
public:
|
||||
MOCK_METHOD(absl::StatusOr<std::string>, GetBaseEncryptedMetadataKey,
|
||||
(const PresenceIdentity& identity), (override));
|
||||
@@ -47,7 +47,7 @@ class MockCertificateManager : public CertificateManager {
|
||||
|
||||
TEST(AdvertisementFactory, CreateAdvertisementFromPrivateIdentity) {
|
||||
std::string salt = "AB";
|
||||
NiceMock<MockCertificateManager> certificate_manager;
|
||||
NiceMock<MockCredentialManager> credential_manager;
|
||||
PresenceIdentity identity;
|
||||
std::vector<DataElement> data_elements;
|
||||
data_elements.emplace_back(DataElement::kActionFieldType,
|
||||
@@ -58,14 +58,14 @@ TEST(AdvertisementFactory, CreateAdvertisementFromPrivateIdentity) {
|
||||
.SetSalt(salt)
|
||||
.SetTxPower(5)
|
||||
.SetAction(action));
|
||||
EXPECT_CALL(certificate_manager, GetBaseEncryptedMetadataKey(identity))
|
||||
EXPECT_CALL(credential_manager, GetBaseEncryptedMetadataKey(identity))
|
||||
.WillOnce(Return(absl::HexStringToBytes("1011121314151617181920212223")));
|
||||
EXPECT_CALL(
|
||||
certificate_manager,
|
||||
credential_manager,
|
||||
EncryptDataElements(identity, salt, absl::HexStringToBytes("1505260080")))
|
||||
.WillOnce(Return(absl::HexStringToBytes("5051525354")));
|
||||
|
||||
AdvertisementFactory factory(&certificate_manager);
|
||||
AdvertisementFactory factory(&credential_manager);
|
||||
absl::StatusOr<BleAdvertisementData> result =
|
||||
factory.CreateAdvertisement(request);
|
||||
|
||||
@@ -80,9 +80,8 @@ TEST(AdvertisementFactory, CreateAdvertisementFromPrivateIdentity) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(AdvertisementFactory,
|
||||
CreateAdvertisementFailsWhenCertificateManagerFails) {
|
||||
NiceMock<MockCertificateManager> certificate_manager;
|
||||
TEST(AdvertisementFactory, CreateAdvertisementFailsWhenCredentialManagerFails) {
|
||||
NiceMock<MockCredentialManager> credential_manager;
|
||||
PresenceIdentity identity;
|
||||
std::vector<DataElement> data_elements;
|
||||
data_elements.emplace_back(DataElement::kActionFieldType,
|
||||
@@ -93,11 +92,11 @@ TEST(AdvertisementFactory,
|
||||
.SetSalt("AB")
|
||||
.SetTxPower(5)
|
||||
.SetAction(action));
|
||||
EXPECT_CALL(certificate_manager, GetBaseEncryptedMetadataKey(identity))
|
||||
EXPECT_CALL(credential_manager, GetBaseEncryptedMetadataKey(identity))
|
||||
.WillOnce(Return(absl::UnimplementedError(
|
||||
"GetBaseEncryptedMetadataKey not implemented")));
|
||||
|
||||
AdvertisementFactory factory(&certificate_manager);
|
||||
AdvertisementFactory factory(&credential_manager);
|
||||
EXPECT_THAT(factory.CreateAdvertisement(request),
|
||||
StatusIs(absl::StatusCode::kUnimplemented));
|
||||
}
|
||||
@@ -83,6 +83,17 @@ class CredentialManager {
|
||||
virtual absl::StatusOr<std::string> DecryptDataElements(
|
||||
absl::string_view metadata_key, absl::string_view salt,
|
||||
absl::string_view data_elements) = 0;
|
||||
|
||||
// Returns encrypted metadata key associated with `identity` for Base NP
|
||||
// advertisement.
|
||||
virtual absl::StatusOr<std::string> GetBaseEncryptedMetadataKey(
|
||||
const PresenceIdentity& identity) = 0;
|
||||
|
||||
// Encrypts `data_elements` using certificate associated with `identity` and
|
||||
// `salt`.
|
||||
virtual absl::StatusOr<std::string> EncryptDataElements(
|
||||
const PresenceIdentity& identity, absl::string_view salt,
|
||||
absl::string_view data_elements) = 0;
|
||||
};
|
||||
|
||||
} // namespace presence
|
||||
|
||||
@@ -69,6 +69,18 @@ class CredentialManagerImpl : public CredentialManager {
|
||||
return absl::UnimplementedError("DecryptDataElements unimplemented");
|
||||
}
|
||||
|
||||
absl::StatusOr<std::string> GetBaseEncryptedMetadataKey(
|
||||
const PresenceIdentity& identity) override {
|
||||
return absl::UnimplementedError(
|
||||
"GetBaseEncryptedMetadataKey unimplemented");
|
||||
}
|
||||
|
||||
absl::StatusOr<std::string> EncryptDataElements(
|
||||
const PresenceIdentity& identity, absl::string_view salt,
|
||||
absl::string_view data_elements) override {
|
||||
return absl::UnimplementedError("EncryptDataElements unimplemented");
|
||||
}
|
||||
|
||||
private:
|
||||
FRIEND_TEST(CredentialManagerImpl, CreateOneCredentialSuccessfully);
|
||||
|
||||
|
||||
@@ -23,8 +23,5 @@ cc_library(
|
||||
visibility = [
|
||||
"//presence/implementation:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:comm",
|
||||
],
|
||||
deps = ["//internal/platform:comm"],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user