mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iam): add GenerateCredentialReport and GetCredentialReport (#4298)
Generates and downloads the account's credential report, the 23 columns AWS documents for it, in CSV, Base64-encoded per the wire model. The report is a single value per account, stored the same way accountAliases and passwordPolicies already are, and regenerated only once the previous one is older than 4 hours, matching AWS's documented caching window. CSV content: every IAM user gets real password_enabled, password_last_used, password_last_changed and access_key_1/2_active/last_rotated where Floci already tracks that state, plus the documented placeholder values for what it does not model (MFA devices, X.509 signing certificates, access-key last-used date/region/service, and password_last_used itself, since nothing in the codebase ever sets IamUser's passwordLastUsed field). The root account row is present, as AWS always includes it, but is placeholder throughout: Floci does not model root account credentials at all, the same reason GetAccountSummary's AccountPasswordPresent and AccountAccessKeysPresent are always zero. password_last_changed needed a new LoginProfile field: UpdateLoginProfile changes the password without touching LoginProfile's own createDate (its meaning stays 'profile created'), so reading createDate for this column would have kept reporting the original creation time after a password change. passwordLastChanged is set alongside createDate initially and updated whenever UpdateLoginProfile actually changes the password, falling back to createDate for a profile persisted before this field existed. additional_credentials_info is Floci's own wording, since AWS does not document the exact text; in practice it is unreachable, since CreateAccessKey already enforces the real 2-key-per-user quota. Generating a report is effectively instant here, so GenerateCredentialReport never actually returns INPROGRESS; a GetCredentialReport call right after it always finds the report ready. The STARTED state and its description for the no-report-exists case match AWS's own documented example response verbatim; the report-expired case's wording is Floci's own, since AWS does not document it.
This commit is contained in:
1 parent
1952ba4052
commit
910f0922c9
7 files changed
+468
-5
No files matched your search
+86
@@ -0,0 +1,86 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Verifies GenerateCredentialReport and GetCredentialReport against the real wire shape.
|
||||
* IAM state, including the one stored credential report, is shared across the whole test
|
||||
* suite, so a report may already exist for this account by the time these tests run: none of
|
||||
* them assume a specific prior State, only that a Generate/Get pair always succeeds and that
|
||||
* the CSV content is well-formed.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CredentialReportIntegrationTest {
|
||||
|
||||
private static final String IAM_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request";
|
||||
|
||||
private static io.restassured.specification.RequestSpecification iam(String action) {
|
||||
return given().header("Authorization", IAM_AUTH).formParam("Action", action);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCredentialReportReturnsAValidState() {
|
||||
iam("GenerateCredentialReport")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("GenerateCredentialReportResponse.GenerateCredentialReportResult.State",
|
||||
anyOf(equalTo("STARTED"), equalTo("COMPLETE")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCredentialReportAfterGenerateReturnsWellFormedCsv() {
|
||||
iam("GenerateCredentialReport")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
String content = iam("GetCredentialReport")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("GetCredentialReportResponse.GetCredentialReportResult.ReportFormat", equalTo("text/csv"))
|
||||
.extract().path("GetCredentialReportResponse.GetCredentialReportResult.Content");
|
||||
|
||||
String csv = new String(Base64.getDecoder().decode(content));
|
||||
assertTrue(csv.startsWith("user,arn,user_creation_time,password_enabled,password_last_used,"
|
||||
+ "password_last_changed,password_next_rotation,mfa_active,"),
|
||||
"expected the documented column header, got: " + csv);
|
||||
assertTrue(csv.contains("<root_account>,arn:aws:iam::"), "expected a root account row, got: " + csv);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCredentialReportIncludesAUserCreatedAfterTheLastGenerate() {
|
||||
String user = "cred-report-it-user-" + Long.toString(System.nanoTime(), 36);
|
||||
iam("CreateUser").formParam("UserName", user)
|
||||
.when().post("/").then().statusCode(200);
|
||||
iam("GenerateCredentialReport")
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
String content = iam("GetCredentialReport")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.extract().path("GetCredentialReportResponse.GetCredentialReportResult.Content");
|
||||
String csv = new String(Base64.getDecoder().decode(content));
|
||||
|
||||
assertTrue(csv.lines().anyMatch(line -> line.startsWith(user + ",")),
|
||||
"expected a row for " + user + ", got: " + csv);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCredentialReportResponseHasNoOtherUnexpectedElements() {
|
||||
iam("GenerateCredentialReport")
|
||||
.when().post("/").then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<GenerateCredentialReportResponse"))
|
||||
.body(containsString("<State>"))
|
||||
.body(containsString("<Description>"));
|
||||
}
|
||||
}
|
||||
@@ -9,19 +9,23 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.iam.model.AccessKey;
|
||||
import io.github.hectorvent.floci.services.iam.model.AccountPasswordPolicy;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.CredentialReport;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamGroup;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamPolicy;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamUser;
|
||||
import io.github.hectorvent.floci.services.iam.model.LoginProfile;
|
||||
import io.github.hectorvent.floci.services.iam.model.OpenIDConnectProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.OrganizationRootFeatures;
|
||||
import io.github.hectorvent.floci.services.iam.model.InstanceProfile;
|
||||
import io.github.hectorvent.floci.services.iam.model.PolicyVersion;
|
||||
import io.github.hectorvent.floci.services.iam.model.SessionCredential;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -2055,4 +2059,130 @@ class IamServiceTest {
|
||||
assertTrue(details.groups().stream().anyMatch(g -> g.getGroupName().equals("aad-list-group")));
|
||||
assertTrue(details.roles().stream().anyMatch(r -> r.getRoleName().equals("aad-list-role")));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Credential Report
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void getCredentialReportBeforeAnyGenerateReturnsReportNotPresent() {
|
||||
AwsException ex = assertThrows(AwsException.class, () -> iamService.getCredentialReport());
|
||||
assertEquals("ReportNotPresent", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCredentialReportReturnsStartedWhenNoneExists() {
|
||||
IamService.CredentialReportGeneration generation = iamService.generateCredentialReport();
|
||||
|
||||
assertEquals("STARTED", generation.state());
|
||||
assertEquals("No report exists. Starting a new report generation task", generation.description());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCredentialReportReturnsCompleteWithinFourHoursOfAnExistingReport() {
|
||||
iamService.generateCredentialReport();
|
||||
|
||||
IamService.CredentialReportGeneration second = iamService.generateCredentialReport();
|
||||
|
||||
assertEquals("COMPLETE", second.state());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCredentialReportAfterGenerateReturnsDecodableCsvWithTheHeaderAndRootRow() {
|
||||
iamService.generateCredentialReport();
|
||||
|
||||
IamService.CredentialReportContent content = iamService.getCredentialReport();
|
||||
|
||||
assertEquals("text/csv", content.reportFormat());
|
||||
String csv = new String(Base64.getDecoder().decode(content.base64Content()));
|
||||
assertTrue(csv.startsWith("user,arn,user_creation_time,password_enabled,"),
|
||||
"expected the documented column header, got: " + csv);
|
||||
assertTrue(csv.contains("<root_account>,arn:aws:iam::"), "expected a root account row, got: " + csv);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCredentialReportIncludesEveryUserWithPasswordAndAccessKeyState() {
|
||||
iamService.createUser("cred-report-user", "/");
|
||||
iamService.createLoginProfile("cred-report-user", "Sup3r-Secret!", false);
|
||||
iamService.createAccessKey("cred-report-user");
|
||||
iamService.generateCredentialReport();
|
||||
|
||||
String csv = new String(Base64.getDecoder().decode(iamService.getCredentialReport().base64Content()));
|
||||
String row = csv.lines().filter(line -> line.startsWith("cred-report-user,")).findFirst()
|
||||
.orElseThrow(() -> new AssertionError("no row for cred-report-user in: " + csv));
|
||||
String[] fields = row.split(",", -1);
|
||||
|
||||
assertEquals("TRUE", fields[3], "password_enabled");
|
||||
assertEquals("TRUE", fields[8], "access_key_1_active");
|
||||
assertEquals("FALSE", fields[7], "mfa_active is never modeled");
|
||||
}
|
||||
|
||||
/**
|
||||
* UpdateLoginProfile changes the password without touching LoginProfile's own createDate
|
||||
* (its meaning stays "profile created"), so the credential report's password_last_changed
|
||||
* column has to be backed by a separate field, updated whenever the password itself
|
||||
* changes: otherwise a changed password would still report the original creation time.
|
||||
*/
|
||||
@Test
|
||||
void updateLoginProfilePasswordChangeMovesPasswordLastChanged() {
|
||||
iamService.createUser("cred-report-changed-user", "/");
|
||||
iamService.createLoginProfile("cred-report-changed-user", "Original-P4ss!", false);
|
||||
Instant createdAt = iamService.getLoginProfile("cred-report-changed-user").getPasswordLastChanged();
|
||||
|
||||
iamService.updateLoginProfile("cred-report-changed-user", "Updated-P4ss!", null);
|
||||
Instant changedAt = iamService.getLoginProfile("cred-report-changed-user").getPasswordLastChanged();
|
||||
|
||||
assertTrue(changedAt.isAfter(createdAt),
|
||||
"expected password_last_changed to move: created=" + createdAt + " changed=" + changedAt);
|
||||
}
|
||||
|
||||
/** Toggling only passwordResetRequired is not a password change, so the timestamp must not move. */
|
||||
@Test
|
||||
void updateLoginProfileWithoutAPasswordChangeLeavesPasswordLastChangedAlone() {
|
||||
iamService.createUser("cred-report-unchanged-user", "/");
|
||||
iamService.createLoginProfile("cred-report-unchanged-user", "Original-P4ss!", false);
|
||||
Instant createdAt = iamService.getLoginProfile("cred-report-unchanged-user").getPasswordLastChanged();
|
||||
|
||||
iamService.updateLoginProfile("cred-report-unchanged-user", null, true);
|
||||
|
||||
assertEquals(createdAt, iamService.getLoginProfile("cred-report-unchanged-user").getPasswordLastChanged());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCredentialReportOnAnExpiredReportThrowsReportExpired() {
|
||||
StorageBackend<String, CredentialReport> credentialReports = new InMemoryStorage<>();
|
||||
credentialReports.put("credential-report",
|
||||
new CredentialReport("dGVzdA==", Instant.now().minus(Duration.ofHours(5))));
|
||||
IamService withExpiredReport = new IamService(
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), credentialReports,
|
||||
new RegionResolver("us-east-1", "000000000000"), false, null);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, withExpiredReport::getCredentialReport);
|
||||
assertEquals("ReportExpired", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCredentialReportOnAnExpiredReportStartsANewOne() {
|
||||
StorageBackend<String, CredentialReport> credentialReports = new InMemoryStorage<>();
|
||||
credentialReports.put("credential-report",
|
||||
new CredentialReport("dGVzdA==", Instant.now().minus(Duration.ofHours(5))));
|
||||
IamService withExpiredReport = new IamService(
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), credentialReports,
|
||||
new RegionResolver("us-east-1", "000000000000"), false, null);
|
||||
|
||||
IamService.CredentialReportGeneration generation = withExpiredReport.generateCredentialReport();
|
||||
|
||||
assertEquals("STARTED", generation.state());
|
||||
assertEquals("The previous report has expired. Starting a new report generation task",
|
||||
generation.description());
|
||||
assertDoesNotThrow(withExpiredReport::getCredentialReport);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user