Skip to content

Commit

Permalink
Merge pull request #566 from helium/jg/add-mobile-pcs-keys
Browse files Browse the repository at this point in the history
allow mobile config service to register pcs signing keys
  • Loading branch information
jeffgrunewald authored Jul 10, 2023
2 parents e253815 + d93f624 commit 50a7437
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 112 deletions.
4 changes: 2 additions & 2 deletions 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 file_store/src/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mod tests {
cbsd_category: "category".to_string(),
cbsd_id: "id".to_string(),
signature: vec![],
coverage_object: vec![],
}),
};

Expand Down
14 changes: 11 additions & 3 deletions ingest/src/server_mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use futures_util::TryFutureExt;
use helium_crypto::{Network, PublicKey};
use helium_proto::services::poc_mobile::{
self, CellHeartbeatIngestReportV1, CellHeartbeatReqV1, CellHeartbeatRespV1,
DataTransferSessionIngestReportV1, DataTransferSessionReqV1, DataTransferSessionRespV1,
SpeedtestIngestReportV1, SpeedtestReqV1, SpeedtestRespV1, SubscriberLocationIngestReportV1,
SubscriberLocationReqV1, SubscriberLocationRespV1,
CoverageObjectReqV1, CoverageObjectRespV1, DataTransferSessionIngestReportV1,
DataTransferSessionReqV1, DataTransferSessionRespV1, SpeedtestIngestReportV1, SpeedtestReqV1,
SpeedtestRespV1, SubscriberLocationIngestReportV1, SubscriberLocationReqV1,
SubscriberLocationRespV1,
};
use std::path::Path;
use tonic::{metadata::MetadataValue, transport, Request, Response, Status};
Expand Down Expand Up @@ -171,6 +172,13 @@ impl poc_mobile::PocMobile for GrpcServer {
id: timestamp.to_string(),
}))
}

async fn submit_coverage_object(
&self,
_request: Request<CoverageObjectReqV1>,
) -> GrpcResult<CoverageObjectRespV1> {
unimplemented!()
}
}

pub async fn grpc_server(shutdown: triggered::Listener, settings: &Settings) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE key_role ADD VALUE IF NOT EXISTS 'pcs';
4 changes: 2 additions & 2 deletions mobile_config/src/admin_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
key_cache::{self, CacheKeys, KeyCache, KeyRole},
key_cache::{self, CacheKeys, KeyCache},
settings::Settings,
telemetry, verify_public_key, GrpcResult,
telemetry, verify_public_key, GrpcResult, KeyRole,
};
use anyhow::{anyhow, Result};
use chrono::Utc;
Expand Down
6 changes: 2 additions & 4 deletions mobile_config/src/authorization_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
key_cache::{KeyCache, KeyRole},
telemetry, verify_public_key, GrpcResult,
};
use crate::{key_cache::KeyCache, telemetry, verify_public_key, GrpcResult, KeyRole};
use chrono::Utc;
use file_store::traits::{MsgVerify, TimestampEncode};
use helium_crypto::{Keypair, PublicKey, Sign};
Expand Down Expand Up @@ -116,6 +113,7 @@ impl From<NetworkKeyRole> for KeyRole {
match role {
NetworkKeyRole::MobileRouter => KeyRole::Router,
NetworkKeyRole::MobileCarrier => KeyRole::Carrier,
NetworkKeyRole::MobilePcs => KeyRole::Pcs,
}
}
}
61 changes: 1 addition & 60 deletions mobile_config/src/key_cache.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::settings::Settings;
use crate::{settings::Settings, KeyRole};
use anyhow::anyhow;
use file_store::traits::MsgVerify;
use helium_crypto::{PublicKey, PublicKeyBinary};
use helium_proto::services::mobile_config::AdminKeyRole as ProtoKeyRole;
use serde::Serialize;
use std::collections::HashSet;
use tokio::sync::watch;

Expand Down Expand Up @@ -97,63 +95,6 @@ impl KeyCache {
}
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, sqlx::Type)]
#[sqlx(type_name = "key_role", rename_all = "snake_case")]
pub enum KeyRole {
Administrator,
Carrier,
Oracle,
Router,
}

