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

feat: adds useful info to logs #268

Merged
merged 1 commit into from
Feb 15, 2021
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 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
14 changes: 9 additions & 5 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 All @@ -32,7 +33,6 @@ impl Client {
tracing::trace!("Request Body: {}", serde_json::to_string(&body)?);

let response = self.client.post(&self.uri).headers(h).json(&body).send()?;
tracing::trace!(response_status = ?response.status(), response_headers = ?response.headers());

Client::handle_response::<Q>(response)
}
Expand All @@ -46,9 +46,13 @@ 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()?;
tracing::debug!(response_status = ?response.status(), response_headers = ?response.headers());
let response_text = response.text()?;
tracing::debug!("{}", &response_text);
EverlastingBugstopper marked this conversation as resolved.
Show resolved Hide resolved
let response_body: graphql_client::Response<Q::ResponseData> =
serde_json::from_str(&response_text)?;
EverlastingBugstopper marked this conversation as resolved.
Show resolved Hide resolved

if let Some(errs) = response_body.errors {
return Err(RoverClientError::GraphQL {
Expand Down
2 changes: 1 addition & 1 deletion installers/npm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This `README` contains just enough info to get you started with Rover. Our [docs

## Usage

A few useful Rover comamnds to interact with your graphs:
A few useful Rover commands to interact with your graphs:

1. Fetch a graph from a federated remote endpoint.

Expand Down
Loading