fluidattacks-core-git 0.68.0

Fluid Attacks Core Git
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use std::time::Duration;

use aws_config::timeout::TimeoutConfig;
use aws_sdk_sts::error::{ConnectorError, DisplayErrorContext, ProvideErrorMetadata, SdkError};
use aws_sdk_sts::operation::assume_role::{AssumeRoleError, AssumeRoleOutput};
use regex::Regex;
use uuid::Uuid;

use crate::errors::StsError;

/// STS answers in tenths of a second; this leaves room for a slow one, as git's
/// ls-remote does. Without one the SDK waits as long as STS holds the connection open.
const READ_TIMEOUT: Duration = Duration::from_secs(20);

pub struct AwsTempCreds {
    pub access_key: String,
    pub secret_key: String,
    pub session_token: String,
    pub region: String,
}

struct Hop<'a> {
    role_arn: &'a str,
    external_id: Option<&'a str>,
    policy: Option<&'static str>,
}

/// The intermediate session may only assume a role, so a chained caller never
/// holds the intermediate role's other permissions. The resource stays open
/// because a policy would match the client's ARN as typed, case included.
const ONLY_ASSUME_ROLE: &str = r#"{
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "*"
    }]
}"#;

/// Chained when the caller is not the principal the client's trust policy pins.
const fn chain<'a>(
    arn: &'a str,
    external_id: &'a str,
    via: Option<&'a str>,
) -> (Hop<'a>, Option<Hop<'a>>) {
    let client = Hop {
        role_arn: arn,
        external_id: Some(external_id),
        policy: None,
    };

    match via {
        Some(intermediate) => (
            Hop {
                role_arn: intermediate,
                external_id: None,
                policy: Some(ONLY_ASSUME_ROLE),
            },
            Some(client),
        ),
        None => (client, None),
    }
}

pub async fn assume_role(
    arn: &str,
    external_id: &str,
    url: &str,
    via: Option<&str>,
) -> Result<AwsTempCreds, StsError> {
    let region = extract_region(url);
    let (first, then) = chain(arn, external_id, via);

    let mut creds = Sts::signed_as(&region, None).await.assume(&first).await?;
    if let Some(hop) = then {
        let chained = Sts::signed_as(&region, Some(&creds)).await;
        creds = chained.assume(&hop).await?;
    }

    Ok(creds)
}

/// STS in the repository's region, signed by the worker or by the previous hop's session.
struct Sts {
    client: aws_sdk_sts::Client,
    region: String,
}

impl Sts {
    async fn signed_as(region: &str, from: Option<&AwsTempCreds>) -> Self {
        Self {
            client: aws_sdk_sts::Client::new(&config(region, from).load().await),
            region: region.to_owned(),
        }
    }

    async fn assume(&self, hop: &Hop<'_>) -> Result<AwsTempCreds, StsError> {
        let mut request = self
            .client
            .assume_role()
            .role_arn(hop.role_arn)
            .role_session_name(format!("session-{}", Uuid::new_v4()));

        if let Some(external_id) = hop.external_id {
            request = request.external_id(external_id);
        }

        if let Some(policy) = hop.policy {
            request = request.policy(policy);
        }

        let resp = request
            .send()
            .await
            .map_err(|error| refusal(hop.role_arn, &error))?;

        session(hop.role_arn, &resp, &self.region)
    }
}

fn config(region: &str, from: Option<&AwsTempCreds>) -> aws_config::ConfigLoader {
    // Only the read timeout: the SDK's connect timeout stays.
    let timeouts = TimeoutConfig::builder().read_timeout(READ_TIMEOUT).build();
    let mut config = aws_config::defaults(aws_config::BehaviorVersion::latest())
        .region(aws_config::Region::new(region.to_owned()))
        .timeout_config(timeouts);

    if let Some(session) = from {
        config = config.credentials_provider(aws_sdk_sts::config::Credentials::new(
            session.access_key.clone(),
            session.secret_key.clone(),
            Some(session.session_token.clone()),
            None,
            "fluidattacks-core-chain",
        ));
    }

    config
}

