Skip to content

Commit 9c7cd94

Browse files
authored
Merge pull request #102 from rlupton20/tidy-id-queries
Improve uid/gid queries
2 parents 4cfc68c + 23aa1b4 commit 9c7cd94

5 files changed

Lines changed: 53 additions & 26 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Status: Available for use
1212
## [Unreleased]
1313
### Changed
1414
- Cleanup of resolution of working directory to use proper path types - PATCH
15+
- Cleanup and improve handling of user uid and gid - PATCH
1516

1617
### Added
1718

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ uuid = { version = "0.8", features = ["v4"] }
2929
yaml-rust = "0.4.4"
3030
rust-crypto = "^0.2"
3131
simplelog = "0.8"
32+
nix = "0.17"
3233

3334
[dev-dependencies]
3435
tempdir = "0.3.7"

src/environment.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ use failure::Error;
44
use std::env;
55
use std::ffi::OsString;
66
use std::path;
7-
use std::process::Command;
7+
8+
#[derive(Debug, Clone, Copy)]
9+
pub struct User {
10+
/// The users uid
11+
pub uid: nix::unistd::Uid,
12+
/// The users gid
13+
pub gid: nix::unistd::Gid,
14+
}
815

916
#[derive(Debug)]
1017
pub struct Environment {
1118
/// User uid and gid
12-
pub user_details: (String, String),
19+
pub user_details: User,
1320
/// The directory floki was launched in
1421
pub current_directory: path::PathBuf,
1522
/// The root directory for floki (may be different from
@@ -28,30 +35,24 @@ impl Environment {
2835
/// Gather information on the environment floki is running in
2936
pub fn gather(config_file: &Option<path::PathBuf>) -> Result<Self, Error> {
3037
let (floki_root, config_path) = resolve_floki_root_and_config(config_file)?;
38+
let user = get_user_details();
3139
Ok(Environment {
32-
user_details: get_user_details()?,
40+
user_details: user,
3341
current_directory: get_current_working_directory()?,
3442
floki_root: floki_root,
3543
config_file: normalize_path(config_path)?,
3644
ssh_agent_socket: get_ssh_agent_socket_path(),
37-
floki_workspace: get_floki_work_path()?,
45+
floki_workspace: get_floki_work_path(user.uid),
3846
})
3947
}
4048
}
4149

42-
/// Run a command and extract stdout as a String
43-
fn run_and_get_raw_output(cmd: &mut Command) -> Result<String, Error> {
44-
let output = String::from_utf8(cmd.output()?.stdout)?;
45-
Ok(output.trim_end().into())
46-
}
47-
4850
/// Get the user and group ids of the current user
49-
fn get_user_details() -> Result<(String, String), Error> {
50-
let user = run_and_get_raw_output(Command::new("id").arg("-u"))?;
51-
debug!("User's current id: {:?}", user);
52-
let group = run_and_get_raw_output(Command::new("id").arg("-g"))?;
53-
debug!("User's current group: {:?}", group);
54-
Ok((user, group))
51+
fn get_user_details() -> User {
52+
let uid = nix::unistd::getuid();
53+
let gid = nix::unistd::getgid();
54+
debug!("Current user has uid {} and group {}", uid, gid);
55+
User { uid, gid }
5556
}
5657

5758
/// Get the current working directory as a String
@@ -100,11 +101,9 @@ fn resolve_floki_root_and_config(
100101
}
101102

102103
/// Resolve a directory for floki to use for user-global file (caches etc)
103-
fn get_floki_work_path() -> Result<path::PathBuf, Error> {
104-
let root: path::PathBuf = env::var("HOME")
105-
.unwrap_or(format!("/tmp/{}/", get_user_details()?.0))
106-
.into();
107-
Ok(root.join(".floki"))
104+
fn get_floki_work_path(uid: nix::unistd::Uid) -> path::PathBuf {
105+
let root: path::PathBuf = env::var("HOME").unwrap_or(format!("/tmp/{}/", uid)).into();
106+
root.join(".floki")
108107
}
109108

110109
/// Normalize the filepath - this turns a relative path into an absolute one - to

src/interpret.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ fn configure_dind(
6060
}
6161

6262
fn configure_floki_user_env(cmd: DockerCommandBuilder, env: &Environment) -> DockerCommandBuilder {
63-
let (ref user, ref group) = env.user_details;
64-
let new_cmd = cmd.add_environment("FLOKI_HOST_UID", &user);
65-
new_cmd.add_environment("FLOKI_HOST_GID", &group)
63+
let user = env.user_details;
64+
let new_cmd = cmd.add_environment("FLOKI_HOST_UID", user.uid.to_string());
65+
new_cmd.add_environment("FLOKI_HOST_GID", user.gid.to_string())
6666
}
6767

6868
fn configure_floki_host_mountdir_env(
@@ -78,9 +78,9 @@ fn configure_forward_user(
7878
env: &Environment,
7979
) -> DockerCommandBuilder {
8080
if config.forward_user {
81-
let (ref user, ref group) = env.user_details;
81+
let user = env.user_details;
8282
cmd.add_docker_switch("--user")
83-
.add_docker_switch(&format!("{}:{}", user, group))
83+
.add_docker_switch(&format!("{}:{}", user.uid, user.gid))
8484
} else {
8585
cmd
8686
}

0 commit comments

Comments
 (0)