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): Accept sessions with auto ip_address #827

Merged
merged 4 commits into from
Oct 30, 2020
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Rename upstream retries histogram metric and add upstream requests duration metric. ([#816](https://github.com/getsentry/relay/pull/816))
- Add options for metrics buffering (`metrics.buffering`) and sampling (`metrics.sample_rate`). ([#821](https://github.com/getsentry/relay/pull/821))

**Bug Fixes**:

- Accept sessions with IP address set to `{{auto}}`. This was previously rejected and silently dropped. ([#827](https://github.com/getsentry/relay/pull/827))

**Internal**:

- Always apply cache debouncing for project states. This reduces pressure on the Redis and file system cache. ([#819](https://github.com/getsentry/relay/pull/819))
Expand Down
19 changes: 17 additions & 2 deletions relay-general/src/protocol/session.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::net::IpAddr;
use std::time::SystemTime;

use chrono::{DateTime, Utc};
use failure::Fail;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::protocol::IpAddr;

/// The type of session event we're dealing with.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -218,12 +219,26 @@ mod tests {
attributes: SessionAttributes {
release: "sentry-test@1.0.0".to_owned(),
environment: Some("production".to_owned()),
ip_address: Some("::1".parse().unwrap()),
ip_address: Some(IpAddr::parse("::1").unwrap()),
user_agent: Some("Firefox/72.0".to_owned()),
},
};

assert_eq_dbg!(update, SessionUpdate::parse(json.as_bytes()).unwrap());
assert_eq_str!(json, serde_json::to_string_pretty(&update).unwrap());
}

#[test]
fn test_session_ip_addr_auto() {
let json = r#"{
"started": "2020-02-07T14:16:00Z",
"attrs": {
"release": "sentry-test@1.0.0",
"ip_address": "{{auto}}"
}
}"#;

let update = SessionUpdate::parse(json.as_bytes()).unwrap();
assert_eq_dbg!(update.attributes.ip_address, Some(IpAddr::auto()));
}
}
13 changes: 10 additions & 3 deletions relay-server/src/actors/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use relay_config::{Config, HttpEncoding, RelayMode};
use relay_general::pii::{PiiAttachmentsProcessor, PiiProcessor};
use relay_general::processor::{process_value, ProcessingState};
use relay_general::protocol::{
Breadcrumb, Csp, Event, EventId, EventType, ExpectCt, ExpectStaple, Hpkp, LenientString,
Metrics, SecurityReportType, SessionUpdate, Timestamp, UserReport, Values,
Breadcrumb, Csp, Event, EventId, EventType, ExpectCt, ExpectStaple, Hpkp, IpAddr,
LenientString, Metrics, SecurityReportType, SessionUpdate, Timestamp, UserReport, Values,
};
use relay_general::store::ClockDriftProcessor;
use relay_general::types::{Annotated, Array, Object, ProcessingAction, Value};
Expand All @@ -45,7 +45,6 @@ use {
failure::ResultExt,
minidump::Minidump,
relay_filter::FilterStatKey,
relay_general::protocol::IpAddr,
relay_general::store::{GeoIpLookup, StoreConfig, StoreProcessor},
relay_quotas::{DataCategory, RateLimitingError, RedisRateLimiter},
};
Expand Down Expand Up @@ -301,6 +300,7 @@ impl EventProcessor {
fn process_sessions(&self, state: &mut ProcessEnvelopeState) {
let envelope = &mut state.envelope;
let received = state.received_at;
let client_addr = envelope.meta().client_addr();

let clock_drift_processor =
ClockDriftProcessor::new(envelope.sent_at(), received).at_least(MINIMUM_CLOCK_DRIFT);
Expand Down Expand Up @@ -359,6 +359,13 @@ impl EventProcessor {
return false;
}

if let Some(ref ip_address) = session.attributes.ip_address {
if ip_address.is_auto() {
session.attributes.ip_address = client_addr.map(IpAddr::from);
changed = true;
}
}

if changed {
let json_string = match serde_json::to_string(&session) {
Ok(json) => json,
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,27 @@ def test_session_disabled(mini_sentry, relay_with_processing, sessions_consumer)
)

assert sessions_consumer.poll() is None


def test_session_auto_ip(mini_sentry, relay_with_processing, sessions_consumer):
relay = relay_with_processing()
sessions_consumer = sessions_consumer()

project_config = mini_sentry.full_project_config()
project_config["config"]["eventRetention"] = 17
mini_sentry.project_configs[42] = project_config

timestamp = datetime.now(tz=timezone.utc)
relay.send_session(
42,
{
"sid": "8333339f-5675-4f89-a9a0-1c935255ab58",
"timestamp": timestamp.isoformat(),
"started": timestamp.isoformat(),
"attrs": {"release": "sentry-test@1.0.0", "ip_address": "{{auto}}"},
},
)

# Can't test ip_address since it's not posted to Kafka. Just test that it is accepted.
session = sessions_consumer.get_session()
assert session