mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(appsync): use ApiKey.id as the API key value (#2645)
CreateApiKey returned a short random id and kept the value the execute endpoint validates in a non-model apiKey field, which the AWS CLI and SDKs drop. On AWS the id itself is the da2- key value sent as x-api-key, so make id that value, validate against it, and drop the extra field. Keys persisted by earlier builds keep their short id, stay listable and deletable, and never authenticate (as before).
This commit is contained in:
9 files changed
+70
-42
No files matched your search
@@ -17,6 +17,7 @@ import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
@@ -26,6 +27,12 @@ import java.util.Base64;
|
||||
public class AppSyncService {
|
||||
private static final Logger LOG = Logger.getLogger(AppSyncService.class);
|
||||
|
||||
// AWS issues API keys as "da2-" followed by 26 lowercase alphanumerics, and ApiKey.id is
|
||||
// that value: it is what clients send in the x-api-key header.
|
||||
private static final String API_KEY_PREFIX = "da2-";
|
||||
private static final String API_KEY_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
private static final int API_KEY_RANDOM_LENGTH = 26;
|
||||
|
||||
private final StorageBackend<String, GraphqlApi> apiStore;
|
||||
private final AccountAwareStorageBackend<String> schemaStore;
|
||||
private final AccountAwareStorageBackend<SchemaCreationStatus> schemaStatusStore;
|
||||
@@ -33,6 +40,8 @@ public class AppSyncService {
|
||||
private final StorageBackend<String, Resolver> resolverStore;
|
||||
private final StorageBackend<String, FunctionConfiguration> functionStore;
|
||||
private final StorageBackend<String, ApiKey> apiKeyStore;
|
||||
// Instance field on purpose: a static SecureRandom would be captured in the native image heap.
|
||||
private final SecureRandom apiKeyRandom = new SecureRandom();
|
||||
private final StorageBackend<String, AppSyncType> typeStore;
|
||||
private final StorageBackend<String, DomainName> domainStore;
|
||||
private final StorageBackend<String, String> associationStore;
|
||||
@@ -585,7 +594,7 @@ public class AppSyncService {
|
||||
"The API key exceeded a limit.", 400);
|
||||
}
|
||||
ApiKey key = new ApiKey();
|
||||
key.setId(generateShortId());
|
||||
key.setId(generateApiKeyId());
|
||||
key.setApiId(apiId);
|
||||
key.setDescription((String) request.get("description"));
|
||||
Object expiresValue = request.get("expires");
|
||||
@@ -594,8 +603,6 @@ public class AppSyncService {
|
||||
: parseExpires(expiresValue);
|
||||
applyApiKeyExpires(key, expires);
|
||||
|
||||
key.setApiKey("da2-" + generateShortId());
|
||||
|
||||
apiKeyStore.put(apiKey(apiId, key.getId()), key);
|
||||
return key;
|
||||
}
|
||||
@@ -615,7 +622,9 @@ public class AppSyncService {
|
||||
}
|
||||
long now = clock.instant().getEpochSecond();
|
||||
for (ApiKey key : apiKeyStore.scan(k -> k.startsWith(apiId + "::"))) {
|
||||
if (keyValue.equals(key.getApiKey())) {
|
||||
// Keys persisted by earlier builds have a short id that was never a valid
|
||||
// x-api-key value; keep them listable and deletable but never authenticate them.
|
||||
if (key.getId() != null && key.getId().startsWith(API_KEY_PREFIX) && keyValue.equals(key.getId())) {
|
||||
if (key.getExpires() != null && key.getExpires() <= now) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@@ -1095,6 +1104,14 @@ public class AppSyncService {
|
||||
return UUID.randomUUID().toString().replace("-", "").substring(0, 7);
|
||||
}
|
||||
|
||||
private String generateApiKeyId() {
|
||||
StringBuilder sb = new StringBuilder(API_KEY_PREFIX);
|
||||
for (int i = 0; i < API_KEY_RANDOM_LENGTH; i++) {
|
||||
sb.append(API_KEY_ALPHABET.charAt(apiKeyRandom.nextInt(API_KEY_ALPHABET.length())));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String apiKey(String apiId, String name) {
|
||||
return apiId + "::" + name;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ public class ApiKey {
|
||||
private Long expires;
|
||||
private Long deletes;
|
||||
private String apiId;
|
||||
private String apiKey;
|
||||
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
@@ -29,7 +28,4 @@ public class ApiKey {
|
||||
|
||||
public String getApiId() { return apiId; }
|
||||
public void setApiId(String apiId) { this.apiId = apiId; }
|
||||
|
||||
public String getApiKey() { return apiKey; }
|
||||
public void setApiKey(String apiKey) { this.apiKey = apiKey; }
|
||||
}
|
||||
Reference in new issue
Block a user