mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sts): pass transitive session tags into a chained role session (#9308)
A session tag never reached the session assumed from it, so the opt-in AWS publishes for role chaining was missing: "if you set a tag key as transitive, the corresponding key and value passes to subsequent sessions in a role chain". All three operations that mint a tagged session now read the keys where AWS publishes them, the request's TransitiveTagKeys member, the assertion's multivalued TransitiveTagKeys attribute and the token's transitive_tag_keys claim in either published format, and GetFederationToken declares none because its credentials cannot assume a role at all. A session assumed from one carrying transitive tags takes those tags as its own, under the ones its own request passes, and keeps their keys transitive so the next link inherits them too. A role's own tags never pass down. A session tag whose key the calling session passes down is refused, which AWS publishes as the operation failing without saying what it answers, so the code is the one this operation already gives the other rule that turns on the caller being a session and the sentence is this package's own. The inherited tags count against the session token size like any other state the session carries, while the keys cost nothing. sts:TransitiveTagKeys now reaches the role's trust policy with the keys the request names, beside sts:RoleSessionName and sts:ExternalId there. It stays out of the caller's identity-policy context, which is filled from the generated request-condition-keys.json, and that file does not list the key for the action. One rule of the chain is left and the docs say why: a transitive tag replaces the assumed role's matching ResourceTag value, and Floci publishes no aws:ResourceTag for the role in either evaluation, so there is nothing yet for it to replace. An inherited tag needs the role's tagging permission as much as a passed one does, so the trust policy is read for sts:TagSession when the calling session passes any tag down, with the request carrying none: the guide lists "incoming transitive session tags" among the tags a principal carries, and where a base role's own tags are passed on by STS "the trust policy must also allow sts:TagSession". The caller's own policy is read for a request that carries Tags, which the generated tagging table keys on, so a chain of inherited tags alone is held to the role's permission and not to the calling session's, where AWS reads both; the docs say so.
This commit is contained in:
1 parent
014256293c
commit
adb5b9edc0
13 files changed
+732
-58
No files matched your search
@@ -11,11 +11,14 @@ import java.util.Map;
|
||||
* @param audiences the {@code aud} claim, normalized to a list (JWT allows string or array)
|
||||
* @param principalTags the session tags the token passes, from either of the two claim formats
|
||||
* AWS publishes for them, and empty when it passes none
|
||||
* @param transitiveTagKeys the keys among those tags the token asks to set as transitive, from
|
||||
* either format, and empty when it asks for none
|
||||
*/
|
||||
public record WebIdentityToken(String issuer, String subject, List<String> audiences,
|
||||
Map<String, String> principalTags) {
|
||||
Map<String, String> principalTags,
|
||||
List<String> transitiveTagKeys) {
|
||||
|
||||
public WebIdentityToken(String issuer, String subject, List<String> audiences) {
|
||||
this(issuer, subject, audiences, Map.of());
|
||||
this(issuer, subject, audiences, Map.of(), List.of());
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,12 @@ public class WebIdentityTokenVerifier {
|
||||
/** The namespace AWS publishes a token's session tags under, whichever format they take. */
|
||||
private static final String TAGS_CLAIM = "https://aws.amazon.com/tags";
|
||||
private static final String FLATTENED_TAG_CLAIM = TAGS_CLAIM + "/principal_tags/";
|
||||
/** The nested member, and the flattened claim, naming the keys to set as transitive. */
|
||||
private static final String TRANSITIVE_KEYS = "transitive_tag_keys";
|
||||
private static final String FLATTENED_TRANSITIVE_CLAIM = TAGS_CLAIM + "/" + TRANSITIVE_KEYS;
|
||||
/** What a transitive tag key claim of any other shape is refused with, Floci's own wording. */
|
||||
static final String TRANSITIVE_KEYS_MALFORMED =
|
||||
"The web identity token's transitive tag keys do not read as an array of strings";
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@@ -126,7 +132,8 @@ public class WebIdentityTokenVerifier {
|
||||
throw new InvalidTokenException("The web identity token has no subject claim");
|
||||
}
|
||||
|
||||
return new WebIdentityToken(issuer, subject, audiences, readPrincipalTags(claims));
|
||||
return new WebIdentityToken(issuer, subject, audiences, readPrincipalTags(claims),
|
||||
readTransitiveTagKeys(claims));
|
||||
}
|
||||
|
||||
private void verifyValidityWindow(JsonNode claims) throws InvalidTokenException {
|
||||
@@ -172,9 +179,7 @@ public class WebIdentityTokenVerifier {
|
||||
* published nested example writes each as an array holding that one value, so a single-element
|
||||
* array is read as the value it holds. Anything else is a multi-valued session tag, which "AWS
|
||||
* STS does not support", and the token is refused rather than verified with that tag dropped:
|
||||
* a tag a policy would have been evaluated with cannot go missing quietly. The transitive tag
|
||||
* keys the same namespace carries are not read: nothing here passes a tag into a later session
|
||||
* yet, and a key stored unread would say otherwise.
|
||||
* a tag a policy would have been evaluated with cannot go missing quietly.
|
||||
*/
|
||||
private Map<String, String> readPrincipalTags(JsonNode claims) throws InvalidTokenException {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
@@ -194,6 +199,46 @@ public class WebIdentityTokenVerifier {
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys the token asks to set as transitive, in the order the claim lists them, read in
|
||||
* both formats as the tags are: nested, where "transitive tag keys are represented in an array
|
||||
* under the transitive_tag_keys key" of the same namespace, and flattened, "in a single claim
|
||||
* as an array of strings with the prefix https://aws.amazon.com/tags/transitive_tag_keys".
|
||||
*
|
||||
* <p>Both published examples write an array, and a lone string is read as the one key it names
|
||||
* rather than refused, which is the same reading {@link #singleValue} gives a tag written
|
||||
* without its array. A claim of any other shape, or one holding something that is not a
|
||||
* string, is refused rather than read past: a key dropped quietly would leave a chained
|
||||
* session short of a tag whose absence a policy can turn on. Which keys select a tag is the
|
||||
* session's business, not this one's.
|
||||
*/
|
||||
private static List<String> readTransitiveTagKeys(JsonNode claims) throws InvalidTokenException {
|
||||
List<String> keys = new ArrayList<>();
|
||||
readTransitiveKeysInto(claims.path(TAGS_CLAIM).path(TRANSITIVE_KEYS), keys);
|
||||
readTransitiveKeysInto(claims.path(FLATTENED_TRANSITIVE_CLAIM), keys);
|
||||
return keys;
|
||||
}
|
||||
|
||||
private static void readTransitiveKeysInto(JsonNode claim, List<String> keys)
|
||||
throws InvalidTokenException {
|
||||
if (claim.isMissingNode() || claim.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (claim.isTextual()) {
|
||||
keys.add(claim.asText());
|
||||
return;
|
||||
}
|
||||
if (!claim.isArray()) {
|
||||
throw new InvalidTokenException(TRANSITIVE_KEYS_MALFORMED);
|
||||
}
|
||||
for (JsonNode key : claim) {
|
||||
if (!key.isTextual()) {
|
||||
throw new InvalidTokenException(TRANSITIVE_KEYS_MALFORMED);
|
||||
}
|
||||
keys.add(key.asText());
|
||||
}
|
||||
}
|
||||
|
||||
/** The one value a tag claim carries, written as a string or as an array holding it alone. */
|
||||
private static String singleValue(String key, JsonNode value) throws InvalidTokenException {
|
||||
if (value.isTextual()) {
|
||||
|
||||
@@ -5198,8 +5198,18 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* tag keys", and a second such tag "replaces the first tag. A second tag is not added." Stored
|
||||
* as two they would reach a policy as one key with the other's value dropped, and a Deny keyed
|
||||
* on that value would stop matching.
|
||||
*
|
||||
* <p>{@code transitiveTagKeys} are the keys among them the request asked to set as transitive,
|
||||
* which is what {@link #callerTransitiveSessionTags} then carries into a session assumed from
|
||||
* this one. Only a key naming one of these tags selects anything: a key naming none carries no
|
||||
* tag onward, there being no tag of that key in the session to carry, and the keys are matched
|
||||
* against the tags the same way a tag key is compared anywhere else here, without regard to
|
||||
* case. The spellings kept are the ones the request sent, as the tag keys themselves are, and a
|
||||
* key named twice, which a chained request can do by naming one it already inherits, is one
|
||||
* key.
|
||||
*/
|
||||
public void recordSessionTags(String sessionAccessKeyId, Map<String, String> tags) {
|
||||
public void recordSessionTags(String sessionAccessKeyId, Map<String, String> tags,
|
||||
List<String> transitiveTagKeys) {
|
||||
if (sessionAccessKeyId == null || tags == null || tags.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
@@ -5208,12 +5218,55 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
oneTagPerKey.keySet().removeIf(stored -> stored.equalsIgnoreCase(key));
|
||||
oneTagPerKey.put(key, value);
|
||||
});
|
||||
List<String> transitive = new ArrayList<>();
|
||||
if (transitiveTagKeys != null) {
|
||||
for (String key : transitiveTagKeys) {
|
||||
if (oneTagPerKey.keySet().stream().anyMatch(key::equalsIgnoreCase)
|
||||
&& transitive.stream().noneMatch(key::equalsIgnoreCase)) {
|
||||
transitive.add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
sessions.get(sessionAccessKeyId).ifPresent(session -> {
|
||||
session.setSessionTags(oneTagPerKey);
|
||||
session.setTransitiveTagKeys(transitive);
|
||||
sessions.put(sessionAccessKeyId, session);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The transitive session tags the credentials {@code accessKeyId} carry, which a session
|
||||
* assumed from them inherits: "if you used temporary credentials to perform this operation,
|
||||
* the new session inherits any transitive session tags from the calling session".
|
||||
*
|
||||
* <p>Only session tags are ever in it. A role's own tags are never transitive, which the guide
|
||||
* states outright, "you cannot set role tags as transitive", and shows: the second session of
|
||||
* its chain inherits the first session's two transitive tags and not the tag attached to the
|
||||
* role it assumed. A long-term key carries none, there being no session behind it, and so does
|
||||
* a session that named no transitive key.
|
||||
*/
|
||||
public Map<String, String> callerTransitiveSessionTags(String accessKeyId) {
|
||||
return findSessionForCallerContext(accessKeyId)
|
||||
.map(IamService::transitiveSessionTags)
|
||||
.orElseGet(Map::of);
|
||||
}
|
||||
|
||||
private static Map<String, String> transitiveSessionTags(SessionCredential session) {
|
||||
if (session.getTransitiveTagKeys().isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
Set<String> transitive = session.getTransitiveTagKeys().stream()
|
||||
.map(key -> key.toLowerCase(Locale.ROOT))
|
||||
.collect(Collectors.toSet());
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
session.getSessionTags().forEach((key, value) -> {
|
||||
if (transitive.contains(key.toLowerCase(Locale.ROOT))) {
|
||||
tags.put(key, value);
|
||||
}
|
||||
});
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the credentials {@code accessKeyId} names last authenticated with an MFA device, empty
|
||||
* for a session minted without one and for every long-term key. A session assumed from such a
|
||||
|
||||
@@ -42,6 +42,14 @@ final class SAMLAssertionVerifier {
|
||||
/** What a session tag attribute naming no key is refused with, this package's own wording. */
|
||||
static final String TAG_KEY_MISSING =
|
||||
"A PrincipalTag attribute must name the tag key after the colon.";
|
||||
/**
|
||||
* What an attribute naming the keys to set as transitive is named: "include another Attribute
|
||||
* element with the Name attribute set to
|
||||
* https://aws.amazon.com/SAML/Attributes/TransitiveTagKeys", which unlike a tag attribute "is
|
||||
* an optional multivalued attribute", so each of its values is one key.
|
||||
*/
|
||||
private static final String TRANSITIVE_TAG_KEYS =
|
||||
"https://aws.amazon.com/SAML/Attributes/TransitiveTagKeys";
|
||||
// The pattern AWS names in its error for a RoleSessionName attribute outside it.
|
||||
static final String ROLE_SESSION_NAME_INVALID = "RoleSessionName in AuthnResponse must match [a-zA-Z_0-9+=,.@-]{2,64}";
|
||||
private static final Pattern ROLE_SESSION_NAME_PATTERN = Pattern.compile("[a-zA-Z_0-9+=,.@-]{2,64}");
|
||||
@@ -52,12 +60,14 @@ final class SAMLAssertionVerifier {
|
||||
private SAMLAssertionVerifier() {}
|
||||
|
||||
/**
|
||||
* {@code roleSessionName} is null when the assertion carries no RoleSessionName attribute, and
|
||||
* {@code principalTags} is empty when it carries no session tags.
|
||||
* {@code roleSessionName} is null when the assertion carries no RoleSessionName attribute,
|
||||
* {@code principalTags} is empty when it carries no session tags, and
|
||||
* {@code transitiveTagKeys} is empty when it asks for none to be transitive, which is AWS's
|
||||
* default for a session.
|
||||
*/
|
||||
record Verified(String issuer, String subject, String subjectType, String nameQualifier,
|
||||
Instant expiration, List<RolePair> roles, String roleSessionName,
|
||||
Map<String, String> principalTags) {}
|
||||
Map<String, String> principalTags, List<String> transitiveTagKeys) {}
|
||||
record RolePair(String roleArn, String principalArn) {}
|
||||
|
||||
static Verified verify(String encoded, SAMLProvider provider, Instant now) throws InvalidAssertionException {
|
||||
@@ -190,7 +200,7 @@ final class SAMLAssertionVerifier {
|
||||
String nameQualifier = Base64.getEncoder().encodeToString(
|
||||
Digests.sha1((issuer + provider.getArn()).getBytes(StandardCharsets.UTF_8)));
|
||||
return new Verified(issuer, name, subjectType, nameQualifier, notOnOrAfter, roles,
|
||||
roleSessionName, principalTags(attributes));
|
||||
roleSessionName, principalTags(attributes), transitiveTagKeys(attributes));
|
||||
} catch (InvalidAssertionException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
@@ -255,6 +265,30 @@ final class SAMLAssertionVerifier {
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys the assertion asks to set as transitive, in the order its values read.
|
||||
*
|
||||
* <p>This attribute is the one AWS publishes as multivalued, each value naming one key, so
|
||||
* several values are read rather than refused as a session tag's would be, and an attribute
|
||||
* carrying none asks for nothing. The value is taken as the assertion signed it, as a tag key
|
||||
* attribute's is. Which of them select a tag is the session's business, not this one's: a key
|
||||
* naming no session tag carries no tag onward, and nothing published says AWS refuses it.
|
||||
*/
|
||||
private static List<String> transitiveTagKeys(NodeList attributes) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (int i = 0; i < attributes.getLength(); i++) {
|
||||
Element attribute = (Element) attributes.item(i);
|
||||
if (!TRANSITIVE_TAG_KEYS.equals(attribute.getAttribute("Name"))) {
|
||||
continue;
|
||||
}
|
||||
NodeList values = attribute.getElementsByTagNameNS(SAML, "AttributeValue");
|
||||
for (int j = 0; j < values.getLength(); j++) {
|
||||
keys.add(values.item(j).getTextContent());
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/** What an attribute carrying no single value for its tag is refused with, Floci's wording. */
|
||||
static String singleValueRequired(String key) {
|
||||
return "Session tag " + key + " must carry exactly one AttributeValue:"
|
||||
|
||||
@@ -42,6 +42,7 @@ import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -147,7 +148,8 @@ public class StsQueryHandler {
|
||||
// A session whose token will not fit is refused before anything is minted, on every
|
||||
// operation that carries state into the token it mints.
|
||||
if (PACKED_SESSION_OPERATIONS.contains(action)) {
|
||||
AssembledSession session = assembledSession(action, params);
|
||||
AssembledSession session = assembledSession(action, params,
|
||||
inheritedTransitiveTags(action));
|
||||
if (session.tooLarge()) {
|
||||
return session.refusal();
|
||||
}
|
||||
@@ -198,8 +200,13 @@ public class StsQueryHandler {
|
||||
String callerAccountId = regionResolver.getAccountId();
|
||||
String accountId = AwsArnUtils.accountOrDefault(roleArn, callerAccountId);
|
||||
|
||||
// What the calling session passes down to this one, which is nothing unless it was itself
|
||||
// minted with transitive tags. It is read before the trust policy, which has to permit
|
||||
// tagging the session for these as well as for the tags the request passes.
|
||||
Map<String, String> inheritedTags = inheritedTransitiveTags("AssumeRole");
|
||||
|
||||
IamRole role = iamService.findRole(accountId, roleName).orElse(null);
|
||||
Response trustDenied = enforceTrustPolicy(roleArn, role, accountId, params, mfa);
|
||||
Response trustDenied = enforceTrustPolicy(roleArn, role, accountId, params, mfa, inheritedTags);
|
||||
if (trustDenied != null) {
|
||||
return trustDenied;
|
||||
}
|
||||
@@ -215,6 +222,14 @@ public class StsQueryHandler {
|
||||
AwsNamespaces.STS, 400);
|
||||
}
|
||||
|
||||
// The collision is read beside the other rule that turns on the caller being a session,
|
||||
// the chaining cap above, and after the role has allowed the assume: nothing published
|
||||
// says which of the two refusals AWS reaches first.
|
||||
Response collision = refuseInheritedTagCollision(inheritedTags, params);
|
||||
if (collision != null) {
|
||||
return collision;
|
||||
}
|
||||
|
||||
String sessionRoleArn = canonicalRoleArn(role, accountId, roleName);
|
||||
String assumedRoleArn = assumedRoleArn(sessionRoleArn, accountId, roleName, sessionName);
|
||||
String assumedRoleId = assumedRoleId(role, sessionName);
|
||||
@@ -228,7 +243,15 @@ public class StsQueryHandler {
|
||||
iamService.registerSession(
|
||||
accessKeyId, secretKey, sessionToken, sessionRoleArn, expiration, sessionPolicy, callerAccountId,
|
||||
sessionName, assumedRoleId);
|
||||
iamService.recordSessionTags(accessKeyId, requestTags(params));
|
||||
// The tags inherited from the calling session are this session's own from here on, under
|
||||
// the ones the request passes, and the keys they came in under stay transitive so the next
|
||||
// session in the chain inherits them too: in the guide's chain the second session's
|
||||
// inherited tags "continue to be transitive" without the second request naming them.
|
||||
Map<String, String> sessionTags = new LinkedHashMap<>(inheritedTags);
|
||||
sessionTags.putAll(requestTags(params));
|
||||
List<String> transitiveKeys = new ArrayList<>(inheritedTags.keySet());
|
||||
transitiveKeys.addAll(transitiveTagKeys(params));
|
||||
iamService.recordSessionTags(accessKeyId, sessionTags, transitiveKeys);
|
||||
|
||||
String result = new XmlBuilder()
|
||||
.raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration))
|
||||
@@ -236,7 +259,7 @@ public class StsQueryHandler {
|
||||
.elem("Arn", assumedRoleArn)
|
||||
.elem("AssumedRoleId", assumedRoleId)
|
||||
.end("AssumedRoleUser")
|
||||
.raw(sessionSizeXml("AssumeRole", params))
|
||||
.raw(sessionSizeXml("AssumeRole", assembledSession("AssumeRole", params, inheritedTags)))
|
||||
.build();
|
||||
return Response.ok(AwsQueryResponse.envelope("AssumeRole", AwsNamespaces.STS, result)).build();
|
||||
}
|
||||
@@ -321,10 +344,12 @@ public class StsQueryHandler {
|
||||
* the two operations that mint a session out of the caller's own credentials declare no state
|
||||
* at all, so a parameter STS would ignore cannot be made to cost a caller anything.
|
||||
*
|
||||
* <p>{@code carriedTags} is for the tags an operation takes from its message instead: they are
|
||||
* state the session carries like any other, "session policies and session tags add to the
|
||||
* session token size", and they are known only once the assertion or the token they arrived in
|
||||
* has been verified, which is where those two operations read this.
|
||||
* <p>{@code carriedTags} is for the tags that reach a session other than as a member of its
|
||||
* own request: they are state it carries like any other, "session policies and session tags
|
||||
* add to the session token size". For the two operations that take their tags from a message
|
||||
* they are known only once the assertion or the token has been verified, which is where those
|
||||
* read this; for a chained assume they are the tags inherited from the calling session, which
|
||||
* the new session carries as its own and so pays for in the same way.
|
||||
*/
|
||||
private AssembledSession assembledSession(String action, MultivaluedMap<String, String> params) {
|
||||
return assembledSession(action, params, Map.of());
|
||||
@@ -484,7 +509,8 @@ public class StsQueryHandler {
|
||||
* principal does wherever else Floci builds it.
|
||||
*/
|
||||
private Response enforceTrustPolicy(String roleArn, IamRole role, String roleAccountId,
|
||||
MultivaluedMap<String, String> params, MfaOutcome mfa) {
|
||||
MultivaluedMap<String, String> params, MfaOutcome mfa,
|
||||
Map<String, String> inheritedTags) {
|
||||
String callerAccount = regionResolver.getAccountId();
|
||||
IamService.CallerArns caller = iamService.resolveCallerArns(callerAccessKeyId())
|
||||
.orElseGet(() -> IamService.CallerArns.accountRoot(
|
||||
@@ -518,15 +544,27 @@ public class StsQueryHandler {
|
||||
if (externalId != null) {
|
||||
trustContext.put("sts:ExternalId", List.of(externalId));
|
||||
}
|
||||
// "Compares the transitive session tag keys in the request with those specified in the
|
||||
// policy", so it is the keys this request names rather than the ones it inherits, and a
|
||||
// request naming none leaves the key absent, which is what the guide's own policy example
|
||||
// tests with a Null condition on it.
|
||||
List<String> requestedTransitiveKeys = transitiveTagKeys(params);
|
||||
if (!requestedTransitiveKeys.isEmpty()) {
|
||||
trustContext.put("sts:TransitiveTagKeys", requestedTransitiveKeys);
|
||||
}
|
||||
if (!trustPolicyEvaluator.allows(role.getAssumeRolePolicyDocument(), caller, callerAccount,
|
||||
trustContext)) {
|
||||
return AwsQueryResponse.error("AccessDenied",
|
||||
"User: " + callerArn + " is not authorized to perform: sts:AssumeRole on resource: " + roleArn,
|
||||
AwsNamespaces.STS, 403);
|
||||
}
|
||||
// A request that carries session tags needs the trust policy's permission to carry them:
|
||||
// "for roles without this permission in the trust policy, the AssumeRole operation fails".
|
||||
if (!requestTags(params).isEmpty()
|
||||
// A session the request tags needs the trust policy's permission to carry them: "for roles
|
||||
// without this permission in the trust policy, the AssumeRole operation fails". The tags
|
||||
// the calling session passes down count as much, being session tags of the session this
|
||||
// mints: the guide lists "incoming transitive session tags" among the tags a principal
|
||||
// carries, and where a base role's tags are passed on by STS "the trust policy must also
|
||||
// allow sts:TagSession", the refusal naming that action as this one does.
|
||||
if ((!requestTags(params).isEmpty() || !inheritedTags.isEmpty())
|
||||
&& !trustPolicyEvaluator.allowsTagging(role.getAssumeRolePolicyDocument(), caller,
|
||||
callerAccount, trustContext)) {
|
||||
return AwsQueryResponse.error("AccessDenied", "User: " + callerArn
|
||||
@@ -707,8 +745,10 @@ public class StsQueryHandler {
|
||||
accessKeyId, secretKey, sessionToken, sessionRoleArn, expiration, sessionPolicy, callerAccountId,
|
||||
sessionName, assumedRoleId);
|
||||
// The token's own claims, this operation declaring no Tags member: a session tag reaches it
|
||||
// "in the JSON Web Token (JWT) when you submit the AssumeRoleWithWebIdentity request".
|
||||
iamService.recordSessionTags(accessKeyId, carriedTags);
|
||||
// "in the JSON Web Token (JWT) when you submit the AssumeRoleWithWebIdentity request", and
|
||||
// the keys to set as transitive arrive in the same namespace.
|
||||
iamService.recordSessionTags(accessKeyId, carriedTags,
|
||||
verified != null ? verified.transitiveTagKeys() : List.of());
|
||||
|
||||
String result = new XmlBuilder()
|
||||
.raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration))
|
||||
@@ -745,7 +785,8 @@ public class StsQueryHandler {
|
||||
|
||||
/** The claims of a token Floci issued and verified, used to fill the response accurately. */
|
||||
private record VerifiedWebIdentity(String issuer, String subject, String audience,
|
||||
Map<String, String> principalTags) {}
|
||||
Map<String, String> principalTags,
|
||||
List<String> transitiveTagKeys) {}
|
||||
|
||||
private record WebIdentityOutcome(VerifiedWebIdentity verified, IamRole role, Response denial) {
|
||||
|
||||
@@ -829,7 +870,7 @@ public class StsQueryHandler {
|
||||
// verify() already required the audience list to contain STS_AUDIENCE.
|
||||
return WebIdentityOutcome.allow(
|
||||
new VerifiedWebIdentity(claims.issuer(), claims.subject(), STS_AUDIENCE,
|
||||
claims.principalTags()), role.get());
|
||||
claims.principalTags(), claims.transitiveTagKeys()), role.get());
|
||||
}
|
||||
|
||||
private static String assumedRoleId(IamRole role, String sessionName) {
|
||||
@@ -950,8 +991,10 @@ public class StsQueryHandler {
|
||||
|
||||
iamService.registerSession(accessKeyId, secretKey, sessionToken, sessionRoleArn, expiration, null,
|
||||
callerAccountId, sessionName, assumedRoleId);
|
||||
// The assertion's own attributes, this operation declaring no Tags member either.
|
||||
iamService.recordSessionTags(accessKeyId, verified.principalTags());
|
||||
// The assertion's own attributes, this operation declaring no Tags member either, the
|
||||
// keys to set as transitive arriving as an attribute of their own beside the tags.
|
||||
iamService.recordSessionTags(accessKeyId, verified.principalTags(),
|
||||
verified.transitiveTagKeys());
|
||||
String result = new XmlBuilder()
|
||||
.raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration))
|
||||
.start("AssumedRoleUser").elem("Arn", assumedRoleArn).elem("AssumedRoleId", assumedRoleId).end("AssumedRoleUser")
|
||||
@@ -1005,7 +1048,11 @@ public class StsQueryHandler {
|
||||
// minted it, so the caller is recorded with it.
|
||||
iamService.registerIssuedSession(accessKeyId, secretKey, sessionToken, federatedUserArn, expiration,
|
||||
sessionPolicy, accountId, callerAccessKeyId());
|
||||
iamService.recordSessionTags(accessKeyId, requestTags(params));
|
||||
// None of them is transitive: this operation declares no TransitiveTagKeys member, and the
|
||||
// guide says why, that "you can't set session tags as transitive when you use
|
||||
// GetFederationToken, because you can't use the temporary credentials" it returns to
|
||||
// assume a role.
|
||||
iamService.recordSessionTags(accessKeyId, requestTags(params), List.of());
|
||||
|
||||
String result = new XmlBuilder()
|
||||
.raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration))
|
||||
@@ -1427,6 +1474,66 @@ public class StsQueryHandler {
|
||||
return claims;
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys the request's own {@code TransitiveTagKeys} member names, in the order their
|
||||
* indexes read, each as it was sent: the model holds every one of them to a minimum length of
|
||||
* 1, so by here none is absent, and holds the list to 50 members of 128 characters apiece.
|
||||
*
|
||||
* <p>A key naming no tag of this request selects nothing, there being no tag of that key for a
|
||||
* later session to inherit, and nothing published says AWS refuses it.
|
||||
*/
|
||||
private static List<String> transitiveTagKeys(MultivaluedMap<String, String> params) {
|
||||
return AwsQueryLists.members(params, "TransitiveTagKeys", null).stream()
|
||||
.map(member -> params.getFirst(member.prefix()))
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* The transitive session tags the caller passes down to the session this request mints: "if
|
||||
* you used temporary credentials to perform this operation, the new session inherits any
|
||||
* transitive session tags from the calling session". A long-term key carries none, and neither
|
||||
* does a session that set none transitive, which is AWS's default.
|
||||
*
|
||||
* <p>Only the operation that declares {@code TransitiveTagKeys} chains them, which is the
|
||||
* generated request shapes answering which one that is rather than this naming it: the rule is
|
||||
* published on that operation's own {@code Tags} member, and the other operation taking a
|
||||
* {@code Tags} member mints a session that "can't" be used to assume a role at all.
|
||||
*/
|
||||
private Map<String, String> inheritedTransitiveTags(String action) {
|
||||
if (!StsRequestShapes.hasMember(action, "TransitiveTagKeys")) {
|
||||
return Map.of();
|
||||
}
|
||||
return iamService.callerTransitiveSessionTags(callerAccessKeyId());
|
||||
}
|
||||
|
||||
/**
|
||||
* The refusal for a session tag whose key the calling session already passes down: "if you
|
||||
* pass a session tag with the same key as an inherited tag, the operation fails". The key is
|
||||
* matched without regard to case, as a tag key is everywhere else here, so a request passing
|
||||
* {@code department} cannot sidestep an inherited {@code Department} either.
|
||||
*
|
||||
* <p>That it fails is published; what AWS answers is not. The code is the one this operation
|
||||
* already gives the other rule that turns on the caller being a session, the chaining cap, and
|
||||
* the sentence is this package's own, naming the key as the request spelled it. Taking the
|
||||
* request's tag instead would have let a chain quietly drop a tag a policy turns on, which the
|
||||
* inheritance exists to carry.
|
||||
*/
|
||||
private static Response refuseInheritedTagCollision(Map<String, String> inheritedTags,
|
||||
MultivaluedMap<String, String> params) {
|
||||
if (inheritedTags.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
for (String key : requestTags(params).keySet()) {
|
||||
if (inheritedTags.keySet().stream().anyMatch(inherited -> inherited.equalsIgnoreCase(key))) {
|
||||
return AwsQueryResponse.error("ValidationError", "Session tag " + key
|
||||
+ " cannot be passed because the calling session carries it as a transitive"
|
||||
+ " tag.", AwsNamespaces.STS, 400);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code jwtAlgorithmType} publishes a fixed length of 5 and no enum, and the reference names
|
||||
* the two values that length is for, so the value set is documented where the length is
|
||||
|
||||
@@ -89,6 +89,15 @@ public class SessionCredential {
|
||||
* Floci recorded them.
|
||||
*/
|
||||
private Map<String, String> sessionTags = new LinkedHashMap<>();
|
||||
/**
|
||||
* The keys among those session tags the session was asked to set as transitive, in the order
|
||||
* the request named them: "if you set a tag key as transitive, the corresponding key and value
|
||||
* passes to subsequent sessions in a role chain". A session assumed from this one inherits
|
||||
* those tags and keeps their keys transitive, so a chain carries them all the way down. Empty
|
||||
* for a session that named none, which is AWS's default: "if you choose not to specify a
|
||||
* transitive tag key, then no tags are passed from this session to any subsequent sessions".
|
||||
*/
|
||||
private List<String> transitiveTagKeys = new ArrayList<>();
|
||||
|
||||
public SessionCredential() {}
|
||||
|
||||
@@ -220,4 +229,13 @@ public class SessionCredential {
|
||||
public void setSessionTags(Map<String, String> sessionTags) {
|
||||
this.sessionTags = sessionTags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(sessionTags);
|
||||
}
|
||||
|
||||
public List<String> getTransitiveTagKeys() {
|
||||
return transitiveTagKeys;
|
||||
}
|
||||
|
||||
public void setTransitiveTagKeys(List<String> transitiveTagKeys) {
|
||||
this.transitiveTagKeys = transitiveTagKeys == null
|
||||
? new ArrayList<>() : new ArrayList<>(transitiveTagKeys);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user