Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/risingwavelabs/risingwave i…
Browse files Browse the repository at this point in the history
…nto li0k/test_builder_oom
  • Loading branch information
Li0k committed May 20, 2024
2 parents 4bc3042 + 1cc36e1 commit 37f13c3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion src/expr/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ futures-util = "0.3"
itertools = { workspace = true }
linkme = { version = "0.3", features = ["used_linker"] }
num-traits = "0.2"
openssl = { version = "0.10", features = ["vendored"] }
parse-display = "0.9"
paste = "1"
prometheus = "0.13"
Expand Down
19 changes: 3 additions & 16 deletions src/expr/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,13 @@ pub enum ExprError {
#[error("invalid state: {0}")]
InvalidState(String),

#[error("error in cryptography: {0}")]
Cryptography(Box<CryptographyError>),

/// Function error message returned by UDF.
#[error("{0}")]
Custom(String),
}

#[derive(Debug)]
pub enum CryptographyStage {
Encrypt,
Decrypt,
}

#[derive(Debug, Error)]
#[error("{stage:?} stage, reason: {reason}")]
pub struct CryptographyError {
pub stage: CryptographyStage,
#[source]
pub reason: openssl::error::ErrorStack,
/// Error from a function call.
#[error("{0}")]
Function(#[source] Box<dyn std::error::Error + Send + Sync>),
}

static_assertions::const_assert_eq!(std::mem::size_of::<ExprError>(), 40);
Expand Down
2 changes: 1 addition & 1 deletion src/expr/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ pub mod sig;
pub mod table_function;
pub mod window_function;

pub use error::{ContextUnavailable, CryptographyError, CryptographyStage, ExprError, Result};
pub use error::{ContextUnavailable, ExprError, Result};
pub use risingwave_common::{bail, ensure};
pub use risingwave_expr_macro::*;
44 changes: 28 additions & 16 deletions src/expr/impl/src/scalar/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::sync::LazyLock;
use openssl::error::ErrorStack;
use openssl::symm::{Cipher, Crypter, Mode as CipherMode};
use regex::Regex;
use risingwave_expr::{function, CryptographyError, CryptographyStage, ExprError, Result};
use risingwave_expr::{function, ExprError, Result};

#[derive(Debug, Clone, PartialEq)]
enum Algorithm {
Expand Down Expand Up @@ -129,14 +129,13 @@ impl CipherConfig {
})
}

fn eval(&self, input: &[u8], stage: CryptographyStage) -> Result<Box<[u8]>> {
fn eval(&self, input: &[u8], stage: CryptographyStage) -> Result<Box<[u8]>, CryptographyError> {
let operation = match stage {
CryptographyStage::Encrypt => CipherMode::Encrypt,
CryptographyStage::Decrypt => CipherMode::Decrypt,
};
self.eval_inner(input, operation).map_err(|reason| {
ExprError::Cryptography(Box::new(CryptographyError { stage, reason }))
})
self.eval_inner(input, operation)
.map_err(|reason| CryptographyError { stage, reason })
}

fn eval_inner(
Expand All @@ -163,18 +162,32 @@ impl CipherConfig {
"decrypt(bytea, bytea, varchar) -> bytea",
prebuild = "CipherConfig::parse_cipher_config($1, $2)?"
)]
pub fn decrypt(data: &[u8], config: &CipherConfig) -> Result<Box<[u8]>> {
fn decrypt(data: &[u8], config: &CipherConfig) -> Result<Box<[u8]>, CryptographyError> {
config.eval(data, CryptographyStage::Decrypt)
}

#[function(
"encrypt(bytea, bytea, varchar) -> bytea",
prebuild = "CipherConfig::parse_cipher_config($1, $2)?"
)]
pub fn encrypt(data: &[u8], config: &CipherConfig) -> Result<Box<[u8]>> {
fn encrypt(data: &[u8], config: &CipherConfig) -> Result<Box<[u8]>, CryptographyError> {
config.eval(data, CryptographyStage::Encrypt)
}

#[derive(Debug)]
enum CryptographyStage {
Encrypt,
Decrypt,
}

#[derive(Debug, thiserror::Error)]
#[error("{stage:?} stage, reason: {reason}")]
struct CryptographyError {
pub stage: CryptographyStage,
#[source]
pub reason: openssl::error::ErrorStack,
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -197,24 +210,23 @@ mod test {

#[test]
fn encrypt_testcase() {
let encrypt_wrapper = |data: &[u8], key: &[u8], mode: &str| -> Result<Box<[u8]>> {
let config = CipherConfig::parse_cipher_config(key, mode)?;
encrypt(data, &config)
let encrypt_wrapper = |data: &[u8], key: &[u8], mode: &str| -> Box<[u8]> {
let config = CipherConfig::parse_cipher_config(key, mode).unwrap();
encrypt(data, &config).unwrap()
};
let decrypt_wrapper = |data: &[u8], key: &[u8], mode: &str| -> Result<Box<[u8]>> {
let config = CipherConfig::parse_cipher_config(key, mode)?;
decrypt(data, &config)
let decrypt_wrapper = |data: &[u8], key: &[u8], mode: &str| -> Box<[u8]> {
let config = CipherConfig::parse_cipher_config(key, mode).unwrap();
decrypt(data, &config).unwrap()
};
let key = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";

let encrypted = encrypt_wrapper(
b"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff",
key,
"aes-ecb/pad:none",
)
.unwrap();
);

let decrypted = decrypt_wrapper(&encrypted, key, "aes-ecb/pad:none").unwrap();
let decrypted = decrypt_wrapper(&encrypted, key, "aes-ecb/pad:none");
assert_eq!(
decrypted,
(*b"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff").into()
Expand Down
4 changes: 2 additions & 2 deletions src/expr/macro/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,13 @@ impl FunctionAttr {
ReturnTypeKind::Result => quote! {
match #output {
Ok(x) => Some(x),
Err(e) => { errors.push(e); None }
Err(e) => { errors.push(ExprError::Function(Box::new(e))); None }
}
},
ReturnTypeKind::ResultOption => quote! {
match #output {
Ok(x) => x,
Err(e) => { errors.push(e); None }
Err(e) => { errors.push(ExprError::Function(Box::new(e))); None }
}
},
};
Expand Down

0 comments on commit 37f13c3

Please sign in to comment.