Skip to content

Implement cargo compatibility status code enforcement feature flag #7776

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

Merged
merged 3 commits into from
Dec 21, 2023

Conversation

Turbo87
Copy link
Member

@Turbo87 Turbo87 commented Dec 20, 2023

In #7715 we implemented a middleware that ensured 200 OK response status codes for all API endpoints that are relevant for cargo (see https://doc.rust-lang.org/cargo/reference/registry-web-api.html).

The PR also mentioned:

This will allow us to remove cargo_err() and use proper status codes internally. This is particularly useful for helper functions that are used by both cargo- and non-cargo endpoints. It also makes it easier to phase out the 200 OK requirement since the middleware could easily be disabled by a feature flag in the future.

This PR implements such a feature flag, based on the value of a CARGO_COMPAT_STATUS_CODES environment variable. If that env var is set to "n" it will disable this part of the middleware and return the real status codes instead.

Also, our integration test suite is using this behavior by default now. The middleware has dedicated unit tests which already verify that the middleware works as expected. In the integration tests we can test what would happen if we toggled the status code enforcement off eventually.

Related:

@Turbo87 Turbo87 added C-internal 🔧 Category: Nonessential work that would make the codebase more consistent or clear A-backend ⚙️ labels Dec 20, 2023
@Turbo87 Turbo87 requested a review from a team December 20, 2023 14:44
Copy link
Contributor

@LawnGnome LawnGnome left a comment

Choose a reason for hiding this comment

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

LGTM other than the test failure. 🙈

The middleware has its own unit tests to verify its functionality. Here, we can test what would happen if we toggled the status code enforcement off eventually.
@Turbo87
Copy link
Member Author

Turbo87 commented Dec 21, 2023

shouldn't have pushed in last minute fixes... 😅

Comment on lines +8 to +34
#[derive(Clone, Copy, Debug)]
pub enum StatusCodeConfig {
/// Use the original response status code that the backend returned.
Disabled,
/// Use `200 OK` for all responses to cargo-relevant endpoints.
AdjustAll,
/// Use `200 OK` for all `2xx` responses to cargo-relevant endpoints, and
/// the original status code for all other responses.
AdjustSuccess,
}

#[derive(Debug, thiserror::Error)]
#[error("Failed to parse StatusCodeConfig")]
pub struct StatusCodeConfigError;

impl FromStr for StatusCodeConfig {
type Err = StatusCodeConfigError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"disabled" => Ok(Self::Disabled),
"adjust-all" => Ok(Self::AdjustAll),
"adjust-success" => Ok(Self::AdjustSuccess),
_ => Err(StatusCodeConfigError),
}
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I've changed the implementation from a simple bool to an enum. This allows us to use the original status codes in tests, 200 everywhere in production, and e.g. 200 for 2xx on staging or eventually on production too.

Comment on lines +229 to +230
cargo_compat_status_code_config: var_parsed("CARGO_COMPAT_STATUS_CODES")?
.unwrap_or(StatusCodeConfig::AdjustAll),
Copy link
Member Author

Choose a reason for hiding this comment

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

if no value is set it will default to AdjustAll (i.e. convert everything to 200 OK)

@Turbo87 Turbo87 merged commit 2cdaf38 into rust-lang:main Dec 21, 2023
@Turbo87 Turbo87 deleted the cargo-compat branch December 21, 2023 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-backend ⚙️ C-internal 🔧 Category: Nonessential work that would make the codebase more consistent or clear
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants