mirror of
https://github.com/andrewyng/openworker.git
synced 2026-11-20 20:37:44 +00:00
fix: handle Azure CLI cloud preflight arguments and endpoints
This commit is contained in:
@@ -51,15 +51,13 @@ impl Az {
|
||||
return Err("timeout");
|
||||
}
|
||||
let mut cmd = Command::new("az");
|
||||
cmd.args(command)
|
||||
.args(args)
|
||||
.args([
|
||||
"--subscription",
|
||||
&self.subscription,
|
||||
"--only-show-errors",
|
||||
"--output",
|
||||
"json",
|
||||
])
|
||||
cmd.args(command).args(args);
|
||||
// `az cloud show` reads CLI-wide cloud configuration and rejects
|
||||
// --subscription. Every resource-scoped read must still pin the target.
|
||||
if command != ["cloud", "show"] {
|
||||
cmd.args(["--subscription", &self.subscription]);
|
||||
}
|
||||
cmd.args(["--only-show-errors", "--output", "json"])
|
||||
.env("AZURE_CORE_ONLY_SHOW_ERRORS", "true")
|
||||
.env("AZURE_CORE_COLLECT_TELEMETRY", "false")
|
||||
.env("AZURE_EXTENSION_USE_DYNAMIC_INSTALL", "no")
|
||||
@@ -106,6 +104,7 @@ impl Az {
|
||||
if message.contains("AuthorizationFailed") || message.contains("Forbidden") {
|
||||
"access_denied"
|
||||
} else if message.contains("ResourceNotFound")
|
||||
|| message.contains("ResourceGroupNotFound")
|
||||
|| message.contains("RoleDefinitionDoesNotExist")
|
||||
{
|
||||
"not_found"
|
||||
@@ -249,7 +248,12 @@ pub async fn preflight(o: &Preflight) -> Result<()> {
|
||||
api.read(&["cloud", "show"], &[]).await.and_then(|v| {
|
||||
if v["name"] == "AzureCloud"
|
||||
&& v["endpoints"]["resourceManager"] == "https://management.azure.com/"
|
||||
&& v["endpoints"]["activeDirectory"] == "https://login.microsoftonline.com/"
|
||||
&& matches!(
|
||||
v["endpoints"]["activeDirectory"].as_str(),
|
||||
Some(
|
||||
"https://login.microsoftonline.com" | "https://login.microsoftonline.com/"
|
||||
)
|
||||
)
|
||||
{
|
||||
Ok(json!({"cloud":"AzureCloud","publicManagementEndpoint":true}))
|
||||
} else {
|
||||
|
||||
@@ -91,7 +91,9 @@ impl Fixture {
|
||||
"--out",
|
||||
self.path(out).to_str().unwrap(),
|
||||
"--timeout-seconds",
|
||||
"1",
|
||||
// Keep the deliberate timeout case short; ordinary fake CLI
|
||||
// startups need headroom when the full suite runs in parallel.
|
||||
if scenario == "timeout" { "1" } else { "5" },
|
||||
"--overall-timeout-seconds",
|
||||
"20",
|
||||
])
|
||||
@@ -222,9 +224,54 @@ fn read_only_preflight_verifies_exact_grant_without_claiming_deployment_readines
|
||||
assert_eq!(f.log().len(), 13);
|
||||
}
|
||||
#[test]
|
||||
fn cloud_read_omits_subscription_but_all_resource_reads_pin_the_target() {
|
||||
let f = Fixture::ready();
|
||||
let r = f.preflight("ready", "argument-contract");
|
||||
assert!(
|
||||
r.status.success(),
|
||||
"{} {}",
|
||||
String::from_utf8_lossy(&r.stderr),
|
||||
String::from_utf8_lossy(&r.stdout)
|
||||
);
|
||||
let calls = f.log();
|
||||
assert_eq!(calls.len(), 13);
|
||||
assert_eq!(&calls[0][..2], ["cloud", "show"]);
|
||||
assert!(!calls[0].iter().any(|a| a == "--subscription"));
|
||||
for call in &calls[1..] {
|
||||
assert_eq!(call.iter().filter(|a| *a == "--subscription").count(), 1);
|
||||
assert!(call
|
||||
.windows(2)
|
||||
.any(|w| w == ["--subscription", "66666666-7777-4888-8999-aaaaaaaaaaaa"]));
|
||||
assert!(call[0] != "rest" || call.windows(2).any(|w| w == ["--method", "get"]));
|
||||
}
|
||||
assert_eq!(f.report("argument-contract")["mutations"], 0);
|
||||
}
|
||||
#[test]
|
||||
fn missing_resource_group_is_reported_without_querying_or_changing_grants() {
|
||||
let f = Fixture::ready();
|
||||
let r = f.preflight("missing-group", "missing-group-report");
|
||||
assert!(!r.status.success());
|
||||
let report = f.report("missing-group-report");
|
||||
assert_eq!(report["checks"][0]["status"], "ok");
|
||||
assert_eq!(report["checks"][1]["status"], "ok");
|
||||
assert_eq!(report["checks"][2]["code"], "not_found");
|
||||
assert_eq!(report["mutations"], 0);
|
||||
assert_eq!(f.log().len(), 3);
|
||||
assert!(!report.to_string().contains("sensitive-error-canary"));
|
||||
}
|
||||
#[test]
|
||||
fn public_auth_endpoint_with_trailing_slash_remains_supported() {
|
||||
let f = Fixture::ready();
|
||||
let r = f.preflight("auth-trailing-slash", "trailing-slash");
|
||||
assert!(r.status.success(), "{}", String::from_utf8_lossy(&r.stderr));
|
||||
assert_eq!(f.report("trailing-slash")["checks"][0]["status"], "ok");
|
||||
}
|
||||
#[test]
|
||||
fn wrong_cloud_scope_principal_or_role_fails_closed_and_errors_are_redacted() {
|
||||
for scenario in [
|
||||
"custom-endpoint",
|
||||
"custom-auth-endpoint",
|
||||
"auth-endpoint-path",
|
||||
"wrong-tenant",
|
||||
"wrong-group",
|
||||
"broad-role",
|
||||
|
||||
+14
-1
@@ -35,13 +35,23 @@ if scenario == "too-large":
|
||||
print("x" * (1024 * 1024 + 20))
|
||||
sys.exit(0)
|
||||
if args[:2] == ["cloud", "show"]:
|
||||
# Match the real Azure CLI: cloud configuration is not subscription-scoped.
|
||||
if "--subscription" in args:
|
||||
print("ERROR: unrecognized arguments: --subscription", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
c = {
|
||||
"name": "AzureCloud",
|
||||
"endpoints": {
|
||||
"resourceManager": "https://management.azure.com/",
|
||||
"activeDirectory": "https://login.microsoftonline.com/",
|
||||
"activeDirectory": "https://login.microsoftonline.com",
|
||||
},
|
||||
}
|
||||
if scenario == "auth-trailing-slash":
|
||||
c["endpoints"]["activeDirectory"] += "/"
|
||||
if scenario == "custom-auth-endpoint":
|
||||
c["endpoints"]["activeDirectory"] = "https://login.microsoftonline.com.example.invalid/"
|
||||
if scenario == "auth-endpoint-path":
|
||||
c["endpoints"]["activeDirectory"] += "/untrusted"
|
||||
if scenario == "custom-endpoint":
|
||||
c["endpoints"]["resourceManager"] = "https://example.invalid/"
|
||||
query = value("--query")
|
||||
@@ -59,6 +69,9 @@ if args[:2] == ["account", "show"]:
|
||||
a["tenantId"] = "11111111-1111-1111-1111-111111111111"
|
||||
emit(a.get(value("--query")) if value("--query") else a)
|
||||
if args[:2] == ["group", "show"]:
|
||||
if scenario == "missing-group":
|
||||
print("ResourceGroupNotFound sensitive-error-canary", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
g = {"id": scope, "properties": {"provisioningState": "Succeeded"}}
|
||||
if scenario == "wrong-group":
|
||||
g["id"] = scope + "-other"
|
||||
|
||||
Reference in New Issue
Block a user