Python
#!/usr/bin/python3
"""Isolated Azure CLI contract fixture. Never makes a network request."""
import json
import os
from pathlib import Path
import sys
import time
args = sys.argv[1:]
home = Path(os.environ["HOME"])
with (home / "az.log").open("a") as f:
f.write(json.dumps(args) + "\n")
scenario = os.environ.get("OW_AZ_TEST_SCENARIO", "ready")
bundle = Path(os.environ["OW_AZ_TEST_BUNDLE"])
ident = json.loads((bundle / "azure-identities.json").read_text())
target = ident["target"]
scope = ident["resourceGroupScope"]
def value(name):
return args[args.index(name) + 1] if name in args else None
def emit(v):
print(v if value("-o") == "tsv" else json.dumps(v))
sys.exit(0)
if scenario == "timeout":
time.sleep(5)
if scenario == "invalid-json":
print("not-json-sensitive-canary")
sys.exit(0)
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",
},
}
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")
emit(c["name"] if query == "name" else c["endpoints"]["resourceManager"] if query else c)
if value("--subscription") != target["subscription"]:
raise RuntimeError("Wrong explicit subscription")
if args[:2] == ["account", "show"]:
a = {
"id": target["subscription"],
"tenantId": target["tenant"],
"environmentName": "AzureCloud",
"state": "Enabled",
}
if scenario == "wrong-tenant":
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"
emit(g["id"] if value("--query") else g)
if args[:3] == ["role", "definition", "list"]:
assert value("--scope") == scope and value("--name") == ident["roleDefinitionGuid"]
emit(1 if scenario == "existing-role" else 0)
if args[:3] == ["role", "assignment", "list"]:
if "--scope" in args and "--all" in args:
print("ERROR: group or scope are not required when --all is used", file=sys.stderr)
sys.exit(2)
assert value("--scope") == scope
if scenario == "assignment-read-fails":
print("AuthorizationFailed", file=sys.stderr)
sys.exit(1)
emit(0)
if args[:2] == ["provider", "show"]:
provider = value("--namespace")
emit({"namespace": provider, "registrationState": "NotRegistered" if scenario == "missing-provider" else "Registered"})
def sku(size):
family = "StandardDasv7Family" if size in ["Standard_D4as_v7", "Standard_D2as_v7"] else "standardDSv5Family"
caps = {"CpuArchitectureType":"x64", "vCPUs":"8" if size == "Standard_D8s_v5" else "2" if size == "Standard_D2as_v7" else "4", "MemoryGB":"16", "PremiumIO":"True"}
if scenario == "arm-sku": caps["CpuArchitectureType"] = "Arm64"
return {"name":size,"family":family,"resourceType":"virtualMachines","locations":[target["location"]],"capabilities":[{"name":k,"value":v} for k,v in caps.items()],"restrictions":[{"type":"Location","reasonCode":"NotAvailableForSubscription"}] if scenario == "restricted-sku" else []}
if args[:2] == ["vm", "list-usage"]:
assert value("--location") == target["location"]
emit([{"name":{"value":name},"currentValue":"4" if scenario == "quota-used" else "0","limit":"0" if scenario == "quota-zero" else "12" if name == "cores" else "10"} for name in ["cores","StandardDasv7Family","standardDSv5Family"]])
if args[0] == "rest":
url = value("--url")
if "/Microsoft.Compute/skus?" in url:
from urllib.parse import urlparse, parse_qs
assert value("--method") == "get"
assert parse_qs(urlparse(url).query)["$filter"] == ["location eq '" + target["location"] + "'"]
query=value("--query")
names=[name for name in ["Standard_D4as_v7","Standard_D2as_v7","Standard_D4s_v5","Standard_D8s_v5"] if "name=='"+name+"'" in query]
emit({"skus":[sku(name) for name in names],"nextLink":"https://untrusted.invalid/page" if scenario == "sku-pagination" else None})
assert url in [ident["roleUrl"], ident["assignmentUrl"]], "Unknown URL"
is_role = url == ident["roleUrl"]
payload = json.loads((bundle / ("azure-setup-role.json" if is_role else "azure-setup-assignment.json")).read_text())
payload["name"] = ident["roleDefinitionGuid"] if is_role else ident["roleAssignmentGuid"]
payload["id"] = ident["roleDefinitionId"] if is_role else scope + "/providers/Microsoft.Authorization/roleAssignments/" + ident["roleAssignmentGuid"]
if not is_role:
payload["properties"]["scope"] = scope
method = value("--method")
if method == "put":
if scenario == "assignment-fails" and not is_role:
print("AuthorizationFailed secret-error-canary", file=sys.stderr)
sys.exit(1)
emit(payload)
assert method == "get", "Unexpected mutation"
if scenario == "missing-role" and is_role:
print("ResourceNotFound sensitive-error-canary", file=sys.stderr)
sys.exit(1)
if scenario == "broad-role" and is_role:
payload["properties"]["permissions"][0]["actions"] = ["*"]
if scenario == "wrong-principal" and not is_role:
payload["properties"]["principalId"] = "22222222-2222-2222-2222-222222222222"
if scenario == "conditional-assignment" and not is_role:
payload["properties"]["condition"] = "unreviewed-condition"
emit(payload)
raise RuntimeError("Non-allowlisted fake Azure command: " + json.dumps(args))