-
Notifications
You must be signed in to change notification settings - Fork 665
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
Conversation
a2c0394
to
ad926bf
Compare
There was a problem hiding this 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.
ad926bf
to
9adb205
Compare
shouldn't have pushed in last minute fixes... 😅 |
#[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), | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.
cargo_compat_status_code_config: var_parsed("CARGO_COMPAT_STATUS_CODES")? | ||
.unwrap_or(StatusCodeConfig::AdjustAll), |
There was a problem hiding this comment.
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)
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 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: