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

Add better context config for coprocessor to help migration #6752

Merged
merged 17 commits into from
Feb 12, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,19 @@ expression: "&schema"
},
"type": "object"
},
"ContextConf": {
"anyOf": [
{
"description": "Deprecated configuration using a boolean",
"type": "boolean"
},
{
"$ref": "#/definitions/NewContextConf",
"description": "#/definitions/NewContextConf"
}
],
"description": "Configures the context"
},
"Cors": {
"additionalProperties": false,
"description": "Cross origin request configuration.",
Expand Down Expand Up @@ -2873,9 +2886,8 @@ expression: "&schema"
"type": "boolean"
},
"context": {
"default": false,
"description": "Send the context",
"type": "boolean"
"$ref": "#/definitions/ContextConf",
"description": "#/definitions/ContextConf"
},
"headers": {
"default": false,
Expand Down Expand Up @@ -2910,9 +2922,8 @@ expression: "&schema"
"type": "boolean"
},
"context": {
"default": false,
"description": "Send the context",
"type": "boolean"
"$ref": "#/definitions/ContextConf",
"description": "#/definitions/ContextConf"
},
"headers": {
"default": false,
Expand Down Expand Up @@ -4259,6 +4270,42 @@ expression: "&schema"
"description": "Config for the test plugin",
"type": "object"
},
"NewContextConf": {
"description": "Configures the context",
"oneOf": [
{
"description": "Send all context keys to coprocessor",
"enum": [
"all"
],
"type": "string"
},
{
"description": "Send all context keys using deprecated names (from router 1.x) to coprocessor",
"enum": [
"deprecated"
],
"type": "string"
},
{
"additionalProperties": false,
"description": "Only send the list of context keys to coprocessor",
"properties": {
"selective": {
"items": {
"type": "string"
},
"type": "array",
"uniqueItems": true
}
},
"required": [
"selective"
],
"type": "object"
}
]
},
"Operation": {
"oneOf": [
{
Expand Down Expand Up @@ -5149,9 +5196,8 @@ expression: "&schema"
"nullable": true
},
"context": {
"default": false,
"description": "Send the context",
"type": "boolean"
"$ref": "#/definitions/ContextConf",
"description": "#/definitions/ContextConf"
},
"headers": {
"default": false,
Expand Down Expand Up @@ -5191,9 +5237,8 @@ expression: "&schema"
"nullable": true
},
"context": {
"default": false,
"description": "Send the context",
"type": "boolean"
"$ref": "#/definitions/ContextConf",
"description": "#/definitions/ContextConf"
},
"headers": {
"default": false,
Expand Down Expand Up @@ -6239,9 +6284,8 @@ expression: "&schema"
"nullable": true
},
"context": {
"default": false,
"description": "Send the context",
"type": "boolean"
"$ref": "#/definitions/ContextConf",
"description": "#/definitions/ContextConf"
},
"headers": {
"default": false,
Expand Down Expand Up @@ -6286,9 +6330,8 @@ expression: "&schema"
"nullable": true
},
"context": {
"default": false,
"description": "Send the context",
"type": "boolean"
"$ref": "#/definitions/ContextConf",
"description": "#/definitions/ContextConf"
},
"headers": {
"default": false,
Expand Down Expand Up @@ -6994,9 +7037,8 @@ expression: "&schema"
"nullable": true
},
"context": {
"default": false,
"description": "Send the context",
"type": "boolean"
"$ref": "#/definitions/ContextConf",
"description": "#/definitions/ContextConf"
},
"headers": {
"default": false,
Expand Down Expand Up @@ -7031,9 +7073,8 @@ expression: "&schema"
"nullable": true
},
"context": {
"default": false,
"description": "Send the context",
"type": "boolean"
"$ref": "#/definitions/ContextConf",
"description": "#/definitions/ContextConf"
},
"headers": {
"default": false,
Expand Down
119 changes: 119 additions & 0 deletions apollo-router/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,60 @@ use serde::Serialize;
use tower::BoxError;

use crate::json_ext::Value;
use crate::plugins::authentication::APOLLO_AUTHENTICATION_JWT_CLAIMS;
use crate::plugins::authentication::DEPRECATED_APOLLO_AUTHENTICATION_JWT_CLAIMS;
use crate::plugins::authorization::AUTHENTICATION_REQUIRED_KEY;
use crate::plugins::authorization::DEPRECATED_AUTHENTICATION_REQUIRED_KEY;
use crate::plugins::authorization::DEPRECATED_REQUIRED_POLICIES_KEY;
use crate::plugins::authorization::DEPRECATED_REQUIRED_SCOPES_KEY;
use crate::plugins::authorization::REQUIRED_POLICIES_KEY;
use crate::plugins::authorization::REQUIRED_SCOPES_KEY;
use crate::plugins::demand_control::COST_ACTUAL_KEY;
use crate::plugins::demand_control::COST_ESTIMATED_KEY;
use crate::plugins::demand_control::COST_RESULT_KEY;
use crate::plugins::demand_control::COST_STRATEGY_KEY;
use crate::plugins::demand_control::DEPRECATED_COST_ACTUAL_KEY;
use crate::plugins::demand_control::DEPRECATED_COST_ESTIMATED_KEY;
use crate::plugins::demand_control::DEPRECATED_COST_RESULT_KEY;
use crate::plugins::demand_control::DEPRECATED_COST_STRATEGY_KEY;
use crate::plugins::expose_query_plan::DEPRECATED_ENABLED_CONTEXT_KEY;
use crate::plugins::expose_query_plan::DEPRECATED_FORMATTED_QUERY_PLAN_CONTEXT_KEY;
use crate::plugins::expose_query_plan::DEPRECATED_QUERY_PLAN_CONTEXT_KEY;
use crate::plugins::expose_query_plan::ENABLED_CONTEXT_KEY;
use crate::plugins::expose_query_plan::FORMATTED_QUERY_PLAN_CONTEXT_KEY;
use crate::plugins::expose_query_plan::QUERY_PLAN_CONTEXT_KEY;
use crate::plugins::progressive_override::DEPRECATED_LABELS_TO_OVERRIDE_KEY;
use crate::plugins::progressive_override::DEPRECATED_UNRESOLVED_LABELS_KEY;
use crate::plugins::progressive_override::LABELS_TO_OVERRIDE_KEY;
use crate::plugins::progressive_override::UNRESOLVED_LABELS_KEY;
use crate::plugins::telemetry::CLIENT_NAME;
use crate::plugins::telemetry::CLIENT_VERSION;
use crate::plugins::telemetry::DEPRECATED_CLIENT_NAME;
use crate::plugins::telemetry::DEPRECATED_CLIENT_VERSION;
use crate::plugins::telemetry::DEPRECATED_STUDIO_EXCLUDE;
use crate::plugins::telemetry::DEPRECATED_SUBGRAPH_FTV1;
use crate::plugins::telemetry::STUDIO_EXCLUDE;
use crate::plugins::telemetry::SUBGRAPH_FTV1;
use crate::query_planner::APOLLO_OPERATION_ID;
use crate::query_planner::DEPRECATED_APOLLO_OPERATION_ID;
use crate::services::layers::apq::DEPRECATED_PERSISTED_QUERY_CACHE_HIT;
use crate::services::layers::apq::DEPRECATED_PERSISTED_QUERY_REGISTERED;
use crate::services::layers::apq::PERSISTED_QUERY_CACHE_HIT;
use crate::services::layers::apq::PERSISTED_QUERY_REGISTERED;
use crate::services::layers::query_analysis::ParsedDocument;
use crate::services::DEPRECATED_FIRST_EVENT_CONTEXT_KEY;
use crate::services::FIRST_EVENT_CONTEXT_KEY;

pub(crate) mod extensions;

/// The key of the resolved operation name. This is subject to change and should not be relied on.
pub(crate) const OPERATION_NAME: &str = "apollo::supergraph::operation_name";
/// The deprecated key (1.x) of the resolved operation name. This is subject to change and should not be relied on.
pub(crate) const DEPRECATED_OPERATION_NAME: &str = "operation_name";
/// The key of the resolved operation kind. This is subject to change and should not be relied on.
pub(crate) const OPERATION_KIND: &str = "apollo::supergraph::operation_kind";
/// The deprecated key (1.x) of the resolved operation kind. This is subject to change and should not be relied on.
pub(crate) const DEPRECATED_OPERATION_KIND: &str = "operation_kind";
/// The key to know if the response body contains at least 1 GraphQL error
pub(crate) const CONTAINS_GRAPHQL_ERROR: &str = "apollo::telemetry::contains_graphql_error";

Expand Down Expand Up @@ -77,6 +123,17 @@ impl Context {
}
}

impl FromIterator<(String, Value)> for Context {
fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
Self {
entries: Arc::new(DashMap::from_iter(iter)),
extensions: ExtensionsMutex::default(),
created_at: Instant::now(),
id: String::new(),
}
}
}

impl Context {
/// Returns extensions of the context.
///
Expand Down Expand Up @@ -246,6 +303,68 @@ impl Default for Context {
}
}

/// Convert context key to the deprecated context key (mainly useful for coprocessor/rhai)
/// If the context key is not part of a deprecated one it just returns the original one because it doesn't have to be renamed
pub(crate) fn context_key_to_deprecated(key: String) -> String {
match key.as_str() {
OPERATION_NAME => DEPRECATED_OPERATION_NAME.to_string(),
OPERATION_KIND => DEPRECATED_OPERATION_KIND.to_string(),
APOLLO_AUTHENTICATION_JWT_CLAIMS => DEPRECATED_APOLLO_AUTHENTICATION_JWT_CLAIMS.to_string(),
AUTHENTICATION_REQUIRED_KEY => DEPRECATED_AUTHENTICATION_REQUIRED_KEY.to_string(),
REQUIRED_SCOPES_KEY => DEPRECATED_REQUIRED_SCOPES_KEY.to_string(),
REQUIRED_POLICIES_KEY => DEPRECATED_REQUIRED_POLICIES_KEY.to_string(),
APOLLO_OPERATION_ID => DEPRECATED_APOLLO_OPERATION_ID.to_string(),
UNRESOLVED_LABELS_KEY => DEPRECATED_UNRESOLVED_LABELS_KEY.to_string(),
LABELS_TO_OVERRIDE_KEY => DEPRECATED_LABELS_TO_OVERRIDE_KEY.to_string(),
FIRST_EVENT_CONTEXT_KEY => DEPRECATED_FIRST_EVENT_CONTEXT_KEY.to_string(),
CLIENT_NAME => DEPRECATED_CLIENT_NAME.to_string(),
CLIENT_VERSION => DEPRECATED_CLIENT_VERSION.to_string(),
STUDIO_EXCLUDE => DEPRECATED_STUDIO_EXCLUDE.to_string(),
SUBGRAPH_FTV1 => DEPRECATED_SUBGRAPH_FTV1.to_string(),
COST_ESTIMATED_KEY => DEPRECATED_COST_ESTIMATED_KEY.to_string(),
COST_ACTUAL_KEY => DEPRECATED_COST_ACTUAL_KEY.to_string(),
COST_RESULT_KEY => DEPRECATED_COST_RESULT_KEY.to_string(),
COST_STRATEGY_KEY => DEPRECATED_COST_STRATEGY_KEY.to_string(),
ENABLED_CONTEXT_KEY => DEPRECATED_ENABLED_CONTEXT_KEY.to_string(),
FORMATTED_QUERY_PLAN_CONTEXT_KEY => DEPRECATED_FORMATTED_QUERY_PLAN_CONTEXT_KEY.to_string(),
QUERY_PLAN_CONTEXT_KEY => DEPRECATED_QUERY_PLAN_CONTEXT_KEY.to_string(),
PERSISTED_QUERY_CACHE_HIT => DEPRECATED_PERSISTED_QUERY_CACHE_HIT.to_string(),
PERSISTED_QUERY_REGISTERED => DEPRECATED_PERSISTED_QUERY_REGISTERED.to_string(),
_ => key,
}
}

/// Convert context key from deprecated to new one (mainly useful for coprocessor/rhai)
/// If the context key is not part of a deprecated one it just returns the original one because it doesn't have to be renamed
pub(crate) fn context_key_from_deprecated(key: String) -> String {
match key.as_str() {
DEPRECATED_OPERATION_NAME => OPERATION_NAME.to_string(),
DEPRECATED_OPERATION_KIND => OPERATION_KIND.to_string(),
DEPRECATED_APOLLO_AUTHENTICATION_JWT_CLAIMS => APOLLO_AUTHENTICATION_JWT_CLAIMS.to_string(),
DEPRECATED_AUTHENTICATION_REQUIRED_KEY => AUTHENTICATION_REQUIRED_KEY.to_string(),
DEPRECATED_REQUIRED_SCOPES_KEY => REQUIRED_SCOPES_KEY.to_string(),
DEPRECATED_REQUIRED_POLICIES_KEY => REQUIRED_POLICIES_KEY.to_string(),
DEPRECATED_APOLLO_OPERATION_ID => APOLLO_OPERATION_ID.to_string(),
DEPRECATED_UNRESOLVED_LABELS_KEY => UNRESOLVED_LABELS_KEY.to_string(),
DEPRECATED_LABELS_TO_OVERRIDE_KEY => LABELS_TO_OVERRIDE_KEY.to_string(),
DEPRECATED_FIRST_EVENT_CONTEXT_KEY => FIRST_EVENT_CONTEXT_KEY.to_string(),
DEPRECATED_CLIENT_NAME => CLIENT_NAME.to_string(),
DEPRECATED_CLIENT_VERSION => CLIENT_VERSION.to_string(),
DEPRECATED_STUDIO_EXCLUDE => STUDIO_EXCLUDE.to_string(),
DEPRECATED_SUBGRAPH_FTV1 => SUBGRAPH_FTV1.to_string(),
DEPRECATED_COST_ESTIMATED_KEY => COST_ESTIMATED_KEY.to_string(),
DEPRECATED_COST_ACTUAL_KEY => COST_ACTUAL_KEY.to_string(),
DEPRECATED_COST_RESULT_KEY => COST_RESULT_KEY.to_string(),
DEPRECATED_COST_STRATEGY_KEY => COST_STRATEGY_KEY.to_string(),
DEPRECATED_ENABLED_CONTEXT_KEY => ENABLED_CONTEXT_KEY.to_string(),
DEPRECATED_FORMATTED_QUERY_PLAN_CONTEXT_KEY => FORMATTED_QUERY_PLAN_CONTEXT_KEY.to_string(),
DEPRECATED_QUERY_PLAN_CONTEXT_KEY => QUERY_PLAN_CONTEXT_KEY.to_string(),
DEPRECATED_PERSISTED_QUERY_CACHE_HIT => PERSISTED_QUERY_CACHE_HIT.to_string(),
DEPRECATED_PERSISTED_QUERY_REGISTERED => PERSISTED_QUERY_REGISTERED.to_string(),
_ => key,
}
}

#[cfg(test)]
mod test {
use crate::spec::Query;
Expand Down
2 changes: 2 additions & 0 deletions apollo-router/src/plugins/authentication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ mod tests;

pub(crate) const AUTHENTICATION_SPAN_NAME: &str = "authentication_plugin";
pub(crate) const APOLLO_AUTHENTICATION_JWT_CLAIMS: &str = "apollo::authentication::jwt_claims";
pub(crate) const DEPRECATED_APOLLO_AUTHENTICATION_JWT_CLAIMS: &str =
"apollo_authentication::JWT::claims";
const HEADER_TOKEN_TRUNCATED: &str = "(truncated)";

#[derive(Debug, Display, Error)]
Expand Down
5 changes: 5 additions & 0 deletions apollo-router/src/plugins/authorization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ pub(crate) mod authenticated;
pub(crate) mod policy;
pub(crate) mod scopes;

pub(crate) const DEPRECATED_AUTHENTICATION_REQUIRED_KEY: &str =
"apollo_authorization::authenticated::required";
pub(crate) const AUTHENTICATION_REQUIRED_KEY: &str =
"apollo::authorization::authentication_required";
pub(crate) const REQUIRED_SCOPES_KEY: &str = "apollo::authorization::required_scopes";
pub(crate) const DEPRECATED_REQUIRED_SCOPES_KEY: &str = "apollo_authorization::scopes::required";
pub(crate) const REQUIRED_POLICIES_KEY: &str = "apollo::authorization::required_policies";
pub(crate) const DEPRECATED_REQUIRED_POLICIES_KEY: &str =
"apollo_authorization::policies::required";

#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct CacheKeyMetadata {
Expand Down
Loading