Skip to content

Commit

Permalink
fix: deployment file modes (#5)
Browse files Browse the repository at this point in the history
Docker & other things want access to the generated secret directories, yet you can't just bind-mount them elsewhere as deployer user might be different from app user, resulting in "Permission denied" errors. Grant other users permission to read secret files and access secret directories without giving access to the outer directories.

* Set correct permissions for all files and dirs

* Fixes after review
  • Loading branch information
vklachkov authored Oct 24, 2024
1 parent d2d0a9b commit 5b829f9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "baedeker"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
description = "Substrate network orchestration framework"
license = "MIT"
Expand Down
14 changes: 14 additions & 0 deletions src/fs_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::{fs::DirBuilder, io, os::unix::fs::DirBuilderExt, path::Path};

/// Recursively create a directory and all of its parent components if they
/// are missing with given permissions.
///
/// # Errors
///
/// The same as from [`std::fs::create_dir_all`]
pub fn create_dir_mode<P: AsRef<Path>>(path: P, mode: u32) -> io::Result<()> {
DirBuilder::new()
.recursive(true)
.mode(mode)
.create(path.as_ref())
}
24 changes: 18 additions & 6 deletions src/keystore.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{
env, fs,
env,
fs::{self, Permissions, create_dir_all},
io::{self, ErrorKind, Write},
os::unix::fs::PermissionsExt,
path::PathBuf,
result,
str::FromStr,
Expand All @@ -12,6 +14,8 @@ use sp_core::crypto::{SecretStringError, Ss58AddressFormat};
use tempfile::{NamedTempFile, PersistError};
use tracing::info;

use crate::fs_utils::create_dir_mode;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("io: {0}")]
Expand Down Expand Up @@ -109,17 +113,21 @@ impl FileNodeKeys {
}
fn keystore_dir(&self, node: &str) -> Result<Option<PathBuf>> {
let mut path = self.root.to_path_buf();
path.push(format!("keystore-{node}"));
path.push(format!("keystore/{node}"));
if !path.is_dir() {
return Ok(None);
}
Ok(Some(path))
}

fn keystore_dir_create(&self, node: &str) -> Result<PathBuf> {
let mut path = self.root.to_path_buf();
path.push(format!("keystore-{node}"));
fs::create_dir_all(&path)?;
Ok(path)
let keystore_path = self.root.join("keystore");
create_dir_all(&keystore_path)?;

let keystore_node_path = keystore_path.join(node);
create_dir_mode(&keystore_node_path, 0o744)?;

Ok(keystore_node_path)
}
fn wallet_dir(&self) -> Result<Option<PathBuf>> {
let mut path = self.root.to_path_buf();
Expand All @@ -144,6 +152,8 @@ impl SecretStorage for FileNodeKeys {

let mut temp = NamedTempFile::new_in(&self.root)?;
temp.write_all(keypair.secret().as_ref())?;
temp.as_file_mut()
.set_permissions(Permissions::from_mode(0o600))?;
temp.persist(path)?;

Ok(())
Expand Down Expand Up @@ -193,6 +203,8 @@ impl SecretStorage for FileNodeKeys {
{
let mut file = NamedTempFile::new_in(&dir)?;
file.write_all(serde_json::to_string(&suri).unwrap().as_bytes())?;
file.as_file_mut()
.set_permissions(Permissions::from_mode(0o600))?;
file.persist(&secret)?;
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::docker::EMPTY_IMAGE;

// mod asset;
mod docker;
mod fs_utils;
mod keystore;
mod library;
mod spec_builder;
Expand Down

0 comments on commit 5b829f9

Please sign in to comment.