impl KeyRole {
pub fn from_i32(v: i32) -> anyhow::Result<Self> {
ProtoKeyRole::from_i32(v)
.map(|kr| kr.into())
.ok_or_else(|| anyhow!("unsupported key role {}", v))
}
}

impl From<KeyRole> for ProtoKeyRole {
fn from(key_role: KeyRole) -> Self {
ProtoKeyRole::from(&key_role)
}
}

impl From<&KeyRole> for ProtoKeyRole {
fn from(skr: &KeyRole) -> Self {
match skr {
KeyRole::Administrator => ProtoKeyRole::Administrator,
KeyRole::Carrier => ProtoKeyRole::Carrier,
KeyRole::Oracle => ProtoKeyRole::Oracle,
KeyRole::Router => ProtoKeyRole::Router,
}
}
}

impl From<ProtoKeyRole> for KeyRole {
fn from(kt: ProtoKeyRole) -> Self {
match kt {
ProtoKeyRole::Administrator => KeyRole::Administrator,
ProtoKeyRole::Carrier => KeyRole::Carrier,
ProtoKeyRole::Oracle => KeyRole::Oracle,
ProtoKeyRole::Router => KeyRole::Router,
}
}
}

impl std::fmt::Display for KeyRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Administrator => "administrator",
Self::Carrier => "carrier",
Self::Oracle => "oracle",
Self::Router => "router",
};
f.write_str(s)
}
}

pub(crate) mod db {
use super::{CacheKeys, KeyRole, PublicKey, PublicKeyBinary};
use sqlx::Row;
Expand Down
69 changes: 69 additions & 0 deletions mobile_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use helium_crypto::PublicKey;
use helium_proto::services::mobile_config::AdminKeyRole as ProtoKeyRole;
use serde::Serialize;
use tokio_stream::wrappers::ReceiverStream;
use tonic::{Response, Status};

Expand All @@ -20,3 +22,70 @@ pub type GrpcStreamResult<T> = ReceiverStream<Result<T, Status>>;
pub fn verify_public_key(bytes: &[u8]) -> Result<PublicKey, Status> {
PublicKey::try_from(bytes).map_err(|_| Status::invalid_argument("invalid public key"))
}

#[derive(clap::ValueEnum, Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, sqlx::Type)]
#[sqlx(type_name = "key_role", rename_all = "snake_case")]
pub enum KeyRole {
Administrator,
Carrier,
Oracle,
Router,
Pcs,
}

impl KeyRole {
pub fn from_i32(v: i32) -> anyhow::Result<Self> {
ProtoKeyRole::from_i32(v)
.map(|kr| kr.into())
.ok_or_else(|| anyhow::anyhow!("unsupported key role {}", v))
}
}

impl From<KeyRole> for i32 {
fn from(value: KeyRole) -> Self {
ProtoKeyRole::from(value) as i32
}
}

impl From<KeyRole> for ProtoKeyRole {
fn from(key_role: KeyRole) -> Self {
Self::from(&key_role)
}
}

impl From<&KeyRole> for ProtoKeyRole {
fn from(skr: &KeyRole) -> Self {
match skr {
KeyRole::Administrator => Self::Administrator,
KeyRole::Carrier => Self::Carrier,
KeyRole::Oracle => Self::Oracle,
KeyRole::Router => Self::Router,
KeyRole::Pcs => Self::Pcs,
}
}
}

impl From<ProtoKeyRole> for KeyRole {
fn from(kt: ProtoKeyRole) -> Self {
match kt {
ProtoKeyRole::Administrator => Self::Administrator,
ProtoKeyRole::Carrier => Self::Carrier,
ProtoKeyRole::Oracle => Self::Oracle,
ProtoKeyRole::Router => Self::Router,
ProtoKeyRole::Pcs => Self::Pcs,
}
}
}

impl std::fmt::Display for KeyRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::Administrator => "administrator",
Self::Carrier => "carrier",
Self::Oracle => "oracle",
Self::Router => "router",
Self::Pcs => "pcs",
};
f.write_str(s)
}
}
4 changes: 3 additions & 1 deletion mobile_config_cli/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{cmds::gateway::GatewayInfo, current_timestamp, KeyRole, NetworkKeyRole, Result};
use crate::{cmds::gateway::GatewayInfo, current_timestamp, NetworkKeyRole, Result};

