mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(iam): authorize a server-certificate rename against both names (#5016)
AWS requires the principal to hold permission on the old name and the new one, so that a rename into a name they may not write fails. ResourceArnBuilder named only the stored certificate, so a Deny on the destination did not match and the rename went through. UpdateServerCertificate now names both the current ARN and the one the request would produce, which the filter authorizes once per resource. The destination is built beside the stored ARN so it keeps the certificate's partition and account, and an update that renames and moves nothing still names a single resource.
This commit is contained in:
1 parent
a22de628e6
commit
cd57deac18
4 files changed
+222
-27
No files matched your search
@@ -742,6 +742,16 @@ every dispatched IAM action, which is tracked in
|
||||
[#4979](https://github.com/floci-io/floci/issues/4979) rather than bundled into the
|
||||
server-certificate work that mapped the first few.
|
||||
|
||||
**A certificate rename names two resources.** `UpdateServerCertificate` is evaluated against both
|
||||
the certificate's current ARN and the ARN that `NewServerCertificateName` or `NewPath` would
|
||||
produce, because AWS requires the principal to hold permission on the old name and the new one: a
|
||||
principal allowed to update `ProductionCert` but not `ProdCert` cannot rename the first into the
|
||||
second. A request naming several resources is authorized once per resource, so a `Deny` on either
|
||||
name refuses the rename, and the certificate keeps its original name and path. An update that
|
||||
changes neither the name nor the path names a single resource. The destination ARN is built beside
|
||||
the stored one, keeping the certificate's own partition and account, since a rename moves a
|
||||
certificate within an account rather than between partitions.
|
||||
|
||||
**Exception:** a bare 12-digit account-id key that equals its own account and sits under
|
||||
an effective SCP ceiling is **not** treated as an unknown key — it is evaluated against
|
||||
the SCP chain as the account root (see [Service control policies](#service-control-policies-scps)
|
||||
|
||||
@@ -18,6 +18,7 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -82,7 +83,7 @@ public class ResourceArnBuilder {
|
||||
case "secretsmanager" -> List.of(buildSecretsManagerArn(ctx, region, accountId));
|
||||
case "ssm" -> List.of(buildSsmArn(ctx, region, accountId));
|
||||
case "kms" -> List.of(buildKmsArn(path, region, accountId));
|
||||
case "iam" -> List.of(buildIamArn(ctx, region, accountId));
|
||||
case "iam" -> buildIamArns(ctx, region, accountId);
|
||||
default -> List.of("*");
|
||||
};
|
||||
}
|
||||
@@ -104,34 +105,72 @@ public class ResourceArnBuilder {
|
||||
*
|
||||
* <p>The path is taken from the stored certificate when one exists: a policy names the full
|
||||
* ARN, path included, and only the store knows the path.
|
||||
*
|
||||
* <p>A rename names two resources rather than one; see {@link #renameTargetArn}.
|
||||
*/
|
||||
private String buildIamArn(ContainerRequestContext ctx, String region, String accountId) {
|
||||
private List<String> buildIamArns(ContainerRequestContext ctx, String region,
|
||||
String accountId) {
|
||||
String action = AwsQueryServiceResolver.action(
|
||||
RequestBodyReader.formField(ctx, "Action"),
|
||||
RequestBodyReader.formField(ctx, "Operation"));
|
||||
if (action == null || !SERVER_CERTIFICATE_ACTIONS.contains(action)) {
|
||||
return "*";
|
||||
return List.of("*");
|
||||
}
|
||||
String name = RequestBodyReader.formField(ctx, "ServerCertificateName");
|
||||
if (name == null || name.isBlank()) {
|
||||
return "*";
|
||||
return List.of("*");
|
||||
}
|
||||
// Every operation but the upload acts on a certificate that already exists, so the check
|
||||
// uses that certificate's own stored ARN instead of deriving one again. The stored ARN
|
||||
// carries the path and, more to the point, the partition the certificate was created in:
|
||||
// a resource stays in its partition, so re-minting from the caller's signing region would
|
||||
// name an ARN that no resource has and leave a deny on the real one unmatched.
|
||||
if (!"UploadServerCertificate".equals(action)) {
|
||||
return storedServerCertificateArn(name);
|
||||
if ("UploadServerCertificate".equals(action)) {
|
||||
// An upload names a certificate that does not exist yet, so its ARN is minted from
|
||||
// the request: the path it asks for, in the partition it is being created in. Reading
|
||||
// a stored path here would authorize an upload to /team/ against the root path.
|
||||
// IAM is global, so the ARN carries no region. Minted in the request's partition
|
||||
// rather than through a blank-region Arn.of, which would silently mean the
|
||||
// commercial one.
|
||||
String path = normalizeArnPath(RequestBodyReader.formField(ctx, "Path"));
|
||||
return List.of(AwsArnUtils.Arn.global(AwsRegions.partitionFor(region), "iam",
|
||||
accountId, "server-certificate" + path + name).toString());
|
||||
}
|
||||
// An upload names a certificate that does not exist yet, so its ARN is minted from the
|
||||
// request: the path it asks for, in the partition it is being created in. Reading a stored
|
||||
// path here would authorize an upload to /team/ against the root path.
|
||||
String path = normalizeArnPath(RequestBodyReader.formField(ctx, "Path"));
|
||||
// IAM is global, so the ARN carries no region. Minted in the request's partition rather
|
||||
// than through a blank-region Arn.of, which would silently mean the commercial one.
|
||||
return AwsArnUtils.Arn.global(AwsRegions.partitionFor(region), "iam", accountId,
|
||||
"server-certificate" + path + name).toString();
|
||||
// Every other operation acts on a certificate that already exists, so the check uses that
|
||||
// certificate's own stored ARN instead of deriving one again. The stored ARN carries the
|
||||
// path and, more to the point, the partition the certificate was created in: a resource
|
||||
// stays in its partition, so re-minting from the caller's signing region would name an
|
||||
// ARN that no resource has and leave a deny on the real one unmatched.
|
||||
Optional<ServerCertificate> stored = storedServerCertificate(name);
|
||||
if (stored.isEmpty()) {
|
||||
return List.of("*");
|
||||
}
|
||||
String storedArn = stored.get().getArn();
|
||||
if (!"UpdateServerCertificate".equals(action)) {
|
||||
return List.of(storedArn);
|
||||
}
|
||||
String target = renameTargetArn(ctx, stored.get(), name, region, accountId);
|
||||
return storedArn.equals(target) ? List.of(storedArn) : List.of(storedArn, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN a rename or a move would produce. AWS requires the principal to be allowed on both
|
||||
* the old and the new name, so that a rename into a name they cannot write fails: naming both
|
||||
* resources is what enforces it, because the filter authorizes a request once per resource.
|
||||
*
|
||||
* <p>Built beside the stored ARN so it keeps the certificate's partition and account. Only
|
||||
* the path and the name change, and taking the partition from the caller's signing region
|
||||
* instead would name a resource in a partition the certificate does not live in. Returns the
|
||||
* stored ARN unchanged when the request renames and moves nothing.
|
||||
*/
|
||||
private String renameTargetArn(ContainerRequestContext ctx, ServerCertificate stored,
|
||||
String name, String region, String accountId) {
|
||||
String newName = RequestBodyReader.formField(ctx, "NewServerCertificateName");
|
||||
String newPath = RequestBodyReader.formField(ctx, "NewPath");
|
||||
String targetName = newName == null || newName.isBlank() ? name : newName;
|
||||
String targetPath = newPath == null || newPath.isBlank()
|
||||
? normalizeArnPath(stored.getPath())
|
||||
: normalizeArnPath(newPath);
|
||||
String partition = AwsArnUtils.partitionOrDefault(stored.getArn(),
|
||||
AwsRegions.partitionFor(region));
|
||||
String account = AwsArnUtils.accountOrDefault(stored.getArn(), accountId);
|
||||
return AwsArnUtils.Arn.global(partition, "iam", account,
|
||||
"server-certificate" + targetPath + targetName).toString();
|
||||
}
|
||||
|
||||
/** A request's Path as it appears in an ARN: slash-delimited, defaulting to a bare slash. */
|
||||
@@ -144,18 +183,18 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* The certificate's own ARN as stored, or {@code *} when this account has no certificate of
|
||||
* that name: the operation fails as NoSuchEntity either way, and minting an ARN for a resource
|
||||
* that is not there would only offer a policy something to match.
|
||||
* The stored certificate of that name in this account, carrying its own ARN. Empty when the
|
||||
* account has none, or when the stored record has no ARN to read: the operation fails as
|
||||
* NoSuchEntity either way, and minting an ARN for a resource that is not there would only
|
||||
* offer a policy something to match.
|
||||
*/
|
||||
private String storedServerCertificateArn(String name) {
|
||||
private Optional<ServerCertificate> storedServerCertificate(String name) {
|
||||
if (iamService == null) {
|
||||
return "*";
|
||||
return Optional.empty();
|
||||
}
|
||||
return iamService.get().findServerCertificate(name)
|
||||
.map(ServerCertificate::getArn)
|
||||
.filter(arn -> !arn.isBlank())
|
||||
.orElse("*");
|
||||
.filter(certificate -> certificate.getArn() != null
|
||||
&& !certificate.getArn().isBlank());
|
||||
}
|
||||
|
||||
// ── S3 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -144,6 +144,58 @@ class ResourceArnBuilderTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A rename is authorized against both names. The model requires the principal to be allowed on
|
||||
* the old and the new one, and the filter authorizes a request once per resource, so naming
|
||||
* both is what stops a rename into a name the caller may not write.
|
||||
*/
|
||||
@Test
|
||||
void iamNamesBothTheOldAndTheNewNameOnARename() {
|
||||
String stored = "arn:aws:iam::000000000000:server-certificate/old";
|
||||
setFormBody("Action=UpdateServerCertificate&ServerCertificateName=old"
|
||||
+ "&NewServerCertificateName=new");
|
||||
assertEquals(
|
||||
List.of(stored, "arn:aws:iam::000000000000:server-certificate/new"),
|
||||
backedBy("old", stored).buildResources("iam", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
/** A move renames the path, so the destination path is named the same way. */
|
||||
@Test
|
||||
void iamNamesTheDestinationPathOnAMove() {
|
||||
String stored = "arn:aws:iam::000000000000:server-certificate/old";
|
||||
setFormBody("Action=UpdateServerCertificate&ServerCertificateName=old&NewPath=/team/");
|
||||
assertEquals(
|
||||
List.of(stored, "arn:aws:iam::000000000000:server-certificate/team/old"),
|
||||
backedBy("old", stored).buildResources("iam", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
/**
|
||||
* An update that renames and moves nothing acts on one resource, so it names one. Repeating
|
||||
* the same ARN would make the request look like it touches two.
|
||||
*/
|
||||
@Test
|
||||
void iamNamesOneResourceWhenAnUpdateChangesNeitherNameNorPath() {
|
||||
String stored = "arn:aws:iam::000000000000:server-certificate/old";
|
||||
setFormBody("Action=UpdateServerCertificate&ServerCertificateName=old");
|
||||
assertEquals(List.of(stored),
|
||||
backedBy("old", stored).buildResources("iam", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The destination is built beside the stored ARN, so it keeps the certificate's partition. A
|
||||
* rename does not move a resource between partitions, and taking the partition from the
|
||||
* caller's signing region would name one that does not exist.
|
||||
*/
|
||||
@Test
|
||||
void iamBuildsTheRenameDestinationInTheStoredPartition() {
|
||||
String stored = "arn:aws-cn:iam::000000000000:server-certificate/old";
|
||||
setFormBody("Action=UpdateServerCertificate&ServerCertificateName=old"
|
||||
+ "&NewServerCertificateName=new");
|
||||
assertEquals(
|
||||
List.of(stored, "arn:aws-cn:iam::000000000000:server-certificate/new"),
|
||||
backedBy("old", stored).buildResources("iam", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Nothing stored under that name means nothing to name: the operation fails as NoSuchEntity
|
||||
* regardless, and minting an ARN for an absent resource would hand a policy something to match.
|
||||
|
||||
+94
@@ -15,6 +15,7 @@ import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* A policy statement naming a single server certificate has to bind to that certificate and no
|
||||
@@ -192,6 +193,99 @@ class ServerCertificateIamEnforcementIntegrationTest {
|
||||
userIam(akid, "ListServerCertificates", Map.of()).statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* The model is explicit that a rename needs permission on both names, so that a principal
|
||||
* allowed on the old one cannot rename a certificate into a name they may not write: "to
|
||||
* change the certificate named ProductionCert to ProdCert, the principal must have a policy
|
||||
* that allows them to update both certificates".
|
||||
*/
|
||||
@Test
|
||||
void aRenameIsDeniedWhenOnlyTheDestinationNameIsDenied() {
|
||||
String suffix = suffix();
|
||||
String from = "rename-from-" + suffix;
|
||||
String reserved = "rename-reserved-" + suffix;
|
||||
upload(from, null);
|
||||
|
||||
String akid = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"iam:*","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"iam:UpdateServerCertificate","Resource":"%s"}]}"""
|
||||
.formatted(arn(reserved)));
|
||||
|
||||
userIam(akid, "UpdateServerCertificate", Map.of("ServerCertificateName", from,
|
||||
"NewServerCertificateName", reserved))
|
||||
.statusCode(403).body(containsString("AccessDenied"));
|
||||
|
||||
// The rename did not happen: the certificate is still reachable under its old name, and
|
||||
// the denied name holds nothing.
|
||||
adminIam("GetServerCertificate", Map.of("ServerCertificateName", from)).statusCode(200);
|
||||
adminIam("GetServerCertificate", Map.of("ServerCertificateName", reserved)).statusCode(404);
|
||||
}
|
||||
|
||||
/** A move is a rename of the path, so the destination path is checked the same way. */
|
||||
@Test
|
||||
void aMoveIsDeniedWhenOnlyTheDestinationPathIsDenied() {
|
||||
String name = "moved-" + suffix();
|
||||
upload(name, null);
|
||||
|
||||
String akid = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"iam:*","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"iam:UpdateServerCertificate","Resource":"%s"}]}"""
|
||||
.formatted(arn("reserved/" + name)));
|
||||
|
||||
userIam(akid, "UpdateServerCertificate", Map.of("ServerCertificateName", name,
|
||||
"NewPath", "/reserved/"))
|
||||
.statusCode(403).body(containsString("AccessDenied"));
|
||||
|
||||
// The name alone proves nothing here: a moved certificate is still reachable by name, so
|
||||
// the path and the ARN are what show the move did not happen.
|
||||
adminIam("GetServerCertificate", Map.of("ServerCertificateName", name))
|
||||
.statusCode(200)
|
||||
.body("GetServerCertificateResponse.GetServerCertificateResult.ServerCertificate"
|
||||
+ ".ServerCertificateMetadata.Path", equalTo("/"))
|
||||
.body("GetServerCertificateResponse.GetServerCertificateResult.ServerCertificate"
|
||||
+ ".ServerCertificateMetadata.Arn", equalTo(arn(name)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The old name still has to be allowed. Naming the destination must not become a way to
|
||||
* rename a certificate the principal cannot otherwise touch.
|
||||
*/
|
||||
@Test
|
||||
void aRenameIsDeniedWhenOnlyTheSourceNameIsDenied() {
|
||||
String suffix = suffix();
|
||||
String from = "source-denied-" + suffix;
|
||||
upload(from, null);
|
||||
|
||||
String akid = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"iam:*","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"iam:UpdateServerCertificate","Resource":"%s"}]}"""
|
||||
.formatted(arn(from)));
|
||||
|
||||
userIam(akid, "UpdateServerCertificate", Map.of("ServerCertificateName", from,
|
||||
"NewServerCertificateName", "source-denied-target-" + suffix))
|
||||
.statusCode(403).body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
/** With both names allowed the rename goes through, so the extra resource is not a blanket no. */
|
||||
@Test
|
||||
void aRenameSucceedsWhenBothNamesAreAllowed() {
|
||||
String suffix = suffix();
|
||||
String from = "both-from-" + suffix;
|
||||
String to = "both-to-" + suffix;
|
||||
upload(from, null);
|
||||
|
||||
String akid = userWith("""
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"iam:*","Resource":"*"}]}""");
|
||||
|
||||
userIam(akid, "UpdateServerCertificate", Map.of("ServerCertificateName", from,
|
||||
"NewServerCertificateName", to)).statusCode(200);
|
||||
adminIam("GetServerCertificate", Map.of("ServerCertificateName", to)).statusCode(200);
|
||||
}
|
||||
|
||||
private static String suffix() {
|
||||
return UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user