Skip to content

Commit 050db32

Browse files
committed
feat(tls): initialize crypto provider for rustls and enhance database container configuration
- Added `init_crypto_provider` function to initialize the crypto provider for rustls, ensuring secure connections. - Updated `main.rs` to call the new initialization function at startup. - Enhanced `DatabaseContainer` to support TLS configurations, including validation and secure defaults for MySQL and MariaDB containers. - Refactored `TestDatabase` to include TLS-enabled variants for better integration testing. These changes improve the security posture of the Gold Digger project by ensuring proper TLS setup for database connections.
1 parent beb3ea7 commit 050db32

8 files changed

Lines changed: 348 additions & 93 deletions

File tree

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
use std::{env, ffi::OsStr, path::Path};
1+
use std::{env, ffi::OsStr, path::Path, sync::Once};
22

33
use anyhow::{Context, Result};
44
use mysql::Row;
55

6+
static INIT: Once = Once::new();
7+
8+
/// Initialize crypto provider for rustls
9+
pub fn init_crypto_provider() {
10+
INIT.call_once(|| {
11+
let _ = rustls::crypto::ring::default_provider().install_default();
12+
});
13+
}
14+
615
/// CLI interface module.
716
pub mod cli;
817
/// CSV output module.

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ fn redact_sql_error(message: &str) -> String {
4242
///
4343
/// Parses CLI arguments and environment variables, executes a database query, and writes the output in the specified format.
4444
fn main() {
45+
// Initialize crypto provider for rustls
46+
gold_digger::init_crypto_provider();
47+
4548
let cli = Cli::parse();
4649

4750
// Handle subcommands first

tests/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Gold Digger Integration Tests
22

3-
This directory contains comprehensive integration tests for Gold Digger's MySQL/MariaDB functionality, including TLS support, data type handling, and output format validation.
3+
This directory contains comprehensive integration tests for Gold Digger's MySQL/MariaDB
4+
functionality, including TLS support, data type handling, and output format validation.
45

56
## Test Structure
67

@@ -117,8 +118,10 @@ cargo test --features "integration_tests additional_mysql_types" -- --include-ig
117118

118119
#### Current Test Categories
119120

120-
1. **Unit Tests** (`tls_unit_tests`): Test TLS configuration and validation without external dependencies
121-
2. **TLS Integration Tests** (`tls_integration`): Test actual TLS connections and certificate validation (require Docker)
121+
1. **Unit Tests** (`tls_unit_tests`): Test TLS configuration and validation without external
122+
dependencies
123+
2. **TLS Integration Tests** (`tls_integration`): Test actual TLS connections and certificate
124+
validation (require Docker)
122125
3. **Type Safety Tests** (`type_safety`): Test MySQL data type conversion and NULL value handling
123126
4. **Exit Code Tests** (`exit_codes`): Test proper exit code mapping for different error scenarios
124127

tests/fixtures/seed_data.sql

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
-- Basic test data
22
INSERT INTO test_basic (item_name, table_value)
3-
VALUES ('test1', 100)
4-
ON DUPLICATE KEY UPDATE table_value = VALUES (table_value);
3+
VALUES ('test1', 100) AS new
4+
ON DUPLICATE KEY UPDATE table_value = new.table_value;
55

66
INSERT INTO test_basic (item_name, table_value)
7-
VALUES ('test2', 200)
8-
ON DUPLICATE KEY UPDATE table_value = VALUES (table_value);
7+
VALUES ('test2', 200) AS new
8+
ON DUPLICATE KEY UPDATE table_value = new.table_value;
99

1010
INSERT INTO test_basic (item_name, table_value)
11-
VALUES ('test3', 300)
12-
ON DUPLICATE KEY UPDATE table_value = VALUES (table_value);
11+
VALUES ('test3', 300) AS new
12+
ON DUPLICATE KEY UPDATE table_value = new.table_value;
1313

1414
-- Comprehensive data types test data
1515
INSERT INTO test_data_types (
@@ -53,8 +53,27 @@ VALUES (
5353
'medium',
5454
'red,blue',
5555
TRUE
56-
)
57-
ON DUPLICATE KEY UPDATE varchar_col = VALUES (varchar_col);
56+
) AS new
57+
ON DUPLICATE KEY UPDATE
58+
varchar_col = new.varchar_col,
59+
text_col = new.text_col,
60+
int_col = new.int_col,
61+
bigint_col = new.bigint_col,
62+
decimal_col = new.decimal_col,
63+
float_col = new.float_col,
64+
double_col = new.double_col,
65+
date_col = new.date_col,
66+
datetime_col = new.datetime_col,
67+
timestamp_col = new.timestamp_col,
68+
time_col = new.time_col,
69+
year_col = new.year_col,
70+
binary_col = new.binary_col,
71+
varbinary_col = new.varbinary_col,
72+
blob_col = new.blob_col,
73+
json_col = new.json_col,
74+
enum_col = new.enum_col,
75+
set_col = new.set_col,
76+
bool_col = new.bool_col;
5877

5978
-- Edge cases test data
6079
INSERT INTO test_edge_cases (
@@ -78,10 +97,10 @@ VALUES (
7897
'12345',
7998
0,
8099
-42
81-
)
82-
ON DUPLICATE KEY UPDATE unicode_text = VALUES (unicode_text);
100+
) AS new
101+
ON DUPLICATE KEY UPDATE unicode_text = new.unicode_text;
83102

84-
INSERT INTO test_edge_cases (
103+
INSERT INTO test_edge_cases AS new (
85104
id,
86105
null_varchar,
87106
empty_string,
@@ -103,4 +122,4 @@ VALUES (
103122
0,
104123
-100
105124
)
106-
ON DUPLICATE KEY UPDATE unicode_text = VALUES (unicode_text);
125+
ON DUPLICATE KEY UPDATE unicode_text = new.unicode_text;

tests/integration/common.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ impl TestEnvironment {
328328
// Store original value for restoration
329329
self.original_env.insert(key.to_string(), std::env::var(key).ok());
330330

331-
// Set new value
331+
// Set new value - std::env::set_var is safe in single-threaded tests
332332
unsafe {
333333
std::env::set_var(key, value);
334334
}
@@ -339,7 +339,7 @@ impl TestEnvironment {
339339
// Store original value for restoration
340340
self.original_env.insert(key.to_string(), std::env::var(key).ok());
341341

342-
// Remove variable
342+
// Remove variable - std::env::remove_var is safe in single-threaded tests
343343
unsafe {
344344
std::env::remove_var(key);
345345
}
@@ -356,13 +356,11 @@ impl Drop for TestEnvironment {
356356
/// Restore original environment variables when dropped
357357
fn drop(&mut self) {
358358
for (key, original_value) in &self.original_env {
359-
match original_value {
360-
Some(value) => unsafe {
361-
std::env::set_var(key, value);
362-
},
363-
None => unsafe {
364-
std::env::remove_var(key);
365-
},
359+
unsafe {
360+
match original_value {
361+
Some(value) => std::env::set_var(key, value),
362+
None => std::env::remove_var(key),
363+
}
366364
}
367365
}
368366
}

0 commit comments

Comments
 (0)