use crate::{
decode_wire_avro_value, decode_wire_value, encode_typed_envelope, AvroValue, Client, Result,
DEFAULT_CODEC,
};
use serde_json::Value;
#[cfg(feature = "payload-protection")]
use crate::payload_protection::{ProtectionContext, ProtectionError};
#[cfg(feature = "payload-protection")]
use crate::{PayloadEnvelope, WorkflowDescription};
pub(crate) const PROTECTED_VALUE_V1_HEADER: [u8; 10] =
[0xc3, 0x01, 0x0b, 0xa7, 0x0c, 0x3f, 0xd6, 0xc3, 0x11, 0xca];
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Boundary {
WorkflowInput,
WorkflowResult,
ActivityInput,
ActivityResult,
SignalInput,
UpdateInput,
UpdateResult,
QueryInput,
QueryResult,
SideEffectResult,
}
#[cfg(feature = "payload-protection")]
impl Boundary {
pub(crate) fn wire_name(self) -> &'static str {
match self {
Self::WorkflowInput => "workflow_input",
Self::WorkflowResult => "workflow_result",
Self::ActivityInput => "activity_input",
Self::ActivityResult => "activity_result",
Self::SignalInput => "signal_input",
Self::UpdateInput => "update_input",
Self::UpdateResult => "update_result",
Self::QueryInput => "query_input",
Self::QueryResult => "query_result",
Self::SideEffectResult => "side_effect_result",
}
}
}
impl Client {
pub(super) fn encode_client_payload(
&self,
value: &AvroValue,
_boundary: Boundary,
) -> Result<Value> {
#[cfg(feature = "payload-protection")]
if let Some(protection) = &self.payload_protection {
let context = ProtectionContext::new(&self.namespace, _boundary)?;
return Ok(serde_json::to_value(protection.protect(value, context)?)?);
}
encode_typed_envelope(value, DEFAULT_CODEC)
}
pub(super) fn decode_client_payload(
&self,
value: &Value,
_boundary: Boundary,
) -> Result<AvroValue> {
#[cfg(feature = "payload-protection")]
if let Some(protection) = &self.payload_protection {
let context = ProtectionContext::new(&self.namespace, _boundary)?;
let envelope = exact_inline(value)?;
return Ok(protection.open(&envelope, context)?);
}
decode_wire_avro_value(value, DEFAULT_CODEC)
}
pub(super) fn decode_client_json(&self, value: &Value, boundary: Boundary) -> Result<Value> {
if self.protection_enabled() {
return self.decode_client_payload(value, boundary)?.into_json();
}
decode_wire_value(value, DEFAULT_CODEC)
}
pub(super) fn protection_enabled(&self) -> bool {
#[cfg(feature = "payload-protection")]
{
self.payload_protection.is_some()
}
#[cfg(not(feature = "payload-protection"))]
{
false
}
}
pub(super) fn refuse_unsupported_protected_boundary(&self) -> Result<()> {
#[cfg(feature = "payload-protection")]
if self.protection_enabled() {
return Err(ProtectionError::BoundaryUnsupported.into());
}
Ok(())
}
pub(super) fn assert_protected_response_namespace(
&self,
_namespace: Option<&Value>,
) -> Result<()> {
#[cfg(feature = "payload-protection")]
if self.protection_enabled()
&& _namespace.is_some_and(|value| value.as_str() != Some(&self.namespace))
{
return Err(ProtectionError::AuthenticationFailed.into());
}
Ok(())
}
pub(super) fn assert_protected_result_receipt(
&self,
_receipt: &Value,
_require_envelope: bool,
) -> Result<()> {
#[cfg(feature = "payload-protection")]
if self.protection_enabled() {
self.assert_protected_response_namespace(_receipt.get("namespace"))?;
let envelope = _receipt.get("result_envelope");
let has_plain_projection = _receipt
.get("result")
.into_iter()
.chain(_receipt.pointer("/control_plane/result"))
.any(|value| !safe_projection(value));
if (_require_envelope && !envelope.is_some_and(Value::is_object))
|| (has_plain_projection && !self.is_migration_envelope(envelope))
{
return Err(ProtectionError::ProtectionRequired.into());
}
}
Ok(())
}
pub(super) fn decode_workflow_description(
&self,
description: &mut crate::WorkflowDescription,
) -> Result<()> {
#[cfg(feature = "payload-protection")]
if self.protection_enabled() {
return self.decode_protected_workflow_description(description);
}
description.decode_payloads()
}
#[cfg(feature = "payload-protection")]
fn decode_protected_workflow_description(
&self,
description: &mut WorkflowDescription,
) -> Result<()> {
self.assert_protected_response_namespace(description.raw.get("namespace"))?;
let input = description
.raw
.get("input_envelope")
.filter(|value| !value.is_null());
let output = description
.output_envelope
.as_ref()
.filter(|value| !value.is_null());
let input_preview = description
.raw
.get("input")
.filter(|value| !value.is_null());
if (input_preview.is_some_and(|value| !safe_projection(value))
&& !self.is_migration_envelope(input))
|| (description
.output
.as_ref()
.is_some_and(|value| !safe_projection(value))
&& !self.is_migration_envelope(output))
{
return Err(ProtectionError::ProtectionRequired.into());
}
if let Some(input) = input {
self.decode_client_payload(input, Boundary::WorkflowInput)?;
} else if input_preview.is_some() {
return Err(ProtectionError::ProtectionRequired.into());
}
if let Some(output) = output {
let value = self.decode_client_payload(output, Boundary::WorkflowResult)?;
description.output = Some(value.clone().into_json()?);
description.output_avro_value = Some(value);
} else if description.is_completed() {
return Err(ProtectionError::ProtectionRequired.into());
}
Ok(())
}
#[cfg(feature = "payload-protection")]
fn is_migration_envelope(&self, value: Option<&Value>) -> bool {
self.payload_protection
.as_ref()
.is_some_and(|protection| protection.plaintext_reads_enabled())
&& value
.and_then(|value| exact_inline(value).ok())
.is_some_and(|envelope| crate::decode_avro_value(&envelope).is_ok())
}
}
#[cfg(feature = "payload-protection")]
pub(super) fn exact_inline(value: &Value) -> Result<PayloadEnvelope> {
if !value.as_object().is_some_and(|fields| fields.len() == 2)
|| value.get("codec").and_then(Value::as_str) != Some(DEFAULT_CODEC)
{
return Err(ProtectionError::EnvelopeUnsupported.into());
}
let blob = value
.get("blob")
.and_then(Value::as_str)
.ok_or(ProtectionError::EnvelopeUnsupported)?;
Ok(PayloadEnvelope {
codec: DEFAULT_CODEC.to_owned(),
blob: blob.to_owned(),
})
}
#[cfg(feature = "payload-protection")]
fn safe_projection(value: &Value) -> bool {
value.is_null()
|| (value.as_object().is_some_and(|fields| fields.len() == 2)
&& value["$type"] == "protected"
&& value["version"] == 1)
}