mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): populate aws:CurrentTime, aws:EpochTime and aws:TokenIssueTime (#8131)
Every request context now carries aws:CurrentTime and aws:EpochTime, the time of the request, including S3's own bucket-policy check and the context of a call a service makes. Every session Floci issues records when it was issued, the STS sessions and the EC2, ECS and Lambda role sessions among them, and a request signed with it carries that as aws:TokenIssueTime, which long-term access keys leave out, as the IAM User Guide describes. A role policy that denies sessions issued before a time, the guide's way of revoking them, now takes effect. Closes #8130
This commit is contained in:
1 parent
1e66899a95
commit
8eda666f82
10 files changed
+346
-41
No files matched your search
@@ -590,9 +590,23 @@ public class IamConditionContextResolver {
|
||||
conditions.putIfAbsent("aws:ViaAWSService", List.of("false"));
|
||||
// Enforcement evaluates signed IAM credentials, not direct AWS service principals.
|
||||
conditions.put("aws:PrincipalIsAWSService", List.of("false"));
|
||||
putRequestTime(conditions);
|
||||
return conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* The time of the request, which the IAM User Guide says "is always included in the request
|
||||
* context" under two keys: {@code aws:CurrentTime}, a Date, and {@code aws:EpochTime}, "the
|
||||
* date and time of the request in epoch or Unix time", a Date and a Numeric, so whole seconds.
|
||||
* Every context a request is evaluated against carries them, including those built outside
|
||||
* {@link #withGlobalContext}, such as S3's own bucket-policy check.
|
||||
*/
|
||||
public static void putRequestTime(Map<String, List<String>> conditions) {
|
||||
Instant now = Instant.now();
|
||||
conditions.put("aws:CurrentTime", List.of(now.toString()));
|
||||
conditions.put("aws:EpochTime", List.of(Long.toString(now.getEpochSecond())));
|
||||
}
|
||||
|
||||
private static void putResourceAccount(Map<String, List<String>> conditions, String resourceArn,
|
||||
String resourceOwnerAccountId, String action) {
|
||||
// S3 ARNs omit the owner account. Both condition keys must use the stored bucket owner,
|
||||
@@ -613,7 +627,8 @@ public class IamConditionContextResolver {
|
||||
* {@code true}; {@code aws:PrincipalServiceName} and {@code aws:PrincipalServiceNamesList} name the
|
||||
* service; {@code aws:SourceArn} and {@code aws:SourceAccount} name the resource whose
|
||||
* configuration triggered the call, when it has them; and {@code aws:ResourceAccount} and
|
||||
* {@code aws:RequestedRegion} describe the target. The keys that describe an IAM principal,
|
||||
* {@code aws:RequestedRegion} describe the target; {@code aws:CurrentTime} and
|
||||
* {@code aws:EpochTime} carry the time of the request, as on every request. The keys that describe an IAM principal,
|
||||
* {@code aws:PrincipalArn} and {@code aws:PrincipalAccount} among them, are left out: no IAM
|
||||
* principal makes the call. A call to an SNS topic from a service SNS lists as setting it also
|
||||
* carries {@code aws:SourceOwner}, the source account, which is what the default topic policy
|
||||
@@ -641,6 +656,7 @@ public class IamConditionContextResolver {
|
||||
}
|
||||
putResourceAccount(conditions, resourceArn, resourceOwnerAccountId, action);
|
||||
putIfPresent(conditions, "aws:RequestedRegion", AwsArnUtils.regionOrDefault(resourceArn, null));
|
||||
putRequestTime(conditions);
|
||||
return conditions;
|
||||
}
|
||||
|
||||
|
||||
@@ -5115,7 +5115,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* Stores an assumed-role session so the enforcement filter can resolve its policies.
|
||||
*/
|
||||
public void registerSession(String sessionAccessKeyId, String roleArn, Instant expiration) {
|
||||
sessions.put(sessionAccessKeyId, new SessionCredential(sessionAccessKeyId, roleArn, expiration));
|
||||
sessions.put(sessionAccessKeyId, issuedNow(new SessionCredential(sessionAccessKeyId, roleArn, expiration)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5124,7 +5124,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public void registerSession(String sessionAccessKeyId, String roleArn, Instant expiration,
|
||||
String sessionPolicyDocument) {
|
||||
sessions.put(sessionAccessKeyId,
|
||||
new SessionCredential(sessionAccessKeyId, roleArn, expiration, sessionPolicyDocument));
|
||||
issuedNow(new SessionCredential(sessionAccessKeyId, roleArn, expiration, sessionPolicyDocument)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5140,8 +5140,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public void registerSession(String sessionAccessKeyId, String secretAccessKey, String sessionToken,
|
||||
String roleArn, Instant expiration, String sessionPolicyDocument) {
|
||||
sessions.put(sessionAccessKeyId,
|
||||
new SessionCredential(sessionAccessKeyId, secretAccessKey, sessionToken, roleArn,
|
||||
expiration, sessionPolicyDocument));
|
||||
issuedNow(new SessionCredential(sessionAccessKeyId, secretAccessKey, sessionToken, roleArn,
|
||||
expiration, sessionPolicyDocument)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5172,7 +5172,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
roleArn, expiration, sessionPolicyDocument, originAccountId);
|
||||
session.setRoleSessionName(roleSessionName);
|
||||
session.setAssumedRoleId(assumedRoleId);
|
||||
sessions.put(sessionAccessKeyId, session);
|
||||
sessions.put(sessionAccessKeyId, issuedNow(session));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5202,7 +5202,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
} else if (issuingAccessKeyId == null || !holdsKey(issuingAccessKeyId)) {
|
||||
session.setIssuerArn(regionResolver.buildGlobalArn("iam", originAccountId, "root"));
|
||||
}
|
||||
sessions.put(sessionAccessKeyId, session);
|
||||
sessions.put(sessionAccessKeyId, issuedNow(session));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5243,7 +5243,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
null, expiration, sessionPolicyDocument, ownerAccountId);
|
||||
session.setDelegatedApproverArn(delegatedApproverArn);
|
||||
session.setDelegatedRoleBoundaryArns(roleBoundaryArns);
|
||||
sessions.put(sessionAccessKeyId, session);
|
||||
sessions.put(sessionAccessKeyId, issuedNow(session));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5296,7 +5296,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
sessionAccessKeyId, secretAccessKey, sessionToken, roleArn, expiration, sessionPolicyDocument,
|
||||
accountId);
|
||||
session.setRoleSessionName(roleSessionName);
|
||||
putSessionForAccount(accountId, sessionAccessKeyId, session);
|
||||
putSessionForAccount(accountId, sessionAccessKeyId, issuedNow(session));
|
||||
}
|
||||
|
||||
/** An internal URL credential with both a session policy and an exact action/resource guard. */
|
||||
@@ -5310,7 +5310,17 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
accessKeyId, secretAccessKey, sessionToken, null, expiration, policyDocument, accountId);
|
||||
session.setPresignedAction(action);
|
||||
session.setPresignedResourceArn(resourceArn);
|
||||
putSessionForAccount(accountId, accessKeyId, session);
|
||||
putSessionForAccount(accountId, accessKeyId, issuedNow(session));
|
||||
}
|
||||
|
||||
/**
|
||||
* Records that {@code session} is issued now, which is its {@code aws:TokenIssueTime}: "the
|
||||
* exact time at which the temporary security credentials were created" (IAM User Guide,
|
||||
* revoking role sessions).
|
||||
*/
|
||||
private SessionCredential issuedNow(SessionCredential session) {
|
||||
session.setIssuedAt(clock.instant());
|
||||
return session;
|
||||
}
|
||||
|
||||
private void putSessionForAccount(String accountId, String sessionAccessKeyId, SessionCredential session) {
|
||||
@@ -5378,6 +5388,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
session.setLambdaExecutionRole(true);
|
||||
session.setRoleSessionName(roleSessionName);
|
||||
session.setAssumedRoleId(assumedRoleId);
|
||||
issuedNow(session);
|
||||
if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
|
||||
aware.putForAccount(accountId, sessionAccessKeyId, session);
|
||||
} else {
|
||||
@@ -5393,6 +5404,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
|| session.getEc2InstanceId() == null || session.getEc2InstanceId().isBlank()) {
|
||||
throw new IllegalArgumentException("EC2 session account and instance ID must not be blank");
|
||||
}
|
||||
issuedNow(session);
|
||||
if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
|
||||
aware.putForAccount(session.getOriginAccountId(), session.getAccessKeyId(), session);
|
||||
} else {
|
||||
@@ -5420,6 +5432,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
|| session.getEcsTaskArn() == null || session.getEcsTaskArn().isBlank()) {
|
||||
throw new IllegalArgumentException("ECS task session account and task ARN must not be blank");
|
||||
}
|
||||
issuedNow(session);
|
||||
if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
|
||||
aware.putForAccount(session.getOriginAccountId(), session.getAccessKeyId(), session);
|
||||
} else {
|
||||
@@ -5965,11 +5978,14 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* @param organization the organization the principal's account belongs to, whose three keys go
|
||||
* onto the context beside the ones above, or null when the account is in
|
||||
* none and when Organizations is not deployed
|
||||
* @param tokenIssueTime the request's {@code aws:TokenIssueTime}: when the temporary credentials
|
||||
* the caller signed with were issued, or null for long-term credentials
|
||||
*/
|
||||
public record CallerArns(String callerArn, String principalArn, String principalType,
|
||||
String userId, String userName, boolean permissionsBoundary,
|
||||
Map<String, String> principalTags, SessionMfa sessionMfa,
|
||||
PrincipalOrganization.OrganizationContext organization) {
|
||||
PrincipalOrganization.OrganizationContext organization,
|
||||
Instant tokenIssueTime) {
|
||||
|
||||
public CallerArns {
|
||||
principalTags = Map.copyOf(principalTags);
|
||||
@@ -5980,7 +5996,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
String userId, String userName, boolean permissionsBoundary,
|
||||
Map<String, String> principalTags, SessionMfa sessionMfa) {
|
||||
this(callerArn, principalArn, principalType, userId, userName, permissionsBoundary,
|
||||
principalTags, sessionMfa, null);
|
||||
principalTags, sessionMfa, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5995,18 +6011,21 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public record SessionMfa(boolean present, long authAgeSeconds) {
|
||||
}
|
||||
|
||||
/** A caller whose credentials say nothing about MFA, which is every long-term key. */
|
||||
/**
|
||||
* A caller signing with long-term credentials, whose credentials say nothing about MFA and
|
||||
* carry no {@code aws:TokenIssueTime}.
|
||||
*/
|
||||
public CallerArns(String callerArn, String principalArn, String principalType,
|
||||
String userId, String userName, boolean permissionsBoundary,
|
||||
Map<String, String> principalTags) {
|
||||
this(callerArn, principalArn, principalType, userId, userName, permissionsBoundary,
|
||||
principalTags, null);
|
||||
principalTags, null, null, null);
|
||||
}
|
||||
|
||||
/** Copy of this caller with what the session it signed with says about MFA. */
|
||||
public CallerArns withSessionMfa(SessionMfa mfa) {
|
||||
return new CallerArns(callerArn, principalArn, principalType, userId, userName,
|
||||
permissionsBoundary, principalTags, mfa, organization);
|
||||
permissionsBoundary, principalTags, mfa, organization, tokenIssueTime);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -6016,7 +6035,14 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
*/
|
||||
public CallerArns withOrganization(PrincipalOrganization.OrganizationContext context) {
|
||||
return context == null ? this : new CallerArns(callerArn, principalArn, principalType,
|
||||
userId, userName, permissionsBoundary, principalTags, sessionMfa, context);
|
||||
userId, userName, permissionsBoundary, principalTags, sessionMfa, context,
|
||||
tokenIssueTime);
|
||||
}
|
||||
|
||||
/** The same caller, signing with temporary credentials issued at {@code issuedAt}. */
|
||||
public CallerArns withTokenIssueTime(Instant issuedAt) {
|
||||
return new CallerArns(callerArn, principalArn, principalType, userId, userName,
|
||||
permissionsBoundary, principalTags, sessionMfa, organization, issuedAt);
|
||||
}
|
||||
|
||||
/** A caller without stored principal tags, including account-root and federated identities. */
|
||||
@@ -6079,6 +6105,10 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* key left absent is what makes a policy that matches on it fail to match, which is the
|
||||
* behaviour AWS documents for a key with no value, so omitting it is the enforcement, not
|
||||
* a gap in it.
|
||||
*
|
||||
* <p>{@code aws:TokenIssueTime} follows the same rule: it is "included in the request
|
||||
* context only when the principal uses temporary credentials to make the request" and "is
|
||||
* not present in AWS CLI, AWS API, or AWS SDK requests that are made using access keys".
|
||||
*/
|
||||
public void putPrincipalKeys(Map<String, List<String>> context) {
|
||||
context.put("aws:PrincipalArn", List.of(principalArn));
|
||||
@@ -6096,6 +6126,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
if (organization != null) {
|
||||
organization.putOrganizationKeys(context);
|
||||
}
|
||||
if (tokenIssueTime != null) {
|
||||
context.put("aws:TokenIssueTime", List.of(tokenIssueTime.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -6212,11 +6245,12 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
deleteSession(accessKeyId, session);
|
||||
return Optional.empty();
|
||||
}
|
||||
// Whatever identity the session resolves to, the MFA state belongs to the credentials
|
||||
// rather than to the identity, so it is attached once here instead of at each of the
|
||||
// identities below.
|
||||
// Whatever identity the session resolves to, the MFA state and the issue time belong
|
||||
// to the credentials rather than to the identity, so they are attached once here
|
||||
// instead of at each of the identities below.
|
||||
CallerArns.SessionMfa mfa = sessionMfa(session);
|
||||
return callerForSession(session).map(caller -> caller.withSessionMfa(mfa));
|
||||
return callerForSession(session)
|
||||
.map(caller -> caller.withSessionMfa(mfa).withTokenIssueTime(session.getIssuedAt()));
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
|
||||
@@ -18,6 +18,11 @@ public class SessionCredential {
|
||||
private String roleSessionName;
|
||||
private String assumedRoleId;
|
||||
private Instant expiration;
|
||||
/**
|
||||
* When the credentials were issued, the request's {@code aws:TokenIssueTime}. Null for a
|
||||
* session stored before Floci recorded it.
|
||||
*/
|
||||
private Instant issuedAt;
|
||||
/** Inline session policy passed to AssumeRole/GetFederationToken — further restricts role policies. */
|
||||
private String sessionPolicyDocument;
|
||||
/**
|
||||
@@ -137,6 +142,9 @@ public class SessionCredential {
|
||||
public Instant getExpiration() { return expiration; }
|
||||
public void setExpiration(Instant expiration) { this.expiration = expiration; }
|
||||
|
||||
public Instant getIssuedAt() { return issuedAt; }
|
||||
public void setIssuedAt(Instant issuedAt) { this.issuedAt = issuedAt; }
|
||||
|
||||
public String getSessionPolicyDocument() { return sessionPolicyDocument; }
|
||||
public void setSessionPolicyDocument(String sessionPolicyDocument) { this.sessionPolicyDocument = sessionPolicyDocument; }
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.Base64Url;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.FileTrees;
|
||||
import io.floci.aws.core.common.IamConditionContextResolver;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
@@ -1745,6 +1746,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
* whether S3 evaluates it or the enforcement filter does. For a role session
|
||||
* {@code aws:PrincipalArn} is the role's ARN. {@code requestConditions}, when present, are the keys
|
||||
* the request itself offers, such as an object creation's tags and Object Lock settings.
|
||||
* {@code aws:CurrentTime} and {@code aws:EpochTime} are there as on every request.
|
||||
*/
|
||||
private static Map<String, List<String>> bucketPolicyContext(CallerArns caller,
|
||||
Map<String, List<String>> requestConditions) {
|
||||
@@ -1753,6 +1755,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
context.putAll(requestConditions);
|
||||
}
|
||||
context.put("aws:PrincipalIsAWSService", List.of("false"));
|
||||
IamConditionContextResolver.putRequestTime(context);
|
||||
if (caller != null) {
|
||||
caller.putPrincipalKeys(context);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user