mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr): answer GetClusterSessionCredentials for runtime role clusters (#8714)
GetClusterSessionCredentials, "temporary, HTTP basic credentials that are associated with a given runtime IAM role and used by a cluster with fine-grained access control activated", was not served. Floci now answers it with a random username and password that expire in an hour, for a RUNNING or WAITING cluster on emr-6.11.0 or later whose security configuration turns on runtime roles (EnableApplicationScopedIAMRole); any other cluster is refused. Floci runs no endpoint that checks them, and keeps none. A request without an ExecutionRoleArn is refused, as the credentials are "associated with a given runtime IAM role". Whether a cluster is a runtime role enabled one is now recorded as it is created, so a security configuration deleted or reused later changes nothing; CreatePersistentAppUI's RuntimeRoleEnabledCluster reads the same.
This commit is contained in:
1 parent
da39feee4e
commit
e078bb7b00
6 files changed
+194
-6
No files matched your search
@@ -109,6 +109,7 @@ public class EmrHandler {
|
||||
case "DescribePersistentAppUI" -> handleDescribePersistentAppUi(request);
|
||||
case "GetPersistentAppUIPresignedURL" -> handleGetPersistentAppUiPresignedUrl(request);
|
||||
case "GetOnClusterAppUIPresignedURL" -> handleGetOnClusterAppUiPresignedUrl(request);
|
||||
case "GetClusterSessionCredentials" -> handleGetClusterSessionCredentials(request);
|
||||
case "AddInstanceGroups" -> handleAddInstanceGroups(request);
|
||||
case "ListInstanceGroups" -> handleListInstanceGroups(request);
|
||||
case "AddInstanceFleet" -> handleAddInstanceFleet(request);
|
||||
@@ -915,6 +916,17 @@ public class EmrHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleGetClusterSessionCredentials(JsonNode request) {
|
||||
EmrService.ClusterSessionCredentials credentials = service.clusterSessionCredentials(
|
||||
string(request, "ClusterId"), string(request, "ExecutionRoleArn"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ObjectNode usernamePassword = response.putObject("Credentials").putObject("UsernamePassword");
|
||||
usernamePassword.put("Username", credentials.username());
|
||||
usernamePassword.put("Password", credentials.password());
|
||||
putEpoch(response, "ExpiresAt", credentials.expiresAt());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
// ──────────────────────────── Instance groups / fleets ────────────────────────────
|
||||
|
||||
private Response handleAddInstanceGroups(JsonNode request) {
|
||||
|
||||
@@ -59,6 +59,7 @@ import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -105,6 +106,9 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
private static final Logger LOG = Logger.getLogger(EmrService.class);
|
||||
private static final String UPPER_ALNUM = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
private static final int SESSION_PASSWORD_BYTES = 24;
|
||||
// No source gives how long GetClusterSessionCredentials' credentials last; Floci's expire in an hour.
|
||||
private static final Duration SESSION_CREDENTIALS_VALIDITY = Duration.ofHours(1);
|
||||
// AWS's answer to a change on a terminated cluster, for AddJobFlowSteps and ModifyInstanceFleet; Floci
|
||||
// gives it to AddInstanceGroups and AddInstanceFleet too, for one shutting down.
|
||||
private static final String JOB_FLOW_SHUT_DOWN =
|
||||
@@ -468,6 +472,9 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
throw new AwsException("ValidationException",
|
||||
"Security configuration does not exist: " + cluster.getSecurityConfiguration(), 400);
|
||||
}
|
||||
// A security configuration applies to the cluster as it is created: deleting or reusing its name later
|
||||
// changes nothing about the cluster.
|
||||
cluster.setRuntimeRoleEnabled(runtimeRoleEnabled(region, cluster.getSecurityConfiguration()));
|
||||
String id = "j-" + randomId(13);
|
||||
cluster.setId(id);
|
||||
cluster.setRegion(region);
|
||||
@@ -1057,15 +1064,21 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the cluster's security configuration turns on runtime roles, its
|
||||
* {@code AuthorizationConfiguration.IAMConfiguration.EnableApplicationScopedIAMRole}: false for a cluster with
|
||||
* none, or whose configuration has since been deleted.
|
||||
* Whether the cluster's security configuration turned on runtime roles as the cluster was created, its
|
||||
* {@code AuthorizationConfiguration.IAMConfiguration.EnableApplicationScopedIAMRole}; for a cluster stored before
|
||||
* Floci kept that, its configuration as it now stands. False for a cluster with none.
|
||||
*/
|
||||
public boolean runtimeRoleEnabled(EmrCluster cluster) {
|
||||
if (cluster.getSecurityConfiguration() == null) {
|
||||
return cluster.getRuntimeRoleEnabled() != null ? cluster.getRuntimeRoleEnabled()
|
||||
: runtimeRoleEnabled(regionOf(cluster), cluster.getSecurityConfiguration());
|
||||
}
|
||||
|
||||
// Whether the named security configuration of the Region turns on runtime roles; false for none, or one deleted.
|
||||
private boolean runtimeRoleEnabled(String region, String securityConfiguration) {
|
||||
if (securityConfiguration == null) {
|
||||
return false;
|
||||
}
|
||||
return secConfigStore.get(securityConfigurationKey(regionOf(cluster), cluster.getSecurityConfiguration()))
|
||||
return secConfigStore.get(securityConfigurationKey(region, securityConfiguration))
|
||||
.map(SecurityConfiguration::getSecurityConfiguration)
|
||||
.map(EmrService::rawJson)
|
||||
.map(json -> json.path("AuthorizationConfiguration").path("IAMConfiguration")
|
||||
@@ -1073,6 +1086,48 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* GetClusterSessionCredentials: "temporary, HTTP basic credentials that are associated with a given runtime IAM
|
||||
* role and used by a cluster with fine-grained access control activated". The cluster must be RUNNING or WAITING,
|
||||
* a runtime role enabled one ({@link #runtimeRoleEnabled}), on "Amazon EMR release 6.11 or higher", from which a
|
||||
* Workspace attaches to a cluster with a runtime role. Each refusal is InvalidRequestException in Floci's own
|
||||
* wording. Floci runs no endpoint that checks the credentials, and keeps none; no source gives how long AWS's
|
||||
* last, and Floci's expire in an hour.
|
||||
*/
|
||||
public ClusterSessionCredentials clusterSessionCredentials(String clusterId, String executionRoleArn) {
|
||||
if (executionRoleArn != null && (executionRoleArn.length() < 20 || executionRoleArn.length() > 2048)) {
|
||||
throw SmithyValidationErrors.validationException(
|
||||
SmithyValidationErrors.lengthOutOfRange("executionRoleArn", executionRoleArn, 20, 2048));
|
||||
}
|
||||
// The model leaves ExecutionRoleArn optional, but the credentials are "associated with a given runtime IAM
|
||||
// role", and with none there is none to associate them with; AWS's answer is not recorded.
|
||||
if (executionRoleArn == null) {
|
||||
throw invalid("ExecutionRoleArn is required: the credentials are for a runtime role.");
|
||||
}
|
||||
return call(() -> {
|
||||
EmrCluster cluster = settle(requireCluster(clusterId));
|
||||
if (!runsOrWaits(cluster)) {
|
||||
throw invalid("Cluster " + clusterId + " is " + cluster.getState()
|
||||
+ ", and its endpoints are there only while it is RUNNING or WAITING.");
|
||||
}
|
||||
if (!releaseAtLeast(cluster.getReleaseLabel(), 6, 11, 0)) {
|
||||
throw invalid("Cluster session credentials need a cluster of release emr-6.11.0 or later.");
|
||||
}
|
||||
if (!runtimeRoleEnabled(cluster)) {
|
||||
throw invalid("Cluster " + clusterId + " is not a runtime role enabled cluster: its security"
|
||||
+ " configuration does not turn on EnableApplicationScopedIAMRole.");
|
||||
}
|
||||
byte[] password = new byte[SESSION_PASSWORD_BYTES];
|
||||
RANDOM.nextBytes(password);
|
||||
return new ClusterSessionCredentials("emr-" + randomId(16).toLowerCase(),
|
||||
HexFormat.of().formatHex(password), now().plus(SESSION_CREDENTIALS_VALIDITY));
|
||||
});
|
||||
}
|
||||
|
||||
/** GetClusterSessionCredentials' username and password, and when they expire. */
|
||||
public record ClusterSessionCredentials(String username, String password, Instant expiresAt) {
|
||||
}
|
||||
|
||||
public SecurityConfiguration describeSecurityConfiguration(String name, String region) {
|
||||
return secConfigStore.get(securityConfigurationKey(region, name)).orElseThrow(() -> new AwsException(
|
||||
"InvalidRequestException", "Security configuration does not exist: " + name, 400));
|
||||
|
||||
@@ -41,6 +41,9 @@ public class EmrCluster {
|
||||
private boolean keepJobFlowAliveWhenNoSteps;
|
||||
// RunJobFlow's SessionEnabled: whether the cluster takes Spark Connect sessions.
|
||||
private boolean sessionEnabled;
|
||||
// Whether its security configuration turned on runtime roles as it was created; null for a cluster stored before
|
||||
// Floci kept it.
|
||||
private Boolean runtimeRoleEnabled;
|
||||
private int normalizedInstanceHours;
|
||||
private int stepConcurrencyLevel = 1;
|
||||
private int ebsRootVolumeSize;
|
||||
@@ -252,4 +255,7 @@ public class EmrCluster {
|
||||
|
||||
public boolean isSessionEnabled() { return sessionEnabled; }
|
||||
public void setSessionEnabled(boolean sessionEnabled) { this.sessionEnabled = sessionEnabled; }
|
||||
|
||||
public Boolean getRuntimeRoleEnabled() { return runtimeRoleEnabled; }
|
||||
public void setRuntimeRoleEnabled(Boolean runtimeRoleEnabled) { this.runtimeRoleEnabled = runtimeRoleEnabled; }
|
||||
}
|
||||
Reference in new issue
Block a user