// botocore says "Unknown" for a code or message STS left out.
fn refusal<R: std::fmt::Debug>(arn: &str, error: &SdkError<AssumeRoleError, R>) -> StsError {
    let arn = arn.to_owned();
    if let SdkError::ServiceError(refused) = error {
        return StsError::Refused {
            arn,
            code: refused.err().code().unwrap_or("Unknown").to_owned(),
            message: refused.err().message().unwrap_or("Unknown").to_owned(),
        };
    }

    let reason = DisplayErrorContext(error).to_string();
    let SdkError::DispatchFailure(failure) = error else {
        return StsError::Unreachable { arn, reason };
    };
    let Some(connector) = failure.as_connector_error() else {
        return StsError::Unreachable { arn, reason };
    };

    // Told apart as botocore tells its classes apart, since integrates catches those.
    let timed_out = connector.is_timeout();
    if never_connected(connector) {
        return if timed_out {
            StsError::ConnectTimeout { arn, reason }
        } else {
            StsError::NoConnection { arn, reason }
        };
    }
    if timed_out {
        StsError::ReadTimeout { arn, reason }
    } else if hung_up(connector) {
        StsError::ConnectionClosed { arn, reason }
    } else {
        StsError::Unreachable { arn, reason }
    }
}

fn never_connected(connector: &ConnectorError) -> bool {
    causes(connector).any(|cause| {
        cause
            .downcast_ref::<hyper_util::client::legacy::Error>()
            .is_some_and(hyper_util::client::legacy::Error::is_connect)
    })
}

fn hung_up(connector: &ConnectorError) -> bool {
    connector.is_io()
        || causes(connector).any(|cause| {
            cause
                .downcast_ref::<hyper::Error>()
                .is_some_and(|hyper| hyper.is_incomplete_message() || hyper.is_closed())
        })
}

fn causes<'a>(
    error: &'a (dyn std::error::Error + 'static),
) -> impl Iterator<Item = &'a (dyn std::error::Error + 'static)> {
    std::iter::successors(Some(error), |current| current.source())
}

fn session(arn: &str, resp: &AssumeRoleOutput, region: &str) -> Result<AwsTempCreds, StsError> {
    let no_credentials = || StsError::NoCredentials {
        arn: arn.to_owned(),
    };
    let creds = resp.credentials().ok_or_else(no_credentials)?;

    let access_key = creds.access_key_id().to_owned();
    let secret_key = creds.secret_access_key().to_owned();
    let session_token = creds.session_token().to_owned();

    if access_key.is_empty() || secret_key.is_empty() {
        return Err(no_credentials());
    }

    Ok(AwsTempCreds {
        access_key,
        secret_key,
        session_token,
        region: region.to_owned(),
    })
}

fn extract_region(url: &str) -> String {
    [
        r"codecommit::([a-z0-9-]+)://",
        r"git-codecommit\.([a-z0-9-]+)\.amazonaws\.com",
    ]
    .into_iter()
    .find_map(|pattern| {
        let region = Regex::new(pattern).ok()?.captures(url)?.get(1)?;
        Some(region.as_str().to_owned())
    })
    .unwrap_or_else(|| "us-east-1".to_owned())
}

#[cfg(test)]
mod tests {
    use aws_sdk_sts::error::{ErrorMetadata, SdkError};
    use aws_sdk_sts::operation::assume_role::{AssumeRoleError, AssumeRoleOutput};
    use aws_sdk_sts::primitives::DateTime;
    use aws_sdk_sts::types::Credentials;
    use aws_sdk_sts::Client;
    use std::io::{BufRead, BufReader, Read, Write};
    use std::net::{TcpListener, TcpStream};
    use std::time::Duration;

    use super::{chain, config, extract_region, refusal, session, Hop, Sts, TimeoutConfig};
    use crate::errors::StsError;

    const ROLE: &str = "arn:aws:iam::1:role/client";

    #[test]
    fn a_refusal_carries_the_code_and_message_sts_answered() {
        let refused = AssumeRoleError::generic(
            ErrorMetadata::builder()
                .code("AccessDenied")
                .message("not authorized to perform sts:AssumeRole")
                .build(),
        );

        let error = refusal(ROLE, &SdkError::service_error(refused, ()));

        assert_eq!(
            error.to_string(),
            "STS refused the role arn:aws:iam::1:role/client: AccessDenied: not authorized to perform sts:AssumeRole"
        );
    }

