Skip to content

Commit

Permalink
feat: adds useful info to logs
Browse files Browse the repository at this point in the history
- environment variables print with values when accessed
do not print at all if they do not exist

- each response from rover-client is logged

- some misc. refactors/cleanup associated with the above changes
  • Loading branch information
EverlastingBugstopper committed Feb 12, 2021
1 parent fe059a7 commit d0eb599
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 45 deletions.
1 change: 1 addition & 0 deletions crates/houston/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod profile;
pub use config::Config;
pub use error::HoustonProblem;

pub use profile::mask_key;
/// Utilites for saving, loading, and deleting configuration profiles.
pub use profile::LoadOpts;
pub use profile::Profile;
58 changes: 30 additions & 28 deletions crates/houston/src/profile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod sensitive;

use crate::{Config, HoustonProblem};
use regex::Regex;
use sensitive::Sensitive;
use serde::{Deserialize, Serialize};

use std::fmt;
use std::fs;
use std::path::PathBuf;
use std::{fmt, fs, io};

/// Collects configuration related to a profile.
#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -51,14 +51,20 @@ impl Profile {
///
/// Takes an optional `profile` argument. Defaults to `"default"`.
pub fn get_api_key(name: &str, config: &Config) -> Result<String, HoustonProblem> {
tracing::debug!(APOLLO_KEY = ?mask_key(&config.override_api_key));
match &config.override_api_key {
let api_key: Result<String, HoustonProblem> = match &config.override_api_key {
Some(api_key) => Ok(api_key.to_string()),
None => {
let opts = LoadOpts { sensitive: true };
Ok(Profile::load(name, config, opts)?.sensitive.api_key)
let profile = Profile::load(name, config, opts)?;
Ok(profile.sensitive.api_key)
}
}
};

let api_key = api_key?;

tracing::debug!("using API key {}", mask_key(&api_key));

Ok(api_key)
}

/// Saves configuration options for a specific profile to the file system,
Expand Down Expand Up @@ -89,7 +95,7 @@ impl Profile {
let dir = Profile::dir(name, config);
tracing::debug!(dir = ?dir);
Ok(fs::remove_dir_all(dir).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => HoustonProblem::ProfileNotFound(name.to_string()),
io::ErrorKind::NotFound => HoustonProblem::ProfileNotFound(name.to_string()),
_ => HoustonProblem::IOError(e),
})?)
}
Expand All @@ -107,7 +113,7 @@ impl Profile {
let entry_path = entry?.path();
if entry_path.is_dir() {
let profile = entry_path.file_stem().unwrap();
tracing::debug!(profile = ?profile);
tracing::debug!(?profile);
profiles.push(profile.to_string_lossy().into_owned());
}
}
Expand All @@ -122,18 +128,14 @@ impl fmt::Display for Profile {
}
}

// Masks all but the first 4 and last 4 chars of a key with a set number of *
// valid keys are all at least 22 chars. We don't care if invalid keys
/// Masks all but the first 4 and last 4 chars of a key with a set number of *
/// valid keys are all at least 22 chars.
// We don't care if invalid keys
// are printed, so we don't need to worry about strings 8 chars or less,
// which this fn would just print back out
pub fn mask_key(key: &Option<String>) -> Option<String> {
if let Some(key) = key {
let ex = regex::Regex::new(r"(?im)^(.{4})(.*)(.{4})$").unwrap();
let masked = ex.replace(key, "$1******************$3").into();
Some(masked)
} else {
None
}
pub fn mask_key(key: &str) -> String {
let ex = Regex::new(r"(?im)^(.{4})(.*)(.{4})$").expect("Could not create regular expression.");
ex.replace(key, "$1******************$3").to_string()
}

#[cfg(test)]
Expand All @@ -143,15 +145,15 @@ mod tests {
#[test]
#[allow(clippy::many_single_char_names)]
fn masks_valid_keys_properly() {
let a = Some("user:gh.foo:djru4788dhsg3657fhLOLO".to_string());
assert_eq!(mask_key(&a), Some("user******************LOLO".to_string()));
let b = Some("service:foo:dh47dh27sg18aj49dkLOLO".to_string());
assert_eq!(mask_key(&b), Some("serv******************LOLO".to_string()));
let c = Some("some nonsense".to_string());
assert_eq!(mask_key(&c), Some("some******************ense".to_string()));
let d = Some("".to_string());
assert_eq!(mask_key(&d), Some("".to_string()));
let e = Some("short".to_string());
assert_eq!(mask_key(&e), Some("short".to_string()));
let a = "user:gh.foo:djru4788dhsg3657fhLOLO";
assert_eq!(mask_key(a), "user******************LOLO".to_string());
let b = "service:foo:dh47dh27sg18aj49dkLOLO";
assert_eq!(mask_key(b), "serv******************LOLO".to_string());
let c = "some nonsense";
assert_eq!(mask_key(c), "some******************ense".to_string());
let d = "";
assert_eq!(mask_key(d), "".to_string());
let e = "short";
assert_eq!(mask_key(e), "short".to_string());
}
}
3 changes: 1 addition & 2 deletions crates/houston/src/profile/sensitive.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{profile::Profile, Config, HoustonProblem};
use serde::{Deserialize, Serialize};

use std::fmt;
use std::fs;
use std::path::PathBuf;
use std::{fmt, fs};

/// Holds sensitive information regarding authentication.
#[derive(Debug, Serialize, Deserialize)]
Expand Down
12 changes: 8 additions & 4 deletions crates/rover-client/src/blocking/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{headers, RoverClientError};
use graphql_client::GraphQLQuery;
use reqwest::blocking::{Client as ReqwestClient, Response};
use std::collections::HashMap;

/// Represents a generic GraphQL client for making http requests.
pub struct Client {
client: reqwest::blocking::Client,
client: ReqwestClient,
uri: String,
}

Expand All @@ -13,7 +14,7 @@ impl Client {
/// This client is used for generic GraphQL requests, such as introspection.
pub fn new(uri: &str) -> Client {
Client {
client: reqwest::blocking::Client::new(),
client: ReqwestClient::new(),
uri: uri.to_string(),
}
}
Expand Down Expand Up @@ -46,9 +47,12 @@ impl Client {
///
/// If successful, it will return body.data, unwrapped
pub fn handle_response<Q: graphql_client::GraphQLQuery>(
response: reqwest::blocking::Response,
response: Response,
) -> Result<Q::ResponseData, RoverClientError> {
let response_body: graphql_client::Response<Q::ResponseData> = response.json()?;
let response_text = response.text()?;
tracing::debug!("{}", &response_text);
let response_body: graphql_client::Response<Q::ResponseData> =
serde_json::from_str(&response_text)?;

if let Some(errs) = response_body.errors {
return Err(RoverClientError::GraphQL {
Expand Down
Loading

0 comments on commit d0eb599

Please sign in to comment.