Skip to content

Timestamp data type #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion packages/cipherstash-proxy-integration/src/map_params.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::common::{connect_with_tls, id, reset_schema, trace, PROXY};
use chrono::NaiveDate;
use chrono::prelude::*;

#[tokio::test]
async fn map_text() {
Expand Down Expand Up @@ -130,6 +130,32 @@ mod tests {
}
}

#[tokio::test]
async fn map_int8_as_biguint() {
trace();

let client = connect_with_tls(PROXY).await;

let id = id();
let encrypted_int8: i64 = 42;

let sql = "INSERT INTO encrypted (id, encrypted_int8_as_biguint) VALUES ($1, $2)";
client.query(sql, &[&id, &encrypted_int8]).await.unwrap();

let sql = "SELECT id, encrypted_int8_as_biguint FROM encrypted WHERE id = $1";
let rows = client.query(sql, &[&id]).await.unwrap();

assert_eq!(rows.len(), 1);

for row in rows {
let result_id: i64 = row.get("id");
let result_int: i64 = row.get("encrypted_int8_as_biguint");

assert_eq!(id, result_id);
assert_eq!(encrypted_int8, result_int);
}
}

#[tokio::test]
async fn map_float8() {
trace();
Expand Down Expand Up @@ -182,6 +208,66 @@ mod tests {
}
}

#[tokio::test]
async fn map_timestamp() {
trace();

let client = connect_with_tls(PROXY).await;

let id = id();
let encrypted_timestamp = "2000-10-01T17:30:00Z".parse::<DateTime<Utc>>().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be a good idea to add another test case set in a different time zone than UTC?


let sql = "INSERT INTO encrypted (id, encrypted_timestamp) VALUES ($1, $2)";
client
.query(sql, &[&id, &encrypted_timestamp])
.await
.unwrap();

let sql = "SELECT id, encrypted_timestamp FROM encrypted WHERE id = $1";
let rows = client.query(sql, &[&id]).await.unwrap();

assert_eq!(rows.len(), 1);

for row in rows {
let result_id: i64 = row.get("id");
let result: DateTime<Utc> = row.get("encrypted_timestamp");

assert_eq!(id, result_id);
assert_eq!(encrypted_timestamp, result);
}
}

#[tokio::test]
async fn map_timestamp_with_tz() {
trace();

use chrono_tz::Australia::Sydney;

let client = connect_with_tls(PROXY).await;

let id = id();
let encrypted_timestamp = "2000-10-01T17:30:00Z".parse::<DateTime<Utc>>().unwrap();

let sql = "INSERT INTO encrypted (id, encrypted_timestamp) VALUES ($1, $2)";
client
.query(sql, &[&id, &encrypted_timestamp])
.await
.unwrap();

let sql = "SELECT id, encrypted_timestamp FROM encrypted WHERE id = $1";
let rows = client.query(sql, &[&id]).await.unwrap();

assert_eq!(rows.len(), 1);

for row in rows {
let result_id: i64 = row.get("id");
let result: DateTime<Utc> = row.get("encrypted_timestamp");

assert_eq!(id, result_id);
assert_eq!(encrypted_timestamp, result);
}
}

