Skip to content

Commit

Permalink
feat(backend-api): Add env var to toggle GQL variable logging
Browse files Browse the repository at this point in the history
Adds an env var WASMER_API_INSECURE_LOG_VARIABLES to enable logging of
request variables.

This is useful for debugging, but somewhat dangerous because it can lead
to sensitive data being logged.
  • Loading branch information
theduke committed Sep 2, 2024
1 parent 5cb6f2a commit dbbe522
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions lib/backend-api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ pub struct WasmerClient {
pub(crate) client: reqwest::Client,
pub(crate) user_agent: reqwest::header::HeaderValue,
#[allow(unused)]
extra_debugging: bool,
log_variables: bool,
}

impl WasmerClient {
/// Env var used to enable logging of request variables.
///
/// This is somewhat dangerous since it can log sensitive information, hence
/// it is gated by a custom env var.
const ENV_VAR_LOG_VARIABLES: &'static str = "WASMER_API_INSECURE_LOG_VARIABLES";

pub fn graphql_endpoint(&self) -> &Url {
&self.graphql_endpoint
}
Expand All @@ -43,12 +49,29 @@ impl WasmerClient {
graphql_endpoint: Url,
user_agent: &str,
) -> Result<Self, anyhow::Error> {

let log_variables = {
let v = std::env::var(Self::ENV_VAR_LOG_VARIABLES).unwrap_or_default();
match v.as_str() {
"1" | "true" => true,
"0" | "false" => false,
other => {
bail!(
"invalid value for {} - expected 0/false|1/true: '{}'",
Self::ENV_VAR_LOG_VARIABLES,
other
);
}
}
};


Ok(Self {
client,
auth_token: None,
user_agent: Self::parse_user_agent(user_agent)?,
graphql_endpoint,
extra_debugging: false,
log_variables,
})
}

Expand Down Expand Up @@ -113,20 +136,22 @@ impl WasmerClient {
req
};

if self.extra_debugging {
let query = operation.query.clone();

if self.log_variables {
tracing::trace!(
query=%operation.query,
endpoint=%self.graphql_endpoint,
query=serde_json::to_string(&operation).unwrap_or_default(),
vars=?operation.variables,
"running GraphQL query"
"sending graphql query"
);
} else {
tracing::trace!(
endpoint=%self.graphql_endpoint,
query=serde_json::to_string(&operation).unwrap_or_default(),
"sending graphql query"
);
}
let query = operation.query.clone();

tracing::trace!(
endpoint=%self.graphql_endpoint,
query=serde_json::to_string(&operation).unwrap_or_default(),
"sending graphql query"
);

let res = req.json(&operation).send().await;

Expand Down Expand Up @@ -170,7 +195,10 @@ impl WasmerClient {
res
}
Err(err) => {
tracing::error!(?err, "GraphQL query failed");
tracing::error!(
?err,
"GraphQL query failed"
);
return Err(err.into());
}
};
Expand Down

0 comments on commit dbbe522

Please sign in to comment.