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

relay.span-normalization.allowed_hosts global options failed to deserialized #3936

Closed
aldy505 opened this issue Aug 16, 2024 · 1 comment · Fixed by #3939
Closed

relay.span-normalization.allowed_hosts global options failed to deserialized #3936

aldy505 opened this issue Aug 16, 2024 · 1 comment · Fixed by #3939
Assignees

Comments

@aldy505
Copy link
Contributor

aldy505 commented Aug 16, 2024

I implemented this a while back: #3813

On 24.8.0 (self-hosted), it turns out String or &str can't be properly deserialized into Host type. There's this upstream issue on the url crate: servo/rust-url#543 and servo/rust-url#895.

The solution is quite simple, we'll just have to override the deserialize method for Host specifically. We'll just need to implement a custom deserializer function and change this specific line:

deserialize_with = "default_on_error",

I made a working repro:

Cargo.toml

[package]
name = "rust-host-vec"
version = "0.1.0"
edition = "2021"

[dependencies]
serde_json = "1"
serde = { version = "1.0.159", features = ["derive", "rc"] }
serde-transcode = "1.1.1"
url = { version = "2", features = ["serde"] }

src/main.rs

use std::fmt;

use serde::{de, Deserialize};
use url::Host;

#[derive(Default, Clone, Debug, Deserialize, PartialEq)]
#[serde(default)]
pub struct GlobalConfig {
    #[serde(
        rename = "relay.span-normalization.allowed_hosts",
        deserialize_with = "deserialize_host",
        skip_serializing_if = "Vec::is_empty"
    )]
    pub http_span_allowed_hosts: Vec<Host>,
}

fn deserialize_host<'de, D>(deserializer: D) -> Result<Vec<Host>, D::Error>
where
    D: serde::de::Deserializer<'de>,
{
    // Stolen from https://users.rust-lang.org/t/need-help-with-serde-deserialize-with/18374
    struct HostVisitor;

    impl<'de> de::Visitor<'de> for HostVisitor {
        type Value = Vec<Host>;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("a string containing host data")
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: de::SeqAccess<'de>,
        {
            let mut values = Vec::<Host>::new();
            while let Some(value) = seq.next_element()? {
                let this = Host::parse(value);
                let op = de::Error::custom;
                match this {
                    Ok(t) => values.push(t),
                    Err(e) => return Err(op(e)),
                }
            }

            Ok(values)
        }
    }

    deserializer.deserialize_any(HostVisitor)
}

fn main() {
    let input = r###"{
        "relay.span-normalization.allowed_hosts": [
            "vault.internal.example.com"
        ]
      }"###;

    match serde_json::from_str::<GlobalConfig>(input) {
        Ok(config) => {
            println!("{:?}", config);
        }
        Err(e) => {
            println!("{:?}", e);
        }
    }
}

Might work on this later this weekend, or the next weekend. I can't work on this right now since I got work to do. Also I guess I need to write few tests here and there for deserializing the new global option I made 😒

@aldy505
Copy link
Contributor Author

aldy505 commented Aug 16, 2024

Figured gonna do it this weekend and brace myself to use nightly...

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Aug 16, 2024
@jjbayer jjbayer closed this as completed in 516f45a Sep 2, 2024
jjbayer added a commit that referenced this issue Sep 3, 2024
…spans (#3939)

Closes #3936

### Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated
in the State of Delaware in 2015 as Functional Software, Inc. and is
gonna need some rights from me in order to utilize my contributions in
this here PR. So here's the deal: I retain all rights, title and
interest in and to my contributions, and by keeping this boilerplate
intact I confirm that Sentry can use, modify, copy, and redistribute my
contributions, under Sentry's choice of terms.

---------

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants