Skip to content

Commit

Permalink
verify routing keys of incoming data session requests
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Jun 27, 2023
1 parent 91ae5a5 commit 89abd74
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 50 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

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

37 changes: 34 additions & 3 deletions file_store/src/cli/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
file_source,
heartbeat::{CellHeartbeat, CellHeartbeatIngestReport},
iot_packet::IotValidPacket,
mobile_session::DataTransferSessionIngestReport,
mobile_session::{DataTransferSessionIngestReport, InvalidDataTransferIngestReport},
mobile_subscriber::{SubscriberLocationIngestReport, VerifiedSubscriberLocationIngestReport},
speedtest::{CellSpeedtest, CellSpeedtestIngestReport},
traits::MsgDecode,
Expand All @@ -15,11 +15,12 @@ use futures::stream::StreamExt;
use helium_crypto::PublicKey;
use helium_proto::{
services::{
packet_verifier::ValidDataTransferSession as ValidDataTransferSessionProto,
poc_lora::{LoraBeaconIngestReportV1, LoraPocV1, LoraWitnessIngestReportV1},
poc_mobile::{
mobile_reward_share::Reward, CellHeartbeatIngestReportV1, CellHeartbeatReqV1,
Heartbeat, MobileRewardShare, RadioRewardShare, SpeedtestAvg, SpeedtestIngestReportV1,
SpeedtestReqV1,
Heartbeat, InvalidDataTransferIngestReportV1, MobileRewardShare, RadioRewardShare,
SpeedtestAvg, SpeedtestIngestReportV1, SpeedtestReqV1,
},
router::PacketRouterPacketReportV1,
},
Expand Down Expand Up @@ -78,6 +79,36 @@ impl Cmd {
"timestamp": dtr.report.data_transfer_usage.timestamp,
}))?;
}
FileType::InvalidDataTransferSessionIngestReport => {
let msg: InvalidDataTransferIngestReport =
InvalidDataTransferIngestReportV1::decode(msg)?.try_into()?;
print_json(&json!({
"invalid_reason": msg.reason,
"invalid_timestamp": msg.timestamp,
"received_timestamp": msg.report.received_timestamp,
"reward_cancelled": msg.report.report.reward_cancelled,
"hotspot_key": PublicKey::try_from(msg.report.report.data_transfer_usage.pub_key)?,
"upload_bytes": msg.report.report.data_transfer_usage.upload_bytes,
"download_bytes": msg.report.report.data_transfer_usage.download_bytes,
"radio_access_technology": msg.report.report.data_transfer_usage.radio_access_technology,
"event_id": msg.report.report.data_transfer_usage.event_id,
"payer": PublicKey::try_from(msg.report.report.data_transfer_usage.payer)?,
"event_timestamp": msg.report.report.data_transfer_usage.timestamp,
}))?;
}
FileType::ValidDataTransferSession => {
let msg = ValidDataTransferSessionProto::decode(msg)?;
print_json(&json!({
"pub_key": PublicKey::try_from(msg.pub_key)?,
"upload_bytes": msg.upload_bytes,
"download_bytes": msg.download_bytes,
"num_dcs": msg.num_dcs,
"upload_bytes": msg.upload_bytes,
"payer": PublicKey::try_from(msg.payer)?,
"first_timestamp": msg.first_timestamp,
"last_timestamp": msg.last_timestamp,
}))?;
}
FileType::IotBeaconIngestReport => {
let dec_msg = LoraBeaconIngestReportV1::decode(msg)?;
let json = json!({
Expand Down
12 changes: 12 additions & 0 deletions file_store/src/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub const INVALID_PACKET: &str = "invalid_packet";
pub const NON_REWARDABLE_PACKET: &str = "non_rewardable_packet";
pub const IOT_REWARD_SHARE: &str = "iot_reward_share";
pub const DATA_TRANSFER_SESSION_INGEST_REPORT: &str = "data_transfer_session_ingest_report";
pub const INVALID_DATA_TRANSFER_SESSION_INGEST_REPORT: &str =
"invalid_data_transfer_session_ingest_report";
pub const VALID_DATA_TRANSFER_SESSION: &str = "valid_data_transfer_session";
pub const PRICE_REPORT: &str = "price_report";
pub const MOBILE_REWARD_SHARE: &str = "mobile_reward_share";
Expand Down Expand Up @@ -151,6 +153,7 @@ pub enum FileType {
NonRewardablePacket,
IotRewardShare,
DataTransferSessionIngestReport,
InvalidDataTransferSessionIngestReport,
ValidDataTransferSession,
PriceReport,
MobileRewardShare,
Expand Down Expand Up @@ -190,6 +193,9 @@ impl fmt::Display for FileType {
Self::NonRewardablePacket => NON_REWARDABLE_PACKET,
Self::IotRewardShare => IOT_REWARD_SHARE,
Self::DataTransferSessionIngestReport => DATA_TRANSFER_SESSION_INGEST_REPORT,
Self::InvalidDataTransferSessionIngestReport => {
INVALID_DATA_TRANSFER_SESSION_INGEST_REPORT
}
Self::ValidDataTransferSession => VALID_DATA_TRANSFER_SESSION,
Self::PriceReport => PRICE_REPORT,
Self::MobileRewardShare => MOBILE_REWARD_SHARE,
Expand Down Expand Up @@ -229,6 +235,9 @@ impl FileType {
Self::NonRewardablePacket => NON_REWARDABLE_PACKET,
Self::IotRewardShare => IOT_REWARD_SHARE,
Self::DataTransferSessionIngestReport => DATA_TRANSFER_SESSION_INGEST_REPORT,
Self::InvalidDataTransferSessionIngestReport => {
INVALID_DATA_TRANSFER_SESSION_INGEST_REPORT
}
Self::ValidDataTransferSession => VALID_DATA_TRANSFER_SESSION,
Self::PriceReport => PRICE_REPORT,
Self::MobileRewardShare => MOBILE_REWARD_SHARE,
Expand Down Expand Up @@ -268,6 +277,9 @@ impl FromStr for FileType {
NON_REWARDABLE_PACKET => Self::NonRewardablePacket,
IOT_REWARD_SHARE => Self::IotRewardShare,
DATA_TRANSFER_SESSION_INGEST_REPORT => Self::DataTransferSessionIngestReport,
INVALID_DATA_TRANSFER_SESSION_INGEST_REPORT => {
Self::InvalidDataTransferSessionIngestReport
}
VALID_DATA_TRANSFER_SESSION => Self::ValidDataTransferSession,
PRICE_REPORT => Self::PriceReport,
MOBILE_REWARD_SHARE => Self::MobileRewardShare,
Expand Down
124 changes: 115 additions & 9 deletions file_store/src/mobile_session.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use crate::{
traits::{MsgDecode, MsgTimestamp, TimestampDecode},
error::DecodeError,
traits::{MsgDecode, MsgTimestamp, TimestampDecode, TimestampEncode},
Error, Result,
};
use chrono::{DateTime, Utc};
use helium_crypto::PublicKeyBinary;
use helium_proto::services::poc_mobile::{
invalid_data_transfer_ingest_report_v1::DataTransferIngestReportStatus,
DataTransferEvent as DataTransferEventProto, DataTransferRadioAccessTechnology,
DataTransferSessionIngestReportV1, DataTransferSessionReqV1,
DataTransferSessionIngestReportV1, DataTransferSessionReqV1, InvalidDataTransferIngestReportV1,
};
use serde::Serialize;

impl MsgTimestamp<Result<DateTime<Utc>>> for DataTransferSessionIngestReportV1 {
fn timestamp(&self) -> Result<DateTime<Utc>> {
self.received_timestamp.to_timestamp_millis()
}
}
use serde::Serialize;

#[derive(Serialize, Clone, Debug)]
pub struct DataTransferSessionIngestReport {
Expand All @@ -26,6 +23,18 @@ impl MsgDecode for DataTransferSessionIngestReport {
type Msg = DataTransferSessionIngestReportV1;
}

impl MsgTimestamp<Result<DateTime<Utc>>> for DataTransferSessionIngestReportV1 {
fn timestamp(&self) -> Result<DateTime<Utc>> {
self.received_timestamp.to_timestamp_millis()
}
}

impl MsgTimestamp<u64> for DataTransferSessionIngestReport {
fn timestamp(&self) -> u64 {
self.received_timestamp.encode_timestamp_millis()
}
}

impl TryFrom<DataTransferSessionIngestReportV1> for DataTransferSessionIngestReport {
type Error = Error;

Expand All @@ -40,6 +49,69 @@ impl TryFrom<DataTransferSessionIngestReportV1> for DataTransferSessionIngestRep
}
}

impl From<DataTransferSessionIngestReport> for DataTransferSessionIngestReportV1 {
fn from(v: DataTransferSessionIngestReport) -> Self {
let received_timestamp = v.timestamp();
let report: DataTransferSessionReqV1 = v.report.into();
Self {
report: Some(report),
received_timestamp,
}
}
}

#[derive(Serialize, Clone, Debug)]
pub struct InvalidDataTransferIngestReport {
pub report: DataTransferSessionIngestReport,
pub reason: DataTransferIngestReportStatus,
pub timestamp: DateTime<Utc>,
}

impl MsgDecode for InvalidDataTransferIngestReport {
type Msg = InvalidDataTransferIngestReportV1;
}

impl MsgTimestamp<Result<DateTime<Utc>>> for InvalidDataTransferIngestReportV1 {
fn timestamp(&self) -> Result<DateTime<Utc>> {
self.timestamp.to_timestamp_millis()
}
}

impl MsgTimestamp<u64> for InvalidDataTransferIngestReport {
fn timestamp(&self) -> u64 {
self.timestamp.encode_timestamp_millis()
}
}

impl TryFrom<InvalidDataTransferIngestReportV1> for InvalidDataTransferIngestReport {
type Error = Error;
fn try_from(v: InvalidDataTransferIngestReportV1) -> Result<Self> {
let reason = DataTransferIngestReportStatus::from_i32(v.reason).ok_or_else(|| {
DecodeError::unsupported_status_reason("invalid_data_transfer_session_reason", v.reason)
})?;
Ok(Self {
timestamp: v.timestamp()?,
report: v
.report
.ok_or_else(|| Error::not_found("data transfer session ingest report"))?
.try_into()?,
reason,
})
}
}

impl From<InvalidDataTransferIngestReport> for InvalidDataTransferIngestReportV1 {
fn from(v: InvalidDataTransferIngestReport) -> Self {
let timestamp = v.timestamp();
let report: DataTransferSessionIngestReportV1 = v.report.into();
Self {
report: Some(report),
reason: v.reason as i32,
timestamp,
}
}
}

#[derive(Serialize, Clone, Debug)]
pub struct DataTransferEvent {
pub pub_key: PublicKeyBinary,
Expand All @@ -54,7 +126,13 @@ pub struct DataTransferEvent {

impl MsgTimestamp<Result<DateTime<Utc>>> for DataTransferEventProto {
fn timestamp(&self) -> Result<DateTime<Utc>> {
self.timestamp.to_timestamp_millis()
self.timestamp.to_timestamp()
}
}

impl MsgTimestamp<u64> for DataTransferEvent {
fn timestamp(&self) -> u64 {
self.timestamp.encode_timestamp()
}
}

Expand All @@ -81,6 +159,22 @@ impl TryFrom<DataTransferEventProto> for DataTransferEvent {
}
}

impl From<DataTransferEvent> for DataTransferEventProto {
fn from(v: DataTransferEvent) -> Self {
let timestamp = v.timestamp();
Self {
pub_key: v.pub_key.into(),
upload_bytes: v.upload_bytes,
download_bytes: v.download_bytes,
radio_access_technology: v.radio_access_technology as i32,
event_id: v.event_id,
payer: v.payer.into(),
timestamp,
signature: v.signature,
}
}
}

#[derive(Serialize, Clone, Debug)]
pub struct DataTransferSessionReq {
pub data_transfer_usage: DataTransferEvent,
Expand Down Expand Up @@ -108,3 +202,15 @@ impl TryFrom<DataTransferSessionReqV1> for DataTransferSessionReq {
})
}
}

impl From<DataTransferSessionReq> for DataTransferSessionReqV1 {
fn from(v: DataTransferSessionReq) -> Self {
let report: DataTransferEventProto = v.data_transfer_usage.into();
Self {
data_transfer_usage: Some(report),
reward_cancelled: v.reward_cancelled,
pub_key: v.pub_key.into(),
signature: v.signature,
}
}
}
Loading

0 comments on commit 89abd74

Please sign in to comment.