#[tokio::test]
async fn map_jsonb() {
trace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ pub enum CastAs {
BigInt,
Boolean,
Date,
Timestamp,
Real,
Double,
Int,
SmallInt,
#[serde(rename = "big_uint")]
BigUInt,
#[default]
Text,
#[serde(rename = "jsonb")]
Expand Down Expand Up @@ -126,8 +129,10 @@ impl From<CastAs> for ColumnType {
CastAs::Int => ColumnType::Int,
CastAs::Boolean => ColumnType::Boolean,
CastAs::Date => ColumnType::Date,
CastAs::Timestamp => ColumnType::Timestamp,
CastAs::Real | CastAs::Double => ColumnType::Float,
CastAs::Text => ColumnType::Utf8Str,
CastAs::BigUInt => ColumnType::BigUInt,
CastAs::JsonB => ColumnType::JsonB,
}
}
Expand Down
14 changes: 8 additions & 6 deletions packages/cipherstash-proxy/src/postgresql/data/from_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use bigdecimal::BigDecimal;
use bytes::BytesMut;
use chrono::NaiveDate;
use chrono::{DateTime, NaiveDate, Utc};
use cipherstash_client::{encryption::Plaintext, schema::ColumnType};
use postgres_types::FromSql;
use postgres_types::Type;
Expand Down Expand Up @@ -153,9 +153,10 @@ fn text_from_sql(
.map_err(|_| MappingError::CouldNotParseParameter)
.map(Plaintext::new),

(&Type::TIMESTAMPTZ, _) => {
unimplemented!("TIMESTAMPTZ")
}
(&Type::TIMESTAMP | &Type::TIMESTAMPTZ, ColumnType::Timestamp) => val
.parse::<DateTime<Utc>>()
.map_err(|_| MappingError::CouldNotParseParameter)
.map(Plaintext::new),
(&Type::TEXT | &Type::JSONB, ColumnType::JsonB) => {
serde_json::from_str::<serde_json::Value>(val)
.map_err(|_| MappingError::CouldNotParseParameter)
Expand Down Expand Up @@ -237,8 +238,9 @@ fn binary_from_sql(
(&Type::TEXT, _) => parse_bytes_from_sql::<String>(bytes, pg_type)
.and_then(|val| text_from_sql(&val, pg_type, col_type)),

// TODO: timestamps
(_, ColumnType::Timestamp) => unimplemented!("TIMESTAMPTZ"),
(&Type::TIMESTAMP | &Type::TIMESTAMPTZ, ColumnType::Timestamp) => {
parse_bytes_from_sql::<DateTime<Utc>>(bytes, pg_type).map(Plaintext::new)
}

// Unsupported
(ty, _) => Err(MappingError::UnsupportedParameterType {
Expand Down
8 changes: 6 additions & 2 deletions packages/cipherstash-proxy/src/postgresql/data/to_sql.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::error::EncryptError;
use crate::{error::Error, postgresql::format_code::FormatCode};
use bigdecimal::FromPrimitive;
use bytes::BytesMut;
use cipherstash_client::encryption::Plaintext;
use postgres_types::ToSql;
use postgres_types::Type;
use rust_decimal::Decimal;

pub fn to_sql(plaintext: &Plaintext, format_code: &FormatCode) -> Result<Option<BytesMut>, Error> {
let bytes = match format_code {
Expand Down Expand Up @@ -47,8 +49,10 @@ fn binary_to_sql(plaintext: &Plaintext) -> Result<BytesMut, Error> {
Plaintext::Utf8Str(x) => x.to_sql_checked(&Type::TEXT, &mut bytes),
Plaintext::JsonB(x) => x.to_sql_checked(&Type::JSONB, &mut bytes),
Plaintext::Decimal(x) => x.to_sql_checked(&Type::NUMERIC, &mut bytes),
// TODO: Implement these
Plaintext::BigUInt(_x) => unimplemented!(),
Plaintext::BigUInt(x) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this needed here for timestamp, or is this a separate implementation? If so, is this covered with a test case?

let d = x.map(|x| Decimal::from_u64(x)).flatten();
d.to_sql_checked(&Type::NUMERIC, &mut bytes)
}
};

match result {
Expand Down
7 changes: 7 additions & 0 deletions tests/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CREATE TABLE encrypted (
encrypted_int2 eql_v1_encrypted,
encrypted_int4 eql_v1_encrypted,
encrypted_int8 eql_v1_encrypted,
encrypted_int8_as_biguint eql_v1_encrypted,
encrypted_float8 eql_v1_encrypted,
encrypted_date eql_v1_encrypted,
encrypted_jsonb eql_v1_encrypted,
Expand Down Expand Up @@ -108,6 +109,12 @@ SELECT eql_v1.add_index(
'big_int'
);

SELECT eql_v1.add_index(
'encrypted',
'encrypted_int8_as_biguint',
'unique',
'big_uint'
);

SELECT eql_v1.add_index(
'encrypted',
Expand Down