From 98c91683986595d4430f1770eaffe61ac5ce5ebd Mon Sep 17 00:00:00 2001 From: Gordon Wang <36049150+gordonwang0@users.noreply.github.com> Date: Tue, 23 Mar 2021 14:04:48 -0700 Subject: [PATCH] Add iotedge system reprovision (#4660) Adds a command to reprovision device with IoT Hub. Also restarts edged so that the new provisioning info is used. --- edgelet/Cargo.lock | 2 ++ edgelet/identity-client/src/client.rs | 2 +- edgelet/iotedge/Cargo.toml | 2 ++ edgelet/iotedge/src/main.rs | 5 +++++ edgelet/iotedge/src/system.rs | 25 +++++++++++++++++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/edgelet/Cargo.lock b/edgelet/Cargo.lock index a31e56c9ca1..682d8f2793b 100644 --- a/edgelet/Cargo.lock +++ b/edgelet/Cargo.lock @@ -1306,6 +1306,7 @@ dependencies = [ "atty", "aziot-certd-config", "aziot-identity-common", + "aziot-identity-common-http", "aziot-identityd-config", "aziot-keyd-config", "aziot-keys-common", @@ -1331,6 +1332,7 @@ dependencies = [ "hyper", "hyper-proxy", "hyper-tls", + "identity-client", "lazy_static", "libc", "log", diff --git a/edgelet/identity-client/src/client.rs b/edgelet/identity-client/src/client.rs index 35437405c41..ad249fb3ce4 100644 --- a/edgelet/identity-client/src/client.rs +++ b/edgelet/identity-client/src/client.rs @@ -56,7 +56,7 @@ impl IdentityClient { let res = build_request_uri(&self.host, &uri) .into_future() .and_then(move |uri| { - request::<_, _, ()>(&client, hyper::Method::POST, &uri, Some(&body)) + request_no_content::<_, _>(&client, hyper::Method::POST, &uri, Some(&body)) }); Box::new(res) diff --git a/edgelet/iotedge/Cargo.toml b/edgelet/iotedge/Cargo.toml index 74a43399f1d..7995ebcf502 100644 --- a/edgelet/iotedge/Cargo.toml +++ b/edgelet/iotedge/Cargo.toml @@ -39,6 +39,7 @@ zip = "0.5.3" aziot-certd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "main" } aziot-identity-common = { git = "https://github.com/Azure/iot-identity-service", branch = "main" } +aziot-identity-common-http = { git = "https://github.com/Azure/iot-identity-service", branch = "main" } aziot-identityd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "main" } aziot-keyd-config = { git = "https://github.com/Azure/iot-identity-service", branch = "main" } aziot-keys-common = { git = "https://github.com/Azure/iot-identity-service", branch = "main" } @@ -51,6 +52,7 @@ edgelet-docker = { path = "../edgelet-docker" } edgelet-http = { path = "../edgelet-http" } edgelet-http-mgmt = { path = "../edgelet-http-mgmt" } edgelet-utils = { path = "../edgelet-utils" } +identity-client = { path = "../identity-client" } management = { path = "../management" } support-bundle = { path = "../support-bundle" } diff --git a/edgelet/iotedge/src/main.rs b/edgelet/iotedge/src/main.rs index 0a3b624d6b2..00dc0a497ad 100644 --- a/edgelet/iotedge/src/main.rs +++ b/edgelet/iotedge/src/main.rs @@ -313,6 +313,10 @@ fn run() -> Result<(), Error> { .required(true), ) ) + .subcommand( + SubCommand::with_name("reprovision") + .about("Reprovision device with IoT Hub.") + ) ) .subcommand( SubCommand::with_name("support-bundle") @@ -519,6 +523,7 @@ fn run() -> Result<(), Error> { log::Level::from_str(args.value_of("log_level").expect("Value is required")) .expect("Value is restricted to parsable fields"), ), + ("reprovision", Some(_args)) => System::reprovision(&mut tokio_runtime), (command, _) => { eprintln!("Unknown system subcommand {:?}", command); diff --git a/edgelet/iotedge/src/system.rs b/edgelet/iotedge/src/system.rs index 70eae81ad2f..ae116c2a25d 100644 --- a/edgelet/iotedge/src/system.rs +++ b/edgelet/iotedge/src/system.rs @@ -9,6 +9,9 @@ use aziotctl_common::{ SERVICE_DEFINITIONS as IS_SERVICES, }; +use aziot_identity_common_http::ApiVersion; +use identity_client::IdentityClient; + use crate::error::{Error, ErrorKind}; lazy_static! { @@ -71,4 +74,26 @@ impl System { Error::from(ErrorKind::System) }) } + + pub fn reprovision(runtime: &mut tokio::runtime::Runtime) -> Result<(), Error> { + let uri = url::Url::parse("unix:///run/aziot/identityd.sock") + .expect("hard-coded URI should parse"); + let client = IdentityClient::new(ApiVersion::V2020_09_01, &uri); + + runtime + .block_on(client.reprovision_device()) + .map_err(|err| { + eprintln!("Failed to reprovision: {}", err); + Error::from(ErrorKind::System) + })?; + + println!("Successfully reprovisioned with IoT Hub."); + + restart(&[&IOTEDGED]).map_err(|err| { + eprintln!("{:#?}", err); + Error::from(ErrorKind::System) + })?; + + Ok(()) + } }