fix(supabase): R5.5 gate round 1, final errors, four attempts, keys off argv, runbook
This commit is contained in:
1 parent
a481af52c7
commit
56593af25c
10 files changed
+330
-17
No files matched your search
@@ -107,8 +107,17 @@ fn required_env(name: &str) -> Result<String, UploadError> {
|
||||
.ok_or_else(|| refuse(UploadKind::Destination, format!("{name} is required")))
|
||||
}
|
||||
|
||||
/// The DEV address counts only if it is one of this machine's own addresses (a socket can be
|
||||
/// bound to it); otherwise it is ignored and only loopback stays allowed.
|
||||
fn own_address(host: &str) -> bool {
|
||||
host.parse::<std::net::IpAddr>()
|
||||
.is_ok_and(|ip| std::net::UdpSocket::bind((ip, 0)).is_ok())
|
||||
}
|
||||
|
||||
fn connect() -> Result<Storage, UploadError> {
|
||||
let dev_host = env::var("FAUNOSTEO_DEV_HOST").ok();
|
||||
let dev_host = env::var("FAUNOSTEO_DEV_HOST")
|
||||
.ok()
|
||||
.filter(|h| own_address(h));
|
||||
let destination = check_destination(&required_env("SUPABASE_URL")?, dev_host.as_deref())?;
|
||||
let key = Secret::new(required_env("SUPABASE_SERVICE_ROLE_KEY")?);
|
||||
Ok(Storage::new(&destination, key))
|
||||
|
||||
@@ -13,7 +13,8 @@ use serde_json::{Value, json};
|
||||
use ureq::Agent;
|
||||
|
||||
const MAX_BODY: u64 = 1024 * 1024;
|
||||
const ATTEMPTS: u32 = 3;
|
||||
/// One try and three retries.
|
||||
const ATTEMPTS: u32 = 4;
|
||||
|
||||
/// A key that never prints: `Debug` hides it and there is no `Display`.
|
||||
pub struct Secret(String);
|
||||
@@ -61,8 +62,8 @@ impl Storage {
|
||||
}
|
||||
}
|
||||
|
||||
/// One exchange, retried on a transport error or a 5xx only (a refusal is final). The
|
||||
/// request is built again on each attempt.
|
||||
/// One exchange, retried on a transport error or a 5xx only (a refusal, an oversized body
|
||||
/// and a malformed request are final). The request is built again on each attempt.
|
||||
fn exchange(
|
||||
&self,
|
||||
limit: u64,
|
||||
@@ -77,7 +78,10 @@ impl Storage {
|
||||
{
|
||||
Ok(reply) if reply.status < 500 => return Ok(reply),
|
||||
Ok(reply) => last = refuse(UploadKind::Network, format!("HTTP {}", reply.status)),
|
||||
Err(error) => last = refuse(UploadKind::Network, error.to_string()),
|
||||
Err(error) if is_transient(&error) => {
|
||||
last = refuse(UploadKind::Network, error.to_string());
|
||||
}
|
||||
Err(error) => return Err(final_error(&error)),
|
||||
}
|
||||
}
|
||||
Err(last)
|
||||
@@ -181,6 +185,34 @@ impl Storage {
|
||||
}
|
||||
}
|
||||
|
||||
/// The connection could not be made or broke: worth another attempt.
|
||||
fn is_transient(error: &ureq::Error) -> bool {
|
||||
matches!(
|
||||
error,
|
||||
ureq::Error::Io(_)
|
||||
| ureq::Error::Timeout(_)
|
||||
| ureq::Error::HostNotFound
|
||||
| ureq::Error::ConnectionFailed
|
||||
| ureq::Error::Protocol(_)
|
||||
)
|
||||
}
|
||||
|
||||
/// Not retried: a body over its cap is a destination holding something it should not (an
|
||||
/// object larger than any drawing), anything else a request the tool cannot build (a key with
|
||||
/// characters a header refuses). The key itself is never part of the message.
|
||||
fn final_error(error: &ureq::Error) -> UploadError {
|
||||
match error {
|
||||
ureq::Error::BodyExceedsLimit(limit) => refuse(
|
||||
UploadKind::State,
|
||||
format!("the destination answered more than {limit} bytes"),
|
||||
),
|
||||
_ => refuse(
|
||||
UploadKind::Handshake,
|
||||
"the request could not be built (check the key)",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn row_state_of(row: &Value, expected_sha256: &str) -> RowState {
|
||||
let found = row
|
||||
.get("sha256")
|
||||
|
||||
@@ -314,3 +314,41 @@ fn a_usage_error_names_the_usage() {
|
||||
assert_eq!(output.status.code(), Some(1));
|
||||
assert!(text(&output).contains("usage"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_oversized_object_is_a_final_state_refusal_not_retried() {
|
||||
let (state, url) = stub();
|
||||
let huge = vec![b'x'; 16 * 1024 * 1024 + 2];
|
||||
state
|
||||
.lock()
|
||||
.unwrap()
|
||||
.objects
|
||||
.insert(format!("{SHA}.svg"), huge);
|
||||
let output = run(&url, &[DRAFT]);
|
||||
let said = text(&output);
|
||||
assert_eq!(output.status.code(), Some(1), "{said}");
|
||||
assert!(said.contains("State"), "{said}");
|
||||
let log = state.lock().unwrap().log.clone();
|
||||
assert_eq!(
|
||||
log.iter()
|
||||
.filter(|l| l.contains("/storage/v1/object/"))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert_eq!(writes(&state), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dev_address_that_is_not_this_machine_is_ignored() {
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_upload-sheet-assets"))
|
||||
.args(["--check", DRAFT])
|
||||
.env_clear()
|
||||
.env("SUPABASE_URL", "http://10.255.255.254:54321")
|
||||
.env("FAUNOSTEO_DEV_HOST", "10.255.255.254")
|
||||
.env("SUPABASE_SERVICE_ROLE_KEY", KEY)
|
||||
.output()
|
||||
.unwrap();
|
||||
let said = text(&output);
|
||||
assert_eq!(output.status.code(), Some(1), "{said}");
|
||||
assert!(said.contains("Destination"), "{said}");
|
||||
}
|
||||
Reference in new issue
Block a user