aws_config/imds/client/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Error types for [`ImdsClient`](crate::imds::client::Client)

use aws_smithy_runtime_api::client::orchestrator::HttpResponse;
use aws_smithy_runtime_api::client::result::SdkError;
use std::error::Error;
use std::fmt;

/// Error context for [`ImdsError::FailedToLoadToken`]
#[derive(Debug)]
pub struct FailedToLoadToken {
    source: SdkError<TokenError, HttpResponse>,
}

impl FailedToLoadToken {
    /// Returns `true` if a dispatch failure caused the token to fail to load
    pub fn is_dispatch_failure(&self) -> bool {
        matches!(self.source, SdkError::DispatchFailure(_))
    }

    pub(crate) fn into_source(self) -> SdkError<TokenError, HttpResponse> {
        self.source
    }
}

/// Error context for [`ImdsError::ErrorResponse`]
#[derive(Debug)]
pub struct ErrorResponse {
    raw: HttpResponse,
}

impl ErrorResponse {
    /// Returns the raw response from IMDS
    pub fn response(&self) -> &HttpResponse {
        &self.raw
    }
}

/// Error context for [`ImdsError::IoError`]
#[derive(Debug)]
pub struct IoError {
    source: Box<dyn Error + Send + Sync + 'static>,
}

/// Error context for [`ImdsError::Unexpected`]
#[derive(Debug)]
pub struct Unexpected {
    source: Box<dyn Error + Send + Sync + 'static>,
}

/// An error retrieving metadata from IMDS
#[derive(Debug)]
#[non_exhaustive]
pub enum ImdsError {
    /// An IMDSv2 Token could not be loaded
    ///
    /// Requests to IMDS must be accompanied by a token obtained via a `PUT` request. This is handled
    /// transparently by the [`Client`](crate::imds::client::Client).
    FailedToLoadToken(FailedToLoadToken),

    /// An error response was returned from IMDS
    ErrorResponse(ErrorResponse),

    /// IO Error
    ///
    /// An error occurred communication with IMDS
    IoError(IoError),

    /// An unexpected error occurred communicating with IMDS
    Unexpected(Unexpected),
}

impl ImdsError {
    pub(super) fn failed_to_load_token(source: SdkError<TokenError, HttpResponse>) -> Self {
        Self::FailedToLoadToken(FailedToLoadToken { source })
    }

    pub(super) fn error_response(raw: HttpResponse) -> Self {
        Self::ErrorResponse(ErrorResponse { raw })
    }

    pub(super) fn io_error(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
        Self::IoError(IoError {
            source: source.into(),
        })
    }

    pub(super) fn unexpected(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
        Self::Unexpected(Unexpected {
            source: source.into(),
        })
    }
}

impl fmt::Display for ImdsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ImdsError::FailedToLoadToken(_) => {
                write!(f, "failed to load IMDS session token")
            }
            ImdsError::ErrorResponse(context) => write!(
                f,
                "error response from IMDS (code: {}). {:?}",
                context.raw.status().as_u16(),
                context.raw
            ),
            ImdsError::IoError(_) => {
                write!(f, "an IO error occurred communicating with IMDS")
            }
            ImdsError::Unexpected(_) => {
                write!(f, "an unexpected error occurred communicating with IMDS",)
            }
        }
    }
}

impl Error for ImdsError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match &self {
            ImdsError::FailedToLoadToken(context) => Some(&context.source),
            ImdsError::IoError(context) => Some(context.source.as_ref()),
            ImdsError::Unexpected(context) => Some(context.source.as_ref()),
            ImdsError::ErrorResponse(_) => None,
        }
    }
}

#[derive(Debug)]
pub(super) enum InnerImdsError {
    BadStatus,
    InvalidUtf8,
}

impl fmt::Display for InnerImdsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            InnerImdsError::BadStatus => write!(f, "failing status code returned from IMDS"),
            InnerImdsError::InvalidUtf8 => write!(f, "IMDS did not return valid UTF-8"),
        }
    }
}

impl Error for InnerImdsError {}

/// Invalid Endpoint Mode
#[derive(Debug)]
pub struct InvalidEndpointMode {
    mode: String,
}

impl InvalidEndpointMode {
    pub(super) fn new(mode: impl Into<String>) -> Self {
        Self { mode: mode.into() }
    }
}

impl fmt::Display for InvalidEndpointMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "`{}` is not a valid endpoint mode. Valid values are [`IPv4`, `IPv6`]",
            &self.mode
        )
    }
}

impl Error for InvalidEndpointMode {}

#[derive(Debug)]
#[allow(clippy::enum_variant_names)]
enum BuildErrorKind {
    /// The endpoint mode was invalid
    InvalidEndpointMode(InvalidEndpointMode),

    /// The specified endpoint was not a valid URI
    InvalidEndpointUri(Box<dyn Error + Send + Sync + 'static>),
}

/// Error constructing IMDSv2 Client
#[derive(Debug)]
pub struct BuildError {
    kind: BuildErrorKind,
}

impl BuildError {
    pub(super) fn invalid_endpoint_mode(source: InvalidEndpointMode) -> Self {
        Self {
            kind: BuildErrorKind::InvalidEndpointMode(source),
        }
    }

    pub(super) fn invalid_endpoint_uri(
        source: impl Into<Box<dyn Error + Send + Sync + 'static>>,
    ) -> Self {
        Self {
            kind: BuildErrorKind::InvalidEndpointUri(source.into()),
        }
    }
}

impl fmt::Display for BuildError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        use BuildErrorKind::*;
        write!(f, "failed to build IMDS client: ")?;
        match self.kind {
            InvalidEndpointMode(_) => write!(f, "invalid endpoint mode"),
            InvalidEndpointUri(_) => write!(f, "invalid URI"),
        }
    }
}

impl Error for BuildError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        use BuildErrorKind::*;
        match &self.kind {
            InvalidEndpointMode(e) => Some(e),
            InvalidEndpointUri(e) => Some(e.as_ref()),
        }
    }
}

#[derive(Debug)]
pub(super) enum TokenErrorKind {
    /// The token was invalid
    ///
    /// Because tokens must be eventually sent as a header, the token must be a valid header value.
    InvalidToken,

    /// No TTL was sent
    ///
    /// The token response must include a time-to-live indicating the lifespan of the token.
    NoTtl,

    /// The TTL was invalid
    ///
    /// The TTL must be a valid positive integer.
    InvalidTtl,

    /// Invalid Parameters
    ///
    /// The request to load a token was malformed. This indicates an SDK bug.
    InvalidParameters,

    /// Forbidden
    ///
    /// IMDS is disabled or has been disallowed via permissions.
    Forbidden,
}

/// Error retrieving token from IMDS
#[derive(Debug)]
pub struct TokenError {
    kind: TokenErrorKind,
}

impl fmt::Display for TokenError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use TokenErrorKind::*;
        match self.kind {
            InvalidToken => write!(f, "invalid token"),
            NoTtl => write!(f, "token response did not contain a TTL header"),
            InvalidTtl => write!(f, "the returned TTL was invalid"),
            InvalidParameters => {
                write!(f, "invalid request parameters. This indicates an SDK bug.")
            }
            Forbidden => write!(
                f,
                "request forbidden: IMDS is disabled or the caller has insufficient permissions."
            ),
        }
    }
}

impl Error for TokenError {}

impl From<TokenErrorKind> for TokenError {
    fn from(kind: TokenErrorKind) -> Self {
        Self { kind }
    }
}