fix(iam): look the user up before the service name on a credential request (#8314)

CreateServiceSpecificCredential resolved the service name first, so a request
that named a user who does not exist and a service that is not supported was
answered for the service. AWS answers for the user: a recording validated
against a real account asks for a credential as a user that was never created,
once with CodeCommit and once with a service name nothing supports, and both
come back NoSuchEntity with "The user with name <name> cannot be found."

The user is confirmed before the service name is read now, under the lock it was
already taken under. ListServiceSpecificCredentials had the same shape, with its
ServiceName filter resolved ahead of the user it lists for, and reads the two the
same way round. Under AllUsers there is no user to look up, so an unsupported
filter still answers for itself.

What the bounds of CredentialAgeDays say is settled ahead of both by the
constraints every request is held to, so only the question that needs the service
is left behind it: whether this service takes the member at all. The in-operation
bounds stay where they are as the backstop they became.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 13:44:43 -04:00
1 parent a138f88094
commit aa2adac677
3 files changed
+79 -17

No files matched your search

+9
View File
@@ -1534,6 +1534,15 @@ These are what AWS calls Git credentials when they are used with CodeCommit. A s
support them is `NoSuchEntity` with status 404 and the message "No such service `<name>` is
supported for Service Specific Credentials".
A request wrong in both halves answers for the user. Recordings validated against AWS answer a
`UserName` that names no user with "The user with name `<name>` cannot be found." whether the
`ServiceName` beside it is supported or not, so the user is looked up before the service name is
read, on `CreateServiceSpecificCredential` and on `ListServiceSpecificCredentials` alike. Under
`AllUsers` there is no user to look up and the filter is the only thing left to be wrong about. What
the bounds of `CredentialAgeDays` say is settled ahead of both, by the constraints every request is
held to; whether the service takes the member at all cannot be asked before the service is known,
so that answer stays behind them.
That is not what the wire model would suggest. The model declares `ServiceNotSupportedException`,
code `NotSupportedService`, on both `CreateServiceSpecificCredential` and
`ListServiceSpecificCredentials`, and AWS never raises it: recordings validated against AWS answer
@@ -3905,28 +3905,35 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
*
* <p>The secret half is returned here and by the reset, and nowhere else: there is no get
* operation, and the list is defined without it.
*
* <p>A request wrong in both halves is answered for the user: recordings validated against AWS
* answer a user that does not exist beside a service that is not supported with the user, so
* the user is confirmed before the service name is read. What the bounds of
* {@code CredentialAgeDays} say is settled ahead of either, by the constraints every request
* is held to; what is left here is whether the service this is takes the member at all, which
* cannot be asked before the service is known.
*/
public ServiceSpecificCredential createServiceSpecificCredential(
String userName, String requestedServiceName, Integer credentialAgeDays) {
String serviceName = supportedServiceName(requestedServiceName);
boolean longTermKey = LONG_TERM_API_KEY_SERVICES.contains(serviceName);
if (credentialAgeDays != null) {
if (!longTermKey) {
throw new AwsException("ValidationError",
"CredentialAgeDays is only valid for services that support long-term API "
+ "keys.", 400);
}
if (credentialAgeDays < MIN_CREDENTIAL_AGE_DAYS
|| credentialAgeDays > MAX_CREDENTIAL_AGE_DAYS) {
throw IamValidationErrors.validationError("credentialAgeDays",
String.valueOf(credentialAgeDays), "Member must be between "
+ MIN_CREDENTIAL_AGE_DAYS + " and " + MAX_CREDENTIAL_AGE_DAYS);
}
}
synchronized (serviceCredentialLock) {
// Confirmed under the lock, as the other per-user credentials are: DeleteUser checks
// for these holding the same lock.
getUser(userName);
String serviceName = supportedServiceName(requestedServiceName);
boolean longTermKey = LONG_TERM_API_KEY_SERVICES.contains(serviceName);
if (credentialAgeDays != null) {
if (!longTermKey) {
throw new AwsException("ValidationError",
"CredentialAgeDays is only valid for services that support long-term "
+ "API keys.", 400);
}
if (credentialAgeDays < MIN_CREDENTIAL_AGE_DAYS
|| credentialAgeDays > MAX_CREDENTIAL_AGE_DAYS) {
throw IamValidationErrors.validationError("credentialAgeDays",
String.valueOf(credentialAgeDays), "Member must be between "
+ MIN_CREDENTIAL_AGE_DAYS + " and " + MAX_CREDENTIAL_AGE_DAYS);
}
}
// One scan serves both checks: the quota counts this user's credentials for this
// service, the name search needs every name in the account, and no write can land
// between them while this holds the lock.
@@ -4070,8 +4077,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
/** Every credential of a user, or of every user when the caller asked for all of them. */
public List<ServiceSpecificCredential> listServiceSpecificCredentials(
String userName, String requestedServiceName, boolean allUsers) {
String serviceName = requestedServiceName == null
? null : supportedServiceName(requestedServiceName);
// The user before the service name, as on the create: a request wrong in both halves is
// answered for the user. Under AllUsers there is no user to look up, and the filter is
// then the only thing the request can be wrong about.
List<ServiceSpecificCredential> found;
if (allUsers) {
found = serviceCredentials.scan(key -> true);
@@ -4079,6 +4087,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
getUser(userName); // validates existence
found = userServiceCredentials(userName);
}
String serviceName = requestedServiceName == null
? null : supportedServiceName(requestedServiceName);
return found.stream()
.filter(c -> serviceName == null || serviceName.equals(c.getServiceName()))
.sorted(Comparator.comparing(
@@ -325,6 +325,49 @@ class ServiceSpecificCredentialIntegrationTest {
.when().post("/").then().statusCode(400).body(containsString("ValidationError"));
}
/**
* A request wrong in both halves is answered for the user. Recordings validated against AWS
* answer a user that does not exist with the user whether the service beside it is supported
* or not, so the user is looked up before the service name is read.
*/
@Test
void aRequestNamingNeitherAUserNorASupportedServiceAnswersForTheUser() {
String missing = "ssc-absent-" + suffix();
for (String service : List.of(CODECOMMIT, "nonexistentservice.amazonaws.com",
"lambda.amazonaws.com", "o3on3n3onosneo", "")) {
iam("CreateServiceSpecificCredential")
.formParam("UserName", missing)
.formParam("ServiceName", service)
.when().post("/").then().statusCode(404)
.body(containsString("NoSuchEntity"))
.body(containsString("The user with name " + missing + " cannot be found."))
.body(not(containsString("No such service")));
}
}
/** The list reads the two the same way round, and AllUsers leaves the filter alone to be wrong. */
@Test
void theListAnswersForTheUserBeforeTheServiceFilter() {
String missing = "ssc-absent-list-" + suffix();
iam("ListServiceSpecificCredentials")
.formParam("UserName", missing)
.formParam("ServiceName", "nonexistentservice.amazonaws.com")
.when().post("/").then().statusCode(404)
.body(containsString("NoSuchEntity"))
.body(containsString("The user with name " + missing + " cannot be found."))
.body(not(containsString("No such service")));
iam("ListServiceSpecificCredentials")
.formParam("AllUsers", "true")
.formParam("ServiceName", "nonexistentservice.amazonaws.com")
.when().post("/").then().statusCode(404)
.body(containsString("NoSuchEntity"))
.body(containsString("No such service nonexistentservice.amazonaws.com "
+ "is supported for Service Specific Credentials"));
}
/** The list takes the same filter, so it rejects the same way. */
@Test
void theListRefusesAnUnsupportedServiceFilter() {