Skip to content

Commit

Permalink
Add Trusted Information Retrieval example (#1049)
Browse files Browse the repository at this point in the history
This change adds a Trusted Information Retrieval example.

Fixes #1024
  • Loading branch information
ipetr0v authored Jun 3, 2020
1 parent fedfba2 commit 7392fed
Show file tree
Hide file tree
Showing 18 changed files with 891 additions and 18 deletions.
65 changes: 59 additions & 6 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"chat/module/rust",
"hello_world/module/rust",
"machine_learning/module/rust",
"trusted_information_retrieval/module/rust",
"private_set_intersection/module/rust",
"running_average/module/rust",
"rustfmt/module/rust",
Expand Down
36 changes: 36 additions & 0 deletions examples/trusted_information_retrieval/client/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Copyright 2020 The Project Oak Authors
#
# 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
#
# 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.
#

load("@rules_cc//cc:defs.bzl", "cc_binary")

package(
licenses = ["notice"],
)

cc_binary(
name = "client",
srcs = ["trusted_information_retrieval.cc"],
deps = [
"//examples/trusted_information_retrieval/proto:trusted_information_retrieval_cc_grpc",
"//oak/client:application_client",
"@com_github_google_glog//:glog",
"@com_github_grpc_grpc//:grpc++",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2020 The Project Oak Authors
*
* 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
*
* 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 "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_split.h"
#include "absl/types/optional.h"
#include "examples/trusted_information_retrieval/proto/trusted_information_retrieval.grpc.pb.h"
#include "examples/trusted_information_retrieval/proto/trusted_information_retrieval.pb.h"
#include "glog/logging.h"
#include "include/grpcpp/grpcpp.h"
#include "oak/client/application_client.h"

ABSL_FLAG(std::string, address, "localhost:8080", "Address of the Oak application to connect to");
ABSL_FLAG(std::vector<std::string>, location, std::vector<std::string>{},
"Requested location (latitude and longitude separated by comma)");
ABSL_FLAG(std::string, ca_cert, "", "Path to the PEM-encoded CA root certificate");

using ::oak::examples::trusted_information_retrieval::ListPointsOfInterestRequest;
using ::oak::examples::trusted_information_retrieval::ListPointsOfInterestResponse;
using ::oak::examples::trusted_information_retrieval::Location;
using ::oak::examples::trusted_information_retrieval::PointOfInterest;
using ::oak::examples::trusted_information_retrieval::TrustedInformationRetrieval;

void get_nearest_point_of_interest(TrustedInformationRetrieval::Stub* stub, float latitude,
float longitude) {
grpc::ClientContext context;
LOG(INFO) << "Getting nearest point of interest:";
ListPointsOfInterestRequest request;
Location* location = request.mutable_location();
location->set_latitude(latitude);
location->set_longitude(longitude);
ListPointsOfInterestResponse response;
grpc::Status status = stub->ListPointsOfInterest(&context, request, &response);
if (!status.ok()) {
LOG(ERROR) << "Could not get nearest point of interest: " << status.error_code() << ": "
<< status.error_message();
}
LOG(INFO) << "Response:";
LOG(INFO) << " - name: " << response.point_of_interest().name();
LOG(INFO) << " - latitude: " << response.point_of_interest().location().latitude();
LOG(INFO) << " - longitude: " << response.point_of_interest().location().longitude();
}

int main(int argc, char** argv) {
absl::ParseCommandLine(argc, argv);

std::string address = absl::GetFlag(FLAGS_address);
std::string ca_cert = oak::ApplicationClient::LoadRootCert(absl::GetFlag(FLAGS_ca_cert));
LOG(INFO) << "Connecting to Oak Application: " << address;

auto stub = TrustedInformationRetrieval::NewStub(
oak::ApplicationClient::CreateTlsChannel(address, ca_cert));

// Parse arguments.
auto location = absl::GetFlag(FLAGS_location);
if (location.size() != 2) {
LOG(FATAL) << "Incorrect number of coordinates: " << location.size() << " (expected 2)";
}
float latitude;
if (!absl::SimpleAtof(location.front(), &latitude) && latitude >= -90.0 && latitude <= 90.0) {
LOG(FATAL) << "Latitude must be a valid floating point number >=-90 and <= 90.";
}
float longitude;
if (!absl::SimpleAtof(location.back(), &longitude) && longitude >= -180.0 && longitude <= 180.0) {
LOG(FATAL) << "Longitude must be a valid floating point number >= -180 and <= 180.";
}

// Get nearest point of interest from the server.
get_nearest_point_of_interest(stub.get(), latitude, longitude);

return EXIT_SUCCESS;
}
32 changes: 32 additions & 0 deletions examples/trusted_information_retrieval/config/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# Copyright 2020 The Project Oak Authors
#
# 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
#
# 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.
#

load("//oak/common:app_config.bzl", "serialized_config")

package(
default_visibility = ["//examples/trusted_information_retrieval:__subpackages__"],
licenses = ["notice"],
)

exports_files(srcs = glob(["*.textproto"]))

serialized_config(
name = "config",
modules = {
"app": "//:examples/target/wasm32-unknown-unknown/release/trusted_information_retrieval.wasm",
},
textproto = ":config.textproto",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
initial_node_configuration: {
name: "main"
wasm_config: {
wasm_module_name: "app"
wasm_entrypoint_name: "grpc_oak_main"
}
}
1 change: 1 addition & 0 deletions examples/trusted_information_retrieval/database.xml

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions examples/trusted_information_retrieval/module/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "trusted_information_retrieval"
version = "0.1.0"
authors = ["Ivan Petrov <[email protected]>"]
edition = "2018"
license = "Apache-2.0"

[lib]
crate-type = ["cdylib"]

[dependencies]
itertools = "*"
log = "*"
oak = "=0.1.0"
oak_abi = "=0.1.0"
prost = "*"
serde = "*"
quick-xml = { version = "*", features = ["serialize"] }

[dev-dependencies]
assert_matches = "*"
oak_abi = "=0.1.0"
oak_runtime = "=0.1.0"
oak_tests = "=0.1.0"
maplit = "*"
simple_logger = "*"

[build-dependencies]
oak_utils = "*"
22 changes: 22 additions & 0 deletions examples/trusted_information_retrieval/module/rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Copyright 2020 The Project Oak Authors
//
// 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
//
// 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.
//

fn main() {
oak_utils::compile_protos(
&["../../proto/trusted_information_retrieval.proto"],
&["../../proto"],
);
}
Loading

0 comments on commit 7392fed

Please sign in to comment.