Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graph-builder: switch to using dkregistry and implement authenticated requests #4

Merged
merged 4 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,154 changes: 709 additions & 445 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions graph-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ serde_derive = "^1.0.70"
serde_json = "^1.0.22"
structopt = "^0.2.10"
tar = "^0.4.16"
dkregistry = { git = "https://github.com/camallo/dkregistry-rs.git", rev = "b6573b0e2c18da6f1e50ebfc1a0d6b7ec3c8accf" }
tokio-core = "0.1"
futures = "0.1"
11 changes: 10 additions & 1 deletion graph-builder/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::net::IpAddr;
use std::num::ParseIntError;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;

Expand All @@ -32,7 +33,11 @@ pub struct Options {
pub repository: String,

/// Duration of the pause (in seconds) between scans of the registry
#[structopt(long = "period", default_value = "30", parse(try_from_str = "parse_duration"))]
#[structopt(
long = "period",
default_value = "30",
parse(try_from_str = "parse_duration")
)]
pub period: Duration,

/// Address on which the server will listen
Expand All @@ -42,6 +47,10 @@ pub struct Options {
/// Port to which the server will bind
#[structopt(long = "port", default_value = "8080")]
pub port: u16,

/// Credentials file for authentication against the image registry
#[structopt(long = "credentials-file", parse(from_os_str))]
pub credentials_path: Option<PathBuf>,
}

fn parse_duration(src: &str) -> Result<Duration, ParseIntError> {
Expand Down
23 changes: 19 additions & 4 deletions graph-builder/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate dkregistry;

use actix_web::http::header::{self, HeaderValue};
use actix_web::{HttpMessage, HttpRequest, HttpResponse};
use cincinnati::{AbstractRelease, CONTENT_TYPE, Graph, Release};
use cincinnati::{AbstractRelease, Graph, Release, CONTENT_TYPE};
use config;
use failure::{Error, ResultExt};
use registry;
Expand Down Expand Up @@ -51,9 +53,18 @@ impl State {
}

pub fn run(opts: &config::Options, state: &State) -> ! {
// Read the credentials outside the loop to avoid re-reading the file
let (username, password) =
registry::read_credentials(opts.credentials_path.as_ref(), &opts.registry)
.expect("could not read credentials");

loop {
debug!("Updating graph...");
match create_graph(&opts) {
match create_graph(
&opts,
username.as_ref().map(String::as_ref),
password.as_ref().map(String::as_ref),
) {
Ok(graph) => match serde_json::to_string(&graph) {
Ok(json) => *state.json.write().expect("json lock has been poisoned") = json,
Err(err) => error!("Failed to serialize graph: {}", err),
Expand All @@ -64,10 +75,14 @@ pub fn run(opts: &config::Options, state: &State) -> ! {
}
}

fn create_graph(opts: &config::Options) -> Result<Graph, Error> {
fn create_graph(
opts: &config::Options,
username: Option<&str>,
password: Option<&str>,
) -> Result<Graph, Error> {
let mut graph = Graph::default();

registry::fetch_releases(&opts.registry, &opts.repository)
registry::fetch_releases(&opts.registry, &opts.repository, username, password)
.context("failed to fetch all release metadata")?
.into_iter()
.try_for_each(|release| {
Expand Down
6 changes: 4 additions & 2 deletions graph-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

extern crate actix_web;
extern crate cincinnati;
extern crate dkregistry;
extern crate env_logger;
extern crate itertools;
#[macro_use]
Expand Down Expand Up @@ -69,7 +70,8 @@ fn main() -> Result<(), Error> {
App::with_state(state.clone())
.middleware(Logger::default())
.route("/v1/graph", Method::GET, graph::index)
}).bind(addr)?
.run();
})
.bind(addr)?
.run();
Ok(())
}
Loading