    #[test]
    fn an_sts_that_never_answered_is_not_a_refusal() {
        let error = refusal(
            ROLE,
            &SdkError::<AssumeRoleError, ()>::timeout_error("timed out"),
        );

        assert!(matches!(error, StsError::Unreachable { .. }), "{error:?}");
    }

    #[test]
    fn an_answer_without_a_usable_key_is_no_session() {
        let keys = |access: &str, secret: &str| {
            Credentials::builder()
                .access_key_id(access)
                .secret_access_key(secret)
                .session_token("a-token")
                .expiration(DateTime::from_secs(0))
                .build()
                .unwrap()
        };

        for answer in [
            AssumeRoleOutput::builder().build(),
            AssumeRoleOutput::builder()
                .credentials(keys("", "a-secret-key"))
                .build(),
            AssumeRoleOutput::builder()
                .credentials(keys("an-access-key", ""))
                .build(),
        ] {
            let outcome = session(ROLE, &answer, "us-east-1");

            assert!(
                matches!(outcome, Err(StsError::NoCredentials { .. })),
                "{answer:?}"
            );
        }
    }

    #[test]
    fn the_region_comes_from_the_url_or_falls_back_to_us_east_1() {
        for (url, region) in [
            ("codecommit::eu-west-1://a-repo", "eu-west-1"),
            (
                "https://git-codecommit.sa-east-1.amazonaws.com/v1/repos/a-repo",
                "sa-east-1",
            ),
            ("codecommit://a-repo", "us-east-1"),
        ] {
            assert_eq!(extract_region(url), region);
        }
    }

    #[test]
    fn one_call_takes_the_client_role_with_its_external_id() {
        let (first, then) = chain("arn:aws:iam::1:role/client", "an-external-id", None);

        assert_eq!(first.role_arn, "arn:aws:iam::1:role/client");
        assert_eq!(first.external_id, Some("an-external-id"));
        assert_eq!(first.policy, None);
        assert!(then.is_none());
    }

    #[test]
    fn a_chain_takes_the_intermediate_role_first_and_without_the_external_id() {
        let (first, then) = chain(
            "arn:aws:iam::1:role/client",
            "an-external-id",
            Some("arn:aws:iam::2:role/intermediate"),
        );

        assert_eq!(first.role_arn, "arn:aws:iam::2:role/intermediate");
        assert_eq!(first.external_id, None);

        let client = then.expect("the client role is still taken");
        assert_eq!(client.role_arn, "arn:aws:iam::1:role/client");
        assert_eq!(client.external_id, Some("an-external-id"));
        assert_eq!(client.policy, None);
    }

    #[test]
    fn a_chain_lets_the_intermediate_session_only_assume_a_role() {
        let (first, _) = chain(
            "arn:aws:iam::1:role/client",
            "an-external-id",
            Some("arn:aws:iam::2:role/intermediate"),
        );

        let policy: serde_json::Value =
            serde_json::from_str(first.policy.expect("the intermediate session is narrowed"))
                .expect("the policy is json");
        assert_eq!(
            policy.get("Statement"),
            Some(&serde_json::json!([{
                "Effect": "Allow",
                "Action": "sts:AssumeRole",
                "Resource": "*",
            }]))
        );
    }

