Skip to content

Commit 14829a7

Browse files
committed
thread token passed from --auth through to check for interpreter
Signed-off-by: Matt Wrock <matt@mattwrock.com>
1 parent cd3bb12 commit 14829a7

10 files changed

Lines changed: 47 additions & 25 deletions

File tree

components/common/src/templating.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ lazy_static! {
3939
/// A convenience method that compiles a package's install and uninstall hooks and any configuration
4040
/// templates in its config_install folder
4141
pub async fn compile_for_package_install(package: &PackageInstall,
42-
feature_flags: FeatureFlag)
42+
feature_flags: FeatureFlag,
43+
token: Option<&str>)
4344
-> Result<()> {
44-
let pkg = package::Pkg::from_install(package).await?;
45+
let pkg = package::Pkg::from_install(package, token).await?;
4546

4647
fs::SvcDir::new(&pkg.name, &pkg.svc_user, &pkg.svc_group).create()?;
4748

@@ -464,7 +465,7 @@ mod tests {
464465
create_with_content(config_path.join("config.txt"),
465466
"config message is {{cfg.message}}");
466467

467-
compile_for_package_install(&pkg_install, FeatureFlag::empty()).await
468+
compile_for_package_install(&pkg_install, FeatureFlag::empty(), None).await
468469
.expect("compile package");
469470

470471
assert_eq!(

components/common/src/templating/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ mod tests {
10461046
let output_dir = root.join("output");
10471047
fs::create_dir_all(&output_dir).expect("create output dir");
10481048

1049-
let pkg = Pkg::from_install(&pkg_install).await.unwrap();
1049+
let pkg = Pkg::from_install(&pkg_install, None).await.unwrap();
10501050
let cfg = Cfg::new(&pkg, None).unwrap();
10511051
let ctx = RenderContext::new(&pkg, &cfg);
10521052

components/common/src/templating/hooks.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,11 @@ pub trait PackageMaintenanceHookExt: Hook<ExitValue = ExitStatus> + Sync {
288288
let hook_name = Self::FILE_NAME;
289289
ui.status(Status::Executing,
290290
format!("{} hook for '{}'", hook_name, package.ident()))?;
291-
templating::compile_for_package_install(package, feature_flags).await?;
291+
templating::compile_for_package_install(package, feature_flags, token).await?;
292292

293-
// Only windows uses svc_password
294293
#[cfg(target_os = "windows")]
295294
let pkg = {
296-
let mut pkg = Pkg::from_install(package).await?;
295+
let mut pkg = Pkg::from_install(package, token).await?;
297296
// Hooks do not have access to svc_passwords so we execute them under the
298297
// current user account.
299298
if let Some(user) = habitat_core::os::users::get_current_username()? {
@@ -309,7 +308,7 @@ pub trait PackageMaintenanceHookExt: Hook<ExitValue = ExitStatus> + Sync {
309308
};
310309
#[cfg(not(target_os = "windows"))]
311310
let pkg = {
312-
let mut pkg = Pkg::from_install(package).await?;
311+
let mut pkg = Pkg::from_install(package, token).await?;
313312
// Pass through auth token if provided
314313
if let Some(token_value) = token {
315314
pkg.env
@@ -866,8 +865,8 @@ echo "The message is Hola Mundo"
866865
.join(MetaFile::PackageType.to_string()),
867866
"native");
868867
}
869-
let pkg = Pkg::from_install(&pkg_install).await
870-
.expect("Could not create package!");
868+
let pkg = Pkg::from_install(&pkg_install, None).await
869+
.expect("Could not create package!");
871870

872871
// This is gross, but it actually works
873872
let cfg_path = concrete_path.as_ref().join("default.toml");

components/common/src/templating/package.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ impl Env {
4545
///
4646
/// This means we work on any operating system, as long as you can invoke the Supervisor,
4747
/// without having to worry much about context.
48-
pub async fn new(package: &PackageInstall) -> Result<Self> {
48+
pub async fn new(package: &PackageInstall, token: Option<&str>) -> Result<Self> {
4949
let mut env = package.environment_for_command()?;
50-
let path = Self::transform_path(env.get(PATH_KEY), package.package_type()?).await?;
50+
let path = Self::transform_path(env.get(PATH_KEY), package.package_type()?, token).await?;
5151
env.insert(PATH_KEY.to_string(), path);
5252
Ok(Env(env))
5353
}
@@ -60,13 +60,16 @@ impl Env {
6060
self.0.insert(key, value);
6161
}
6262

63-
async fn transform_path(path: Option<&String>, package_type: PackageType) -> Result<String> {
63+
async fn transform_path(path: Option<&String>,
64+
package_type: PackageType,
65+
token: Option<&str>)
66+
-> Result<String> {
6467
let mut paths: Vec<PathBuf> = match path {
6568
Some(path) => env::split_paths(&path).collect(),
6669
None => vec![],
6770
};
6871
match package_type {
69-
PackageType::Standard => path::append_interpreter_and_env_path(&mut paths).await,
72+
PackageType::Standard => path::append_interpreter_and_env_path(&mut paths, token).await,
7073
PackageType::Native => path::append_env_path(&mut paths),
7174
}
7275
}
@@ -101,7 +104,7 @@ pub struct Pkg {
101104
}
102105

103106
impl Pkg {
104-
pub async fn from_install(package: &PackageInstall) -> Result<Self> {
107+
pub async fn from_install(package: &PackageInstall, token: Option<&str>) -> Result<Self> {
105108
let ident = FullyQualifiedPackageIdent::try_from(&package.ident)?;
106109
let (svc_user, svc_group) = get_user_and_group(package)?;
107110
let pkg = Pkg { svc_path: fs::svc_path(&package.ident.name),
@@ -116,7 +119,7 @@ impl Pkg {
116119
svc_pid_file: fs::svc_pid_file(&package.ident.name),
117120
svc_user,
118121
svc_group,
119-
env: Env::new(package).await?,
122+
env: Env::new(package, token).await?,
120123
deps: package.tdeps()?,
121124
exposes: package.exposes()?,
122125
exports: package.exports()?,

components/common/src/util/path.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/VERSION"));
5858
/// * If a known-working package identifier string cannot be parsed
5959
/// * If the Supervisor is not executing inside a package, and if no interpreter package is
6060
/// installed
61-
async fn interpreter_paths() -> Result<Vec<PathBuf>> {
61+
async fn interpreter_paths(token: Option<&str>) -> Result<Vec<PathBuf>> {
6262
// First, we'll check if we're running inside a package. If we are, then we should be able to
6363
// access the `../DEPS` metadata file and read it to get the specific version of the
6464
// interpreter.
@@ -95,6 +95,9 @@ async fn interpreter_paths() -> Result<Vec<PathBuf>> {
9595
// Nope, no packages of the interpreter installed. Now we're going to see if the
9696
// interpreter command is present on `PATH`.
9797
Err(_) => {
98+
// Prefer the explicitly-passed token; fall back to the environment variable.
99+
let env_token = env::var(habitat_core::AUTH_TOKEN_ENVVAR).ok();
100+
let auth_token = token.or(env_token.as_deref());
98101
match install::type_erased_start(&mut ui::NullUi::new(),
99102
&default_bldr_url(),
100103
&ChannelIdent::default(),
@@ -105,7 +108,7 @@ async fn interpreter_paths() -> Result<Vec<PathBuf>> {
105108
VERSION,
106109
FS_ROOT_PATH.as_path(),
107110
&cache_artifact_path(None::<String>),
108-
env::var("HAB_AUTH_TOKEN").ok().as_deref(),
111+
auth_token,
109112
&InstallMode::default(),
110113
&LocalPackageUsage::default(),
111114
InstallHookMode::default()).await
@@ -129,8 +132,10 @@ fn root_paths(paths: &mut [PathBuf]) {
129132
}
130133

131134
/// Append the the interpreter path and environment PATH variable to the provided path entries
132-
pub async fn append_interpreter_and_env_path(path_entries: &mut Vec<PathBuf>) -> Result<String> {
133-
let mut paths = interpreter_paths().await?;
135+
pub async fn append_interpreter_and_env_path(path_entries: &mut Vec<PathBuf>,
136+
token: Option<&str>)
137+
-> Result<String> {
138+
let mut paths = interpreter_paths(token).await?;
134139
root_paths(&mut paths);
135140
path_entries.append(&mut paths);
136141
append_env_path(path_entries)

components/sup/src/command/shell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub async fn sh() -> Result<()> {
2929

3030
async fn set_path() -> Result<()> {
3131
let mut paths: Vec<PathBuf> = Vec::new();
32-
let new_path = path::append_interpreter_and_env_path(&mut paths).await?;
32+
let new_path = path::append_interpreter_and_env_path(&mut paths, None).await?;
3333

3434
debug!("Setting the PATH to {}", &new_path);
3535
// TODO: Audit that the environment access only happens in single-threaded code.

components/sup/src/manager/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ impl Service {
605605
// the current user.
606606
#[cfg(windows)]
607607
async fn resolve_pkg(package: &PackageInstall, spec: &ServiceSpec) -> Result<Pkg> {
608-
let mut pkg = Pkg::from_install(package).await?;
608+
let mut pkg = Pkg::from_install(package, None).await?;
609609
if spec.svc_encrypted_password.is_none()
610610
&& pkg.svc_user == DEFAULT_USER
611611
&& let Some(user) = users::get_current_username()?
@@ -617,7 +617,7 @@ impl Service {
617617

618618
#[cfg(unix)]
619619
async fn resolve_pkg(package: &PackageInstall, _spec: &ServiceSpec) -> Result<Pkg> {
620-
Ok(Pkg::from_install(package).await?)
620+
Ok(Pkg::from_install(package, None).await?)
621621
}
622622

623623
/// Returns the config root given the package and optional config-from path.

components/sup/src/manager/service/hooks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ mod tests {
751751
.join(MetaFile::PackageType.to_string()),
752752
"native");
753753
}
754-
Pkg::from_install(&pkg_install).await.unwrap()
754+
Pkg::from_install(&pkg_install, None).await.unwrap()
755755
}
756756

757757
fn ctx<'a>(service_group: &'a ServiceGroup,

components/sup/src/manager/service/pipe_hook_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ mod tests {
331331
PathBuf::from("/tmp"),
332332
PathBuf::from("/tmp"),
333333
PathBuf::from("/tmp"));
334-
Pkg::from_install(&pkg_install).await.unwrap()
334+
Pkg::from_install(&pkg_install, None).await.unwrap()
335335
}
336336

337337
#[tokio::test]

test/end-to-end/test_pkg_install.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ Describe "pkg install" {
3939
}
4040
}
4141

42+
if ($IsLinux) {
43+
It "installs interpreter from builder for install hooks using --auth token" {
44+
Remove-Item /hab/pkgs/core/busybox-static -Recurse -force -ErrorAction Ignore
45+
Remove-Item "/hab/cache/artifacts/core-busybox-static-*" -ErrorAction Ignore
46+
$token = $env:HAB_AUTH_TOKEN
47+
$env:HAB_AUTH_TOKEN = $null
48+
$cached = Get-Item "/hab/cache/artifacts/$env:HAB_ORIGIN-dep-pkg-1*"
49+
Write-Host (hab pkg install $cached.FullName --auth "$token" | Out-String)
50+
$env:HAB_AUTH_TOKEN = $token
51+
$LASTEXITCODE | Should -Be 0
52+
Get-Content "$(hab pkg path $env:HAB_ORIGIN/dep-pkg-1)/INSTALL_HOOK_STATUS" | Should -Be "0"
53+
}
54+
}
55+
4256
It "installs all dependencies and executes all install hooks" {
4357
$cached = Get-Item "/hab/cache/artifacts/$env:HAB_ORIGIN-dep-pkg-3*"
4458
hab pkg install $cached.FullName

0 commit comments

Comments
 (0)