|
1 | 1 | // Builders for the various client structs for Docker/Kube etc.
|
2 | 2 |
|
3 |
| -use std::sync::OnceLock; |
| 3 | +use std::{collections::HashMap, sync::OnceLock}; |
4 | 4 |
|
5 |
| -use anyhow::{anyhow, bail, Context, Error, Result}; |
| 5 | +use anyhow::{anyhow, bail, Context, Result}; |
6 | 6 | use bollard;
|
7 |
| -use futures::TryFutureExt; |
8 |
| -use k8s_openapi::api::{ |
9 |
| - apps::v1::Deployment, |
10 |
| - core::v1::{Pod, Service}, |
11 |
| - networking::v1::Ingress, |
12 |
| -}; |
| 7 | +use k8s_openapi::api::core::v1::Service; |
13 | 8 | use kube::{
|
14 | 9 | self,
|
15 | 10 | api::{DynamicObject, GroupVersionKind, Patch, PatchParams},
|
@@ -48,6 +43,93 @@ pub async fn docker() -> Result<&'static bollard::Docker> {
|
48 | 43 | }
|
49 | 44 | }
|
50 | 45 |
|
| 46 | +/// Fetch registry login credentials from ~/.docker/config.json or $DOCKER_CONFIG |
| 47 | +/// |
| 48 | +/// For now, this is only `docker.io` credentials, as it is the only registry |
| 49 | +/// that effectively requires auth for public images. We don't intend for |
| 50 | +/// challenge images to be built from private images. |
| 51 | +/// |
| 52 | +/// If lookup fails, return empty hashmap as anonymous user. |
| 53 | +pub fn docker_creds() -> Result<HashMap<String, bollard::auth::DockerCredentials>> { |
| 54 | + let cred_r = docker_credential::get_credential("docker.io"); |
| 55 | + |
| 56 | + let cred = match cred_r { |
| 57 | + Ok(cred) => cred, |
| 58 | + Err(e) => { |
| 59 | + // dont die if the credentials could not be found. Warn and continue as anonymous |
| 60 | + warn!("could not fetch docker.io registry credentials from Docker config (are you logged in?)"); |
| 61 | + // log full error for debug |
| 62 | + trace!("credentials error: {e:?}"); |
| 63 | + |
| 64 | + warn!("continuing as with anonymous build credentials"); |
| 65 | + return Ok(HashMap::new()); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // convert docker_credential enum to bollad |
| 70 | + let converted = match cred { |
| 71 | + docker_credential::DockerCredential::IdentityToken(token) => { |
| 72 | + bollard::auth::DockerCredentials { |
| 73 | + identitytoken: Some(token), |
| 74 | + ..Default::default() |
| 75 | + } |
| 76 | + } |
| 77 | + docker_credential::DockerCredential::UsernamePassword(u, p) => { |
| 78 | + bollard::auth::DockerCredentials { |
| 79 | + username: Some(u), |
| 80 | + password: Some(p), |
| 81 | + ..Default::default() |
| 82 | + } |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + Ok(std::collections::HashMap::from([( |
| 87 | + "docker.io".to_string(), |
| 88 | + converted, |
| 89 | + )])) |
| 90 | +} |
| 91 | + |
| 92 | +// /// wip to pull all docker creds from json |
| 93 | +// pub async fn all_docker_creds() -> Result<HashMap<String, bollard::auth::DockerCredentials>> { |
| 94 | +// let auth_path = dirs::home_dir() |
| 95 | +// .expect("could not fetch homedir") |
| 96 | +// .join(".docker") |
| 97 | +// .join("config.json"); |
| 98 | +// let auth_file = File::open(auth_path).context("could not read docker auth config.json")?; |
| 99 | +// // json is technically yaml so use the dependency we already bring in |
| 100 | +// let auth_json: serde_yml::Value = serde_yml::from_reader(auth_file).unwrap(); |
| 101 | + |
| 102 | +// let mut map = HashMap::new(); |
| 103 | +// for (raw_reg, _raw_auth) in auth_json.get("auths").unwrap().as_mapping().unwrap() { |
| 104 | +// let reg = raw_reg.as_str().unwrap(); |
| 105 | +// let cred = match engine_type().await { |
| 106 | +// EngineType::Docker => docker_credential::get_credential(reg), |
| 107 | +// EngineType::Podman => docker_credential::get_podman_credential(reg), |
| 108 | +// } |
| 109 | +// .context("could not fetch Docker registry credentials from Docker config")?; |
| 110 | + |
| 111 | +// let creds = match cred { |
| 112 | +// docker_credential::DockerCredential::IdentityToken(token) => { |
| 113 | +// bollard::auth::DockerCredentials { |
| 114 | +// identitytoken: Some(token), |
| 115 | +// ..Default::default() |
| 116 | +// } |
| 117 | +// } |
| 118 | +// docker_credential::DockerCredential::UsernamePassword(u, p) => { |
| 119 | +// bollard::auth::DockerCredentials { |
| 120 | +// username: Some(u), |
| 121 | +// password: Some(p), |
| 122 | +// ..Default::default() |
| 123 | +// } |
| 124 | +// } |
| 125 | +// }; |
| 126 | + |
| 127 | +// map.insert(reg.to_string(), creds); |
| 128 | +// } |
| 129 | + |
| 130 | +// Ok(map) |
| 131 | +// } |
| 132 | + |
51 | 133 | #[derive(Debug)]
|
52 | 134 | pub enum EngineType {
|
53 | 135 | Docker,
|
|
0 commit comments