Skip to content

Commit 5279c1b

Browse files
authored
Merge pull request #328 from cipherstash/feat/cli-args
feat(config): add optional database connection CLI args
2 parents aea5786 + 86b9d09 commit 5279c1b

File tree

7 files changed

+169
-30
lines changed

7 files changed

+169
-30
lines changed

docs/reference/index.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,87 @@ As a convenience for production deployments, with the below environment variable
258258
CS_DATABASE__INSTALL_AWS_RDS_CERT_BUNDLE="true"
259259
```
260260

261+
## Command line options
262+
263+
264+
265+
## Command line interface
266+
267+
The CipherStash Proxy accepts command line arguments.
268+
For example, the upstream database can be specified via command line arguments.
269+
Useful for local dev and testing.
270+
271+
### Usage
272+
273+
```bash
274+
cipherstash-proxy [OPTIONS] [DBNAME] [COMMAND]
275+
```
276+
277+
### Commands
278+
279+
- **encrypt**
280+
Encrypt one or more columns in a table. This command requires a running and properly configured CipherStash Proxy instance.
281+
282+
- **help**
283+
Print the help message or detailed information for the specified subcommand(s).
284+
285+
### Arguments
286+
287+
- **DBNAME**
288+
289+
Optional name of the database to connect to. If not specified, the tool will use the environment variables or configuration file settings.
290+
291+
Default value: none
292+
293+
- **-H, --db-host <DB_HOST>**
294+
295+
Optional database host. This value will default to the one defined in your environment or configuration file if not provided.
296+
297+
Default value: `127.0.0.1`
298+
299+
- **-u, --db-user <DB_USER>**
300+
301+
Optional database user. This value will default to the one defined in your environment or configuration file if not provided.
302+
303+
Default value: `postgres`
304+
305+
- **-p, --config-file-path <CONFIG_FILE_PATH>**
306+
307+
Specifies an optional path to a CipherStash Proxy configuration file.
308+
If provided, the application attempts to load configuration settings from this file.
309+
However, environment variables can be used instead of the file or to override any values defined within it.
310+
311+
Default Value: `cipherstash-proxy.toml`
312+
313+
Note:
314+
The application will look for "cipherstash-proxy.toml" by default if no other file path is specified.
315+
316+
- **-l, --log-level <LOG_LEVEL>**
317+
318+
Sets an optional log level for the application, which controls the verbosity of the logging output.
319+
This can be particularly useful for adjusting the level of detail in application logs
320+
to suit different environments or debugging needs.
321+
322+
Default Value: `info`
323+
324+
Environment Variable: `CS_LOG__LEVEL`
325+
326+
Possible Values: `error`, `warn`, `info`, `debug`, `trace`
327+
328+
- **-f, --log-format <LOG_FORMAT>**
329+
330+
Specifies an optional log format for the output logs.
331+
The default log format is "pretty" when the application detects that it is running in a terminal session,
332+
otherwise it defaults to "structured" for non-interactive environments.
333+
The setting can be overridden by the corresponding environment variable.
334+
335+
Default Value: `pretty` (if running in a terminal session), otherwise `structured`
336+
337+
Environment Variable: `CS_LOG__FORMAT`
338+
339+
Possible Values: `pretty`, `structured`, `text`
340+
341+
261342
## Multitenant operation
262343

263344
CipherStash Proxy supports multitenant applications using ZeroKMS keysets to provide strong cryptographic separation between tenants.

packages/cipherstash-proxy-integration/src/migrate/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ mod tests {
4242
log_level: LogLevel::Debug,
4343
log_format: LogFormat::Pretty,
4444
command: None,
45+
db_host: None,
46+
db_name: None,
47+
db_user: None,
4548
};
4649

4750
let config = match TandemConfig::load(&args) {

packages/cipherstash-proxy/src/cli/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ const DEFAULT_CONFIG_FILE: &str = "cipherstash-proxy.toml";
2121
/// CipherStash Proxy keeps your sensitive data in PostgreSQL encrypted and searchable, with no changes to SQL.
2222
///
2323
pub struct Args {
24+
/// Optional database host to connect to.
25+
/// Uses env or config file if not specified.
26+
#[arg(short = 'H', long)]
27+
pub db_host: Option<String>,
28+
29+
/// Optional database name to connect to.
30+
/// Uses env or config file if not specified.
31+
#[arg(value_name = "DBNAME")]
32+
pub db_name: Option<String>,
33+
34+
/// Optional database user to connect as.
35+
/// Uses env or config file if not specified.
36+
#[arg(short = 'u', long)]
37+
pub db_user: Option<String>,
38+
2439
/// Optional path to a CipherStash Proxy configuration file.
2540
///
2641
/// Default is "cipherstash-proxy.toml".

packages/cipherstash-proxy/src/config/database.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub struct DatabaseConfig {
1414
pub port: u16,
1515

1616
pub name: String,
17+
18+
#[serde(default = "DatabaseConfig::default_username")]
1719
pub username: String,
1820

1921
#[serde(deserialize_with = "protected_string_deserializer")]
@@ -40,6 +42,10 @@ impl DatabaseConfig {
4042
5432
4143
}
4244

45+
pub fn default_username() -> String {
46+
"postgres".to_string()
47+
}
48+
4349
pub const fn default_config_reload_interval() -> u64 {
4450
60
4551
}

packages/cipherstash-proxy/src/config/log.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,30 @@ mod tests {
130130
with_no_cs_vars(|| {
131131
temp_env::with_vars([("CS_LOG__LEVEL", Some("error"))], || {
132132
let config =
133-
TandemConfig::build("tests/config/cipherstash-proxy-test.toml").unwrap();
133+
TandemConfig::build_path("tests/config/cipherstash-proxy-test.toml").unwrap();
134134
assert_eq!(config.log.level, LogLevel::Error);
135135
});
136136

137137
temp_env::with_vars([("CS_LOG__LEVEL", Some("WARN"))], || {
138138
let config =
139-
TandemConfig::build("tests/config/cipherstash-proxy-test.toml").unwrap();
139+
TandemConfig::build_path("tests/config/cipherstash-proxy-test.toml").unwrap();
140140
assert_eq!(config.log.level, LogLevel::Warn);
141141
});
142142

143143
temp_env::with_vars([("CS_LOG__OUTPUT", Some("stderr"))], || {
144144
let config =
145-
TandemConfig::build("tests/config/cipherstash-proxy-test.toml").unwrap();
145+
TandemConfig::build_path("tests/config/cipherstash-proxy-test.toml").unwrap();
146146
assert_eq!(config.log.output, LogOutput::Stderr);
147147
});
148148

149149
temp_env::with_vars([("CS_LOG__FORMAT", Some("Pretty"))], || {
150150
let config =
151-
TandemConfig::build("tests/config/cipherstash-proxy-test.toml").unwrap();
151+
TandemConfig::build_path("tests/config/cipherstash-proxy-test.toml").unwrap();
152152
assert_eq!(config.log.format, LogFormat::Pretty);
153153
});
154154

155155
temp_env::with_vars([("CS_LOG__FORMAT", Some("dEbUG"))], || {
156-
let config = TandemConfig::build("tests/config/cipherstash-proxy-test.toml");
156+
let config = TandemConfig::build_path("tests/config/cipherstash-proxy-test.toml");
157157

158158
assert!(config.is_err());
159159
assert!(matches!(config.unwrap_err(), Error::Config(_)));

0 commit comments

Comments
 (0)