Skip to content

Commit

Permalink
CLI should use Agent in requests - get #986
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Feb 13, 2025
1 parent f7d93b1 commit ab2d74c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ By far most changes relate to `atomic-server`, so if not specified, assume the c
**Changes to JS assets (including the front-end and JS libraries) are not shown here**, but in [`/browser/CHANGELOG`](/browser/CHANGELOG.md).
See [STATUS.md](server/STATUS.md) to learn more about which features will remain stable.

## UNRELEASED

- CLI should use Agent in requests - get #986

## [v0.40.2]

- fix property sort order when importing + add tests #980
Expand Down
6 changes: 6 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ enum Commands {
/// Validates the store
#[command(hide = true)]
Validate,
/// Print the current agent
Agent,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
Expand Down Expand Up @@ -263,6 +265,10 @@ fn exec_command(context: &mut Context) -> AtomicResult<()> {
Commands::Validate => {
validate(context);
}
Commands::Agent => {
let agent = context.read_config();
println!("{}", agent.agent);
}
};
Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion cli/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ pub fn get_path(
// let subcommand_matches = context.matches.subcommand_matches("get").unwrap();
let path_string: String = path_vec.join(" ");

context.read_config();

// Returns a URL or Value
let store = &mut context.store;
let path = store.get_path(
&path_string,
Some(&context.mapping.lock().unwrap()),
&ForAgent::Sudo,
&store.get_default_agent()?.into(),
)?;
let out = match path {
storelike::PathReturn::Subject(subject) => {
Expand Down
13 changes: 13 additions & 0 deletions lib/src/agents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ pub fn verify_public_key(public_key: &str) -> AtomicResult<()> {
Ok(())
}

impl From<Agent> for ForAgent {
fn from(agent: Agent) -> Self {
agent.subject.into()
}
}

impl<'a> From<&'a Agent> for ForAgent {
fn from(agent: &'a Agent) -> Self {
let subject: String = agent.subject.clone();
subject.into()
}
}

#[cfg(test)]
mod test {
#[cfg(test)]
Expand Down
33 changes: 24 additions & 9 deletions lib/src/client/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,33 @@ pub fn fetch_body(
if !url.starts_with("http") {
return Err(format!("Could not fetch url '{}', must start with http.", url).into());
}
if let Some(agent) = client_agent {
get_authentication_headers(url, agent)?;
}

let agent = ureq::builder()
let client = ureq::builder()
.timeout(std::time::Duration::from_secs(2))
.build();
let resp = agent
.get(url)
.set("Accept", content_type)
.call()
.map_err(|e| format!("Error when server tried fetching {} : {}", url, e))?;

let mut req = client.get(url);
if let Some(agent) = client_agent {
let headers = get_authentication_headers(url, agent)?;
for (key, value) in headers {
req = req.set(key.as_str(), value.as_str());
}
}

let resp = match req.set("Accept", content_type).call() {
Ok(response) => response,
Err(ureq::Error::Status(status, response)) => {
let body = response
.into_string()
.unwrap_or_else(|_| "<failed to read response body>".to_string());
return Err(format!(
"Error when server tried fetching {}: Status: {}. Body: {}",
url, status, body
)
.into());
}
Err(e) => return Err(format!("Error when server tried fetching {}: {}", url, e).into()),
};
let status = resp.status();
let body = resp
.into_string()
Expand Down

0 comments on commit ab2d74c

Please sign in to comment.