Skip to content

Commit

Permalink
fix(dev): respect APOLLO_KEY-only uses
Browse files Browse the repository at this point in the history
  - we should be able to run rover dev without running rover config auth
    by setting APOLLO_KEY
  • Loading branch information
aaronArinder committed Jan 14, 2025
1 parent 6a3d561 commit 198be1e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
30 changes: 24 additions & 6 deletions src/command/dev/next/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ use crate::{
read_file::FsReadFile,
write_file::FsWriteFile,
},
env::RoverEnvKey,
},
RoverError, RoverOutput, RoverResult,
RoverOutput, RoverResult,
};

use self::router::config::{RouterAddress, RunRouterConfig};
use self::router::config::RouterAddress;

mod router;

Expand Down Expand Up @@ -75,7 +76,8 @@ impl Dev {
let service = client_config
.get_authenticated_client(profile)?
.studio_graphql_service()?;
let service = WhoAmI::new(service);

let who_am_i_service = WhoAmI::new(service);

let fetch_remote_subgraphs_factory = MakeFetchRemoteSubgraphs::builder()
.studio_client_config(client_config.clone())
Expand Down Expand Up @@ -148,8 +150,19 @@ impl Dev {
None => RouterVersion::Latest,
};

let credential =
Profile::get_credential(&profile.profile_name, &Config::new(None::<&String>, None)?)?;
let api_key_override = match std::env::var(RoverEnvKey::Key.to_string()) {
Ok(key) => Some(key),
Err(_err) => None,
};
let home_override = match std::env::var(RoverEnvKey::Home.to_string()) {
Ok(home) => Some(home),
Err(_err) => None,
};

let credential = Profile::get_credential(
&profile.profile_name,
&Config::new(home_override.as_ref(), api_key_override)?,
)?;

let composition_runner = composition_pipeline
.runner(
Expand Down Expand Up @@ -181,7 +194,11 @@ impl Dev {
.await?
.load_config(&read_file_impl, router_address, router_config_path)
.await?
.load_remote_config(service, graph_ref.clone(), Some(credential))
.load_remote_config(
who_am_i_service,
graph_ref.clone(),
Some(credential.clone()),
)
.await;
let router_address = *run_router.state.config.address();
let mut run_router = run_router
Expand All @@ -191,6 +208,7 @@ impl Dev {
&tmp_config_dir_path,
client_config.clone(),
supergraph_schema,
credential,
)
.await?
.watch_for_changes(write_file_impl, composition_messages)
Expand Down
24 changes: 20 additions & 4 deletions src/command/dev/next/router/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::HashMap, fmt, process::Stdio};
use buildstructor::Builder;
use camino::Utf8PathBuf;
use futures::TryFutureExt;
use houston::Credential;
use rover_std::Style;
use semver::Version;
use tap::TapFallible;
Expand Down Expand Up @@ -107,6 +108,7 @@ pub struct RunRouterBinary<Spawn: Send> {
config_path: Utf8PathBuf,
supergraph_schema_path: Utf8PathBuf,
remote_config: Option<RemoteRouterConfig>,
credential: Credential,
spawn: Spawn,
}

Expand Down Expand Up @@ -134,13 +136,27 @@ where
"info".to_string(),
"--dev".to_string(),
];
let mut env = HashMap::from_iter([("APOLLO_ROVER".to_string(), "true".to_string())]);

// We set the APOLLO_KEY here, but it might be overriden by RemoteRouterConfig. That
// struct takes the who_am_i service, gets an identity, and checks whether the
// associated API key (if present) is of a graph-level actor; if it is, we overwrite
// the env key with it because we know it's associated with the target graph_ref
let api_key =
if let Some(api_key) = remote_config.as_ref().and_then(|c| c.api_key().clone()) {
api_key
} else {
self.credential.api_key.clone()
};

let mut env = HashMap::from_iter([
("APOLLO_ROVER".to_string(), "true".to_string()),
("APOLLO_KEY".to_string(), api_key),
]);

if let Some(graph_ref) = remote_config.as_ref().map(|c| c.graph_ref().to_string()) {
env.insert("APOLLO_GRAPH_REF".to_string(), graph_ref);
}
if let Some(api_key) = remote_config.and_then(|c| c.api_key().clone()) {
env.insert("APOLLO_KEY".to_string(), api_key);
}

let child = spawn
.ready()
.and_then(|spawn| {
Expand Down
2 changes: 2 additions & 0 deletions src/command/dev/next/router/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl RunRouter<state::Run> {
temp_router_dir: &Utf8Path,
studio_client_config: StudioClientConfig,
supergraph_schema: &str,
credential: Credential,
) -> Result<RunRouter<state::Watch>, RunRouterBinaryError>
where
Spawn: Service<ExecCommandConfig, Response = Child> + Send + Clone + 'static,
Expand Down Expand Up @@ -195,6 +196,7 @@ impl RunRouter<state::Run> {
.config_path(hot_reload_config_path.clone())
.supergraph_schema_path(hot_reload_schema_path.clone())
.and_remote_config(self.state.remote_config.clone())
.credential(credential)
.spawn(spawn)
.build();

Expand Down

0 comments on commit 198be1e

Please sign in to comment.