Skip to content

Commit 25d4ba9

Browse files
feat: adds useful info to logs (#268)
- 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
1 parent ef73604 commit 25d4ba9

File tree

7 files changed

+363
-47
lines changed

7 files changed

+363
-47
lines changed

crates/houston/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod profile;
99
pub use config::Config;
1010
pub use error::HoustonProblem;
1111

12+
pub use profile::mask_key;
1213
/// Utilites for saving, loading, and deleting configuration profiles.
1314
pub use profile::LoadOpts;
1415
pub use profile::Profile;

crates/houston/src/profile/mod.rs

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
mod sensitive;
22

33
use crate::{Config, HoustonProblem};
4+
use regex::Regex;
45
use sensitive::Sensitive;
56
use serde::{Deserialize, Serialize};
67

7-
use std::fmt;
8-
use std::fs;
98
use std::path::PathBuf;
9+
use std::{fmt, fs, io};
1010

1111
/// Collects configuration related to a profile.
1212
#[derive(Debug, Serialize, Deserialize)]
@@ -51,14 +51,20 @@ impl Profile {
5151
///
5252
/// Takes an optional `profile` argument. Defaults to `"default"`.
5353
pub fn get_api_key(name: &str, config: &Config) -> Result<String, HoustonProblem> {
54-
tracing::debug!(APOLLO_KEY = ?mask_key(&config.override_api_key));
55-
match &config.override_api_key {
54+
let api_key: Result<String, HoustonProblem> = match &config.override_api_key {
5655
Some(api_key) => Ok(api_key.to_string()),
5756
None => {
5857
let opts = LoadOpts { sensitive: true };
59-
Ok(Profile::load(name, config, opts)?.sensitive.api_key)
58+
let profile = Profile::load(name, config, opts)?;
59+
Ok(profile.sensitive.api_key)
6060
}
61-
}
61+
};
62+
63+
let api_key = api_key?;
64+
65+
tracing::debug!("using API key {}", mask_key(&api_key));
66+
67+
Ok(api_key)
6268
}
6369

6470
/// Saves configuration options for a specific profile to the file system,
@@ -89,7 +95,7 @@ impl Profile {
8995
let dir = Profile::dir(name, config);
9096
tracing::debug!(dir = ?dir);
9197
Ok(fs::remove_dir_all(dir).map_err(|e| match e.kind() {
92-
std::io::ErrorKind::NotFound => HoustonProblem::ProfileNotFound(name.to_string()),
98+
io::ErrorKind::NotFound => HoustonProblem::ProfileNotFound(name.to_string()),
9399
_ => HoustonProblem::IOError(e),
94100
})?)
95101
}
@@ -107,7 +113,7 @@ impl Profile {
107113
let entry_path = entry?.path();
108114
if entry_path.is_dir() {
109115
let profile = entry_path.file_stem().unwrap();
110-
tracing::debug!(profile = ?profile);
116+
tracing::debug!(?profile);
111117
profiles.push(profile.to_string_lossy().into_owned());
112118
}
113119
}
@@ -122,18 +128,14 @@ impl fmt::Display for Profile {
122128
}
123129
}
124130

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

139141
#[cfg(test)]
@@ -143,15 +145,15 @@ mod tests {
143145
#[test]
144146
#[allow(clippy::many_single_char_names)]
145147
fn masks_valid_keys_properly() {
146-
let a = Some("user:gh.foo:djru4788dhsg3657fhLOLO".to_string());
147-
assert_eq!(mask_key(&a), Some("user******************LOLO".to_string()));
148-
let b = Some("service:foo:dh47dh27sg18aj49dkLOLO".to_string());
149-
assert_eq!(mask_key(&b), Some("serv******************LOLO".to_string()));
150-
let c = Some("some nonsense".to_string());
151-
assert_eq!(mask_key(&c), Some("some******************ense".to_string()));
152-
let d = Some("".to_string());
153-
assert_eq!(mask_key(&d), Some("".to_string()));
154-
let e = Some("short".to_string());
155-
assert_eq!(mask_key(&e), Some("short".to_string()));
148+
let a = "user:gh.foo:djru4788dhsg3657fhLOLO";
149+
assert_eq!(mask_key(a), "user******************LOLO".to_string());
150+
let b = "service:foo:dh47dh27sg18aj49dkLOLO";
151+
assert_eq!(mask_key(b), "serv******************LOLO".to_string());
152+
let c = "some nonsense";
153+
assert_eq!(mask_key(c), "some******************ense".to_string());
154+
let d = "";
155+
assert_eq!(mask_key(d), "".to_string());
156+
let e = "short";
157+
assert_eq!(mask_key(e), "short".to_string());
156158
}
157159
}

crates/houston/src/profile/sensitive.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::{profile::Profile, Config, HoustonProblem};
22
use serde::{Deserialize, Serialize};
33

4-
use std::fmt;
5-
use std::fs;
64
use std::path::PathBuf;
5+
use std::{fmt, fs};
76

87
/// Holds sensitive information regarding authentication.
98
#[derive(Debug, Serialize, Deserialize)]

crates/rover-client/src/blocking/client.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{headers, RoverClientError};
22
use graphql_client::GraphQLQuery;
3+
use reqwest::blocking::{Client as ReqwestClient, Response};
34
use std::collections::HashMap;
45

56
/// Represents a generic GraphQL client for making http requests.
67
pub struct Client {
7-
client: reqwest::blocking::Client,
8+
client: ReqwestClient,
89
uri: String,
910
}
1011

@@ -13,7 +14,7 @@ impl Client {
1314
/// This client is used for generic GraphQL requests, such as introspection.
1415
pub fn new(uri: &str) -> Client {
1516
Client {
16-
client: reqwest::blocking::Client::new(),
17+
client: ReqwestClient::new(),
1718
uri: uri.to_string(),
1819
}
1920
}
@@ -32,7 +33,6 @@ impl Client {
3233
tracing::trace!("Request Body: {}", serde_json::to_string(&body)?);
3334

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

3737
Client::handle_response::<Q>(response)
3838
}
@@ -46,9 +46,13 @@ impl Client {
4646
///
4747
/// If successful, it will return body.data, unwrapped
4848
pub fn handle_response<Q: graphql_client::GraphQLQuery>(
49-
response: reqwest::blocking::Response,
49+
response: Response,
5050
) -> Result<Q::ResponseData, RoverClientError> {
51-
let response_body: graphql_client::Response<Q::ResponseData> = response.json()?;
51+
tracing::debug!(response_status = ?response.status(), response_headers = ?response.headers());
52+
let response_text = response.text()?;
53+
tracing::debug!("{}", &response_text);
54+
let response_body: graphql_client::Response<Q::ResponseData> =
55+
serde_json::from_str(&response_text)?;
5256

5357
if let Some(errs) = response_body.errors {
5458
return Err(RoverClientError::GraphQL {

installers/npm/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This `README` contains just enough info to get you started with Rover. Our [docs
1414

1515
## Usage
1616

17-
A few useful Rover comamnds to interact with your graphs:
17+
A few useful Rover commands to interact with your graphs:
1818

1919
1. Fetch a graph from a federated remote endpoint.
2020

0 commit comments

Comments
 (0)