Skip to content

Commit

Permalink
RSDK-5194 - set client_ctx auth to a placeholder value (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog authored Oct 23, 2023
1 parent e981306 commit ff24565
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/viam/examples/dial/example_dial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ int main() {

// ensure we can create clients to the robot
auto gc = robot->resource_by_name<GenericClient>("generic1");
std::cout << "got generic client named " << gc->name() << std::endl;
if (gc) {
std::cout << "got generic client named " << gc->name() << std::endl;
}

robot->close();

return EXIT_SUCCESS;
}
6 changes: 6 additions & 0 deletions src/viam/examples/modules/complex/gizmo/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <google/protobuf/descriptor.h>

#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/registry/registry.hpp>
#include <viam/sdk/resource/resource.hpp>
#include <viam/sdk/rpc/server.hpp>
Expand Down Expand Up @@ -206,6 +207,7 @@ bool GizmoClient::do_one(std::string arg1) {
DoOneResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
request.set_arg1(arg1);
Expand All @@ -221,6 +223,7 @@ bool GizmoClient::do_one(std::string arg1) {
bool GizmoClient::do_one_client_stream(std::vector<std::string> arg1) {
DoOneClientStreamResponse response;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

auto writer(stub_->DoOneClientStream(&ctx, &response));
for (std::string arg : arg1) {
Expand All @@ -244,6 +247,7 @@ bool GizmoClient::do_one_client_stream(std::vector<std::string> arg1) {
std::vector<bool> GizmoClient::do_one_server_stream(std::string arg1) {
DoOneServerStreamRequest request;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
request.set_arg1(arg1);
Expand All @@ -264,6 +268,7 @@ std::vector<bool> GizmoClient::do_one_server_stream(std::string arg1) {

std::vector<bool> GizmoClient::do_one_bidi_stream(std::vector<std::string> arg1) {
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

auto stream(stub_->DoOneBiDiStream(&ctx));
for (std::string arg : arg1) {
Expand Down Expand Up @@ -296,6 +301,7 @@ std::string GizmoClient::do_two(bool arg1) {
DoTwoResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
request.set_arg1(arg1);
Expand Down
2 changes: 2 additions & 0 deletions src/viam/examples/modules/complex/summation/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <google/protobuf/descriptor.h>

#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/registry/registry.hpp>
#include <viam/sdk/resource/resource.hpp>
#include <viam/sdk/rpc/server.hpp>
Expand Down Expand Up @@ -96,6 +97,7 @@ double SummationClient::sum(std::vector<double> numbers) {
SumResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
for (double number : numbers) {
Expand Down
5 changes: 5 additions & 0 deletions src/viam/sdk/common/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <boost/optional/optional.hpp>
#include <boost/variant/get.hpp>
#include <boost/variant/variant.hpp>
#include <grpcpp/client_context.h>

#include <viam/api/common/v1/common.pb.h>

Expand Down Expand Up @@ -130,6 +131,10 @@ bool operator==(const response_metadata& lhs, const response_metadata& rhs) {
return lhs.captured_at == rhs.captured_at;
}

void set_client_ctx_authority(grpc::ClientContext& ctx) {
ctx.set_authority("viam-placeholder");
}

bool from_dm_from_extra(const AttributeMap& extra) {
if (!extra) {
return false;
Expand Down
8 changes: 8 additions & 0 deletions src/viam/sdk/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <boost/optional/optional.hpp>
#include <boost/variant/get.hpp>
#include <boost/variant/variant.hpp>
#include <grpcpp/client_context.h>

#include <viam/api/common/v1/common.pb.h>

Expand Down Expand Up @@ -66,6 +67,13 @@ std::string bytes_to_string(std::vector<unsigned char> const& b);
std::chrono::microseconds from_proto(const google::protobuf::Duration& proto);
google::protobuf::Duration to_proto(const std::chrono::microseconds& duration);

// the authority is sometimes set to an invalid uri on mac, causing `rust-utils` to fail
// to process gRPC requests. Fortunately we don't particularly care what the authority is
// set to within the ctx once a channel is built, so we can just set a placeholder value
// to appease rust and continue to rely on the gRPC channel to process requests correctly.
// For more details, see https://viam.atlassian.net/browse/RSDK-5194.
void set_client_ctx_authority(grpc::ClientContext& ctx);

/// @brief Set the boost trivial logger's severity depending on args.
/// @param argc The number of args.
/// @param argv The commandline arguments to parse.
Expand Down
9 changes: 9 additions & 0 deletions src/viam/sdk/components/base/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void BaseClient::move_straight(int64_t distance_mm, double mm_per_sec, const Att
viam::component::base::v1::MoveStraightResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
request.set_distance_mm(distance_mm);
Expand All @@ -47,6 +48,7 @@ void BaseClient::spin(double angle_deg, double degs_per_sec, const AttributeMap&
viam::component::base::v1::SpinResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
request.set_angle_deg(angle_deg);
Expand All @@ -66,6 +68,7 @@ void BaseClient::set_power(const Vector3& linear,
viam::component::base::v1::SetPowerResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_linear() = Vector3::to_proto(linear);
Expand All @@ -85,6 +88,7 @@ void BaseClient::set_velocity(const Vector3& linear,
viam::component::base::v1::SetVelocityResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_linear() = Vector3::to_proto(linear);
Expand All @@ -102,6 +106,7 @@ grpc::StatusCode BaseClient::stop(const AttributeMap& extra) {
viam::component::base::v1::StopResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -118,6 +123,7 @@ bool BaseClient::is_moving() {
viam::component::base::v1::IsMovingResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();

Expand All @@ -132,6 +138,7 @@ std::vector<GeometryConfig> BaseClient::get_geometries(const AttributeMap& extra
viam::common::v1::GetGeometriesRequest req;
viam::common::v1::GetGeometriesResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();
*req.mutable_extra() = map_to_struct(extra);
Expand All @@ -144,6 +151,7 @@ Base::properties BaseClient::get_properties(const AttributeMap& extra) {
component::base::v1::GetPropertiesRequest req;
component::base::v1::GetPropertiesResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();
*req.mutable_extra() = map_to_struct(extra);
Expand All @@ -157,6 +165,7 @@ AttributeMap BaseClient::do_command(const AttributeMap& command) {
viam::common::v1::DoCommandResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

const google::protobuf::Struct proto_command = map_to_struct(command);
*request.mutable_command() = proto_command;
Expand Down
12 changes: 12 additions & 0 deletions src/viam/sdk/components/board/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Board::status BoardClient::get_status(const AttributeMap& extra) {
viam::component::board::v1::StatusResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -43,6 +44,7 @@ void BoardClient::set_gpio(const std::string& pin, bool high, const AttributeMap
viam::component::board::v1::SetGPIOResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -59,6 +61,7 @@ bool BoardClient::get_gpio(const std::string& pin, const AttributeMap& extra) {
viam::component::board::v1::GetGPIOResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -76,6 +79,7 @@ double BoardClient::get_pwm_duty_cycle(const std::string& pin, const AttributeMa
viam::component::board::v1::PWMResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -95,6 +99,7 @@ void BoardClient::set_pwm_duty_cycle(const std::string& pin,
viam::component::board::v1::SetPWMResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -112,6 +117,7 @@ uint64_t BoardClient::get_pwm_frequency(const std::string& pin, const AttributeM
viam::component::board::v1::PWMFrequencyResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -131,6 +137,7 @@ void BoardClient::set_pwm_frequency(const std::string& pin,
viam::component::board::v1::SetPWMFrequencyResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -148,6 +155,7 @@ AttributeMap BoardClient::do_command(const AttributeMap& command) {
viam::common::v1::DoCommandResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

const google::protobuf::Struct proto_command = map_to_struct(command);
*request.mutable_command() = proto_command;
Expand All @@ -166,6 +174,7 @@ Board::analog_value BoardClient::read_analog(const std::string& analog_reader_na
viam::component::board::v1::ReadAnalogReaderResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

request.set_board_name(this->name());
request.set_analog_reader_name(analog_reader_name);
Expand All @@ -184,6 +193,7 @@ Board::digital_value BoardClient::read_digital_interrupt(const std::string& digi
viam::component::board::v1::GetDigitalInterruptValueResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

request.set_board_name(this->name());
request.set_digital_interrupt_name(digital_interrupt_name);
Expand All @@ -203,6 +213,7 @@ void BoardClient::set_power_mode(power_mode power_mode,
viam::component::board::v1::SetPowerModeResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -221,6 +232,7 @@ std::vector<GeometryConfig> BoardClient::get_geometries(const AttributeMap& extr
viam::common::v1::GetGeometriesRequest req;
viam::common::v1::GetGeometriesResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();
*req.mutable_extra() = map_to_struct(extra);
Expand Down
6 changes: 6 additions & 0 deletions src/viam/sdk/components/camera/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ AttributeMap CameraClient::do_command(AttributeMap command) {
viam::common::v1::DoCommandRequest req;
viam::common::v1::DoCommandResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

const google::protobuf::Struct proto_command = map_to_struct(command);
*req.mutable_command() = proto_command;
Expand All @@ -37,6 +38,7 @@ Camera::raw_image CameraClient::get_image(std::string mime_type, const Attribute
viam::component::camera::v1::GetImageRequest req;
viam::component::camera::v1::GetImageResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

Camera::normalize_mime_type(mime_type);

Expand All @@ -52,6 +54,7 @@ Camera::image_collection CameraClient::get_images() {
viam::component::camera::v1::GetImagesRequest req;
viam::component::camera::v1::GetImagesResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();

Expand All @@ -64,6 +67,7 @@ Camera::point_cloud CameraClient::get_point_cloud(std::string mime_type,
viam::component::camera::v1::GetPointCloudRequest req;
viam::component::camera::v1::GetPointCloudResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();
*req.mutable_mime_type() = mime_type;
Expand All @@ -77,6 +81,7 @@ std::vector<GeometryConfig> CameraClient::get_geometries(const AttributeMap& ext
viam::common::v1::GetGeometriesRequest req;
viam::common::v1::GetGeometriesResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();
*req.mutable_extra() = map_to_struct(extra);
Expand All @@ -89,6 +94,7 @@ Camera::properties CameraClient::get_properties() {
viam::component::camera::v1::GetPropertiesRequest req;
viam::component::camera::v1::GetPropertiesResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();

Expand Down
5 changes: 5 additions & 0 deletions src/viam/sdk/components/encoder/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Encoder::position EncoderClient::get_position(const AttributeMap& extra,
viam::component::encoder::v1::GetPositionResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
request.set_position_type(to_proto(position_type));
Expand All @@ -45,6 +46,7 @@ void EncoderClient::reset_position(const AttributeMap& extra) {
viam::component::encoder::v1::ResetPositionResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -60,6 +62,7 @@ Encoder::properties EncoderClient::get_properties(const AttributeMap& extra) {
viam::component::encoder::v1::GetPropertiesResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*request.mutable_name() = this->name();
*request.mutable_extra() = map_to_struct(extra);
Expand All @@ -75,6 +78,7 @@ std::vector<GeometryConfig> EncoderClient::get_geometries(const AttributeMap& ex
viam::common::v1::GetGeometriesRequest req;
viam::common::v1::GetGeometriesResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();
*req.mutable_extra() = map_to_struct(extra);
Expand All @@ -88,6 +92,7 @@ AttributeMap EncoderClient::do_command(AttributeMap command) {
viam::common::v1::DoCommandResponse response;

grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

const google::protobuf::Struct proto_command = map_to_struct(command);
*request.mutable_command() = proto_command;
Expand Down
3 changes: 3 additions & 0 deletions src/viam/sdk/components/generic/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <viam/api/component/generic/v1/generic.grpc.pb.h>

#include <viam/sdk/common/proto_type.hpp>
#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/config/resource.hpp>
#include <viam/sdk/robot/client.hpp>

Expand All @@ -21,6 +22,7 @@ AttributeMap GenericClient::do_command(AttributeMap command) {
viam::common::v1::DoCommandRequest req;
viam::common::v1::DoCommandResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

const google::protobuf::Struct proto_command = map_to_struct(command);
*req.mutable_command() = proto_command;
Expand All @@ -32,6 +34,7 @@ std::vector<GeometryConfig> GenericClient::get_geometries() {
viam::common::v1::GetGeometriesRequest req;
viam::common::v1::GetGeometriesResponse resp;
grpc::ClientContext ctx;
set_client_ctx_authority(ctx);

*req.mutable_name() = this->name();

Expand Down
Loading

0 comments on commit ff24565

Please sign in to comment.