Skip to content

Commit 39ddb57

Browse files
committed
feat: add ability to login to private registries
1 parent 4ddcfb0 commit 39ddb57

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/lib.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
use anyhow::{anyhow, Result};
22
use clap::{App, Arg};
3-
use docker_api::api::{ContainerCreateOpts, PullOpts, RmContainerOpts};
3+
use docker_api::api::{
4+
ContainerCreateOpts,
5+
PullOpts,
6+
RmContainerOpts,
7+
RegistryAuth
8+
};
49
use futures_util::{StreamExt, TryStreamExt};
510
use podman_api::opts::ContainerCreateOpts as PodmanContainerCreateOpts;
611
use podman_api::opts::PullOpts as PodmanPullOpts;
12+
use podman_api::opts::RegistryAuth as PodmanRegistryAuth;
713
use std::path::PathBuf;
814
use tar::Archive;
915

@@ -28,6 +34,10 @@ pub struct Config {
2834
write_to_stdout: bool,
2935
// What level of logs to output
3036
log_level: String,
37+
// Username for singing into a private registry
38+
username: String,
39+
// Password for signing into a private registry
40+
password: String,
3141
}
3242

3343
pub fn get_args() -> Result<Config> {
@@ -65,6 +75,24 @@ pub fn get_args() -> Result<Config> {
6575
.short("w")
6676
.long("write-to-stdout"),
6777
)
78+
.arg(
79+
Arg::with_name("username")
80+
.value_name("USERNAME")
81+
.help("Username used for singing into a private registry.")
82+
.short("u")
83+
.long("username")
84+
.default_value(""),
85+
86+
)
87+
.arg(
88+
Arg::with_name("password")
89+
.value_name("PASSWORD")
90+
.help("Password used for signing into a private registry. * WARNING *: Writing credentials to your terminal is risky. Be sure you are okay with them showing up in your history")
91+
.short("p")
92+
.long("password")
93+
.default_value(""),
94+
95+
)
6896
.arg(
6997
Arg::with_name("log-level")
7098
.value_name("LOG-LEVEL")
@@ -80,6 +108,9 @@ pub fn get_args() -> Result<Config> {
80108
let content_path = matches.value_of("content-path").unwrap().to_string();
81109
let write_to_stdout = matches.is_present("write-to-stdout");
82110
let log_level = matches.value_of("log-level").unwrap().to_string();
111+
// TODO (tyslaton): Need to come up with a way for this to be extracted from the docker config to be more secure locally.
112+
let username = matches.value_of("username").unwrap().to_string();
113+
let password = matches.value_of("password").unwrap().to_string();
83114

84115
if write_to_stdout {
85116
return Err(anyhow!(
@@ -93,6 +124,8 @@ pub fn get_args() -> Result<Config> {
93124
content_path,
94125
write_to_stdout,
95126
log_level,
127+
username,
128+
password
96129
})
97130
}
98131

@@ -132,7 +165,12 @@ pub async fn run(config: Config) -> Result<()> {
132165
};
133166

134167
if let Some(docker) = &rt.docker {
135-
let pull_opts = PullOpts::builder().image(repo).tag(tag).build();
168+
let auth = RegistryAuth::builder()
169+
.username(config.username)
170+
.password(config.password)
171+
.build();
172+
let pull_opts = PullOpts::builder().image(repo).tag(tag).auth(auth).build();
173+
136174
let images = docker.images();
137175
let mut stream = images.pull(&pull_opts);
138176

@@ -147,8 +185,13 @@ pub async fn run(config: Config) -> Result<()> {
147185
}
148186
}
149187
} else {
188+
let auth = PodmanRegistryAuth::builder()
189+
.username(config.username)
190+
.password(config.password)
191+
.build();
150192
let pull_opts = PodmanPullOpts::builder()
151193
.reference(config.image.clone().trim())
194+
.auth(auth)
152195
.build();
153196
let images = rt.podman.as_ref().unwrap().images();
154197
let mut stream = images.pull(&pull_opts);

0 commit comments

Comments
 (0)