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

[ate,cp] add reference CP test program boilerplate #37

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions run_integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ for sku in "${SKUS[@]}"; do
--parallel_clients=10 \
--total_calls_per_client=10
done

# Run the reference CP flow.
CP_SKUS=("sival")
for sku in "${CP_SKUS[@]}"; do
echo "Running reference CP flow with sku: ${sku} ..."
bazelisk run //src/ate/test_programs:cp -- \
--pa_socket="localhost:5001" \
--sku="${sku}" \
--sku_auth_pw="test_password"
done
2 changes: 1 addition & 1 deletion src/ate/ate_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ typedef struct {
typedef struct {
// Endpoint address in IP or DNS format including port number. For example:
// "localhost:5000".
const char* target;
const char* pa_socket;

// File containing the Client certificate in PEM format. Required when
// `enable_mtls` set to true.
Expand Down
8 changes: 4 additions & 4 deletions src/ate/ate_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ std::unique_ptr<AteClient> AteClient::Create(AteClient::Options options) {
LOG(INFO) << "debug info: In AteClient::Create"
<< " AteClient.options: " << options;

// establish a grpc channel between the client and the targeted server:
// establish a grpc channel between the client (test program) and the targeted
// provisioning appliance server:
// 1. set the grpc channel properties (insecured by default, authenticated and
// encrypted if specified in options.enable_mtls parameter)
auto credentials = grpc::InsecureChannelCredentials();
if (options.enable_mtls) {
credentials = BuildCredentials(options);
}
// 2. create the grpc channel between the client and the targeted server
// (specified in options.target parameter)
auto ate = absl::make_unique<AteClient>(ProvisioningApplianceService::NewStub(
grpc::CreateChannel(options.target, credentials)));
grpc::CreateChannel(options.pa_socket, credentials)));

return ate;
}
Expand Down Expand Up @@ -148,7 +148,7 @@ Status AteClient::SendDeviceRegistrationPayload(RegistrationRequest& request,
// overloads operator<< for AteClient::Options objects printouts
std::ostream& operator<<(std::ostream& os, const AteClient::Options& options) {
// write obj to stream
os << std::endl << "options.target = " << options.target << std::endl;
os << std::endl << "options.pa_socket = " << options.pa_socket << std::endl;
os << "options.enable_mtls = " << options.enable_mtls << std::endl;
os << "options.pem_cert_chain = " << options.pem_cert_chain << std::endl;
os << "options.pem_private_key = " << options.pem_private_key << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/ate/ate_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AteClient {
struct Options {
// Endpoint address in IP or DNS format including port number. For example:
// "localhost:5000".
std::string target;
std::string pa_socket;

// Set to true to enable mTLS connection. When set to false, the connection
// is established with insecure credentials.
Expand Down
2 changes: 1 addition & 1 deletion src/ate/ate_dll.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ DLLEXPORT void CreateClient(

// convert from ate_client_ptr to AteClient::Options
o.enable_mtls = options->enable_mtls;
o.target = options->target;
o.pa_socket = options->pa_socket;
if (o.enable_mtls) {
// Load the PEM data from the pointed files
absl::Status s =
Expand Down
4 changes: 2 additions & 2 deletions src/ate/ate_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "src/pa/proto/pa.grpc.pb.h"
#include "src/version/version.h"

ABSL_FLAG(std::string, target, "",
ABSL_FLAG(std::string, pa_socket, "",
"host:port of the target provisioning appliance server.");
ABSL_FLAG(bool, enable_mtls, false, "Enable mTLS secure channel.");
ABSL_FLAG(std::string, client_key, "",
Expand Down Expand Up @@ -64,7 +64,7 @@ int main(int argc, char **argv) {

AteClient::Options options;
options.enable_mtls = absl::GetFlag(FLAGS_enable_mtls);
options.target = absl::GetFlag(FLAGS_target);
options.pa_socket = absl::GetFlag(FLAGS_pa_socket);
if (options.enable_mtls) {
std::unordered_map<absl::Flag<std::string> *, std::string *> pem_options = {
{&FLAGS_client_key, &options.pem_private_key},
Expand Down
19 changes: 19 additions & 0 deletions src/ate/test_programs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

package(default_visibility = ["//visibility:public"])

cc_binary(
name = "cp",
srcs = ["cp.cc"],
deps = [
"//src/ate:ate_client",
"//src/version",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
],
)
117 changes: 117 additions & 0 deletions src/ate/test_programs/cp.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include <grpcpp/grpcpp.h>

#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>

#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/flags/usage_config.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "src/ate/ate_client.h"
#include "src/pa/proto/pa.grpc.pb.h"
#include "src/version/version.h"

/**
* PA configuration flags.
*/
ABSL_FLAG(std::string, pa_socket, "", "host:port of the PA server.");
ABSL_FLAG(std::string, sku, "", "SKU string to initialize the PA session.");
ABSL_FLAG(std::string, sku_auth_pw, "",
"SKU authorization password string to initialize the PA session.");

/**
* mTLS configuration flags.
*/
ABSL_FLAG(bool, enable_mtls, false, "Enable mTLS secure channel.");
ABSL_FLAG(std::string, client_key, "",
"File path to the PEM encoding of the client's private key.");
ABSL_FLAG(std::string, client_cert, "",
"File path to the PEM encoding of the client's certificate chain.");
ABSL_FLAG(std::string, ca_root_certs, "",
"File path to the PEM encoding of the server root certificates.");

namespace {
using provisioning::VersionFormatted;
using provisioning::ate::AteClient;

// Returns `filename` content in a std::string format
absl::StatusOr<std::string> ReadFile(const std::string &filename) {
auto output_stream = std::ostringstream();
std::ifstream file_stream(filename);
if (!file_stream.is_open()) {
return absl::InvalidArgumentError(
absl::StrCat("Unable to open file: \"", filename, "\""));
}
output_stream << file_stream.rdbuf();
return output_stream.str();
}

} // namespace

int main(int argc, char **argv) {
// Parse cmd line args.
absl::FlagsUsageConfig config;
config.version_string = &VersionFormatted;
absl::SetFlagsUsageConfig(config);
absl::ParseCommandLine(argc, argv);
LOG(INFO) << VersionFormatted();

// Extract and validate ATE client options.
AteClient::Options options;
options.pa_socket = absl::GetFlag(FLAGS_pa_socket);
options.enable_mtls = absl::GetFlag(FLAGS_enable_mtls);
if (options.enable_mtls) {
std::unordered_map<absl::Flag<std::string> *, std::string *> pem_options = {
{&FLAGS_client_key, &options.pem_private_key},
{&FLAGS_client_cert, &options.pem_cert_chain},
{&FLAGS_ca_root_certs, &options.pem_root_certs},
};

for (auto opt : pem_options) {
std::string filename = absl::GetFlag(*opt.first);
if (filename.empty()) {
LOG(ERROR) << "--" << absl::GetFlagReflectionHandle(*opt.first).Name()
<< " not set. This is a required argument when "
<< " --enable_mtls is set to true." << std::endl;
return -1;
}
auto result = ReadFile(filename);
if (!result.ok()) {
LOG(ERROR) << "--" << absl::GetFlagReflectionHandle(*opt.first).Name()
<< " " << result.status() << std::endl;
return -1;
}
*opt.second = result.value();
}
}

// Instantiate a client.
auto ate = AteClient::Create(options);

// Init/Close session.
grpc::Status status = ate->InitSession(absl::GetFlag(FLAGS_sku),
absl::GetFlag(FLAGS_sku_auth_pw));
if (!status.ok()) {
LOG(ERROR) << "InitSession failed with " << status.error_code() << ": "
<< status.error_message() << std::endl;
}

// TODO(#6): add CP test code here.

status = ate->CloseSession();
if (!status.ok()) {
LOG(ERROR) << "CloseSession failed with " << status.error_code() << ": "
<< status.error_message() << std::endl;
}

return 0;
}
Loading