Skip to content

Commit

Permalink
tls
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Jun 25, 2024
1 parent 18125b9 commit 3c855fe
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 114 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use subst::VariableMap;
use crate::file_config::FileConfigEnum;
#[cfg(feature = "fonts")]
use crate::fonts::FontSources;
use crate::pg::init_tls;
use crate::source::{TileInfoSources, TileSources};
#[cfg(feature = "sprites")]
use crate::sprites::{SpriteConfig, SpriteSources};
Expand Down
3 changes: 3 additions & 0 deletions martin/src/pg/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub enum PgError {
#[error(transparent)]
RustlsError(#[from] rustls::Error),

#[error("Unable to install default Rustls provider: {0}")]
RustlsInstallDefaultFailed(String),

#[error("Unknown SSL mode: {0:?}")]
UnknownSslMode(deadpool_postgres::tokio_postgres::config::SslMode),

Expand Down
1 change: 1 addition & 0 deletions martin/src/pg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ pub use config_table::TableInfo;
pub use errors::{PgError, PgResult};
pub use pool::{PgPool, POOL_SIZE_DEFAULT};
pub use query_functions::query_available_function;
pub use tls::init_tls;
8 changes: 7 additions & 1 deletion martin/src/pg/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::pg::PgError::{
BadConnectionString, CannotLoadRoots, CannotOpenCert, CannotParseCert, CannotUseClientKey,
InvalidPrivateKey, UnknownSslMode,
};
use crate::pg::{PgResult, PgSslCerts};
use crate::pg::{PgError, PgResult, PgSslCerts};

/// A temporary workaround for <https://github.com/sfackler/rust-postgres/pull/988>
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -32,6 +32,12 @@ pub enum SslModeOverride {
VerifyFull,
}

pub fn init_tls() -> PgResult<()> {
default_provider()
.install_default()
.map_err(|e| PgError::RustlsInstallDefaultFailed(format!("{e:?}")))
}

/// Special treatment for sslmode=verify-ca & sslmode=verify-full - if found, replace them with sslmode=require
pub fn parse_conn_str(conn_str: &str) -> PgResult<(Config, SslModeOverride)> {
let mut mode = SslModeOverride::Unmodified(SslMode::Disable);
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion mbtiles/src/bindiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl BinDiffer<DifferBefore, DifferAfter> for BinDiffDiffer {
fn process(&self, value: DifferBefore) -> MbtResult<DifferAfter> {
let mut old_tile = value.old_tile_data;
let mut new_tile = value.new_tile_data;
if self.patch_type == PatchType::BinDiff {
if self.patch_type == PatchType::BinDiffGz {
old_tile = GzipEncoder::decode(&old_tile)
.inspect_err(|e| error!("Unable to unzip source tile at {:?}: {e}", value.coord))?;
new_tile = GzipEncoder::decode(&new_tile)
Expand Down
51 changes: 32 additions & 19 deletions mbtiles/src/copier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use crate::mbtiles::PatchFileInfo;
use crate::queries::{
create_tiles_with_hash_view, detach_db, init_mbtiles_schema, is_empty_database,
};
use crate::AggHashType::Verify;
use crate::AggHashType::{Update, Verify};
use crate::IntegrityCheckType::Quick;
use crate::MbtType::{Flat, FlatWithHash, Normalized};
use crate::PatchType::{BinDiffGz, BinDiffRaw, Whole};
use crate::{
action_with_rusqlite, has_bsdiffraw, invert_y_value, reset_db_settings, CopyType, MbtError,
MbtType, MbtTypeCli, Mbtiles, AGG_TILES_HASH, AGG_TILES_HASH_AFTER_APPLY,
AGG_TILES_HASH_BEFORE_APPLY,
action_with_rusqlite, invert_y_value, reset_db_settings, CopyType, MbtError, MbtType,
MbtTypeCli, Mbtiles, AGG_TILES_HASH, AGG_TILES_HASH_AFTER_APPLY, AGG_TILES_HASH_BEFORE_APPLY,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, EnumDisplay)]
Expand All @@ -42,7 +42,7 @@ pub enum PatchType {
#[default]
Whole,
/// Use bin-diff to store only the bytes changed between two versions of each tile. Treats content as gzipped blobs, decoding them before diffing.
BinDiff,
BinDiffGz,
/// Use bin-diff to store only the bytes changed between two versions of each tile. Treats content as blobs without any special encoding.
BinDiffRaw,
}
Expand Down Expand Up @@ -230,7 +230,7 @@ impl MbtileCopierInt {
dif_mbt.attach_to(&mut conn, "diffDb").await?;

let dst_type = self.options.dst_type().unwrap_or(src_info.mbt_type);
if patch_type != PatchType::Whole && dst_type != FlatWithHash {
if patch_type != Whole && dst_type != FlatWithHash {
return Err(MbtError::BinDiffRequiresFlatWithHash(dst_type));
}

Expand All @@ -242,7 +242,11 @@ impl MbtileCopierInt {
dif_type = dif_info.mbt_type,
what = self.copy_text(),
dst_path = self.dst_mbt.filepath(),
patch = if patch_type == PatchType::Whole { "" } else { " with bin-diff" }
patch = match patch_type {
Whole => {""}
BinDiffGz => {" with bin-diff"}
BinDiffRaw => {" with bin-diff-raw"}
}
);

self.init_schema(&mut conn, src_info.mbt_type, dst_type)
Expand All @@ -260,7 +264,7 @@ impl MbtileCopierInt {
detach_db(&mut conn, "diffDb").await?;
detach_db(&mut conn, "sourceDb").await?;

if patch_type != PatchType::Whole {
if patch_type != Whole {
BinDiffDiffer::new(self.src_mbt.clone(), dif_mbt, dif_info.mbt_type, patch_type)
.run(&mut conn, self.get_where_clause("srcTiles."))
.await?;
Expand Down Expand Up @@ -291,14 +295,13 @@ impl MbtileCopierInt {
async fn run_with_patch(self, dif_mbt: Mbtiles) -> MbtResult<SqliteConnection> {
let mut dif_conn = dif_mbt.open_readonly().await?;
let dif_info = dif_mbt.examine_diff(&mut dif_conn).await?;
let use_bindiff = has_bsdiffraw(&mut dif_conn).await?;
self.validate(&dif_mbt, &mut dif_conn).await?;
dif_mbt.validate_diff_info(&dif_info, self.options.force)?;
dif_conn.close().await?;

let src_type = self.validate_src_file().await?.mbt_type;
let dst_type = self.options.dst_type().unwrap_or(src_type);
if use_bindiff && dst_type != FlatWithHash {
if dif_info.patch_type != Whole && dst_type != FlatWithHash {
return Err(MbtError::BinDiffRequiresFlatWithHash(dst_type));
}

Expand All @@ -316,11 +319,12 @@ impl MbtileCopierInt {
src_mbt = self.src_mbt,
what = self.copy_text(),
dst_path = self.dst_mbt.filepath(),
patch = if use_bindiff {
" with bin-diff"
} else {
""
});
patch = match dif_info.patch_type {
Whole => {""}
BinDiffGz => {" with bin-diff"}
BinDiffRaw => {" with bin-diff-raw"}
}
);

self.init_schema(&mut conn, src_type, dst_type).await?;
self.copy_with_rusqlite(
Expand Down Expand Up @@ -356,13 +360,23 @@ impl MbtileCopierInt {
detach_db(&mut conn, "diffDb").await?;
detach_db(&mut conn, "sourceDb").await?;

if use_bindiff {
if dif_info.patch_type != Whole {
BinDiffPatcher::new(self.src_mbt.clone(), dif_mbt, dst_type)
.run(&mut conn, self.get_where_clause("srcTiles."))
.await?;
}

self.validate(&self.dst_mbt, &mut conn).await?;
let hash_type = if dif_info.patch_type == BinDiffGz {
Update
} else {
Verify
};

if self.options.validate {
self.dst_mbt.validate(&mut conn, Quick, hash_type).await?;
} else if hash_type == Update {
self.dst_mbt.update_agg_tiles_hash(&mut conn).await?;
}

Ok(conn)
}
Expand Down Expand Up @@ -744,7 +758,7 @@ fn get_select_from_with_diff(
};
}

let sql_cond = if patch_type == PatchType::Whole {
let sql_cond = if patch_type == Whole {
"OR srcTiles.tile_data != difTiles.tile_data"
} else {
""
Expand Down Expand Up @@ -799,7 +813,6 @@ mod tests {
use sqlx::{Decode, Sqlite, SqliteConnection, Type};

use super::*;
use crate::PatchType::Whole;

// TODO: Most of these tests are duplicating the tests from tests/mbtiles.rs, and should be cleaned up/removed.

Expand Down
3 changes: 3 additions & 0 deletions mbtiles/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub enum MbtError {

#[error("Internal error creating bin-diff table")]
BindiffError,

#[error("BinDiff patch files can be only applied with `mbtiles copy --apply-patch` command")]
UnsupportedPatchType,
}

pub type MbtResult<T> = Result<T, MbtError>;
3 changes: 2 additions & 1 deletion mbtiles/src/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{query, Connection as _, Executor, SqliteConnection, SqliteExecutor, Statement};

use crate::errors::{MbtError, MbtResult};
use crate::{invert_y_value, CopyDuplicateMode, MbtType};
use crate::{invert_y_value, CopyDuplicateMode, MbtType, PatchType};

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize, EnumDisplay)]
#[enum_display(case = "Kebab")]
Expand Down Expand Up @@ -48,6 +48,7 @@ pub struct PatchFileInfo {
pub agg_tiles_hash: Option<String>,
pub agg_tiles_hash_before_apply: Option<String>,
pub agg_tiles_hash_after_apply: Option<String>,
pub patch_type: PatchType,
}

#[derive(Clone, Debug)]
Expand Down
4 changes: 4 additions & 0 deletions mbtiles/src/patcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sqlx::{query, Connection as _};

use crate::queries::detach_db;
use crate::MbtType::{Flat, FlatWithHash, Normalized};
use crate::PatchType::Whole;
use crate::{
MbtError, MbtResult, MbtType, Mbtiles, AGG_TILES_HASH, AGG_TILES_HASH_AFTER_APPLY,
AGG_TILES_HASH_BEFORE_APPLY,
Expand All @@ -16,6 +17,9 @@ pub async fn apply_patch(base_file: PathBuf, patch_file: PathBuf, force: bool) -

let mut conn = patch_mbt.open_readonly().await?;
let patch_info = patch_mbt.examine_diff(&mut conn).await?;
if patch_info.patch_type != Whole {
return Err(MbtError::UnsupportedPatchType);
}
patch_mbt.validate_diff_info(&patch_info, force)?;
let patch_type = patch_info.mbt_type;
conn.close().await?;
Expand Down
Loading

0 comments on commit 3c855fe

Please sign in to comment.