    // An STS on a local port that does `answer` with each request it reads.
    fn sts(answer: fn(TcpStream)) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind");
        let port = listener.local_addr().expect("addr").port();
        std::thread::spawn(move || {
            for stream in listener.incoming().flatten() {
                std::thread::spawn(move || serve(stream, answer));
            }
        });
        format!("http://127.0.0.1:{port}")
    }

    fn serve(stream: TcpStream, answer: fn(TcpStream)) {
        read_request(&stream);
        answer(stream);
    }

    fn read_request(stream: &TcpStream) {
        let mut reader = BufReader::new(stream);
        let mut length = 0;
        let mut line = String::new();
        while reader.read_line(&mut line).is_ok_and(|read| read > 2) {
            if let Some(value) = line.to_lowercase().strip_prefix("content-length: ") {
                length = value.trim().parse().unwrap_or(0);
            }
            line.clear();
        }
        let _ = reader.read_exact(&mut vec![0; length]);
    }

    async fn ask_sts(
        endpoint: &str,
        hop: &Hop<'_>,
        signed: bool,
    ) -> Result<super::AwsTempCreds, StsError> {
        let quick = TimeoutConfig::builder()
            .read_timeout(Duration::from_millis(200))
            .build();
        let mut loader = config("us-east-1", None)
            .endpoint_url(endpoint)
            .timeout_config(quick);
        loader = if signed {
            loader.credentials_provider(aws_sdk_sts::config::Credentials::new(
                "a-key", "a-secret", None, None, "test",
            ))
        } else {
            loader.no_credentials()
        };
        let sts = Sts {
            client: Client::new(&loader.load().await),
            region: "us-east-1".to_owned(),
        };
        sts.assume(hop).await
    }

    fn client_role() -> Hop<'static> {
        chain(ROLE, "an-external-id", None).0
    }

    #[tokio::test]
    async fn sts_has_twenty_seconds_to_answer() {
        let sts = Sts::signed_as("us-east-1", None).await;

        let read_timeout = sts
            .client
            .config()
            .timeout_config()
            .and_then(TimeoutConfig::read_timeout);

        assert_eq!(read_timeout, Some(Duration::from_secs(20)));
    }

    #[tokio::test]
    async fn an_sts_that_keeps_the_request_unanswered_is_a_read_timeout() {
        let endpoint = sts(|_stream| std::thread::sleep(Duration::from_secs(5)));

        let outcome = ask_sts(&endpoint, &client_role(), true).await;

        assert!(
            matches!(outcome, Err(StsError::ReadTimeout { .. })),
            "{:?}",
            outcome.err()
        );
    }

    #[tokio::test]
    async fn an_sts_that_closes_the_connection_unanswered_is_connection_closed() {
        let endpoint = sts(drop);

        let outcome = ask_sts(&endpoint, &client_role(), true).await;

        assert!(
            matches!(outcome, Err(StsError::ConnectionClosed { .. })),
            "{:?}",
            outcome.err()
        );
    }

    #[tokio::test]
    async fn an_sts_that_resets_the_connection_unanswered_is_connection_closed() {
        let endpoint = sts(|stream| {
            // With no linger, closing sends a reset instead of an orderly close.
            rustix::net::sockopt::set_socket_linger(&stream, Some(Duration::ZERO)).expect("linger");
        });

        let outcome = ask_sts(&endpoint, &client_role(), true).await;

        assert!(
            matches!(outcome, Err(StsError::ConnectionClosed { .. })),
            "{:?}",
            outcome.err()
        );
    }

    #[tokio::test]
    async fn a_port_nobody_listens_on_is_no_connection() {
        let closed = TcpListener::bind("127.0.0.1:0").expect("bind");
        let endpoint = format!("http://{}", closed.local_addr().expect("addr"));
        drop(closed);

        let outcome = ask_sts(&endpoint, &client_role(), true).await;

        assert!(
            matches!(outcome, Err(StsError::NoConnection { .. })),
            "{:?}",
            outcome.err()
        );
    }

    #[tokio::test]
    async fn a_caller_without_credentials_to_sign_with_is_unreachable() {
        let endpoint = sts(drop);

        let outcome = ask_sts(&endpoint, &client_role(), false).await;

        assert!(
            matches!(outcome, Err(StsError::Unreachable { .. })),
            "{:?}",
            outcome.err()
        );
    }

    #[tokio::test]
    async fn a_role_below_the_sts_limits_is_left_for_sts_to_refuse() {
        // botocore refused these before asking; the SDK sends them as they are.
        let endpoint = sts(|mut stream| {
            let body = "<ErrorResponse><Error><Code>ValidationError</Code>\
                        <Message>too short</Message></Error></ErrorResponse>";
            let _ = write!(
                stream,
                "HTTP/1.1 400 Bad Request\r\nContent-Type: text/xml\r\n\
                 Content-Length: {}\r\nConnection: close\r\n\r\n{body}",
                body.len()
            );
        });
        let short = Hop {
            role_arn: "x",
            external_id: Some("a"),
            policy: None,
        };

        let outcome = ask_sts(&endpoint, &short, true).await;

        let Err(StsError::Refused { code, .. }) = outcome else {
            panic!("{:?}", outcome.err());
        };
        assert_eq!(code, "ValidationError");
    }
}