diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 107 |
1 files changed, 98 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index 96d3d1e..7c62521 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use crate::docker::DockerRef; use crate::manifest::ManifestFile; use crate::dockerfile::DockerfileFile; use crate::db::{Db, SqliteDb}; -use crate::registry::{Registries, HttpRegistry, AuthInfo, Credentials, basic_auth}; +use crate::registry::{Registries, HttpRegistry, Config, Credentials, basic_auth}; use rand::distr::{Alphanumeric, SampleString}; use std::ops::Range; @@ -82,13 +82,12 @@ fn update_images(now: &chrono::DateTime<chrono::Utc>, db: &dyn Db, reg: &dyn Reg last_checkeds.push(last_checked); } - let cache_max_age = chrono::Duration::hours(24); - for i in 0..imgs.len() { let img = &imgs[i]; let registry = img.registry.as_ref() .map(|x| &file.content[x.clone()]) .unwrap_or("registry.hub.docker.com"); + let cache_max_age = reg.get_cache_ttl(registry); let mut tag = img.tag.as_ref().map(|x| file.content[x.clone()].to_string()); let image_name = &file.content[img.image.clone()]; let image_id = image_ids[i]; @@ -513,9 +512,10 @@ fn main() { None => break, Some("--auth") => { if let Some(registry) = it.next() && let Some(username) = it.next() && let Some(password) = it.next() { - auths.push(AuthInfo { + auths.push(Config { host: registry.clone(), credentials: Credentials::Basic(basic_auth(username, password)), + cache_ttl: chrono::Duration::minutes(1440), }); } else { println!("Error: --auth requires three parameters"); @@ -525,9 +525,10 @@ fn main() { }, Some("--anonymous-auth") => { if let Some(registry) = it.next() { - auths.push(AuthInfo { + auths.push(Config { host: registry.clone(), credentials: Credentials::Anonymous, + cache_ttl: chrono::Duration::minutes(1440), }); } else { println!("Error: --anonymous-auth requires a registry parameter"); @@ -594,6 +595,8 @@ fn main() { } let mut repos = vec![]; + let mut docker_auths: Vec<(String, Credentials)> = vec![]; + let mut registry_ttls: Vec<(String, chrono::Duration)> = vec![]; let workdir = std::env::current_dir().unwrap(); if let Some(config_path) = config_path { @@ -652,10 +655,7 @@ fn main() { match reader.parse_next() { Ok(json_event_parser::JsonEvent::String(v)) => { - auths.push(AuthInfo { - host: key, - credentials: Credentials::Basic(v.into_owned()), - }); + docker_auths.push((key, Credentials::Basic(v.into_owned()))); }, _ => panic!("Invalid config json"), } @@ -766,6 +766,65 @@ fn main() { _ => panic!("Invalid config json"), } } + Some("registry") => { + let file = std::fs::File::open(&path).unwrap(); + let mut reader = json_event_parser::ReaderJsonParser::new(file); + + match reader.parse_next() { + Ok(json_event_parser::JsonEvent::StartObject) => {}, + _ => panic!("Invalid registry config json"), + } + + match reader.parse_next() { + Ok(json_event_parser::JsonEvent::ObjectKey(k)) if &k == "registries" => {}, + _ => panic!("Invalid registry config json"), + } + + match reader.parse_next() { + Ok(json_event_parser::JsonEvent::StartObject) => {}, + _ => panic!("Invalid registry config json"), + } + + let mut possible_key = reader.parse_next(); + while let Ok(json_event_parser::JsonEvent::ObjectKey(k)) = possible_key { + let host = k.into_owned(); + + match reader.parse_next() { + Ok(json_event_parser::JsonEvent::StartObject) => {}, + _ => panic!("Invalid registry config json"), + } + + match reader.parse_next() { + Ok(json_event_parser::JsonEvent::ObjectKey(k)) if &k == "cache_ttl_minutes" => {}, + _ => panic!("Invalid registry config json"), + } + + match reader.parse_next() { + Ok(json_event_parser::JsonEvent::Number(n)) => { + let minutes: i64 = n.parse().unwrap(); + registry_ttls.push((host, chrono::Duration::minutes(minutes))); + }, + _ => panic!("Invalid registry config json"), + } + + match reader.parse_next() { + Ok(json_event_parser::JsonEvent::EndObject) => {}, + _ => panic!("Invalid registry config json"), + } + + possible_key = reader.parse_next(); + } + + match possible_key { + Ok(json_event_parser::JsonEvent::EndObject) => {}, + _ => panic!("Invalid registry config json"), + } + + match reader.parse_next() { + Ok(json_event_parser::JsonEvent::EndObject) => {}, + _ => panic!("Invalid registry config json"), + } + } Some(_) | None => {} } } @@ -773,6 +832,36 @@ fn main() { } } + // Merge docker_auths and registry_ttls into final configs + for (host, creds) in docker_auths { + let mut ttl = chrono::Duration::minutes(1440); + for (h, t) in ®istry_ttls { + if h == &host { + ttl = *t; + break; + } + } + auths.push(Config { host, credentials: creds, cache_ttl: ttl }); + } + + // Add registry_ttls entries that don't have auth (anonymous) + for (host, ttl) in registry_ttls { + let mut found = false; + for c in &auths { + if c.host == host { + found = true; + break; + } + } + if !found { + auths.push(Config { + host, + credentials: Credentials::Anonymous, + cache_ttl: ttl, + }); + } + } + let registry = HttpRegistry::new(auths); metrics::init(); |
