mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): read the tags an AssumeRole passes in the role's trust policy (#9689)
Under enforcement a role whose trust policy conditions the assume or sts:TagSession on aws:RequestTag/<key> or aws:TagKeys refused every tagged AssumeRole, and a Deny keyed on them never applied: STS built the trust policy's context by hand, with the session name and external ID alone. The Service Reference gives sts:AssumeRole both keys, and the IAM user guide's session tags page has a trust policy reading them: with aws:RequestTag "you can allow the principal to pass the specified session tags, but only with the specified values", and with aws:TagKeys "pass only session tags with the specified tag keys". The trust policy's context now takes the request's keys from the generated tables, as the caller's own policies do: the members request-condition-keys.json gives sts:AssumeRole, its RoleSessionName and ExternalId, and the tags iam-tag-on-create.json reads from its Tags, as aws:RequestTag/<key> and aws:TagKeys. The assume and the sts:TagSession permission are each checked once for every set of tags a reading of the list gives, past a gap in its numbering or with each value of a key sent twice, as the enforcement filter checks the caller's policies, so a Deny on a tag holds however the list is sent. The filter's per-set helper moves to IamConditionContextResolver beside requestContextsOfForm, which STS calls, and formParameters shares its first-value reading with it. Both read the list as STS does. TagOnCreate read sts: tags under Tags.member up to the first gap, while STS reads them through AwsQueryLists, Tags.member.N or the bare Tags.N at every index, so a tag sent as Tags.1, or after a gap, reached the session and not the check, and a policy requiring it refused a call AWS allows. sts: is now read through the shared reader, as iam: is, and the shared reader takes the members in the order of their indices, as AwsQueryLists gives them, so a key sent twice keeps the value the handler keeps. Part of #6640.
This commit is contained in:
1 parent
661854b0a7
commit
75f5ec3136
7 files changed
+221
-68
No files matched your search
+12
-6
@@ -3385,6 +3385,9 @@ As with passed roles, an operation Floci does not serve is in the table too.
|
||||
member names and numeric keys are rejected by its handler. Query form
|
||||
parameters are read under the spelling the SDK sends, from 1 to the first number with no key, and
|
||||
under the further spellings the RDS, DocumentDB, Neptune, ElastiCache and Redshift handlers read.
|
||||
IAM's and STS's are read as those handlers read them, through the one Query list reader:
|
||||
`Tags.member.N` or the bare `Tags.N`, at every number sent, in the order of the numbers, so a key
|
||||
sent twice keeps the later number's value, as the session or the resource does.
|
||||
EC2's are read from every tag specification, under `TagSpecification` or `TagSpecifications`, as EC2
|
||||
applies either, to the first that names no resource type. SQS's are read from `Tag.N.Key` and
|
||||
`Tag.N.Value` over the Query protocol. The tagging action is also checked for tags under any other
|
||||
@@ -3799,7 +3802,8 @@ A role trust policy is the narrower case, because `sts:AssumeRole` is authorized
|
||||
session to describe: its context carries `aws:PrincipalArn`, `aws:PrincipalAccount`,
|
||||
`aws:ResourceAccount`, `aws:RequestedRegion`, `aws:PrincipalIsAWSService`, `aws:CurrentTime`,
|
||||
`aws:EpochTime`, `sts:RoleSessionName`, `aws:TokenIssueTime` when the caller is itself a session,
|
||||
and `sts:ExternalId` when the request passes one. `aws:username`, `aws:userid` and
|
||||
`sts:ExternalId` when the request passes one, and `aws:RequestTag/<key>` and `aws:TagKeys` for the
|
||||
tags an `AssumeRole` passes. `aws:username`, `aws:userid` and
|
||||
`aws:PrincipalType` are not among them, so a variable naming one of those resolves to nothing in a
|
||||
trust policy and the value it sits in matches nothing.
|
||||
|
||||
@@ -4228,11 +4232,13 @@ floci populates:
|
||||
the role's trust policy reads them (see [STS](sts.md)); `sts:ExternalId` is absent when the request passes none.
|
||||
The actions and request members come from the same `request-condition-keys.json`.
|
||||
`AssumeRoleWithWebIdentity` and `AssumeRoleWithSAML` are not signed, so no identity-based policy is
|
||||
evaluated for them. `sts:SourceIdentity`, `sts:TransitiveTagKeys`, `aws:RequestTag/<key>` and `aws:TagKeys`
|
||||
are not populated for `AssumeRole`: source identity is not modelled, and the three tag keys are not in
|
||||
this file for the action, which is what fills the context here. A session's own tags are modelled, and
|
||||
reach a policy as `aws:PrincipalTag/<key>`; `sts:TransitiveTagKeys` reaches the role's trust policy,
|
||||
which the STS handler reads with a context of its own (see [STS](sts.md#transitive-tags)).
|
||||
evaluated for them. `sts:SourceIdentity` and `sts:TransitiveTagKeys` are not populated for `AssumeRole`:
|
||||
source identity is not modelled, and the tag key is not in this file for the action, which is what fills
|
||||
the context here. The tags the request passes are its `aws:RequestTag/<key>` and `aws:TagKeys`, from
|
||||
`iam-tag-on-create.json`, for the caller's policies and the role's trust policy alike. A session's own
|
||||
tags are modelled, and reach a policy as `aws:PrincipalTag/<key>`; `sts:TransitiveTagKeys` reaches the
|
||||
role's trust policy, which the STS handler reads with a context of its own (see
|
||||
[STS](sts.md#transitive-tags)).
|
||||
|
||||
**Any other condition key is absent from the request context.** A positive scalar operator
|
||||
without `IfExists` on an absent key makes the whole statement *not apply*: it neither matches nor
|
||||
|
||||
+21
-6
@@ -496,6 +496,19 @@ operation runs, answering `User is not authorized to perform: sts:TagSession`. S
|
||||
take their tags from an assertion or a token carry no `Tags` member, so nothing asks their caller's
|
||||
policy for the action and the trust policy is the whole of it.
|
||||
|
||||
The tags an `AssumeRole` passes are in the trust policy's context too, as `aws:RequestTag/<key>` and
|
||||
`aws:TagKeys`, read from its `Tags` through the generated `iam-tag-on-create.json` as the caller's own
|
||||
policies read them, beside the session name and external ID that `request-condition-keys.json` gives
|
||||
the action. The list is read as STS reads it, `Tags.member.N` or the bare `Tags.N` at every number
|
||||
sent, so a tag the session takes is a tag the trust policy sees. The session tags page has a trust
|
||||
policy reading both: with `aws:RequestTag` "you can allow the principal to pass the specified session
|
||||
tags, but only with the specified values", and with `aws:TagKeys` "pass only session tags with the
|
||||
specified tag keys". The assume and the `sts:TagSession`
|
||||
permission are each read once for every set of tags a reading of the list gives, past a gap in its
|
||||
numbering or with each value of a key sent twice, as the caller's policies are, so a `Deny` on a tag
|
||||
holds however the list is sent. A request passing no tags carries neither key, so a trust statement
|
||||
that requires one does not allow it.
|
||||
|
||||
The session's tags sit over the tags of the identity it acts as. For a role session that is the
|
||||
role's own tags, for a federated user the tags of the user whose key minted it, "the session
|
||||
principal tags include the user tags and the passed session tags", and a session tag "overrides the
|
||||
@@ -613,12 +626,14 @@ states.
|
||||
|
||||
### Known limitations
|
||||
|
||||
- **The tag and source-identity condition keys are not in the request context.**
|
||||
`aws:RequestTag/*`, `aws:TagKeys` and `sts:SourceIdentity` are not populated, so a condition on
|
||||
them does not match. A trust policy keyed on one of them therefore reads as unsatisfied, including
|
||||
on the `sts:TagSession` permission a tagged assume needs, and `sts:SetSourceIdentity` is not
|
||||
checked at all. `sts:TransitiveTagKeys` is populated for the trust policy and not for the caller's
|
||||
own identity-based policies, as [Transitive tags](#transitive-tags) says.
|
||||
- **The source-identity condition key is not in the request context.** `sts:SourceIdentity` is
|
||||
not populated, so a condition on it does not match, and `sts:SetSourceIdentity` is not checked at
|
||||
all. `sts:TransitiveTagKeys` is populated for the trust policy and not for the caller's own
|
||||
identity-based policies, as [Transitive tags](#transitive-tags) says.
|
||||
- **A federated assume's tags are not request tags of its trust policy.** The trust policy of an
|
||||
`AssumeRoleWithSAML` or `AssumeRoleWithWebIdentity` does not see the tags the assertion or the
|
||||
token passes as `aws:RequestTag/<key>` and `aws:TagKeys`, which the Service Authorization Reference
|
||||
gives both actions; [Session tags](#session-tags) says how they reach the session.
|
||||
|
||||
## Web Identity Validation (IRSA)
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -447,6 +448,45 @@ public class IamConditionContextResolver {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The contexts a Query request for {@code action} is checked in by a policy the operation evaluates itself, as STS
|
||||
* does the trust policy of the role an AssumeRole names: {@code context} with each key the request condition key
|
||||
* table reads from the request's members ({@code sts:RoleSessionName}, {@code sts:ExternalId}), once for each set
|
||||
* of tags the tag-on-create table reads from it, with that set's tags as {@code aws:RequestTag/<key>} and
|
||||
* {@code aws:TagKeys}, as the enforcement filter checks the caller's own policies; once with the member keys alone
|
||||
* when it carries none. A repeated parameter keeps its first value, as a Query handler reads it.
|
||||
*/
|
||||
public static List<Map<String, List<String>>> requestContextsOfForm(String action,
|
||||
MultivaluedMap<String, String> params,
|
||||
Map<String, List<String>> context) {
|
||||
Map<String, String> form = firstValues(params);
|
||||
Map<String, List<String>> withMembers = new HashMap<>(context);
|
||||
Map<String, List<String>> members = requestConditions(action, form::get);
|
||||
if (members != null) {
|
||||
withMembers.putAll(members);
|
||||
}
|
||||
return withRequestTags(withMembers, TagOnCreate.ofForm(action, form).filter(TagOnCreate.Tagging::requestTags)
|
||||
.map(TagOnCreate::requestTagContexts).orElse(List.of()));
|
||||
}
|
||||
|
||||
/**
|
||||
* The context once for each set of tags the request carries, with that set's keys where the context has
|
||||
* none of its own, so each resource's tags are checked apart; the context alone when it carries none.
|
||||
*/
|
||||
static List<Map<String, List<String>>> withRequestTags(Map<String, List<String>> context,
|
||||
List<Map<String, List<String>>> requestTags) {
|
||||
List<Map<String, List<String>>> contexts = new ArrayList<>();
|
||||
if (requestTags.isEmpty()) {
|
||||
contexts.add(context);
|
||||
}
|
||||
for (Map<String, List<String>> tags : requestTags) {
|
||||
Map<String, List<String>> withTags = context == null ? new HashMap<>() : new HashMap<>(context);
|
||||
tags.forEach(withTags::putIfAbsent);
|
||||
contexts.add(withTags);
|
||||
}
|
||||
return contexts;
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys a request for {@code action} carries in its own members, such as an OpenTunnel remotePort, read
|
||||
* through {@code parameter} from the table in {@code request-condition-keys.json}; null when it carries none.
|
||||
@@ -1313,13 +1353,7 @@ public class IamConditionContextResolver {
|
||||
return Map.of();
|
||||
}
|
||||
if ("GET".equalsIgnoreCase(ctx.getMethod()) && IamActionRegistry.isQueryRequest(ctx)) {
|
||||
Map<String, String> parameters = new LinkedHashMap<>();
|
||||
ctx.getUriInfo().getQueryParameters().forEach((key, values) -> {
|
||||
if (!values.isEmpty()) {
|
||||
parameters.put(key, values.getFirst());
|
||||
}
|
||||
});
|
||||
return parameters;
|
||||
return firstValues(ctx.getUriInfo().getQueryParameters());
|
||||
}
|
||||
// Only a request the Query controller dispatches carries its members as a form. Another protocol's handler
|
||||
// reads its own body (S3's XML, a REST service's JSON) whatever Content-Type it is sent with, so a form type on
|
||||
@@ -1352,6 +1386,17 @@ public class IamConditionContextResolver {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
/** Each parameter with its first value, the one a Query handler reads through {@code getFirst}. */
|
||||
private static Map<String, String> firstValues(MultivaluedMap<String, String> params) {
|
||||
Map<String, String> parameters = new LinkedHashMap<>();
|
||||
params.forEach((key, values) -> {
|
||||
if (values != null && !values.isEmpty()) {
|
||||
parameters.put(key, values.getFirst());
|
||||
}
|
||||
});
|
||||
return parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the entity fully and puts an equivalent stream back for downstream readers; none for a request that
|
||||
* carries no members, whose entity is left to its handler.
|
||||
|
||||
@@ -595,11 +595,12 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
Optional<TagOnCreate.Tagging> tagOnCreate = TagOnCreate.of(resolvedOperation.operation(), ctx);
|
||||
List<Map<String, List<String>>> requestTags = tagOnCreate.filter(TagOnCreate.Tagging::requestTags)
|
||||
.map(TagOnCreate::requestTagContexts).orElse(List.of());
|
||||
List<Map<String, List<String>>> targetContexts = withRequestTags(conditionContext, requestTags);
|
||||
List<Map<String, List<String>>> targetContexts = IamConditionContextResolver.withRequestTags(conditionContext,
|
||||
requestTags);
|
||||
for (Map<String, List<String>> target : remainingTargets) {
|
||||
Map<String, List<String>> targetContext = new HashMap<>(target);
|
||||
callerArns.ifPresent(arns -> arns.putPrincipalKeys(targetContext));
|
||||
targetContexts.addAll(withRequestTags(targetContext, requestTags));
|
||||
targetContexts.addAll(IamConditionContextResolver.withRequestTags(targetContext, requestTags));
|
||||
}
|
||||
|
||||
RequestPrincipal principal = RequestPrincipal.caller(callerArns.orElse(null));
|
||||
@@ -1066,24 +1067,6 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The context once for each set of tags the request carries, with that set's keys where the context has
|
||||
* none of its own, so each resource's tags are checked apart; the context alone when it carries none.
|
||||
*/
|
||||
private static List<Map<String, List<String>>> withRequestTags(Map<String, List<String>> context,
|
||||
List<Map<String, List<String>>> requestTags) {
|
||||
List<Map<String, List<String>>> contexts = new ArrayList<>();
|
||||
if (requestTags.isEmpty()) {
|
||||
contexts.add(context);
|
||||
}
|
||||
for (Map<String, List<String>> tags : requestTags) {
|
||||
Map<String, List<String>> withTags = context == null ? new HashMap<>() : new HashMap<>(context);
|
||||
tags.forEach(withTags::putIfAbsent);
|
||||
contexts.add(withTags);
|
||||
}
|
||||
return contexts;
|
||||
}
|
||||
|
||||
/**
|
||||
* A request that tags what it creates also needs the service's tagging action (see {@link TagOnCreate}),
|
||||
* checked as the caller for each set of tags, under any spelling a handler might read: on the resource a
|
||||
|
||||
@@ -5,6 +5,7 @@ import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -100,6 +101,15 @@ final class TagOnCreate {
|
||||
return entry == null ? Optional.empty() : of(operation, entry, Map.of(), body);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #of(String, ContainerRequestContext)}, for a Query request an operation reads itself from its form (an
|
||||
* AssumeRole whose role's trust policy STS evaluates).
|
||||
*/
|
||||
static Optional<Tagging> ofForm(String operation, Map<String, String> form) {
|
||||
TagOnCreateMembers.Entry entry = TagOnCreateMembers.of(operation);
|
||||
return entry == null ? Optional.empty() : of(operation, entry, form, null);
|
||||
}
|
||||
|
||||
private static Optional<Tagging> of(String operation, TagOnCreateMembers.Entry entry, Map<String, String> form,
|
||||
JsonNode body) {
|
||||
Set<TagSet> sets = new LinkedHashSet<>();
|
||||
@@ -317,9 +327,10 @@ final class TagOnCreate {
|
||||
*/
|
||||
private static List<Reading> readings(String operation, TagOnCreateMembers.Member member, boolean ec2) {
|
||||
PassedRoleMembers.Step first = member.querySteps().getFirst();
|
||||
// IamQueryHandler reads every list it serves through AwsQueryLists, so this reads the member the table
|
||||
// names by that same rule rather than restating it: nothing here to keep in step with that handler.
|
||||
if (operation.startsWith("iam:")) {
|
||||
// IamQueryHandler reads every list it serves through AwsQueryLists, and StsQueryHandler its tags, so this
|
||||
// reads the member the table names by that same rule rather than restating it: nothing here to keep in step
|
||||
// with either handler.
|
||||
if (operation.startsWith("iam:") || operation.startsWith("sts:")) {
|
||||
return List.of(sharedReader(member, first.name()));
|
||||
}
|
||||
return switch (operation.substring(0, operation.indexOf(':') + 1) + first.name()) {
|
||||
@@ -396,20 +407,19 @@ final class TagOnCreate {
|
||||
}
|
||||
}
|
||||
// The members AwsQueryLists reads, so a handler that reads a list through it needs nothing modelled
|
||||
// here: the spellings it accepts and the indices it takes are read from the one rule both follow.
|
||||
// here: the spellings it accepts and the indices it takes are read from the one rule both follow, in the
|
||||
// order of their indices as it gives them, so a key sent twice keeps the value the handler keeps.
|
||||
case AS_THE_SHARED_READER -> {
|
||||
for (String prefix : reading.prefixes()) {
|
||||
Pattern members = AwsQueryLists.memberKeys(prefix, null);
|
||||
String suffix = "." + reading.key();
|
||||
form.forEach((parameter, key) -> {
|
||||
if (key == null || !parameter.endsWith(suffix)) {
|
||||
return;
|
||||
}
|
||||
String numbered = parameter.substring(0, parameter.length() - suffix.length());
|
||||
if (members.matcher(numbered).matches()) {
|
||||
tags.put(key, form.getOrDefault(numbered + "." + reading.value(), ""));
|
||||
}
|
||||
});
|
||||
form.keySet().stream()
|
||||
.filter(parameter -> parameter.endsWith(suffix) && form.get(parameter) != null)
|
||||
.map(parameter -> parameter.substring(0, parameter.length() - suffix.length()))
|
||||
.filter(numbered -> members.matcher(numbered).matches())
|
||||
.sorted(Comparator.comparingInt(AwsQueryLists::memberIndex))
|
||||
.forEach(numbered -> tags.put(form.get(numbered + suffix),
|
||||
form.getOrDefault(numbered + "." + reading.value(), "")));
|
||||
}
|
||||
}
|
||||
case EITHER_AT_EACH -> {
|
||||
|
||||
@@ -545,11 +545,6 @@ public class StsQueryHandler {
|
||||
|
||||
Map<String, List<String>> trustContext = IamConditionContextResolver.withGlobalContext(
|
||||
null, roleArn, regionResolver.getRegion(), callerAccount, roleAccountId);
|
||||
trustContext.put("sts:RoleSessionName", List.of(getParam(params, "RoleSessionName")));
|
||||
String externalId = getParam(params, "ExternalId");
|
||||
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
|
||||
@@ -562,11 +557,21 @@ public class StsQueryHandler {
|
||||
// to pass session tags only if the role they assume includes the specified tags".
|
||||
trustContext = IamConditionContextResolver.withResourceTags(trustContext,
|
||||
IamConditionContextResolver.resourceTagKeys(roleArn, role.getTags()));
|
||||
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);
|
||||
// The keys the request carries, as the generated tables read them: the members the request condition key
|
||||
// table gives sts:AssumeRole, its session name and external ID, and the tags it passes, which the
|
||||
// tag-on-create table reads as aws:RequestTag/<key> and aws:TagKeys, once for each set of them it reads, as
|
||||
// the caller's own policies are checked. The Service Reference gives the action both keys, and the session
|
||||
// tags page has a trust policy reading them: with aws:RequestTag "you can allow the principal to pass the
|
||||
// specified session tags, but only with the specified values", and with aws:TagKeys "pass only session tags
|
||||
// with the specified tag keys".
|
||||
List<Map<String, List<String>>> trustContexts = IamConditionContextResolver.requestContextsOfForm(
|
||||
"sts:AssumeRole", params, trustContext);
|
||||
for (Map<String, List<String>> context : trustContexts) {
|
||||
if (!trustPolicyEvaluator.allows(role.getAssumeRolePolicyDocument(), caller, callerAccount, context)) {
|
||||
return AwsQueryResponse.error("AccessDenied", "User: " + callerArn
|
||||
+ " is not authorized to perform: sts:AssumeRole on resource: " + roleArn,
|
||||
AwsNamespaces.STS, 403);
|
||||
}
|
||||
}
|
||||
// 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
|
||||
@@ -574,12 +579,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())
|
||||
&& !trustPolicyEvaluator.allowsTagging(role.getAssumeRolePolicyDocument(), caller,
|
||||
callerAccount, trustContext)) {
|
||||
return AwsQueryResponse.error("AccessDenied", "User: " + callerArn
|
||||
+ " is not authorized to perform: " + AssumeRolePolicyEvaluator.TAG_SESSION_ACTION
|
||||
+ " on resource: " + roleArn, AwsNamespaces.STS, 403);
|
||||
if (requestTags(params).isEmpty() && inheritedTags.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
for (Map<String, List<String>> context : trustContexts) {
|
||||
if (!trustPolicyEvaluator.allowsTagging(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);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -199,6 +199,71 @@ class AssumeRoleTrustConditionsIntegrationTest {
|
||||
assumeRoleWithSession(assumeCallerRole(callerRole, "/other/"), namesReplacement).then().statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* A trust policy reads the tags the request passes as {@code aws:RequestTag/<key>} and {@code aws:TagKeys}, as the
|
||||
* session tags page has it: with {@code aws:RequestTag} "you can allow the principal to pass the specified
|
||||
* session tags, but only with the specified values", and with {@code aws:TagKeys} "pass only session tags with
|
||||
* the specified tag keys". A request passing none carries neither, so a statement requiring a tag does not allow
|
||||
* it. The tags are read as STS reads the list, so one it takes from the bare {@code Tags.N} form, or past a gap in
|
||||
* the numbering, satisfies the statement as it does the session.
|
||||
*/
|
||||
@Test
|
||||
void aTrustConditionReadsTheTagsTheRequestPasses() {
|
||||
String role = createRole("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"AWS":"arn:aws:iam::%s:root"},"Action":["sts:AssumeRole","sts:TagSession"],
|
||||
"Condition":{"StringEquals":{"aws:RequestTag/Project":"blue"},
|
||||
"ForAllValues:StringEquals":{"aws:TagKeys":["Project"]}}}]}""".formatted(TRUSTED_ACCOUNT));
|
||||
|
||||
assumeRoleWithTags(TRUSTED_ACCOUNT, role, tag(1, "Project", "blue")).then().statusCode(200);
|
||||
assumeRoleWithTags(TRUSTED_ACCOUNT, role, "&Tags.1.Key=Project&Tags.1.Value=blue").then().statusCode(200);
|
||||
assumeRoleWithTags(TRUSTED_ACCOUNT, role, tag(2, "Project", "blue")).then().statusCode(200);
|
||||
assertTrustDenied(assumeRoleWithTags(TRUSTED_ACCOUNT, role, ""), role);
|
||||
assertTrustDenied(assumeRoleWithTags(TRUSTED_ACCOUNT, role, tag(1, "Project", "red")), role);
|
||||
assertTrustDenied(assumeRoleWithTags(TRUSTED_ACCOUNT, role, tag(1, "Project", "blue") + tag(2, "Team", "a")),
|
||||
role);
|
||||
}
|
||||
|
||||
/**
|
||||
* The trust policy is checked once for each set of tags the request carries as a reading of its list gives it,
|
||||
* as the caller's own policies are: past a gap in the numbering, and with each value of a key sent twice, so a
|
||||
* Deny on a tag holds whatever gaps or order the list is sent in.
|
||||
*/
|
||||
@Test
|
||||
void aDenyOnASessionTagReadsEveryTagTheSessionTakes() {
|
||||
String deniesRed = """
|
||||
{"Effect":"Deny","Principal":{"AWS":"arn:aws:iam::%s:root"},"Action":"sts:TagSession",
|
||||
"Condition":{"StringEquals":{"aws:RequestTag/Project":"red"}}}""";
|
||||
String role = createRole("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"AWS":["arn:aws:iam::%1$s:root","arn:aws:iam::%2$s:root"]},
|
||||
"Action":["sts:AssumeRole","sts:TagSession"]},%3$s]}""".formatted(TRUSTED_ACCOUNT, OTHER_ACCOUNT,
|
||||
deniesRed.formatted(TRUSTED_ACCOUNT)));
|
||||
String gap = tag(1, "Team", "a") + tag(3, "Project", "red");
|
||||
String outOfOrder = tag(2, "Project", "red") + tag(1, "Project", "blue");
|
||||
|
||||
assumeRoleWithTags(TRUSTED_ACCOUNT, role, tag(1, "Project", "blue")).then().statusCode(200);
|
||||
assertTaggingDenied(assumeRoleWithTags(TRUSTED_ACCOUNT, role, gap), role);
|
||||
assertTaggingDenied(assumeRoleWithTags(TRUSTED_ACCOUNT, role, outOfOrder), role);
|
||||
|
||||
// The caller's own Deny, which the enforcement filter reads before the trust policy.
|
||||
String user = "trust-tags-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String accessKeyId = createCallerUser(user);
|
||||
given().formParam("Action", "PutUserPolicy").formParam("UserName", user).formParam("PolicyName", "tags")
|
||||
.formParam("PolicyDocument", """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"sts:TagSession","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"sts:TagSession","Resource":"*",
|
||||
"Condition":{"StringEquals":{"aws:RequestTag/Project":"red"}}}]}""")
|
||||
.header("Authorization", auth(OTHER_ACCOUNT, "iam")).when().post("/").then().statusCode(200);
|
||||
assumeRoleWithTags(accessKeyId, role, tag(1, "Project", "blue")).then().statusCode(200);
|
||||
for (String tags : new String[] {gap, outOfOrder}) {
|
||||
assumeRoleWithTags(accessKeyId, role, tags).then().statusCode(403)
|
||||
.body(containsString("<Code>AccessDenied</Code>"))
|
||||
.body(containsString("is not authorized to perform: sts:TagSession"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A role in another account needs its trust policy to allow an IAM user or a role session
|
||||
* beside the caller's identity policy, so IAM enforcement refuses such a caller the trust
|
||||
@@ -216,6 +281,26 @@ class AssumeRoleTrustConditionsIntegrationTest {
|
||||
+ ROLE_ACCOUNT + ":role/" + role));
|
||||
}
|
||||
|
||||
private static void assertTaggingDenied(Response response, String role) {
|
||||
response.then().statusCode(403)
|
||||
.body(containsString("is not authorized to perform: sts:TagSession on resource: arn:aws:iam::"
|
||||
+ ROLE_ACCOUNT + ":role/" + role));
|
||||
}
|
||||
|
||||
/** An AssumeRole sent as {@code tags} spells its list, in that order, gaps and all. */
|
||||
private static Response assumeRoleWithTags(String accessKeyId, String role, String tags) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.body("Action=AssumeRole&Version=2011-06-15&RoleArn=arn:aws:iam::" + ROLE_ACCOUNT + ":role/" + role
|
||||
+ "&RoleSessionName=sx" + tags)
|
||||
.header("Authorization", auth(accessKeyId, "sts"))
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
private static String tag(int index, String key, String value) {
|
||||
return "&Tags.member." + index + ".Key=" + key + "&Tags.member." + index + ".Value=" + value;
|
||||
}
|
||||
|
||||
private static Response assumeRole(String accessKeyId, String role, String sessionName,
|
||||
String externalId) {
|
||||
RequestSpecification spec = given()
|
||||
|
||||
Reference in new issue
Block a user