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:
Priamo Germosen authored and GitHub committed 2026-10-03 09:37:12 -05:00
1 parent a22de628e6
commit cd57deac18
4 files changed
+222 -27

No files matched your search

@@ -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.
@@ -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);
}