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

fix(server): Skip invalid project keys in batch request [INGEST-406] #1093

Merged
merged 6 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
- Report proper status codes and error messages when sending invalid JSON payloads to an endpoint with a `X-Sentry-Relay-Signature` header. ([#1090](https://github.com/getsentry/relay/pull/1090))

**Internal**:

- Add the exclusive time of the transaction's root span. ([#1083](https://github.com/getsentry/relay/pull/1083))
- Add session.status tag to extracted session.duration metric. ([#1087](https://github.com/getsentry/relay/pull/1087))
- Serve project configs for batched requests where one of the project keys cannot be parsed. ([#1093](https://github.com/getsentry/relay/pull/1093))

## 21.9.0

Expand Down
8 changes: 5 additions & 3 deletions relay-server/src/actors/project_upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ mod _macro {
}
}

#[derive(Debug, Deserialize, Serialize)]
/// A query to retrieve a batch of project states from upstream.
///
/// This query does not implement `Deserialize`. To parse the query, use a wrapper that skips
/// invalid project keys instead of failing the entire batch.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetProjectStates {
pub public_keys: Vec<ProjectKey>,
#[serde(default)]
pub full_config: bool,
#[serde(default)]
pub no_cache: bool,
}

Expand Down
23 changes: 20 additions & 3 deletions relay-server/src/endpoints/project_configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use relay_common::ProjectKey;

use crate::actors::project::{LimitedProjectState, ProjectState};
use crate::actors::project_cache::{GetProjectState, ProjectCache};
use crate::actors::project_upstream::GetProjectStates;
use crate::extractors::SignedJson;
use crate::service::ServiceApp;
use crate::utils::ErrorBoundary;

/// Helper to deserialize the `version` query parameter.
#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -58,14 +58,31 @@ struct GetProjectStatesResponseWrapper {
configs: HashMap<ProjectKey, Option<ProjectStateWrapper>>,
}

/// Request payload of the project config endpoint.
///
/// This is a replica of [`GetProjectStates`](crate::actors::project_upstream::GetProjectStates)
/// which allows skipping invalid project keys.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct GetProjectStatesRequest {
public_keys: Vec<ErrorBoundary<ProjectKey>>,
#[serde(default)]
full_config: bool,
#[serde(default)]
no_cache: bool,
}

fn get_project_configs(
body: SignedJson<GetProjectStates>,
body: SignedJson<GetProjectStatesRequest>,
) -> ResponseFuture<Json<GetProjectStatesResponseWrapper>, Error> {
let relay = body.relay;
let full = relay.internal && body.inner.full_config;
let no_cache = body.inner.no_cache;

let futures = body.inner.public_keys.into_iter().map(move |project_key| {
// Skip unparsable public keys. The downstream Relay will consider them `ProjectState::missing`.
let valid_keys = body.inner.public_keys.into_iter().filter_map(|e| e.ok());

let futures = valid_keys.map(move |project_key| {
let relay = relay.clone();
ProjectCache::from_registry()
.send(GetProjectState::new(project_key).no_cache(no_cache))
Expand Down
8 changes: 8 additions & 0 deletions relay-server/src/utils/error_boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ impl<T> ErrorBoundary<T> {
!self.is_ok()
}

#[inline]
pub fn ok(self) -> Option<T> {
match self {
ErrorBoundary::Err(_) => None,
ErrorBoundary::Ok(value) => Some(value),
}
}

#[inline]
pub fn unwrap_or_else<F>(self, op: F) -> T
where
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/test_projectconfigs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests the project_configs endpoint (/api/0/relays/projectconfigs/)
"""

import json
import uuid
import pytest
from collections import namedtuple
Expand Down Expand Up @@ -128,3 +129,24 @@ def test_invalid_signature(mini_sentry, relay):

assert response.status_code == 401 # Unauthorized
assert "signature" in response.text


def test_broken_projectkey(mini_sentry, relay):
relay = relay(mini_sentry, wait_healthcheck=True)
mini_sentry.add_basic_project_config(42)
public_key = mini_sentry.get_dsn_public_key(42)

body = json.dumps({"public_keys": ["broken", public_key]})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should parametrize over all values that we discussed in FMEA

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. Added everything except:

  • encoding, which we ruled out as a non issue
  • large strings, as this is handled by store body as an independent concern

packed, signature = SecretKey.parse(relay.secret_key).pack(body)

response = relay.post(
"/api/0/relays/projectconfigs/?version=2",
data=packed,
headers={
"X-Sentry-Relay-Id": relay.relay_id,
"X-Sentry-Relay-Signature": signature,
},
)

assert response.ok
assert public_key in response.json()["configs"]