Skip to main content

bedrock_agentcore/identity/
api_key.rs

1use std::fmt;
2
3use aws_sdk_bedrockagentcore::Client;
4
5use super::{check_provider_name, resolve_workload_access_token, secret, service_error};
6use crate::{
7    Error, ErrorKind,
8    context::{self, WorkloadAccessToken},
9};
10
11secret! {
12    /// An API key from a credential provider of AgentCore Identity.
13    ApiKey
14}
15
16/// A request for the API key of one credential provider. Create one with
17/// [`IdentityClient::api_key`](super::IdentityClient::api_key).
18#[derive(Clone)]
19#[must_use = "the request is made when `send` is called and awaited"]
20pub struct ApiKeyBuilder {
21    client: Client,
22    provider: String,
23    workload_access_token: Option<WorkloadAccessToken>,
24}
25
26impl ApiKeyBuilder {
27    pub(super) fn new(client: Client, provider: String) -> Self {
28        Self { client, provider, workload_access_token: None }
29    }
30
31    /// Sets the workload access token of this request.
32    ///
33    /// Without this setter the request uses the token of the ambient request context.
34    /// A token set here is used as given: when it is empty the request fails, even
35    /// inside a context that has a token.
36    pub fn workload_access_token(mut self, token: WorkloadAccessToken) -> Self {
37        self.workload_access_token = Some(token);
38        self
39    }
40
41    /// Requests the API key with `GetResourceApiKey`.
42    ///
43    /// The workload access token is read when the returned future is first polled, so
44    /// the ambient context that counts is the one the future runs in.
45    ///
46    /// # Errors
47    ///
48    /// - [`ErrorKind::InvalidConfig`] when the provider name is not 1 to 128 letters,
49    ///   digits, hyphens and underscores. No request is made.
50    /// - [`ErrorKind::MissingWorkloadToken`] when there is no nonempty token. No
51    ///   request is made.
52    /// - [`ErrorKind::Service`] when the call fails. Its text has the provider, the
53    ///   error code of the service, the HTTP status and the request ID, and its
54    ///   source is the `SdkError` of the generated operation.
55    /// - [`ErrorKind::NoApiKey`] when the call succeeds without an API key.
56    pub async fn send(self) -> Result<ApiKey, Error> {
57        let Self { client, provider, workload_access_token } = self;
58        check_provider_name(&provider)?;
59        let token = resolve_workload_access_token(workload_access_token, context::current().as_ref())?;
60        let output = client
61            .get_resource_api_key()
62            .resource_credential_provider_name(&provider)
63            .workload_identity_token(token.expose())
64            .send()
65            .await
66            .map_err(|error| service_error("GetResourceApiKey", &provider, error))?;
67        if output.api_key.is_empty() {
68            return Err(Error::new(ErrorKind::NoApiKey, format!("No API key returned for provider: {provider}")));
69        }
70        Ok(ApiKey::new(output.api_key))
71    }
72}
73
74impl fmt::Debug for ApiKeyBuilder {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        f.debug_struct("ApiKeyBuilder")
77            .field("provider", &self.provider)
78            .field("workload_access_token", &self.workload_access_token)
79            .finish_non_exhaustive()
80    }
81}