use base64::Engine;
use helium_crypto::{Keypair, PublicKey, Sign, Verify};
use helium_proto::{
Expand All @@ -10,6 +11,7 @@ use helium_proto::{
},
Message,
};
use mobile_config::KeyRole;
use std::str::FromStr;

pub struct AdminClient {
Expand Down
3 changes: 2 additions & 1 deletion mobile_config_cli/src/cmds/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{cmds::env::NetworkArg, KeyRole, NetworkKeyRole, Result};
use crate::{cmds::env::NetworkArg, NetworkKeyRole, Result};
use anyhow::Context;
use clap::{Args, Parser, Subcommand};
use helium_crypto::PublicKey;
use mobile_config::KeyRole;
use std::path::PathBuf;

pub mod admin;
Expand Down
46 changes: 7 additions & 39 deletions mobile_config_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
};

pub mod proto {
pub use helium_proto::services::mobile_config::{AdminKeyRole, NetworkKeyRole};
pub use helium_proto::services::mobile_config::NetworkKeyRole;
}

pub type Result<T = (), E = Error> = anyhow::Result<T, E>;
Expand Down Expand Up @@ -73,55 +73,22 @@ impl<S: ?Sized + serde::Serialize> PrettyJson for S {
}
}

#[derive(Debug, clap::ValueEnum, Clone, Copy, Serialize)]
pub enum KeyRole {
Administrator,
Carrier,
Router,
Oracle,
}

impl From<KeyRole> for proto::AdminKeyRole {
fn from(value: KeyRole) -> Self {
match value {
KeyRole::Administrator => Self::Administrator,
KeyRole::Carrier => Self::Carrier,
KeyRole::Router => Self::Router,
KeyRole::Oracle => Self::Oracle,
}
}
}

impl From<KeyRole> for i32 {
fn from(value: KeyRole) -> Self {
proto::AdminKeyRole::from(value) as i32
}
}

impl Display for KeyRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
KeyRole::Administrator => write!(f, "Administrator"),
KeyRole::Carrier => write!(f, "Carrier"),
KeyRole::Oracle => write!(f, "Oracle"),
KeyRole::Router => write!(f, "Router"),
}
}
}

#[derive(Debug, clap::ValueEnum, Clone, Copy, Serialize)]
pub enum NetworkKeyRole {
#[value(alias("carrier"))]
MobileCarrier,
#[value(alias("router"))]
MobileRouter,
#[value(alias("pcs"))]
MobilePcs,
}

impl From<NetworkKeyRole> for proto::NetworkKeyRole {
fn from(value: NetworkKeyRole) -> Self {
match value {
NetworkKeyRole::MobileRouter => Self::MobileRouter,
NetworkKeyRole::MobileCarrier => Self::MobileCarrier,
NetworkKeyRole::MobilePcs => Self::MobilePcs,
}
}
}
Expand All @@ -135,8 +102,9 @@ impl From<NetworkKeyRole> for i32 {
impl Display for NetworkKeyRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NetworkKeyRole::MobileCarrier => write!(f, "Carrier"),
NetworkKeyRole::MobileRouter => write!(f, "Router"),
NetworkKeyRole::MobileCarrier => write!(f, "carrier"),
NetworkKeyRole::MobileRouter => write!(f, "router"),
NetworkKeyRole::MobilePcs => write!(f, "pcs"),
}
}
}
1 change: 1 addition & 0 deletions mobile_verifier/src/heartbeats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ impl Heartbeat {
cell_type: self.cell_type.unwrap_or(CellType::Neutrino430) as i32, // Is this the right default?
validity: self.validity as i32,
timestamp: self.timestamp.timestamp() as u64,
coverage_object: Vec::with_capacity(0), // Placeholder so the project compiles
},
[],
)
Expand Down

0 comments on commit 50a7437

Please sign in to comment.