Skip to content

Commit 67e201f

Browse files
committed
Properly resolve local registry IP when running remotely
1 parent d751669 commit 67e201f

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed

Cargo.lock

Lines changed: 11 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
@@ -11,6 +11,7 @@ axum = { version = "0.7.1", features = [ "tracing" ] }
1111
base64 = "0.21.5"
1212
constant_time_eq = "0.3.0"
1313
futures = "0.3.29"
14+
gethostname = "0.4.3"
1415
hex = "0.4.3"
1516
itertools = "0.12.0"
1617
nom = "7.1.3"

quick-test.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#!/bin/sh
22

3+
export REGISTRY_ADDR=127.0.0.1:3000
4+
5+
if [ $PODMAN_IS_REMOTE == "true" ]; then
6+
export REGISTRY_ADDR=$(dig +short $(hostname)):3000
7+
fi
8+
9+
echo "registry: ${REGISTRY_ADDR}"
10+
11+
podman login --tls-verify=false --username devuser --password devpw http://${REGISTRY_ADDR}
312
podman pull crccheck/hello-world
4-
podman tag crccheck/hello-world 127.0.0.1:3000/testing/hello:prod
5-
podman push --tls-verify=false 127.0.0.1:3000/testing/hello:prod
13+
podman tag crccheck/hello-world ${REGISTRY_ADDR}/testing/hello:prod
14+
podman push --tls-verify=false ${REGISTRY_ADDR}/testing/hello:prod

src/config.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use constant_time_eq::constant_time_eq;
55
use sec::Secret;
66
use serde::Deserialize;
77

8-
use crate::registry::{AuthProvider, UnverifiedCredentials};
8+
use crate::{
9+
podman_is_remote,
10+
registry::{AuthProvider, UnverifiedCredentials},
11+
};
912

1013
#[derive(Debug, Default, Deserialize)]
1114
#[serde(deny_unknown_fields)]
@@ -150,5 +153,9 @@ impl Default for ReverseProxyConfig {
150153
}
151154

152155
fn default_http_bind() -> SocketAddr {
153-
([127, 0, 0, 1], 3000).into()
156+
if podman_is_remote() {
157+
([0, 0, 0, 0], 3000).into()
158+
} else {
159+
([127, 0, 0, 1], 3000).into()
160+
}
154161
}

src/main.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod reverse_proxy;
55

66
use std::{
77
env, fs,
8-
net::{Ipv4Addr, SocketAddr},
8+
net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs},
99
path::Path,
1010
str::FromStr,
1111
sync::Arc,
@@ -14,6 +14,7 @@ use std::{
1414
use anyhow::Context;
1515
use axum::{async_trait, Router};
1616
use config::Config;
17+
use gethostname::gethostname;
1718
use podman::Podman;
1819
use registry::{
1920
storage::ImageLocation, ContainerRegistry, ManifestReference, Reference, RegistryHooks,
@@ -51,8 +52,7 @@ impl PodmanHook {
5152
local_addr: SocketAddr,
5253
registry_credentials: (String, Secret<String>),
5354
) -> Self {
54-
let is_remote = env::var("PODMAN_IS_REMOTE").unwrap_or_default() == "true";
55-
let podman = Podman::new(podman_path, is_remote);
55+
let podman = Podman::new(podman_path, podman_is_remote());
5656
Self {
5757
podman,
5858
reverse_proxy,
@@ -88,6 +88,10 @@ impl PodmanHook {
8888
}
8989
}
9090

91+
pub(crate) fn podman_is_remote() -> bool {
92+
env::var("PODMAN_IS_REMOTE").unwrap_or_default() == "true"
93+
}
94+
9195
#[derive(Debug, Deserialize)]
9296
#[serde(rename_all = "PascalCase")]
9397
#[allow(dead_code)]
@@ -255,7 +259,25 @@ async fn main() -> anyhow::Result<()> {
255259

256260
debug!(?cfg, "loaded configuration");
257261

258-
let local_addr = SocketAddr::from(([127, 0, 0, 1], cfg.reverse_proxy.http_bind.port()));
262+
let local_ip: IpAddr = if podman_is_remote() {
263+
info!("podman is remote, trying to guess IP address");
264+
let local_hostname = gethostname();
265+
let dummy_addr = (
266+
local_hostname
267+
.to_str()
268+
.ok_or_else(|| anyhow::anyhow!("local hostname is not valid UTF8"))?,
269+
12345,
270+
)
271+
.to_socket_addrs()
272+
.ok()
273+
.and_then(|addrs| addrs.into_iter().next())
274+
.ok_or_else(|| anyhow::anyhow!("failed to resolve local hostname"))?;
275+
dummy_addr.ip()
276+
} else {
277+
[127, 0, 0, 1].into()
278+
};
279+
280+
let local_addr = SocketAddr::from((local_ip, cfg.reverse_proxy.http_bind.port()));
259281
info!(%local_addr, "guessing local registry address");
260282

261283
let reverse_proxy = ReverseProxy::new();

0 commit comments

Comments
 (0)