(
&self,
method: &str,
path: &str,
body: &[u8],
)
| 203 | } |
| 204 | |
| 205 | async fn sign_request( |
| 206 | &self, |
| 207 | method: &str, |
| 208 | path: &str, |
| 209 | body: &[u8], |
| 210 | ) -> Result<reqwest::header::HeaderMap, ProviderError> { |
| 211 | let mut headers = reqwest::header::HeaderMap::new(); |
| 212 | |
| 213 | let now = chrono::Utc::now(); |
| 214 | let amz_date = now.format("%Y%m%dT%H%M%SZ").to_string(); |
| 215 | let date_stamp = now.format("%Y%m%d").to_string(); |
| 216 | |
| 217 | let service = "bedrock"; |
| 218 | let region = &self.config.region; |
| 219 | |
| 220 | let body_hash = hex::encode(sha256(body)); |
| 221 | |
| 222 | headers.insert("Content-Type", "application/json".parse().unwrap()); |
| 223 | headers.insert("X-Amz-Date", amz_date.parse().unwrap()); |
| 224 | headers.insert( |
| 225 | "Host", |
| 226 | format!("bedrock-runtime.{}.amazonaws.com", region) |
| 227 | .parse() |
| 228 | .unwrap(), |
| 229 | ); |
| 230 | |
| 231 | if let Some(ref token) = self.config.session_token { |
| 232 | headers.insert("X-Amz-Security-Token", token.parse().unwrap()); |
| 233 | } |
| 234 | |
| 235 | let canonical_request = |
| 236 | format!( |
| 237 | "{}\n{}\n\nhost:bedrock-runtime.{}.amazonaws.com\nx-amz-date:{}\n\nhost;x-amz-date\n{}", |
| 238 | method, path, region, |
| 239 | headers.get("X-Amz-Date").unwrap().to_str().unwrap(), |
| 240 | body_hash |
| 241 | ); |
| 242 | |
| 243 | let credential_scope = format!("{}/{}/{}/aws4_request", date_stamp, region, service); |
| 244 | |
| 245 | let string_to_sign = format!( |
| 246 | "AWS4-HMAC-SHA256\n{}\n{}\n{}", |
| 247 | headers.get("X-Amz-Date").unwrap().to_str().unwrap(), |
| 248 | credential_scope, |
| 249 | hex::encode(sha256(canonical_request.as_bytes())) |
| 250 | ); |
| 251 | |
| 252 | let signing_key = |
| 253 | Self::get_signature_key(&self.config.secret_access_key, &date_stamp, region, service); |
| 254 | |
| 255 | let signature = hex::encode(Self::hmac_sha256(&signing_key, string_to_sign.as_bytes())); |
| 256 | |
| 257 | let authorization = format!( |
| 258 | "AWS4-HMAC-SHA256 Credential={}/{}, SignedHeaders=host;x-amz-date, Signature={}", |
| 259 | self.config.access_key_id, credential_scope, signature |
| 260 | ); |
| 261 | |
| 262 | headers.insert("Authorization", authorization.parse().unwrap()); |
no test coverage detected