mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 17:58:05 -05:00
fix(iam): read a federated assume's tags in its trust policy, and sts:TagSession from the generated tables (#9721)
Under enforcement a role whose trust policy conditions AssumeRoleWithSAML or AssumeRoleWithWebIdentity on aws:RequestTag/<key> or aws:TagKeys refused every tagged assume: neither trust policy saw the tags the assertion or the token passes, which the Service Reference gives both actions and the IAM user guide's SAML ABAC tutorial's trust policy reads. And all three assumes named sts:TagSession by hand, through AssumeRolePolicyEvaluator.TAG_SESSION_ACTION, with the assertion's PrincipalTag attributes and the token's tags claim named by hand too. The IAM user guide's page on session tags says where an identity provider passes the tags, "include the Attribute element with the Name attribute set to https://aws.amazon.com/SAML/Attributes/PrincipalTag:{TagKey}" and "the nested claim format uses a structure within the https://aws.amazon.com/tags namespace in the JWT", and what passing them needs, "trust policies for all roles connected to the identity provider (IdP) passing tags must have the sts:TagSession permission". tools/aws/fetch_sts_session_tags.py vendors it into tools/aws/sources/sts-session-tags.json, and regen_iam_authorized_actions.py gives both operations a companion naming the attribute or the claim, sts:TagSession, and the reference's aws:RequestTag/${TagKey}, where the reference's action of the operation's name takes that key. The SAML and web identity trust policies now read the tags as aws:RequestTag/<key> and aws:TagKeys, written as an AssumeRole's are, and check the companion's action for them; the assertion and token verifiers read the companion's attribute and claim. AssumeRole's trust check takes the tagging action the tag-on-create table gives it. TAG_SESSION_ACTION and the allowsTagging methods are gone. Closes #6640.
This commit is contained in:
1 parent
b7a12c146b
commit
5ed7187e28
21 files changed
+603
-102
No files matched your search
@@ -469,6 +469,24 @@ public class IamConditionContextResolver {
|
||||
.map(TagOnCreate::requestTagContexts).orElse(List.of()));
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags a request passes as the keys it carries them as, {@code aws:RequestTag/<key>} and {@code aws:TagKeys},
|
||||
* written as the caller's own policies read a tagged create's: for a trust policy an operation reads itself with
|
||||
* tags that arrive outside the request's members, an assertion's or a token's. Empty for no tags.
|
||||
*/
|
||||
public static Map<String, List<String>> requestTagKeys(Map<String, String> tags) {
|
||||
return tags == null || tags.isEmpty() ? Map.of() : TagOnCreate.requestTagContext(tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tagging action the generated tag-on-create table gives {@code operation} for the tags its request passes
|
||||
* ({@code sts:TagSession} for an AssumeRole), or null for an operation it gives none.
|
||||
*/
|
||||
public static String taggingAction(String operation) {
|
||||
TagOnCreateMembers.Entry entry = TagOnCreateMembers.of(operation);
|
||||
return entry == null ? null : entry.tagging();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the request names {@code member}: a Query form's parameter, or a JSON body's top-level member.
|
||||
*/
|
||||
|
||||
@@ -525,6 +525,13 @@ final class TagOnCreate {
|
||||
return contexts;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code tags} as the keys a request passing them carries, {@code aws:RequestTag/<key>} and {@code aws:TagKeys}.
|
||||
*/
|
||||
static Map<String, List<String>> requestTagContext(Map<String, String> tags) {
|
||||
return requestTagContext(new TagSet(null, tags));
|
||||
}
|
||||
|
||||
private static Map<String, List<String>> requestTagContext(TagSet set) {
|
||||
Map<String, List<String>> context = new HashMap<>();
|
||||
set.tags().forEach((key, value) -> context.put("aws:RequestTag/" + key, List.of(value)));
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.floci.aws.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.services.iam.IamAuthorizedActions;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -38,8 +39,14 @@ public class WebIdentityTokenVerifier {
|
||||
|
||||
/** Tolerance for clock drift between Floci and a token minted elsewhere. */
|
||||
private static final long CLOCK_SKEW_SECONDS = 60;
|
||||
/** 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";
|
||||
/**
|
||||
* The namespace AWS publishes a token's session tags under, whichever format they take: the claim the generated
|
||||
* {@code iam-authorized-actions.json} gives AssumeRoleWithWebIdentity's session tags, from the IAM user guide's
|
||||
* "the nested claim format uses a structure within the https://aws.amazon.com/tags namespace in the JWT". None
|
||||
* where the file gives no such companion, and then a token passes no session tags.
|
||||
*/
|
||||
private static final String TAGS_CLAIM = IamAuthorizedActions.sessionTags("sts:AssumeRoleWithWebIdentity")
|
||||
.map(IamAuthorizedActions.Companion::carrier).orElse(null);
|
||||
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";
|
||||
@@ -205,6 +212,9 @@ public class WebIdentityTokenVerifier {
|
||||
*/
|
||||
private Map<String, String> readPrincipalTags(JsonNode claims) throws InvalidTokenException {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
if (TAGS_CLAIM == null) {
|
||||
return tags;
|
||||
}
|
||||
for (Map.Entry<String, JsonNode> tag : claims.path(TAGS_CLAIM).path("principal_tags").properties()) {
|
||||
tags.put(tag.getKey(), singleValue(tag.getKey(), tag.getValue()));
|
||||
}
|
||||
@@ -236,6 +246,9 @@ public class WebIdentityTokenVerifier {
|
||||
*/
|
||||
private static List<String> readTransitiveTagKeys(JsonNode claims) throws InvalidTokenException {
|
||||
List<String> keys = new ArrayList<>();
|
||||
if (TAGS_CLAIM == null) {
|
||||
return keys;
|
||||
}
|
||||
readTransitiveKeysInto(claims.path(TAGS_CLAIM).path(TRANSITIVE_KEYS), keys);
|
||||
readTransitiveKeysInto(claims.path(FLATTENED_TRANSITIVE_CLAIM), keys);
|
||||
return keys;
|
||||
|
||||
@@ -48,13 +48,6 @@ public class AssumeRolePolicyEvaluator {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AssumeRolePolicyEvaluator.class);
|
||||
private static final String ASSUME_ROLE_ACTION = "sts:AssumeRole";
|
||||
/**
|
||||
* What a trust policy has to allow beside the assume for the request's session tags to be
|
||||
* taken: "when using session tags, trust policies for all roles connected to the identity
|
||||
* provider passing tags must have the sts:TagSession permission. For roles without this
|
||||
* permission in the trust policy, the AssumeRole operation fails."
|
||||
*/
|
||||
public static final String TAG_SESSION_ACTION = "sts:TagSession";
|
||||
private static final Pattern ACCOUNT_ROOT_ARN =
|
||||
Pattern.compile("^arn:" + AwsArnUtils.PARTITION_REGEX + ":iam::(\\d{12}):root$");
|
||||
|
||||
@@ -104,22 +97,10 @@ public class AssumeRolePolicyEvaluator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the trust policy lets this caller tag the session it is assuming, which is the
|
||||
* {@link #TAG_SESSION_ACTION} permission read on the same document, with the same principals
|
||||
* and the same conditions as the assume itself.
|
||||
*/
|
||||
public boolean allowsTagging(String trustPolicyDocument, IamService.CallerArns caller,
|
||||
String callerAccount, Map<String, List<String>> requestContext) {
|
||||
TrustDecision decision = decide(TAG_SESSION_ACTION, trustPolicyDocument, caller, callerAccount,
|
||||
requestContext);
|
||||
return decision == TrustDecision.ALLOW || decision == TrustDecision.ALLOW_ACCOUNT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the trust policy lets this caller perform {@code action} on the role beside the assume: an action the
|
||||
* generated {@code iam-authorized-actions.json} gives the assume for a value it sets
|
||||
* ({@code sts:SetSourceIdentity}), read on the same document, with the same principals and the same conditions
|
||||
* as the assume itself.
|
||||
* Whether the trust policy lets this caller perform {@code action} on the role beside the assume: an action a
|
||||
* generated table gives the assume for a value it sets, {@code sts:SetSourceIdentity} from
|
||||
* {@code iam-authorized-actions.json} or {@code sts:TagSession} from {@code iam-tag-on-create.json}, read on the
|
||||
* same document, with the same principals and the same conditions as the assume itself.
|
||||
*/
|
||||
public boolean allowsCompanion(String action, String trustPolicyDocument, IamService.CallerArns caller,
|
||||
String callerAccount, Map<String, List<String>> requestContext) {
|
||||
@@ -447,7 +428,8 @@ public class AssumeRolePolicyEvaluator {
|
||||
|
||||
/**
|
||||
* True if the statement's action element applies to {@code action}, which is the assume itself
|
||||
* or the {@link #TAG_SESSION_ACTION} a tagged assume also needs. An {@code Action} element
|
||||
* or an action a generated table gives the assume beside it ({@code sts:TagSession},
|
||||
* {@code sts:SetSourceIdentity}). An {@code Action} element
|
||||
* applies when any of its patterns match; a {@code NotAction} element applies when none
|
||||
* of its patterns match (AWS semantics, mirroring {@link IamPolicyEvaluator}'s action handling).
|
||||
* A statement with neither key expresses no action constraint and does not apply.
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -101,8 +102,26 @@ public final class IamAuthorizedActions {
|
||||
*/
|
||||
public record Companion(String action, String member, String attribute, String claim, String key,
|
||||
String carried, boolean callerPolicyWhenChained) {
|
||||
|
||||
/**
|
||||
* Whether the companion's key names each tag its value carries, under the Service Reference's
|
||||
* {@code ${TagKey}} placeholder ({@code aws:RequestTag/${TagKey}}), as a session tag companion's does, rather
|
||||
* than one value.
|
||||
*/
|
||||
public boolean namesEachTag() {
|
||||
return key != null && key.contains(TAG_KEY);
|
||||
}
|
||||
|
||||
/** The SAML attribute or token claim the companion reads, its {@code {TagKey}} placeholder left off. */
|
||||
public String carrier() {
|
||||
String named = attribute != null ? attribute : claim;
|
||||
return named == null ? null : named.replace("{TagKey}", "");
|
||||
}
|
||||
}
|
||||
|
||||
/** The placeholder the Service Reference names a tag's key with inside a condition key. */
|
||||
private static final String TAG_KEY = "${TagKey}";
|
||||
|
||||
/**
|
||||
* An operation whose action is one or another by whether its request names {@code parameter}: S3's GetObject is
|
||||
* {@code s3:GetObject}, and {@code s3:GetObjectVersion} for a request naming a {@code versionId}.
|
||||
@@ -179,6 +198,17 @@ public final class IamAuthorizedActions {
|
||||
return Holder.TABLE.companions().getOrDefault(operation, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* The companion of {@code operation} reading the session tags an identity provider passes: the SAML attribute or
|
||||
* token claim carrying them and the action a trust policy has to allow for them. Empty for an operation the file
|
||||
* gives none.
|
||||
*/
|
||||
public static Optional<Companion> sessionTags(String operation) {
|
||||
return companions(operation).stream()
|
||||
.filter(companion -> companion.member() == null && companion.namesEachTag())
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/** The file's entries. */
|
||||
static Table parse(InputStream in) throws IOException {
|
||||
JsonNode document = MAPPER.readTree(in);
|
||||
|
||||
@@ -46,8 +46,14 @@ final class SAMLAssertionVerifier {
|
||||
static final String ASSERTION_INVALID = "The SAML assertion is invalid.";
|
||||
static final String SIGNATURE_INVALID = "Response signature invalid";
|
||||
private static final String ROLE_SESSION_NAME = "https://aws.amazon.com/SAML/Attributes/RoleSessionName";
|
||||
/** What an attribute carrying a session tag is named, the tag's key following the colon. */
|
||||
private static final String PRINCIPAL_TAG = "https://aws.amazon.com/SAML/Attributes/PrincipalTag:";
|
||||
/**
|
||||
* What an attribute carrying a session tag is named, the tag's key following it: the attribute the generated
|
||||
* {@code iam-authorized-actions.json} gives AssumeRoleWithSAML's session tags, from the IAM user guide's "include
|
||||
* the Attribute element with the Name attribute set to https://aws.amazon.com/SAML/Attributes/PrincipalTag:{TagKey}".
|
||||
* None where the file gives no such companion, and then no attribute is a session tag.
|
||||
*/
|
||||
private static final String PRINCIPAL_TAG = IamAuthorizedActions.sessionTags("sts:AssumeRoleWithSAML")
|
||||
.map(IamAuthorizedActions.Companion::carrier).orElse(null);
|
||||
/** 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.";
|
||||
@@ -264,7 +270,7 @@ final class SAMLAssertionVerifier {
|
||||
for (int i = 0; i < attributes.getLength(); i++) {
|
||||
Element attribute = (Element) attributes.item(i);
|
||||
String name = attribute.getAttribute("Name");
|
||||
if (name == null || !name.startsWith(PRINCIPAL_TAG)) {
|
||||
if (PRINCIPAL_TAG == null || name == null || !name.startsWith(PRINCIPAL_TAG)) {
|
||||
continue;
|
||||
}
|
||||
String key = name.substring(PRINCIPAL_TAG.length());
|
||||
|
||||
@@ -34,10 +34,10 @@ public class SAMLTrustPolicyEvaluator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the trust policy lets this assertion's holder perform {@code action} beside the assume, an action the
|
||||
* generated {@code iam-authorized-actions.json} gives it for a value the assertion sets
|
||||
* ({@code sts:SetSourceIdentity}), read on the same document with the same principal and the same conditions as
|
||||
* the assume itself.
|
||||
* Whether the trust policy lets this assertion's holder perform {@code action} beside the assume, an action
|
||||
* the generated {@code iam-authorized-actions.json} gives it for a value the assertion sets
|
||||
* ({@code sts:SetSourceIdentity}, {@code sts:TagSession}), read on the same document with the same principal
|
||||
* and the same conditions as the assume itself.
|
||||
*/
|
||||
public boolean allowsCompanion(String action, String document, String providerArn,
|
||||
Map<String, List<String>> claims, String roleSessionName,
|
||||
@@ -45,15 +45,6 @@ public class SAMLTrustPolicyEvaluator {
|
||||
return allows(action, document, providerArn, claims, roleSessionName, roleTags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the trust policy lets this assertion's holder tag the session it is assuming, read on
|
||||
* the same document with the same principal and the same conditions as the assume itself.
|
||||
*/
|
||||
public boolean allowsTagging(String document, String providerArn, Map<String, List<String>> claims,
|
||||
String roleSessionName, Map<String, List<String>> roleTags) {
|
||||
return allows(AssumeRolePolicyEvaluator.TAG_SESSION_ACTION, document, providerArn, claims,
|
||||
roleSessionName, roleTags);
|
||||
}
|
||||
|
||||
private boolean allows(String action, String document, String providerArn,
|
||||
Map<String, List<String>> claims, String roleSessionName,
|
||||
|
||||
@@ -325,10 +325,21 @@ public class StsQueryHandler {
|
||||
*/
|
||||
private static Optional<IamAuthorizedActions.Companion> federatedSourceIdentity(String operation) {
|
||||
return IamAuthorizedActions.companions(operation).stream()
|
||||
.filter(companion -> companion.attribute() != null || companion.claim() != null)
|
||||
.filter(companion -> (companion.attribute() != null || companion.claim() != null)
|
||||
&& !companion.namesEachTag())
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/** The trust policy's keys given in full, with the tags an assertion or a token passes as its request tags. */
|
||||
private static Map<String, List<String>> withRequestTags(Map<String, List<String>> keys, Map<String, String> tags) {
|
||||
if (tags.isEmpty()) {
|
||||
return keys;
|
||||
}
|
||||
Map<String, List<String>> withTags = new LinkedHashMap<>(keys);
|
||||
withTags.putAll(IamConditionContextResolver.requestTagKeys(tags));
|
||||
return withTags;
|
||||
}
|
||||
|
||||
/**
|
||||
* The source identity an assertion's attribute or a token's claim carries, {@code carrier} naming which: none where
|
||||
* it carries none, and refused with InvalidIdentityToken where it carries several values or one outside
|
||||
@@ -730,15 +741,16 @@ public class StsQueryHandler {
|
||||
// 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()) {
|
||||
String tagging = IamConditionContextResolver.taggingAction("sts:AssumeRole");
|
||||
if (tagging == null || (requestTags(params).isEmpty() && inheritedTags.isEmpty())) {
|
||||
return null;
|
||||
}
|
||||
for (Map<String, List<String>> context : trustContexts) {
|
||||
if (!trustPolicyEvaluator.allowsTagging(role.getAssumeRolePolicyDocument(), caller, callerAccount,
|
||||
context)) {
|
||||
if (!trustPolicyEvaluator.allowsCompanion(tagging, role.getAssumeRolePolicyDocument(), caller,
|
||||
callerAccount, context)) {
|
||||
return AwsQueryResponse.error("AccessDenied", "User: " + callerArn
|
||||
+ " is not authorized to perform: " + AssumeRolePolicyEvaluator.TAG_SESSION_ACTION
|
||||
+ " on resource: " + roleArn, AwsNamespaces.STS, 403);
|
||||
+ " is not authorized to perform: " + tagging + " on resource: " + roleArn,
|
||||
AwsNamespaces.STS, 403);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -1056,8 +1068,14 @@ public class StsQueryHandler {
|
||||
.orElse(null);
|
||||
String sourceIdentity = sourceIdentityKey == null ? null
|
||||
: tokenSourceIdentity(claims.claims().path(sourceIdentityKey.claim()), sourceIdentityKey.claim());
|
||||
Map<String, List<String>> roleTags = withSourceIdentity(roleTagKeys(role.get()), sourceIdentityKey,
|
||||
sourceIdentity);
|
||||
// The tags the token passes are request tags of the trust policy too, aws:RequestTag/<key> and aws:TagKeys,
|
||||
// as the Service Authorization Reference gives the action, and passing them needs the action the generated
|
||||
// table gives the token's tags: "trust policies for all roles connected to the identity provider (IdP)
|
||||
// passing tags must have the sts:TagSession permission".
|
||||
IamAuthorizedActions.Companion taggingKey = IamAuthorizedActions.sessionTags("sts:AssumeRoleWithWebIdentity")
|
||||
.orElse(null);
|
||||
Map<String, List<String>> roleTags = withRequestTags(withSourceIdentity(roleTagKeys(role.get()),
|
||||
sourceIdentityKey, sourceIdentity), claims.principalTags());
|
||||
if (sourceIdentity != null && !webIdentityTrustEvaluator.allowsCompanion(sourceIdentityKey.action(),
|
||||
role.get().getAssumeRolePolicyDocument(), oidcProviderArn, issuerKeyPrefix, conditionClaims,
|
||||
sessionName, roleTags)) {
|
||||
@@ -1071,13 +1089,13 @@ public class StsQueryHandler {
|
||||
roleArn, claims.subject());
|
||||
return WebIdentityOutcome.deny(accessDenied("sts:AssumeRoleWithWebIdentity", roleArn));
|
||||
}
|
||||
if (!claims.principalTags().isEmpty()
|
||||
&& !webIdentityTrustEvaluator.allowsTagging(role.get().getAssumeRolePolicyDocument(),
|
||||
oidcProviderArn, issuerKeyPrefix, conditionClaims, sessionName, roleTags)) {
|
||||
if (!claims.principalTags().isEmpty() && taggingKey != null
|
||||
&& !webIdentityTrustEvaluator.allowsCompanion(taggingKey.action(),
|
||||
role.get().getAssumeRolePolicyDocument(), oidcProviderArn, issuerKeyPrefix, conditionClaims,
|
||||
sessionName, roleTags)) {
|
||||
LOG.debugv("Trust policy on role {0} does not permit tagging the session of subject {1}",
|
||||
roleArn, claims.subject());
|
||||
return WebIdentityOutcome.deny(
|
||||
accessDenied(AssumeRolePolicyEvaluator.TAG_SESSION_ACTION, roleArn));
|
||||
return WebIdentityOutcome.deny(accessDenied(taggingKey.action(), roleArn));
|
||||
}
|
||||
|
||||
// verify() already required the audience list to hold one of the accepted audiences.
|
||||
@@ -1211,7 +1229,15 @@ public class StsQueryHandler {
|
||||
String sourceIdentity = sourceIdentityKey == null ? null : federatedSourceIdentityValue(
|
||||
verified.attributes().getOrDefault(sourceIdentityKey.attribute(), List.of()),
|
||||
"SAML attribute " + sourceIdentityKey.attribute());
|
||||
Map<String, List<String>> roleTags = withSourceIdentity(roleTagKeys(role), sourceIdentityKey, sourceIdentity);
|
||||
// The tags the assertion passes are request tags of the trust policy too, aws:RequestTag/<key> and
|
||||
// aws:TagKeys, as the Service Authorization Reference gives the action and the IAM user guide's SAML ABAC
|
||||
// tutorial's trust policy reads them, and passing them needs the action the generated table gives the
|
||||
// assertion's tags: "trust policies for all roles connected to the identity provider (IdP) passing tags must
|
||||
// have the sts:TagSession permission".
|
||||
IamAuthorizedActions.Companion taggingKey = IamAuthorizedActions.sessionTags("sts:AssumeRoleWithSAML")
|
||||
.orElse(null);
|
||||
Map<String, List<String>> roleTags = withRequestTags(withSourceIdentity(roleTagKeys(role), sourceIdentityKey,
|
||||
sourceIdentity), verified.principalTags());
|
||||
if (sourceIdentity != null && !samlTrustEvaluator.allowsCompanion(sourceIdentityKey.action(),
|
||||
role.getAssumeRolePolicyDocument(), principalArn, assertionClaims, verified.roleSessionName(),
|
||||
roleTags)) {
|
||||
@@ -1222,11 +1248,11 @@ public class StsQueryHandler {
|
||||
verified.roleSessionName(), roleTags)) {
|
||||
throw new AwsException("AccessDenied", "Not authorized to perform sts:AssumeRoleWithSAML on resource: " + roleArn, 403);
|
||||
}
|
||||
if (!verified.principalTags().isEmpty()
|
||||
&& !samlTrustEvaluator.allowsTagging(role.getAssumeRolePolicyDocument(), principalArn,
|
||||
assertionClaims, verified.roleSessionName(), roleTags)) {
|
||||
throw new AwsException("AccessDenied", "Not authorized to perform "
|
||||
+ AssumeRolePolicyEvaluator.TAG_SESSION_ACTION + " on resource: " + roleArn, 403);
|
||||
if (!verified.principalTags().isEmpty() && taggingKey != null
|
||||
&& !samlTrustEvaluator.allowsCompanion(taggingKey.action(), role.getAssumeRolePolicyDocument(),
|
||||
principalArn, assertionClaims, verified.roleSessionName(), roleTags)) {
|
||||
throw new AwsException("AccessDenied", "Not authorized to perform " + taggingKey.action()
|
||||
+ " on resource: " + roleArn, 403);
|
||||
}
|
||||
|
||||
Response roleDurationError = validateMaxSessionDuration(durationSeconds, role);
|
||||
|
||||
@@ -32,8 +32,6 @@ public class WebIdentityTrustPolicyEvaluator {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(WebIdentityTrustPolicyEvaluator.class);
|
||||
private static final String WEB_IDENTITY_ACTION = "sts:assumerolewithwebidentity";
|
||||
/** What a trust policy also has to allow for the token's session tags to be taken. */
|
||||
private static final String TAG_SESSION_ACTION = AssumeRolePolicyEvaluator.TAG_SESSION_ACTION;
|
||||
private static final String ROLE_SESSION_NAME_KEY = "sts:RoleSessionName";
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
@@ -65,10 +63,10 @@ public class WebIdentityTrustPolicyEvaluator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the trust policy lets this token's holder perform {@code action} beside the assume, an action the
|
||||
* generated {@code iam-authorized-actions.json} gives it for a value the token sets
|
||||
* ({@code sts:SetSourceIdentity}), read on the same document with the same principal and the same conditions as
|
||||
* the assume itself.
|
||||
* Whether the trust policy lets this token's holder perform {@code action} beside the assume, an action
|
||||
* the generated {@code iam-authorized-actions.json} gives it for a value the token sets
|
||||
* ({@code sts:SetSourceIdentity}, {@code sts:TagSession}), read on the same document with the same principal
|
||||
* and the same conditions as the assume itself.
|
||||
*/
|
||||
public boolean allowsCompanion(String action, String trustPolicyDocument, String oidcProviderArn,
|
||||
String issuerKeyPrefix, Map<String, List<String>> claims, String roleSessionName,
|
||||
@@ -77,16 +75,6 @@ public class WebIdentityTrustPolicyEvaluator {
|
||||
roleTags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the trust policy lets this token's holder tag the session it is assuming, read on the
|
||||
* same document with the same principal and the same conditions as the assume itself.
|
||||
*/
|
||||
public boolean allowsTagging(String trustPolicyDocument, String oidcProviderArn, String issuerKeyPrefix,
|
||||
Map<String, List<String>> claims, String roleSessionName,
|
||||
Map<String, List<String>> roleTags) {
|
||||
return allows(TAG_SESSION_ACTION, trustPolicyDocument, oidcProviderArn, issuerKeyPrefix, claims,
|
||||
roleSessionName, roleTags);
|
||||
}
|
||||
|
||||
private boolean allows(String action, String trustPolicyDocument, String oidcProviderArn,
|
||||
String issuerKeyPrefix, Map<String, List<String>> claims,
|
||||
|
||||
@@ -1819,6 +1819,13 @@
|
||||
"key": "sts:SourceIdentity",
|
||||
"carried": "aws:SourceIdentity",
|
||||
"callerPolicyWhenChained": false
|
||||
},
|
||||
{
|
||||
"action": "sts:TagSession",
|
||||
"attribute": "https://aws.amazon.com/SAML/Attributes/PrincipalTag:{TagKey}",
|
||||
"key": "aws:RequestTag/${TagKey}",
|
||||
"carried": "aws:PrincipalTag/${TagKey}",
|
||||
"callerPolicyWhenChained": false
|
||||
}
|
||||
],
|
||||
"AssumeRoleWithWebIdentity": [
|
||||
@@ -1828,6 +1835,13 @@
|
||||
"key": "sts:SourceIdentity",
|
||||
"carried": "aws:SourceIdentity",
|
||||
"callerPolicyWhenChained": false
|
||||
},
|
||||
{
|
||||
"action": "sts:TagSession",
|
||||
"claim": "https://aws.amazon.com/tags",
|
||||
"key": "aws:RequestTag/${TagKey}",
|
||||
"carried": "aws:PrincipalTag/${TagKey}",
|
||||
"callerPolicyWhenChained": false
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
+18
@@ -730,6 +730,24 @@ class AssumeRoleWithSamlValidationIntegrationTest {
|
||||
iamService.resolveCallerArns(accessKeyId).orElseThrow().principalTags());
|
||||
}
|
||||
|
||||
/**
|
||||
* The assertion's tags are request tags of the trust policy, {@code aws:RequestTag/<key>} and {@code aws:TagKeys},
|
||||
* as the IAM user guide's SAML ABAC tutorial's trust policy reads them: a statement requiring a tag allows the
|
||||
* assertion passing one of its values, and not one passing another value or none.
|
||||
*/
|
||||
@Test
|
||||
void theAssertionsTagsAreRequestTagsOfTheTrustPolicy() {
|
||||
String role = createRole("saml-request-tags-", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":"
|
||||
+ "\"Allow\",\"Principal\":{\"Federated\":\"" + PROVIDER + "\"},"
|
||||
+ "\"Action\":[\"sts:AssumeRoleWithSAML\",\"sts:TagSession\"],"
|
||||
+ "\"Condition\":{\"StringLike\":{\"aws:RequestTag/access-team\":[\"eng\",\"qas\"]}}}]}");
|
||||
|
||||
assume(role, assertionCarryingTags(role, tag("access-team", "eng")), PROVIDER).statusCode(200);
|
||||
assume(role, assertionCarryingTags(role, tag("access-team", "ops")), PROVIDER).statusCode(403)
|
||||
.body("ErrorResponse.Error.Code", equalTo("AccessDenied"));
|
||||
assume(role, assertionCarryingTags(role, Map.of()), PROVIDER).statusCode(403);
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys the assertion asks to set as transitive pass down to a session assumed from this
|
||||
* one: the attribute naming them "is an optional multivalued attribute that sets your session
|
||||
|
||||
@@ -1143,8 +1143,8 @@ class StsQueryHandlerTest {
|
||||
when(trust.allows(eq("{}"), eq(providerArn), eq("oidc.example.com/id/ABC"), anyMap(), eq("app"), anyMap()))
|
||||
.thenReturn(true);
|
||||
// The token passes a session tag, which the trust policy has to permit as well.
|
||||
when(trust.allowsTagging(eq("{}"), eq(providerArn), eq("oidc.example.com/id/ABC"), anyMap(), eq("app"),
|
||||
anyMap()))
|
||||
when(trust.allowsCompanion(eq("sts:TagSession"), eq("{}"), eq(providerArn), eq("oidc.example.com/id/ABC"),
|
||||
anyMap(), eq("app"), anyMap()))
|
||||
.thenReturn(true);
|
||||
|
||||
StsQueryHandler handler = new StsQueryHandler(iamService, mock(AccountResolver.class),
|
||||
|
||||
@@ -296,6 +296,36 @@ class StsRegisteredOidcProviderIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The token's tags are request tags of the trust policy, {@code aws:RequestTag/<key>} and {@code aws:TagKeys}: a
|
||||
* statement requiring a tag allows the token passing it and not one passing another value.
|
||||
*/
|
||||
@Test
|
||||
void aTokensTagsAreRequestTagsOfTheTrustPolicy() throws Exception {
|
||||
String host = issuer.substring("https://".length());
|
||||
String trust = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{"
|
||||
+ "\"Federated\":\"" + providerArn + "\"},\"Action\":[\"sts:AssumeRoleWithWebIdentity\","
|
||||
+ "\"sts:TagSession\"],\"Condition\":{\"StringEquals\":{\"" + host + ":sub\":\"" + SUBJECT
|
||||
+ "\",\"aws:RequestTag/Project\":\"atlas\"}}}]}";
|
||||
Role tagged = iam.createRole(b -> b.roleName("web-tags-" + UUID.randomUUID().toString().substring(0, 8))
|
||||
.assumeRolePolicyDocument(trust)).role();
|
||||
try {
|
||||
assume(tagged, token(issuer, "sts.amazonaws.com", privateKey, KEY_ID, principalTag("atlas")));
|
||||
StsException other = assertThrows(StsException.class, () -> assume(tagged, token(issuer,
|
||||
"sts.amazonaws.com", privateKey, KEY_ID, principalTag("orion"))));
|
||||
assertEquals(403, other.statusCode());
|
||||
} finally {
|
||||
iam.deleteRole(b -> b.roleName(tagged.roleName()));
|
||||
}
|
||||
}
|
||||
|
||||
/** The nested claim the IAM user guide gives a token's session tags in, passing Project as {@code value}. */
|
||||
private ObjectNode principalTag(String value) {
|
||||
ObjectNode claims = objectMapper.createObjectNode();
|
||||
claims.putObject("https://aws.amazon.com/tags").putObject("principal_tags").putArray("Project").add(value);
|
||||
return claims;
|
||||
}
|
||||
|
||||
private ObjectNode sourceIdentity(JsonNode value) {
|
||||
ObjectNode claims = objectMapper.createObjectNode();
|
||||
claims.set("https://aws.amazon.com/source_identity", value);
|
||||
|
||||
Reference in new issue
Block a user