|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use base64::prelude::*; |
| 4 | + |
| 5 | +use oasis_runtime_sdk::{ |
| 6 | + core::{ |
| 7 | + common::crypto::signature::{PublicKey, Signature}, |
| 8 | + consensus::registry::{EndorsedCapabilityTEE, Node}, |
| 9 | + host::attestation::{LabelAttestation, ATTEST_LABELS_SIGNATURE_CONTEXT}, |
| 10 | + }, |
| 11 | + modules::rofl::{ |
| 12 | + policy::{AllowedEndorsement, EndorsementPolicyEvaluator}, |
| 13 | + Error, |
| 14 | + }, |
| 15 | + types::address::Address, |
| 16 | + Context, |
| 17 | +}; |
| 18 | + |
| 19 | +use crate::{state, types::InstanceId}; |
| 20 | + |
| 21 | +/// Name of the ROFL app instance metadata key used to store the provider attestation. |
| 22 | +const METADATA_KEY_POLICY_PROVIDER_ATTESTATION: &str = "net.oasis.policy.provider"; |
| 23 | +/// Name of the provider label set by the scheduler. |
| 24 | +pub const LABEL_PROVIDER: &str = "net.oasis.provider"; |
| 25 | + |
| 26 | +/// Value of the `LABEL_PROVIDER` label as set by the scheduler. |
| 27 | +#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)] |
| 28 | +pub struct ProviderLabel { |
| 29 | + /// Address of the provider. |
| 30 | + pub provider: Address, |
| 31 | + /// Instance identifier. |
| 32 | + pub instance: InstanceId, |
| 33 | +} |
| 34 | + |
| 35 | +/// Provider attestation metadata stored in `METADATA_KEY_POLICY_PROVIDER_ATTESTATION` label. |
| 36 | +#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)] |
| 37 | +pub struct ProviderAttestation { |
| 38 | + /// A CBOR-serialized `LabelAttestation`. |
| 39 | + pub label_attestation: Vec<u8>, |
| 40 | + /// Signature from endorsing node. |
| 41 | + pub signature: Signature, |
| 42 | +} |
| 43 | + |
| 44 | +/// An endorsement policy evaluator that supports provider-related constraints via lookups |
| 45 | +/// in ROFL market module state. |
| 46 | +pub struct ProviderEndorsementPolicyEvaluator; |
| 47 | + |
| 48 | +impl EndorsementPolicyEvaluator for ProviderEndorsementPolicyEvaluator { |
| 49 | + fn verify_atom<C: Context>( |
| 50 | + _ctx: &C, |
| 51 | + policy: &AllowedEndorsement, |
| 52 | + ect: &EndorsedCapabilityTEE, |
| 53 | + endorsing_node_id: PublicKey, |
| 54 | + _endorsing_node: &Option<Node>, |
| 55 | + metadata: &BTreeMap<String, String>, |
| 56 | + ) -> Result<(), Error> { |
| 57 | + match policy { |
| 58 | + AllowedEndorsement::Provider(address) => { |
| 59 | + // Check if the endorsing node is endorsed by the given provider. |
| 60 | + let provider = state::get_provider(*address).ok_or(Error::NodeNotAllowed)?; |
| 61 | + if !provider.nodes.contains(&endorsing_node_id) { |
| 62 | + return Err(Error::NodeNotAllowed); |
| 63 | + } |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | + AllowedEndorsement::ProviderMachineAdmin(expected_admin) => { |
| 67 | + let pa: ProviderAttestation = cbor::from_slice( |
| 68 | + &BASE64_STANDARD |
| 69 | + .decode( |
| 70 | + metadata |
| 71 | + .get(METADATA_KEY_POLICY_PROVIDER_ATTESTATION) |
| 72 | + .ok_or(Error::NodeNotAllowed)?, |
| 73 | + ) |
| 74 | + .map_err(|_| Error::NodeNotAllowed)?, |
| 75 | + ) |
| 76 | + .map_err(|_| Error::NodeNotAllowed)?; |
| 77 | + |
| 78 | + // Verify node label attestation. |
| 79 | + pa.signature |
| 80 | + .verify( |
| 81 | + &endorsing_node_id, |
| 82 | + ATTEST_LABELS_SIGNATURE_CONTEXT, |
| 83 | + &pa.label_attestation, |
| 84 | + ) |
| 85 | + .map_err(|_| Error::NodeNotAllowed)?; |
| 86 | + let label_attestation: LabelAttestation = |
| 87 | + cbor::from_slice(&pa.label_attestation).map_err(|_| Error::NodeNotAllowed)?; |
| 88 | + if label_attestation.rak != ect.capability_tee.rak { |
| 89 | + return Err(Error::NodeNotAllowed); |
| 90 | + } |
| 91 | + |
| 92 | + // Extract provider label (set by the provider's scheduler). |
| 93 | + let provider_label: ProviderLabel = cbor::from_slice( |
| 94 | + &BASE64_STANDARD |
| 95 | + .decode( |
| 96 | + label_attestation |
| 97 | + .labels |
| 98 | + .get(LABEL_PROVIDER) |
| 99 | + .ok_or(Error::NodeNotAllowed)?, |
| 100 | + ) |
| 101 | + .map_err(|_| Error::NodeNotAllowed)?, |
| 102 | + ) |
| 103 | + .map_err(|_| Error::NodeNotAllowed)?; |
| 104 | + |
| 105 | + let provider = |
| 106 | + state::get_provider(provider_label.provider).ok_or(Error::NodeNotAllowed)?; |
| 107 | + if !provider.nodes.contains(&endorsing_node_id) { |
| 108 | + return Err(Error::NodeNotAllowed); |
| 109 | + } |
| 110 | + |
| 111 | + let instance = |
| 112 | + state::get_instance(provider_label.provider, provider_label.instance) |
| 113 | + .ok_or(Error::NodeNotAllowed)?; |
| 114 | + if &instance.admin != expected_admin { |
| 115 | + return Err(Error::NodeNotAllowed); |
| 116 | + } |
| 117 | + |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | + _ => Err(Error::NodeNotAllowed), |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments