| 76 | } |
| 77 | |
| 78 | fn route_request( |
| 79 | ctx: &MetadataContext, |
| 80 | method: &str, |
| 81 | path: &str, |
| 82 | headers: &[(String, String)], |
| 83 | ) -> MetadataResponse { |
| 84 | if method != "GET" { |
| 85 | emit_metadata_event( |
| 86 | ActivityId::Refuse, |
| 87 | SeverityId::Low, |
| 88 | StatusId::Failure, |
| 89 | &format!("metadata: unsupported method {method}"), |
| 90 | ); |
| 91 | return (405, "text/html", "Method Not Allowed".to_string()); |
| 92 | } |
| 93 | |
| 94 | if let Err(resp) = validate_metadata_headers(headers) { |
| 95 | emit_metadata_event( |
| 96 | ActivityId::Refuse, |
| 97 | SeverityId::Medium, |
| 98 | StatusId::Failure, |
| 99 | &format!("metadata: header validation failed for {path}"), |
| 100 | ); |
| 101 | return resp; |
| 102 | } |
| 103 | |
| 104 | let (route, query) = path.split_once('?').map_or((path, ""), |(r, q)| (r, q)); |
| 105 | let route = route.strip_suffix('/').unwrap_or(route); |
| 106 | let recursive = query.split('&').any(|p| p == "recursive=true"); |
| 107 | |
| 108 | match route { |
| 109 | PATH_TOKEN => handle_token(ctx), |
| 110 | PATH_EMAIL => handle_env(ctx, ENV_GCP_SERVICE_ACCOUNT_EMAIL), |
| 111 | PATH_PROJECT_ID => handle_env(ctx, ENV_GCP_PROJECT_ID), |
| 112 | PATH_ALIASES => (200, "text/plain", "default\n".to_string()), |
| 113 | PATH_SCOPES => ( |
| 114 | 200, |
| 115 | "text/plain", |
| 116 | "https://www.googleapis.com/auth/cloud-platform".to_string(), |
| 117 | ), |
| 118 | PATH_SERVICE_ACCOUNT_DEFAULT => { |
| 119 | if recursive { |
| 120 | handle_service_account_recursive(ctx) |
| 121 | } else { |
| 122 | ( |
| 123 | 200, |
| 124 | "text/plain", |
| 125 | "aliases\nemail\nscopes\ntoken\n".to_string(), |
| 126 | ) |
| 127 | } |
| 128 | } |
| 129 | PATH_SERVICE_ACCOUNTS => (200, "text/plain", "default/\n".to_string()), |
| 130 | "" | "/" | "/computeMetadata" | "/computeMetadata/v1" => { |
| 131 | (200, "text/plain", "computeMetadata/\n".to_string()) |
| 132 | } |
| 133 | "/computeMetadata/v1/instance" => (200, "text/plain", "service-accounts/\n".to_string()), |
| 134 | _ => { |
| 135 | emit_metadata_event( |