Skip to content

Commit 2752d4a

Browse files
authored
feat!: Remove secret definitions through env vars (#666)
Remove the ability to define secrets in the env vars of the typegate. Secrets can now only be defined in the metatype config file and the `--secret` CLI option. #### Motivation and context - [MET-370](https://linear.app/metatypedev/issue/MET-370/easier-way-to-pass-secrets-in-metatypeyaml-config-file) - Security - Better DX #### Migration notes 1. **Metatype config file**: On the node configuration, secrets are defined at `secrets.<tg_name>.key`: ```yaml # before typegates: dev: env: TG_CONSOLE_POSTGRES_CONN: postgresql://postgres:password@localhost:5432/db?schema=console TG_CONSOLE_BASIC_ADMIN: password #after typegates: dev: secrets: console: POSTGRES_CONN: postgresql://postgres:password@localhost:5432/db?schema=console BASIC_ADMIN: password ``` 2. **Secret override option on meta/cli** ```sh # before meta deploy -f my-tg.py --secret TG_CONSOLE_POSTGRES_CONN=postgresql://postgres:password@localhost:5432/db?schema=console # after meta deploy -f my-tg.py --secret POSTGRES_CONN=postgresql://postgres:password@localhost:5432/db?schema=console # or - with the typegraph name meta deploy -f my-tg.py --secret console:POSTGRES_CONN=postgresql://postgres:password@localhost:5432/db?schema=console ``` ### Checklist - [x] The change come with new or modified tests - [ ] Hard-to-understand functions have explanatory comments - [x] End-user documentation is updated to reflect the change
1 parent 4dfeb55 commit 2752d4a

File tree

22 files changed

+201
-224
lines changed

22 files changed

+201
-224
lines changed

.ghjk/deno.lock

Lines changed: 0 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.ghjk/lock.json

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@
8484
"portRef": "[email protected]",
8585
"pluginRepo": "https://github.com/asdf-community/asdf-cmake"
8686
},
87-
"468769528cbf87576285bfcd000271d6398bf3c2b20083d90105d083d61854ba": {
88-
"version": "2.40.1",
89-
"depConfigs": {},
90-
"portRef": "[email protected]"
91-
},
9287
"402fae54fb94ffdba9cd2617602c7b33c55002275f3c3bb588615b01ff0c3c26": {
9388
"version": "v1.6.4",
9489
"depConfigs": {},
@@ -99,7 +94,7 @@
9994
"depConfigs": {},
10095
"portRef": "[email protected]"
10196
},
102-
"f6c92b4425a3c13dfde7fe516efd376287e66cfa74aefd7b6a7a7bb0f3547a47": {
97+
"80635b3a3967246789cc5f09137a8334fcfae9e96d507b6f8143d385093f024f": {
10398
"version": "0.116.0",
10499
"depConfigs": {
105100
"cargo_binstall_ghrel": {
@@ -241,7 +236,7 @@
241236
"version": "1.0.0",
242237
"depConfigs": {
243238
"node_org": {
244-
"version": "v21.7.1",
239+
"version": "v21.7.2",
245240
"depConfigs": {
246241
"tar_aa": {
247242
"version": "1.35",
@@ -256,7 +251,7 @@
256251
"packageName": "@bytecodealliance/jco"
257252
},
258253
"77a0197285cae563176871bd72f2ea24504c195dc596f5c33d4c9cc95eda1fba": {
259-
"version": "v21.7.1",
254+
"version": "v21.7.2",
260255
"depConfigs": {
261256
"tar_aa": {
262257
"version": "1.35",
@@ -270,7 +265,7 @@
270265
"version": "10.0.1",
271266
"depConfigs": {
272267
"node_org": {
273-
"version": "v21.7.1",
268+
"version": "v21.7.2",
274269
"depConfigs": {
275270
"tar_aa": {
276271
"version": "1.35",
@@ -414,37 +409,6 @@
414409
"portRef": "[email protected]",
415410
"crateName": "whiz",
416411
"locked": true
417-
},
418-
"80635b3a3967246789cc5f09137a8334fcfae9e96d507b6f8143d385093f024f": {
419-
"version": "0.116.0",
420-
"depConfigs": {
421-
"cargo_binstall_ghrel": {
422-
"version": "v1.6.4",
423-
"depConfigs": {},
424-
"portRef": "[email protected]"
425-
},
426-
"rust_rustup": {
427-
"version": "1.76.0",
428-
"depConfigs": {
429-
"rustup_rustlang": {
430-
"version": "1.27.0",
431-
"depConfigs": {
432-
"git_aa": {
433-
"version": "2.43.0",
434-
"depConfigs": {},
435-
"portRef": "[email protected]"
436-
}
437-
},
438-
"portRef": "[email protected]"
439-
}
440-
},
441-
"portRef": "[email protected]",
442-
"profile": "minimal"
443-
}
444-
},
445-
"portRef": "[email protected]",
446-
"crateName": "wasm-opt",
447-
"locked": true
448412
}
449413
}
450414
},

libs/common/src/node.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub struct Node {
2323
pub base_url: Url,
2424
pub prefix: Option<String>,
2525
pub auth: Option<BasicAuth>,
26-
pub env: HashMap<String, String>,
2726
}
2827

2928
#[derive(Debug, thiserror::Error)]
@@ -39,13 +38,11 @@ impl Node {
3938
url: U,
4039
prefix: Option<String>,
4140
auth: Option<BasicAuth>,
42-
env: HashMap<String, String>,
4341
) -> Result<Self> {
4442
Ok(Self {
4543
base_url: url.into_url()?,
4644
prefix,
4745
auth,
48-
env,
4946
})
5047
}
5148

libs/metagen/src/tests/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ async fn spin_up_typegate() -> anyhow::Result<(tokio::process::Child, common::no
113113
username: "admin".into(),
114114
password: tg_admin_password.into(),
115115
}),
116-
Default::default(),
117116
)?;
118117
Ok((typegate, node))
119118
}

meta-cli/src/cli/deploy.rs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
22
// SPDX-License-Identifier: MPL-2.0
33

4-
use std::collections::HashMap;
54
use std::path::{Path, PathBuf};
65
use std::sync::{Arc, Mutex};
76

@@ -16,12 +15,12 @@ use crate::deploy::actors::loader::{
1615
};
1716
use crate::deploy::actors::watcher::WatcherActor;
1817
use crate::deploy::push::pusher::PushResult;
18+
use crate::secrets::{RawSecrets, Secrets};
1919
use actix::prelude::*;
2020
use actix_web::dev::ServerHandle;
2121
use anyhow::{bail, Context, Result};
2222
use async_trait::async_trait;
2323
use clap::Parser;
24-
use common::node::Node;
2524
use log::warn;
2625
use normpath::PathExt;
2726
use tokio::sync::mpsc;
@@ -91,32 +90,16 @@ pub struct DeployOptions {
9190
#[clap(long, default_value_t = false)]
9291
pub watch: bool,
9392

94-
/// secret overrides
93+
/// Overrides secrets in the format `[<typegraph-name>:]<secret-name>=<value>`
9594
#[clap(long = "secret")]
9695
pub secrets: Vec<String>,
9796
}
9897

99-
fn override_secrets(
100-
secrets: HashMap<String, String>,
101-
overrides: Vec<String>,
102-
) -> Result<HashMap<String, String>> {
103-
let mut secrets = secrets;
104-
for override_str in overrides {
105-
let parts: Vec<&str> = override_str.splitn(2, '=').collect();
106-
if parts.len() != 2 {
107-
bail!("Invalid secret override: {}", override_str);
108-
}
109-
secrets.insert(parts[0].to_string(), parts[1].to_string());
110-
}
111-
112-
Ok(secrets)
113-
}
114-
11598
pub struct Deploy {
11699
config: Arc<Config>,
117100
base_dir: Arc<Path>,
118101
options: DeployOptions,
119-
node: Node,
102+
secrets: RawSecrets,
120103
file: Option<Arc<Path>>,
121104
max_parallel_loads: Option<usize>,
122105
}
@@ -131,6 +114,7 @@ impl Deploy {
131114
let options = deploy.options.clone();
132115

133116
let node_config = config.node(&deploy.node, &deploy.target);
117+
let secrets = Secrets::load_from_node_config(&node_config);
134118
let node = node_config
135119
.build(&dir)
136120
.await
@@ -152,7 +136,7 @@ impl Deploy {
152136
config,
153137
base_dir: dir.into(),
154138
options,
155-
node,
139+
secrets,
156140
file: deploy
157141
.file
158142
.as_ref()
@@ -225,13 +209,11 @@ mod default_mode {
225209
impl DefaultMode {
226210
pub async fn init(deploy: Deploy) -> Result<Self> {
227211
let console = ConsoleActor::new(Arc::clone(&deploy.config)).start();
228-
let secrets = lade_sdk::hydrate(
229-
override_secrets(deploy.node.env.clone(), deploy.options.secrets.clone())?,
230-
deploy.base_dir.to_path_buf(),
231-
)
232-
.await?;
233212

234-
ServerStore::set_secrets(secrets);
213+
let mut secrets = deploy.secrets.clone();
214+
secrets.apply_overrides(&deploy.options.secrets)?;
215+
216+
ServerStore::set_secrets(secrets.hydrate(deploy.base_dir.clone()).await?);
235217

236218
let (loader_event_tx, loader_event_rx) = mpsc::unbounded_channel();
237219

@@ -348,13 +330,10 @@ mod watch_mode {
348330
.context("setting Ctrl-C handler")?;
349331

350332
loop {
351-
let secrets = lade_sdk::hydrate(
352-
override_secrets(deploy.node.env.clone(), deploy.options.secrets.clone())?,
353-
deploy.base_dir.to_path_buf(),
354-
)
355-
.await?;
333+
let mut secrets = deploy.secrets.clone();
334+
secrets.apply_overrides(&deploy.options.secrets)?;
356335

357-
ServerStore::set_secrets(secrets.clone());
336+
ServerStore::set_secrets(secrets.hydrate(deploy.base_dir.clone()).await?);
358337

359338
let (loader_event_tx, loader_event_rx) = mpsc::unbounded_channel();
360339

meta-cli/src/com/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async fn config(req: HttpRequest) -> impl Responder {
5757
artefact_base_dir.pop(); // pop file.ext
5858

5959
let endpoint = ServerStore::get_endpoint();
60-
let secrets = ServerStore::get_secrets();
60+
let secrets = ServerStore::get_secrets(&parsed.typegraph);
6161
let migration_action_glob = ServerStore::get_migration_action_glob();
6262
let disable_artifact_resolution = !ServerStore::get_artifact_resolution_flag();
6363
let codegen = ServerStore::get_codegen_flag();

meta-cli/src/com/store.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
22
// SPDX-License-Identifier: MPL-2.0
33

4-
use crate::config::Config;
4+
use crate::{config::Config, secrets::Secrets};
55
use anyhow::{bail, Result};
66
use common::node::BasicAuth;
77
use lazy_static::lazy_static;
@@ -65,7 +65,7 @@ pub struct ServerStore {
6565
migration_action_glob: MigrationAction,
6666
/// 1 typegraph => n runtimes
6767
migration_action: HashMap<PathBuf, Arc<Vec<RuntimeMigrationAction>>>,
68-
secrets: HashMap<String, String>,
68+
secrets: Secrets,
6969
endpoint: Endpoint,
7070
prefix: Option<String>,
7171
sdk_responses: HashMap<PathBuf, Arc<HashMap<String, SDKResponse>>>,
@@ -94,12 +94,12 @@ impl ServerStore {
9494
with_store(|s| s.command.clone())
9595
}
9696

97-
pub fn set_secrets(secrets: HashMap<String, String>) {
97+
pub fn set_secrets(secrets: Secrets) {
9898
with_store_mut(|s| s.secrets = secrets)
9999
}
100100

101-
pub fn get_secrets() -> HashMap<String, String> {
102-
with_store(|s| s.secrets.clone())
101+
pub fn get_secrets(tg_name: &str) -> HashMap<String, String> {
102+
with_store(|s| s.secrets.get(tg_name))
103103
}
104104

105105
pub fn set_endpoint(endpoint: Endpoint) {

meta-cli/src/config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub struct NodeConfig {
5656
password: Option<String>,
5757
#[serde(default)]
5858
pub env: HashMap<String, String>,
59+
/// tg_name -> {key -> secret}
60+
#[serde(default)]
61+
pub secrets: HashMap<String, HashMap<String, String>>,
5962
}
6063

6164
impl Default for NodeConfig {
@@ -66,6 +69,7 @@ impl Default for NodeConfig {
6669
username: None,
6770
password: None,
6871
env: HashMap::default(),
72+
secrets: HashMap::default(),
6973
}
7074
}
7175
}
@@ -97,7 +101,6 @@ impl NodeConfig {
97101
self.url.clone(),
98102
self.prefix.clone(),
99103
Some(self.basic_auth(dir).await.context("basic auth")?.into()),
100-
self.env.clone(),
101104
)
102105
}
103106
}

meta-cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod deploy;
99
mod fs;
1010
mod global_config;
1111
mod logger;
12+
mod secrets;
1213

1314
#[cfg(test)]
1415
mod tests;

0 commit comments

Comments
 (0)