mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): hold every policy document a write is handed to being a policy (#8175)
Seven writes take an IAM policy document and every one of them models MalformedPolicyDocument, "the request was rejected because the policy document was malformed". None of them answered it, so a document that was no policy at all was stored and told apart only later, when something tried to decide with it. A role's trust policy was the sharpest case: the trust evaluator reads a document it cannot parse as matching nothing, so a role written that way could no longer be assumed. The reading goes in IamService rather than at the Query front door, for two reasons. Three CloudFormation provisioners write these documents straight through the service and would otherwise be held to nothing. And the order AWS answers in differs per operation, which only the service can express: CreateRole, CreatePolicy, CreatePolicyVersion and UpdateAssumeRolePolicy read the document before the name clash or the entity lookup, where PutUserPolicy, PutGroupPolicy and PutRolePolicy answer NoSuchEntity for a holder that is not there whatever document the request carried. Which reading an operation asks for follows what the document is. A trust policy is held to the grammar alone and under its own validator's wording, "This policy contains invalid Json" for a text that does not parse as a JSON object and "Policy has no statements" for an object carrying no Statement, the element the grammar requires; its statements name a principal rather than resources, so an identity statement's elements are not asked of it. An inline policy is held to that grammar under the identity validator's one sentence for both, "Syntax errors in policy.", and to the values the language defines: a Version outside 2012-10-17 and 2008-10-17, an Effect that is not literally Allow or Deny, and a resource named neither by ARN nor by the wildcard. A managed policy is held to those and to each statement's own elements, "Policy statement must contain actions." and "Policy statement must contain resources.". None of those sentences is published and none is captured. They are the ones AWS's own emulator answers, a first-party implementation rather than a recording, and the split between the two validators' wordings is its reading too, which docs/services/iam.md records. The grammar is read in one place, not two. #8164 landed IamPolicyEvaluator.readsAsPolicyDocument for a document a request supplies to the two simulators or the two context-key readers, which asks exactly what the writes ask of the top level: parses as a JSON object, and carries Statement. The writes need to know which of the two failed in order to word their refusal, so the reading lives here and that method is now its boolean face rather than a second copy of it. Putting a document back is not a new write. A failed CloudFormation update restores the trust policy, the inline policy or the policy version it captured before the attempt, and a document an earlier release accepted must not be refused on the way back: that would leave the role or the policy without what the rollback had just removed, and the stack unable to roll back at all. So those three writes keep the reading on their public entry point and the provisioners restore through restoreAssumeRolePolicy, restoreRolePolicy and restorePolicyVersion, which apply the same write without it. The inline-policy provisioner already restored by writing the captured document straight back, so it needed nothing. This is the same rule as reading a stored document exactly as it was, applied to the one path that writes one. The whole text has to be the document. A member taking one takes "the complete, valid JSON text of an IAM policy", and Jackson reads the first value and stops unless it is told otherwise, so an object with anything after it parsed as a policy. The reader enables FAIL_ON_TRAILING_TOKENS, which is what the reference implementation's parser does by construction. A null document is nothing to judge: a required member the request leaves out is answered as absent by the constraints ahead of the operation. What is already stored is read exactly as it was, so a document an earlier version accepted still decides what it decided. Holding every caller means holding the fixtures too, the compatibility suites included: a Python and a Java SDK test each wrote an inline policy carrying only Version, and two roles in the EKS access-entry test were created with a trust policy of {}. All three are documents IAM refuses, the first two being the case recorded for a document that is valid JSON carrying only Version. Holding every caller means holding the fixtures too. Filler documents across the test tree, "{}" as a trust policy chief among them, were documents real AWS refuses; they now come from one place, io.floci.aws.testing.IamTestPolicies, which also answers a distinct document per marker for a test that needs several. Two fixtures changed in substance rather than in spelling: a Step Functions policy meant to name another event bus named it by bare name, which an identity policy may not, and a managed policy version's statements named no action.
This commit is contained in:
1 parent
38085d1d75
commit
9b24a38afe
45 files changed
+995
-188
No files matched your search
+1
-1
@@ -228,7 +228,7 @@ public class IamManagedPolicyCfnProvisioner implements CfnResourceProvisioner {
|
||||
String document = prunedVersion.getDocument();
|
||||
String restoreContentDescription = "restore pruned policy version content on " + policyArn;
|
||||
if (!CfnRollback.attemptIamCleanup(failure, restoreContentDescription,
|
||||
() -> iamService.createPolicyVersion(policyArn, document, false))) {
|
||||
() -> iamService.restorePolicyVersion(policyArn, document, false))) {
|
||||
cleanupSucceeded = false;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -270,7 +270,7 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
|
||||
String prior = originalInlinePolicies.get(policyName);
|
||||
if (prior != null && !CfnRollback.attemptIamCleanup(failure,
|
||||
"restore inline policy " + policyName + " on role " + resolvedRoleName,
|
||||
() -> iamService.putRolePolicy(resolvedRoleName, policyName, prior))) {
|
||||
() -> iamService.restoreRolePolicy(resolvedRoleName, policyName, prior))) {
|
||||
cleanupSucceeded = false;
|
||||
}
|
||||
}
|
||||
@@ -308,7 +308,7 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
|
||||
// recreated under this name in between, this must not write the restored document
|
||||
// onto the replacement.
|
||||
if (!CfnRollback.attemptIamCleanup(failure, "restore prior trust policy on role " + resolvedRoleName,
|
||||
() -> iamService.updateAssumeRolePolicy(
|
||||
() -> iamService.restoreAssumeRolePolicy(
|
||||
resolvedRoleName, documentToRestore, roleIdToRestore))) {
|
||||
cleanupSucceeded = false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,299 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Whether a policy document a write was handed is a policy document, and the sentence IAM refuses
|
||||
* one that is not with.
|
||||
*
|
||||
* <p>Every operation here models {@code MalformedPolicyDocument}, "the request was rejected because
|
||||
* the policy document was malformed", and nothing in this package answered it: a document that was
|
||||
* no policy at all was stored and only told apart later, when something tried to decide with it. A
|
||||
* trust policy that is not a policy is the sharpest case, since the evaluator reads one it cannot
|
||||
* parse as matching nothing, so a role written that way can no longer be assumed.
|
||||
*
|
||||
* <p>Two readings are applied, and which of them an operation asks for follows what the document
|
||||
* is rather than which operation writes it:
|
||||
*
|
||||
* <ul>
|
||||
* <li>the <em>grammar</em>, which every policy document is held to: it has to parse as a JSON
|
||||
* object and carry {@code Statement}, "the main element for a policy. This element is
|
||||
* required.";</li>
|
||||
* <li>the <em>values</em> the elements declare, which an identity-based document is held to: the
|
||||
* version it declares has to be one the language defines, a statement's {@code Effect} has to
|
||||
* be {@code Allow} or {@code Deny} compared literally, and a resource it names has to be
|
||||
* named by ARN or by the wildcard.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>A managed policy's document is held to its statements' own elements as well, each statement
|
||||
* naming the actions it is about and the resources those apply to. A trust policy is held to the
|
||||
* grammar alone, and to its own wording of it: IAM validates a role's trust policy with a validator
|
||||
* that tells a document which does not parse from one that parses and describes no statement, where
|
||||
* the identity validator answers one sentence for both.
|
||||
*
|
||||
* <p>The wordings are AWS's own, taken from its emulator's implementation rather than from a
|
||||
* capture or a published sentence, which {@code docs/services/iam.md} records. What is already
|
||||
* stored is not re-read: this answers what a request supplies.
|
||||
*
|
||||
* <p>A null document is nothing to judge and passes: a required member the request leaves out is
|
||||
* what the constraints ahead of the operation answer, and they answer it as absent. Every write
|
||||
* here reaches this through {@link IamService}, so a caller that never touches the Query front
|
||||
* door, a CloudFormation provisioner among them, is held to the same reading, and each write asks
|
||||
* for it where AWS answers it: before the name clash on a create, before the entity is resolved on
|
||||
* {@code UpdateAssumeRolePolicy} and {@code CreatePolicyVersion}, and after the holder is resolved
|
||||
* on the three inline writes, which answer {@code NoSuchEntity} for a principal that is not there
|
||||
* whatever document the request carried.
|
||||
*/
|
||||
final class IamPolicyDocuments {
|
||||
|
||||
/** The versions of the policy language that exist, so a document declaring another is refused. */
|
||||
private static final List<String> VERSIONS = List.of("2012-10-17", "2008-10-17");
|
||||
|
||||
/** The two effects a statement may declare, compared as written. */
|
||||
private static final List<String> EFFECTS = List.of("Allow", "Deny");
|
||||
|
||||
/** What every supplied document that is no policy document is refused with. */
|
||||
private static final String SYNTAX_ERRORS_IN_POLICY = "Syntax errors in policy.";
|
||||
|
||||
/** The same for a role's trust policy, whose validator words the two cases apart. */
|
||||
private static final String TRUST_POLICY_INVALID_JSON = "This policy contains invalid Json";
|
||||
|
||||
/** A trust policy that parses and describes no statement. */
|
||||
private static final String TRUST_POLICY_NO_STATEMENTS = "Policy has no statements";
|
||||
|
||||
private static final String STATEMENT_WITHOUT_ACTIONS = "Policy statement must contain actions.";
|
||||
|
||||
private static final String STATEMENT_WITHOUT_RESOURCES = "Policy statement must contain resources.";
|
||||
|
||||
/** What a statement declaring an effect the language does not define is refused with. */
|
||||
private static final String POLICY_PARSE_FAILED = "The policy failed legacy parsing";
|
||||
|
||||
private static final String ANY_RESOURCE = "*";
|
||||
|
||||
private static final String ARN_PREFIX = "arn:";
|
||||
|
||||
/** Parsing is a pure function of the text, and this reader holds no state of its own. */
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper()
|
||||
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
|
||||
|
||||
/**
|
||||
* Why a document is no policy document, which is what lets each caller word it as IAM does: a
|
||||
* text that is no JSON object failed to parse, where an object without {@code Statement}
|
||||
* parsed and described nothing.
|
||||
*/
|
||||
private enum Grammar { SYNTAX, NO_STATEMENTS, NO_ACTIONS, NO_RESOURCES }
|
||||
|
||||
private IamPolicyDocuments() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds {@code document} to what a role's trust policy must be. The grammar alone: a trust
|
||||
* policy names a principal and the actions it may take rather than resources, so the elements
|
||||
* an identity-based statement must carry are not asked of it, and neither are the values an
|
||||
* identity-based document declares.
|
||||
*/
|
||||
static void checkTrustPolicy(String document) {
|
||||
if (document == null) {
|
||||
return;
|
||||
}
|
||||
Grammar defect = grammarDefect(document);
|
||||
if (defect == Grammar.SYNTAX) {
|
||||
throw malformed(TRUST_POLICY_INVALID_JSON);
|
||||
}
|
||||
if (defect == Grammar.NO_STATEMENTS) {
|
||||
throw malformed(TRUST_POLICY_NO_STATEMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds {@code document} to what an inline identity policy must be: the grammar, worded as the
|
||||
* identity validator words it, and the values the language defines.
|
||||
*/
|
||||
static void checkInlinePolicy(String document) {
|
||||
if (document == null) {
|
||||
return;
|
||||
}
|
||||
checkIdentityGrammar(document, false);
|
||||
checkIdentityValues(document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Holds {@code document} to what a managed policy must be, which is an inline policy's reading
|
||||
* and its statements' own elements besides: every statement of an identity-based policy names
|
||||
* the actions it is about and the resources they apply to.
|
||||
*/
|
||||
static void checkManagedPolicy(String document) {
|
||||
if (document == null) {
|
||||
return;
|
||||
}
|
||||
checkIdentityGrammar(document, true);
|
||||
checkIdentityValues(document);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code document} reads as a policy document at all, which is the grammar above
|
||||
* answered as a boolean for a caller that needs no sentence of its own.
|
||||
* {@link IamPolicyEvaluator} asks this of a document a request supplied, the four operations
|
||||
* taking one answering the input rather than deciding from a text they cannot read, so the
|
||||
* writes here and that gate hold a document to the same top-level shape.
|
||||
*/
|
||||
static boolean readsAsPolicyDocument(String document) {
|
||||
return document != null && grammarDefect(document) == null;
|
||||
}
|
||||
|
||||
private static void checkIdentityGrammar(String document, boolean statementElements) {
|
||||
Grammar defect = grammarDefect(document);
|
||||
if (defect == null && statementElements) {
|
||||
defect = statementElementDefect(document);
|
||||
}
|
||||
if (defect == null) {
|
||||
return;
|
||||
}
|
||||
throw malformed(switch (defect) {
|
||||
case SYNTAX, NO_STATEMENTS -> SYNTAX_ERRORS_IN_POLICY;
|
||||
case NO_ACTIONS -> STATEMENT_WITHOUT_ACTIONS;
|
||||
case NO_RESOURCES -> STATEMENT_WITHOUT_RESOURCES;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The values an identity-based document declares, read after the grammar and only for a
|
||||
* document the grammar accepted, so a text that is no policy is answered as one rather than
|
||||
* through whichever value it happens to carry.
|
||||
*/
|
||||
private static void checkIdentityValues(String document) {
|
||||
JsonNode root = parse(document);
|
||||
if (root == null || !root.isObject()) {
|
||||
return;
|
||||
}
|
||||
JsonNode version = root.get("Version");
|
||||
if (version != null && !version.isNull() && !VERSIONS.contains(version.asText())) {
|
||||
throw malformed(SYNTAX_ERRORS_IN_POLICY);
|
||||
}
|
||||
for (JsonNode statement : statements(root)) {
|
||||
JsonNode effect = statement.get("Effect");
|
||||
if (effect != null && !EFFECTS.contains(effect.asText())) {
|
||||
throw malformed(POLICY_PARSE_FAILED);
|
||||
}
|
||||
for (String resource : resourceValues(statement)) {
|
||||
if (!resource.startsWith(ARN_PREFIX) && !ANY_RESOURCE.equals(resource)) {
|
||||
throw malformed("Resource " + resource + " must be in ARN format or \""
|
||||
+ ANY_RESOURCE + "\".");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Why {@code document} is no policy document at all, or null when it is one. The one place the
|
||||
* top-level grammar is read, so every operation taking a document refuses the same texts.
|
||||
*/
|
||||
private static Grammar grammarDefect(String document) {
|
||||
JsonNode root = parse(document);
|
||||
if (root == null || !root.isObject()) {
|
||||
return Grammar.SYNTAX;
|
||||
}
|
||||
return root.has("Statement") ? null : Grammar.NO_STATEMENTS;
|
||||
}
|
||||
|
||||
/**
|
||||
* The element a statement of an identity-based policy names nothing for, or null when every
|
||||
* statement names both. A statement that is not an object is no statement, which is the syntax
|
||||
* defect rather than a missing element.
|
||||
*/
|
||||
private static Grammar statementElementDefect(String document) {
|
||||
JsonNode root = parse(document);
|
||||
JsonNode declared = root == null ? null : root.get("Statement");
|
||||
if (declared == null) {
|
||||
return null;
|
||||
}
|
||||
for (JsonNode statement : declared.isArray() ? declared : List.of(declared)) {
|
||||
if (!statement.isObject()) {
|
||||
return Grammar.SYNTAX;
|
||||
}
|
||||
if (!namesSomething(statement, "Action", "NotAction")) {
|
||||
return Grammar.NO_ACTIONS;
|
||||
}
|
||||
if (!namesSomething(statement, "Resource", "NotResource")) {
|
||||
return Grammar.NO_RESOURCES;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The statements of a document the grammar accepted, a lone statement object included. */
|
||||
private static List<JsonNode> statements(JsonNode root) {
|
||||
JsonNode declared = root.get("Statement");
|
||||
if (declared == null) {
|
||||
return List.of();
|
||||
}
|
||||
List<JsonNode> statements = new ArrayList<>();
|
||||
for (JsonNode statement : declared.isArray() ? declared : List.of(declared)) {
|
||||
if (statement.isObject()) {
|
||||
statements.add(statement);
|
||||
}
|
||||
}
|
||||
return statements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a statement names anything under any of {@code spellings}. An element that is absent,
|
||||
* null or an empty list names nothing, so it satisfies no requirement of the grammar.
|
||||
*/
|
||||
private static boolean namesSomething(JsonNode statement, String... spellings) {
|
||||
for (String spelling : spellings) {
|
||||
JsonNode element = statement.get(spelling);
|
||||
if (element == null || element.isNull()) {
|
||||
continue;
|
||||
}
|
||||
if (element.isArray() && element.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Every value a statement's resource elements name, in the order the document carries them. */
|
||||
private static List<String> resourceValues(JsonNode statement) {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (String spelling : new String[] {"Resource", "NotResource"}) {
|
||||
JsonNode element = statement.get(spelling);
|
||||
if (element == null) {
|
||||
continue;
|
||||
}
|
||||
for (JsonNode value : element.isArray() ? element : List.of(element)) {
|
||||
if (value.isTextual()) {
|
||||
values.add(value.asText());
|
||||
}
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private static JsonNode parse(String document) {
|
||||
if (document == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
// The WHOLE text has to be the document: a member taking one takes "the complete,
|
||||
// valid JSON text of an IAM policy", so an object with anything after it is no policy
|
||||
// document, which is what FAIL_ON_TRAILING_TOKENS answers here.
|
||||
return MAPPER.readTree(document);
|
||||
} catch (JsonProcessingException unreadable) {
|
||||
// Not JSON, so no policy document either, and the caller's sentence says so.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsException malformed(String message) {
|
||||
return new AwsException("MalformedPolicyDocument", message, 400);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
@@ -1691,17 +1690,11 @@ public class IamPolicyEvaluator {
|
||||
* not make every later call fail.
|
||||
*/
|
||||
public boolean readsAsPolicyDocument(String document) {
|
||||
if (document == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
JsonNode root = objectMapper.readTree(document);
|
||||
return root.isObject() && root.has("Statement");
|
||||
} catch (JsonProcessingException unreadable) {
|
||||
// Not JSON, so no policy document either, and the caller words the refusal. A stored
|
||||
// document carrying the same text is logged where it is parsed, in parseUncached.
|
||||
return false;
|
||||
}
|
||||
// One reader of the grammar rather than two: the writes hold a document to this same
|
||||
// top-level shape and need to know which way it failed in order to word their refusal, so
|
||||
// the reading lives in IamPolicyDocuments and this is its boolean face. A stored document
|
||||
// carrying the same text is logged where it is parsed, in parseUncached.
|
||||
return IamPolicyDocuments.readsAsPolicyDocument(document);
|
||||
}
|
||||
|
||||
private List<PolicyStatement> parseAll(List<String> documents) {
|
||||
|
||||
@@ -1422,31 +1422,10 @@ public class IamQueryHandler {
|
||||
|
||||
private Response handleUpdateAssumeRolePolicy(MultivaluedMap<String, String> params) {
|
||||
iamService.updateAssumeRolePolicy(requireParam(params, "RoleName"),
|
||||
requireTrustPolicyDocument(params));
|
||||
requireParam(params, "PolicyDocument"));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("UpdateAssumeRolePolicy", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* The trust policy this operation writes, which a blank is not. {@code policyDocumentType}
|
||||
* declares a minimum of 1 beside a pattern that admits the space, tab, line feed and carriage
|
||||
* return characters, so the shape does not exclude a blank, and this write replaces the policy
|
||||
* a role is already assumed under: the trust evaluator reads a blank document as matching
|
||||
* nothing, which would leave a role that could be assumed unassumable.
|
||||
*
|
||||
* <p>So the one value the shape lets through here is answered with the modelled
|
||||
* {@code MalformedPolicyDocument}, worded as IAM's trust-policy validator words a document that
|
||||
* does not parse, which is what that validator answers rather than the sentence an identity
|
||||
* policy's gets. Nothing further is read: every other document that is no policy is taken by
|
||||
* this operation and by every other write, which is one gap of its own rather than this
|
||||
* member's.
|
||||
*/
|
||||
private String requireTrustPolicyDocument(MultivaluedMap<String, String> params) {
|
||||
String document = requireParam(params, "PolicyDocument");
|
||||
if (document.isBlank()) {
|
||||
throw new AwsException("MalformedPolicyDocument", "This policy contains invalid Json", 400);
|
||||
}
|
||||
return document;
|
||||
}
|
||||
|
||||
private Response handleTagRole(MultivaluedMap<String, String> params) {
|
||||
iamService.tagRole(getParam(params, "RoleName"), extractTags(params, true));
|
||||
|
||||
@@ -1184,6 +1184,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
String description, int maxSessionDuration, Map<String, String> tags,
|
||||
String permissionsBoundaryArn) {
|
||||
validateRoleName(roleName);
|
||||
IamPolicyDocuments.checkTrustPolicy(assumeRolePolicyDocument);
|
||||
validateRoleDescription(description);
|
||||
if (permissionsBoundaryArn != null) {
|
||||
requirePolicy(permissionsBoundaryArn); // validate before anything is created
|
||||
@@ -1578,6 +1579,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
|
||||
public void updateAssumeRolePolicy(String roleName, String policyDocument) {
|
||||
validateRoleName(roleName);
|
||||
IamPolicyDocuments.checkTrustPolicy(policyDocument);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
role.setAssumeRolePolicyDocument(policyDocument);
|
||||
@@ -1592,7 +1594,27 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* that check and this call would silently receive the update meant for the original role.
|
||||
*/
|
||||
public void updateAssumeRolePolicy(String roleName, String policyDocument, String expectedRoleId) {
|
||||
updateAssumeRolePolicy(roleName, policyDocument, expectedRoleId, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes {@code policyDocument} back without holding it to being a policy document, for a caller
|
||||
* putting back what this store already held. A failed CloudFormation update restores what it
|
||||
* captured before the attempt, and a document an earlier version of Floci accepted must not be
|
||||
* refused on the way back: that would leave the role without the policy it was assumed under and the stack unable to
|
||||
* roll back. A new write is held to the reading; putting an old one back is
|
||||
* not, which is the same rule as reading a stored document exactly as it was.
|
||||
*/
|
||||
public void restoreAssumeRolePolicy(String roleName, String policyDocument, String expectedRoleId) {
|
||||
updateAssumeRolePolicy(roleName, policyDocument, expectedRoleId, false);
|
||||
}
|
||||
|
||||
private void updateAssumeRolePolicy(String roleName, String policyDocument, String expectedRoleId,
|
||||
boolean heldToBeingAPolicy) {
|
||||
validateRoleName(roleName);
|
||||
if (heldToBeingAPolicy) {
|
||||
IamPolicyDocuments.checkTrustPolicy(policyDocument);
|
||||
}
|
||||
synchronized (resourceNameLock) {
|
||||
IamRole role = getRole(roleName);
|
||||
requireSameRole(role, roleName, expectedRoleId);
|
||||
@@ -1630,6 +1652,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
|
||||
public IamPolicy createPolicy(String policyName, String path, String description,
|
||||
String document, Map<String, String> tags) {
|
||||
IamPolicyDocuments.checkManagedPolicy(document);
|
||||
synchronized (resourceNameLock) {
|
||||
String normalizedPath = normalizePath(path);
|
||||
String arn = iamArn("policy", normalizedPath, policyName);
|
||||
@@ -1896,6 +1919,25 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
public PolicyVersion createPolicyVersion(String policyArn, String document, boolean setAsDefault) {
|
||||
return createPolicyVersion(policyArn, document, setAsDefault, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes {@code document} back without holding it to being a policy document, for a caller
|
||||
* putting back what this store already held. A failed CloudFormation update restores what it
|
||||
* captured before the attempt, and a document an earlier version of Floci accepted must not be
|
||||
* refused on the way back: that would leave the policy without the version the rollback pruned. A new write is held to the reading; putting an old one back is
|
||||
* not, which is the same rule as reading a stored document exactly as it was.
|
||||
*/
|
||||
public PolicyVersion restorePolicyVersion(String policyArn, String document, boolean setAsDefault) {
|
||||
return createPolicyVersion(policyArn, document, setAsDefault, false);
|
||||
}
|
||||
|
||||
private PolicyVersion createPolicyVersion(String policyArn, String document, boolean setAsDefault,
|
||||
boolean heldToBeingAPolicy) {
|
||||
if (heldToBeingAPolicy) {
|
||||
IamPolicyDocuments.checkManagedPolicy(document);
|
||||
}
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
Map<String, PolicyVersion> versions = policy.getVersions();
|
||||
@@ -2116,6 +2158,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public void putUserPolicy(String userName, String policyName, String policyDocument) {
|
||||
synchronized (resourceNameLock) {
|
||||
IamUser user = getUser(userName);
|
||||
IamPolicyDocuments.checkInlinePolicy(policyDocument);
|
||||
Map<String, String> inlinePolicies = user.getInlinePolicies();
|
||||
synchronized (inlinePolicies) {
|
||||
checkInlinePolicyQuota(inlinePolicies, policyName, policyDocument,
|
||||
@@ -2160,6 +2203,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
public void putGroupPolicy(String groupName, String policyName, String policyDocument) {
|
||||
synchronized (resourceNameLock) {
|
||||
IamGroup group = getGroup(groupName);
|
||||
IamPolicyDocuments.checkInlinePolicy(policyDocument);
|
||||
Map<String, String> inlinePolicies = group.getInlinePolicies();
|
||||
synchronized (inlinePolicies) {
|
||||
checkInlinePolicyQuota(inlinePolicies, policyName, policyDocument,
|
||||
@@ -2202,10 +2246,29 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
// =========================================================================
|
||||
|
||||
public void putRolePolicy(String roleName, String policyName, String policyDocument) {
|
||||
putRolePolicy(roleName, policyName, policyDocument, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes {@code policyDocument} back without holding it to being a policy document, for a caller
|
||||
* putting back what this store already held. A failed CloudFormation update restores what it
|
||||
* captured before the attempt, and a document an earlier version of Floci accepted must not be
|
||||
* refused on the way back: that would leave the role without the inline policy the rollback removed. A new write is held to the reading; putting an old one back is
|
||||
* not, which is the same rule as reading a stored document exactly as it was.
|
||||
*/
|
||||
public void restoreRolePolicy(String roleName, String policyName, String policyDocument) {
|
||||
putRolePolicy(roleName, policyName, policyDocument, false);
|
||||
}
|
||||
|
||||
private void putRolePolicy(String roleName, String policyName, String policyDocument,
|
||||
boolean heldToBeingAPolicy) {
|
||||
synchronized (resourceNameLock) {
|
||||
validateRoleName(roleName);
|
||||
IamRole role = getRole(roleName);
|
||||
requireNotServiceLinked(role, roleName);
|
||||
if (heldToBeingAPolicy) {
|
||||
IamPolicyDocuments.checkInlinePolicy(policyDocument);
|
||||
}
|
||||
Map<String, String> inlinePolicies = role.getInlinePolicies();
|
||||
synchronized (inlinePolicies) {
|
||||
checkInlinePolicyQuota(inlinePolicies, policyName, policyDocument,
|
||||
|
||||
Reference in new issue
Block a user