Skip to content

Commit 22a57ef

Browse files
committed
fix: move age identity to system config
1 parent 5484538 commit 22a57ef

File tree

11 files changed

+69
-104
lines changed

11 files changed

+69
-104
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rooz"
3-
version = "0.127.0"
3+
version = "0.128.0"
44
edition = "2021"
55

66
[dependencies]

src/api/crypt.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::api::CryptApi;
2+
use crate::config::config::SystemConfig;
23
use crate::model::types::AnyError;
34
use age::x25519::Identity;
45
use bollard::models::MountTypeEnum::VOLUME;
@@ -7,7 +8,15 @@ use std::str::FromStr;
78

89
pub const VOLUME_NAME: &'static str = "rooz-age-key-vol";
910

10-
impl<'a> CryptApi<'a> {
11+
impl SystemConfig {
12+
pub fn age_identity(&self) -> Result<Identity, AnyError> {
13+
Ok(age::x25519::Identity::from_str(
14+
self.age_key.as_deref().unwrap(),
15+
)?)
16+
}
17+
}
18+
19+
impl CryptApi {
1120
pub fn mount(&self, target: &str) -> Mount {
1221
Mount {
1322
typ: Some(VOLUME),
@@ -17,21 +26,7 @@ impl<'a> CryptApi<'a> {
1726
}
1827
}
1928

20-
pub async fn read_age_identity(&self) -> Result<Identity, AnyError> {
21-
let work_dir = "/tmp/.age";
2229

23-
let result = self
24-
.api
25-
.container
26-
.one_shot_output(
27-
"read-age-key",
28-
"cat /tmp/.age/age.key".into(),
29-
Some(vec![self.mount(work_dir)]),
30-
None,
31-
)
32-
.await?;
33-
Ok(age::x25519::Identity::from_str(&result.data)?)
34-
}
3530

3631
pub fn encrypt(
3732
&self,

src/api/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ pub struct VolumeApi<'a> {
3131
pub container: &'a ContainerApi<'a>,
3232
}
3333

34-
pub struct CryptApi<'a> {
35-
pub api: &'a Api<'a>,
34+
pub struct CryptApi {
3635
}
3736

3837
pub struct Api<'a> {
@@ -50,12 +49,12 @@ pub struct GitApi<'a> {
5049

5150
pub struct ConfigApi<'a> {
5251
pub api: &'a Api<'a>,
53-
pub crypt: &'a CryptApi<'a>,
52+
pub crypt: &'a CryptApi,
5453
}
5554

5655
pub struct WorkspaceApi<'a> {
5756
pub api: &'a Api<'a>,
5857
pub git: &'a GitApi<'a>,
5958
pub config: &'a ConfigApi<'a>,
60-
pub crypt: &'a CryptApi<'a>,
59+
pub crypt: &'a CryptApi,
6160
}

src/cmd/config/edit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl<'a> ConfigApi<'a> {
1111
let format = FileFormat::from_path(config_path);
1212
let body = fs::read_to_string(&config_path)?;
1313
let mut config = RoozCfg::deserialize_config(&body, format)?.unwrap();
14-
let identity = self.crypt.read_age_identity().await?;
14+
let identity = self.api.system_config.age_identity()?;
1515
self.decrypt(&mut config, &identity).await?;
1616
let decrypted_string = config.to_string(format)?;
1717
let (encrypted_config, edited_string) = self

src/cmd/init.rs

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::str::FromStr;
22

33
use crate::{
4-
api::{self, container, Api},
4+
api::{container, Api},
55
cli::InitParams,
6+
config::config::SystemConfig,
67
constants,
78
model::{
89
types::{AnyError, RunMode, RunSpec, VolumeResult},
@@ -58,9 +59,26 @@ impl<'a> Api<'a> {
5859
pub async fn init(&self, image: &str, uid: &str, spec: &InitParams) -> Result<(), AnyError> {
5960
let image_id = self.image.ensure(&image, false).await?.id;
6061

62+
let age_key = match spec.age_identity.clone() {
63+
None => age::x25519::Identity::generate(),
64+
Some(identity) => age::x25519::Identity::from_str(&identity)?,
65+
};
6166
self.volume
6267
.ensure_mounts(
63-
&vec![RoozVolume::system_config("/tmp/sys", None)],
68+
&vec![RoozVolume::system_config_init(
69+
"/tmp/sys",
70+
SystemConfig {
71+
age_key: Some(age_key.to_string().expose_secret().to_string()),
72+
gitconfig: Some(
73+
r#"
74+
[core]
75+
sshCommand = ssh -i /tmp/.ssh/id_ed25519 -o UserKnownHostsFile=/tmp/.ssh/known_hosts
76+
"#
77+
.trim()
78+
.to_string(),
79+
),
80+
},
81+
)?],
6482
None,
6583
Some(constants::ROOT_UID),
6684
)
@@ -101,57 +119,6 @@ impl<'a> Api<'a> {
101119
println!("Rooz has been already initialized. Use --force to reinitialize.")
102120
}
103121
}
104-
105-
match self
106-
.volume
107-
.ensure_volume(
108-
api::crypt::VOLUME_NAME.into(),
109-
&RoozVolumeRole::AgeKey,
110-
Some("age-key".into()),
111-
spec.force,
112-
)
113-
.await?
114-
{
115-
VolumeResult::Created { .. } => {
116-
let (key, pubkey) = match spec.age_identity.clone() {
117-
None => {
118-
let key = age::x25519::Identity::generate();
119-
let pubkey = key.to_public();
120-
(key, pubkey)
121-
}
122-
Some(identity) => {
123-
let key = age::x25519::Identity::from_str(&identity)?;
124-
let pubkey = key.to_public();
125-
(key, pubkey)
126-
}
127-
};
128-
129-
let entrypoint = &format!(
130-
r#"mkdir -p /tmp/.age && \
131-
echo -n '{}' > /tmp/.age/age.key && \
132-
echo -n '{}' > /tmp/.age/age.pub && \
133-
chmod 400 /tmp/.age/age.key && \
134-
chown -R {} /tmp/.age
135-
"#,
136-
&key.to_string().expose_secret(),
137-
pubkey,
138-
&uid
139-
);
140-
141-
self.execute_init(
142-
"rooz-init-age",
143-
entrypoint,
144-
api::crypt::VOLUME_NAME,
145-
"/tmp/.age",
146-
&image_id,
147-
)
148-
.await?;
149-
println!("{}", pubkey);
150-
}
151-
VolumeResult::AlreadyExists => {
152-
println!("Rooz has been already initialized. Use --force to reinitialize.")
153-
}
154-
}
155122
Ok(())
156123
}
157124
}

src/cmd/new.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::fs;
22

3-
use age::x25519::Identity;
4-
53
use crate::{
64
api::WorkspaceApi,
75
cli::WorkParams,
@@ -30,13 +28,12 @@ impl<'a> WorkspaceApi<'a> {
3028
workspace_key: &str,
3129
force: bool,
3230
work_dir: &str,
33-
identity: &Identity,
3431
) -> Result<EnterSpec, AnyError> {
3532
if let Some(c) = &cli_config {
3633
cfg_builder.from_config(c);
3734
}
3835
cfg_builder.from_cli(cli_params, None);
39-
self.config.decrypt(cfg_builder, identity).await?;
36+
self.config.decrypt(cfg_builder, &self.api.system_config.age_identity()?).await?;
4037
cfg_builder.expand_vars()?;
4138

4239
let cfg = RuntimeConfig::from(&*cfg_builder);
@@ -186,7 +183,6 @@ impl<'a> WorkspaceApi<'a> {
186183
cli_params: &WorkParams,
187184
cli_config_path: Option<ConfigSource>,
188185
ephemeral: bool,
189-
identity: &Identity,
190186
) -> Result<EnterSpec, AnyError> {
191187
let orig_uid = cli_params
192188
.uid
@@ -243,7 +239,6 @@ impl<'a> WorkspaceApi<'a> {
243239
&workspace_key,
244240
false,
245241
work_dir,
246-
identity,
247242
)
248243
.await
249244
}
@@ -292,7 +287,6 @@ impl<'a> WorkspaceApi<'a> {
292287
&workspace_key,
293288
false,
294289
work_dir,
295-
identity,
296290
)
297291
.await
298292
}
@@ -305,13 +299,12 @@ impl<'a> WorkspaceApi<'a> {
305299
}
306300

307301
pub async fn tmp(&self, spec: &WorkParams, root: bool, shell: &str) -> Result<(), AnyError> {
308-
let identity = self.crypt.read_age_identity().await?;
309302
let EnterSpec {
310303
workspace,
311304
git_spec,
312305
config,
313306
} = self
314-
.new(&id::random_suffix("tmp"), spec, None, true, &identity)
307+
.new(&id::random_suffix("tmp"), spec, None, true)
315308
.await?;
316309

317310
let working_dir = git_spec

src/cmd/update.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'a> WorkspaceApi<'a> {
3939
UpdateMode::Purge => self.remove(&workspace_key, true).await?,
4040
};
4141

42-
let identity = self.crypt.read_age_identity().await?;
42+
let identity = self.api.system_config.age_identity()?;
4343

4444
if let Some(labels) = &container.labels {
4545
let config_source = &labels[labels::CONFIG_ORIGIN];
@@ -99,7 +99,6 @@ impl<'a> WorkspaceApi<'a> {
9999
format,
100100
}),
101101
false,
102-
&identity,
103102
)
104103
.await?;
105104
}

src/config/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ impl RoozCfg {
349349
#[derive(Debug, Serialize, Deserialize, Clone)]
350350
#[serde(deny_unknown_fields)]
351351
pub struct SystemConfig {
352+
#[serde(skip_serializing_if = "Option::is_none")]
353+
pub age_key: Option<String>,
354+
352355
#[serde(skip_serializing_if = "Option::is_none")]
353356
pub gitconfig: Option<String>,
354357
}
@@ -357,4 +360,8 @@ impl SystemConfig {
357360
pub fn from_string(config: &str) -> Result<Self, AnyError> {
358361
Ok(serde_yaml::from_str(&config)?)
359362
}
363+
364+
pub fn to_string(config: &Self) -> Result<String, AnyError> {
365+
Ok(serde_yaml::to_string(&config)?)
366+
}
360367
}

src/main.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async fn main() -> Result<(), AnyError> {
9393
"ls /tmp/sys/rooz.config > /dev/null 2>&1 && cat /tmp/sys/rooz.config || echo ''"
9494
.into(),
9595
Some(vec![
96-
RoozVolume::system_config("/tmp/sys", None).to_mount(None)
96+
RoozVolume::system_config_read("/tmp/sys").to_mount(None)
9797
]),
9898
None,
9999
)
@@ -115,7 +115,7 @@ async fn main() -> Result<(), AnyError> {
115115
client: &docker,
116116
};
117117

118-
let crypt_api = CryptApi { api: &rooz };
118+
let crypt_api = CryptApi { };
119119

120120
let git_api = GitApi { api: &rooz };
121121

@@ -159,10 +159,8 @@ async fn main() -> Result<(), AnyError> {
159159
None => Ok(()),
160160
}?;
161161

162-
let identity = crypt_api.read_age_identity().await?;
163-
164162
workspace
165-
.new(&name, &work, config_source, false, &identity)
163+
.new(&name, &work, config_source, false,)
166164
.await?;
167165
println!(
168166
"\nThe workspace is ready. Run 'rooz enter {}' to enter.",
@@ -385,7 +383,7 @@ async fn main() -> Result<(), AnyError> {
385383
.await?;
386384
volume_api
387385
.ensure_mounts(
388-
&vec![RoozVolume::system_config("/tmp/sys", Some(config_string))],
386+
&vec![RoozVolume::system_config("/tmp/sys", config_string)],
389387
None,
390388
Some(constants::ROOT_UID),
391389
)

0 commit comments

Comments
 (0)