Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions identity/aziot-identity-client-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ impl Client {
pub async fn create_module_identity(
&self,
module_name: &str,
managed_by: Option<String>,
) -> Result<Identity, std::io::Error> {
let uri = make_uri!("/identities/modules", self.api_version);

let body = create_module_identity::Request {
id_type: ID_TYPE_AZIOT.to_string(),
module_id: module_name.to_string(),
managed_by,
opts: None,
};

Expand All @@ -154,6 +156,7 @@ impl Client {
pub async fn create_local_identity(
&self,
module_name: &str,
managed_by: Option<String>,
opts: Option<aziot_identity_common::LocalIdOpts>,
) -> Result<Identity, std::io::Error> {
let uri = make_uri!("/identities/modules", self.api_version);
Expand All @@ -162,6 +165,7 @@ impl Client {
let body = create_module_identity::Request {
id_type: ID_TYPE_LOCAL.to_string(),
module_id: module_name.to_string(),
managed_by,
opts: opts.map(|opts| create_module_identity::CreateModuleOpts::LocalIdOpts(opts)),
};

Expand All @@ -179,6 +183,7 @@ impl Client {
pub async fn update_module_identity(
&self,
module_name: &str,
managed_by: Option<String>,
) -> Result<Identity, std::io::Error> {
let uri = make_uri!(
"/identities/modules",
Expand All @@ -190,6 +195,7 @@ impl Client {
let body = update_module_identity::Request {
id_type: ID_TYPE_AZIOT.to_string(),
module_id: module_name.to_string(),
managed_by,
};

let request = HttpRequest::put(self.connector.clone(), uri, body)
Expand Down
4 changes: 4 additions & 0 deletions identity/aziot-identity-common-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub mod create_module_identity {
pub id_type: String,
#[serde(rename = "moduleId")]
pub module_id: String,
#[serde(rename = "managedBy", skip_serializing_if = "Option::is_none")]
pub managed_by: Option<String>,
#[serde(flatten)]
pub opts: Option<CreateModuleOpts>,
}
Expand All @@ -86,6 +88,8 @@ pub mod update_module_identity {
pub id_type: String,
#[serde(rename = "moduleId")]
pub module_id: String,
#[serde(rename = "managedBy", skip_serializing_if = "Option::is_none")]
pub managed_by: Option<String>,
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
Expand Down
2 changes: 2 additions & 0 deletions identity/aziot-identity-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct AzureIoTSpec {
pub gen_id: Option<GenId>,
#[serde(rename = "auth", skip_serializing_if = "Option::is_none")]
pub auth: Option<AuthenticationInfo>,
#[serde(rename = "managedBy", skip_serializing_if = "Option::is_none")]
pub managed_by: Option<String>,
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ impl http_common::server::Route for Route {
};

let identity = match api
.create_identity(auth_id, Some(&body.id_type), &body.module_id, body.opts)
.create_identity(
auth_id,
Some(&body.id_type),
&body.module_id,
body.managed_by,
body.opts,
)
.await
{
Ok(id) => id,
Expand Down
45 changes: 39 additions & 6 deletions identity/aziot-identityd/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl IdentityManager {
pub async fn create_module_identity(
&self,
module_id: &str,
managed_by: Option<String>,
) -> Result<aziot_identity_common::Identity, Error> {
if module_id.trim().is_empty() {
return Err(Error::invalid_parameter(
Expand All @@ -126,7 +127,7 @@ impl IdentityManager {
.with_proxy(self.proxy_uri.clone());

let new_module = client
.create_module(module_id, None, None)
.create_module(module_id, None, managed_by)
.await
.map_err(Error::HubClient)?;

Expand All @@ -148,7 +149,7 @@ impl IdentityManager {
x509_thumbprint: None,
type_: Some(aziot_identity_common::hub::AuthType::Sas),
}),
None,
Some("Gordon".to_string()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, change to propagated one!

)
.await
.map_err(Error::HubClient)?;
Expand Down Expand Up @@ -177,6 +178,7 @@ impl IdentityManager {
auth: Some(aziot_identity_common::AuthenticationInfo::from(
module_credentials,
)),
managed_by: response.managed_by,
});
Ok(identity)
}
Expand Down Expand Up @@ -258,6 +260,7 @@ impl IdentityManager {
auth: Some(aziot_identity_common::AuthenticationInfo::from(
module_credentials,
)),
managed_by: response.managed_by,
});
Ok(identity)
}
Expand All @@ -275,6 +278,7 @@ impl IdentityManager {
module_id: None,
gen_id: None,
auth: Some(self.get_device_identity_key().await?),
managed_by: None,
},
)),
None => Err(Error::DeviceNotFound),
Expand Down Expand Up @@ -366,6 +370,7 @@ impl IdentityManager {
auth: Some(aziot_identity_common::AuthenticationInfo::from(
module_credentials,
)),
managed_by: module.managed_by,
});

Ok(identity)
Expand Down Expand Up @@ -415,6 +420,7 @@ impl IdentityManager {
module_id: Some(aziot_identity_common::ModuleId(module.module_id)),
gen_id: module.generation_id.map(aziot_identity_common::GenId),
auth: None, //Auth information can be requested via get_module_identity
managed_by: module.managed_by,
},
)
})
Expand Down Expand Up @@ -939,9 +945,9 @@ impl IdentityManager {

let hub_module_ids = self.get_module_identities().await?;

for m in hub_module_ids {
for m in &hub_module_ids {
if let aziot_identity_common::Identity::Aziot(m) = m {
if let Some(m) = m.module_id {
if let Some(m) = &m.module_id {
if !current_module_set.contains(&m) && prev_module_set.contains(&m) {
self.delete_module_identity(&m.0).await?;
log::info!("Hub identity {:?} removed", &m.0);
Expand All @@ -960,9 +966,36 @@ impl IdentityManager {
}
}

let hub_module_ids_and_managed_bys: std::collections::HashMap<
String,
Option<String>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_

> = hub_module_ids
.into_iter()
.filter_map(|i| {
if let aziot_identity_common::Identity::Aziot(i) = i {
if let Some(module_id) = &i.module_id {
Some((module_id.0.clone(), i.managed_by.clone()))
} else {
None
}
} else {
None
}
})
.collect();

let mut module_set = vec![];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uncertain on what is 'right' here

for m in current_module_set {
self.create_module_identity(&m.0).await?;
log::info!("Hub identity {:?} added", &m.0);
let managed_by = hub_module_ids_and_managed_bys.get(&m.0).cloned().flatten();

module_set.push((m, managed_by));
}

for (module_id, managed_by) in module_set {
// TODO: do we need to take previous identity and pass along?
self.create_module_identity(&module_id.0, managed_by.clone())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can just put None here, only host-level mods are affected!

.await?;
log::info!("Hub identity {:?} added", &module_id.0);
}

// Write out device state and settings.
Expand Down
3 changes: 2 additions & 1 deletion identity/aziot-identityd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ impl Api {
auth_id: auth::AuthId,
id_type: Option<&str>,
module_id: &str,
managed_by: Option<String>,
opts: Option<aziot_identity_common_http::create_module_identity::CreateModuleOpts>,
) -> Result<aziot_identity_common::Identity, Error> {
if !self.authorizer.authorize(auth::Operation {
Expand All @@ -455,7 +456,7 @@ impl Api {
}

match_id_type!( id_type {
ID_TYPE_AZIOT => { self.id_manager.create_module_identity(module_id).await },
ID_TYPE_AZIOT => { self.id_manager.create_module_identity(module_id, managed_by).await },
ID_TYPE_LOCAL => {
if self.local_identities
.get(&aziot_identity_common::ModuleId(module_id.to_owned()))
Expand Down
1 change: 1 addition & 0 deletions test-common/src/client/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ fn test_identity(module_name: &str) -> Identity {
key_handle: Some(aziot_key_common::KeyHandle(format!("{}-key", module_name))),
cert_id: Some(format!("{}-cert", module_name)),
}),
managed_by: Some("test".to_string()),
})
}

Expand Down