-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract boilerplate from hello_world example into SDK
This CR refactors the hello world example to move as much functionality as possible into the Oak SDK. Now to start your Oak trusted application, you need to: 1) Create an ApplicationHandler that accepts opaque bytes, and returns opaque bytes (which is all that Oak cares about). The interpretation is left to the application designer and is outside of the scope of Oak. 2) On startup, create an OakHandler containing your application handler, the endorsed evidence provided by the orchestrator (or similar), and an InstanceEncryptionKeyHandle. 3) Implement the service using the library of your choice (we use tonic in our example). In the handler for the oak-enabled method, you just need to write: `oak_session(self.oak_handler.clone(), request).await` This will start the stream processing, and either handle oak messages or pass application messages to your handler. * Hello world app is split into app, and app_service * app_service.rs is just the generic service implementation, including a call into the Oak server SDK helper. * app.rs contains the actual application business logic. * Notice the reduced amount of boilerplate in app_service. * oak_handler.rs contains server-tech-independent Oak message processing logic. * tonic.rs contains the helper to connect tonic requests to the oak_handler logic. Bug: b/356858826 Change-Id: I5dc3d95605f7ec8111190174a432c0a80dc7fbc3
- Loading branch information
Showing
9 changed files
with
239 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
oak_containers_examples/hello_world/trusted_app/src/app.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Copyright 2023 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. | ||
|
||
use oak_containers_sdk::ApplicationHandler; | ||
|
||
/// The actual business logic for the hello world application. | ||
pub struct HelloWorldApplicationHandler { | ||
pub application_config: Vec<u8>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl ApplicationHandler for HelloWorldApplicationHandler { | ||
/// This implementation is quite simple, since there's just a single request | ||
/// that is a string. In a real implementation, we'd probably | ||
/// deserialize into a proto, and dispatch to various handlers from | ||
/// there. | ||
async fn handle(&self, request_bytes: &[u8]) -> anyhow::Result<Vec<u8>> { | ||
let name = String::from_utf8_lossy(request_bytes); | ||
let config_len = self.application_config.len(); | ||
Ok( | ||
format!( | ||
"Hello from the trusted side, {name}! Btw, the Trusted App has a config with a length of {config_len} bytes.", | ||
).into_bytes() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.