mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(apigateway): require API keys for imported methods (#5450)
* fix(apigateway): require API keys for imported methods * test(apigateway): cover API-key PutRestApi imports * fix(apigateway): honor OpenAPI security alternatives Keep imported methods public when an empty security alternative exists, reject API-key versus authorizer alternatives that a single API Gateway method cannot express, and verify imported API-key enforcement through deployed calls. * fix(apigateway): honor optional security alternatives * fix(apigateway): restrict imported API key schemes
This commit is contained in:
1 parent
c4d33a6bd8
commit
77113c7f85
2 files changed
+255
-5
No files matched your search
+18
-5
@@ -3390,6 +3390,7 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
// Lambda authorizer land as authorizationType=NONE and are silently open at runtime.
|
||||
Map<String, String> schemeToAuthorizerId = new HashMap<>();
|
||||
Map<String, String> schemeToAuthType = new HashMap<>();
|
||||
Set<String> apiKeySchemes = new HashSet<>();
|
||||
Map<String, String> authorizerNameToId = new HashMap<>();
|
||||
for (Authorizer authorizer : getAuthorizers(region, apiId)) {
|
||||
if (authorizer.getName() != null) {
|
||||
@@ -3407,6 +3408,12 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
SecurityScheme scheme = schemeEntry.getValue();
|
||||
String authtype = importedAuthorizationType(scheme, schemeName);
|
||||
Map<String, Object> authDef = importedAuthorizerDefinition(scheme, schemeName);
|
||||
if (authDef == null && scheme != null
|
||||
&& SecurityScheme.Type.APIKEY.equals(scheme.getType())
|
||||
&& scheme.getIn() != null && "header".equalsIgnoreCase(scheme.getIn().toString())
|
||||
&& scheme.getName() != null && "x-api-key".equalsIgnoreCase(scheme.getName())) {
|
||||
apiKeySchemes.add(schemeName);
|
||||
}
|
||||
if (authDef != null) {
|
||||
String t = importedAuthorizerType(authDef, schemeName); // token | request | cognito_user_pools
|
||||
Map<String, Object> req = new HashMap<>();
|
||||
@@ -3501,7 +3508,8 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
String httpMethod = opEntry.getKey().name().toUpperCase();
|
||||
try {
|
||||
applyOperation(region, apiId, resourceId, httpMethod, opEntry.getValue(), openAPI,
|
||||
schemeToAuthorizerId, schemeToAuthType, validatorNameToId, defaultValidatorId);
|
||||
schemeToAuthorizerId, schemeToAuthType, apiKeySchemes,
|
||||
validatorNameToId, defaultValidatorId);
|
||||
} catch (AwsException e) {
|
||||
if (PARAMETER_NAME_ERROR.equals(e.getMessage())) {
|
||||
throw importParameterNameFailure(httpMethod, path, e);
|
||||
@@ -3522,7 +3530,8 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
Operation anyOperation = parseAnyMethodOperation(path, anyMethodExt);
|
||||
try {
|
||||
applyOperation(region, apiId, resourceId, "ANY", anyOperation, openAPI,
|
||||
schemeToAuthorizerId, schemeToAuthType, validatorNameToId, defaultValidatorId);
|
||||
schemeToAuthorizerId, schemeToAuthType, apiKeySchemes,
|
||||
validatorNameToId, defaultValidatorId);
|
||||
} catch (AwsException e) {
|
||||
if (PARAMETER_NAME_ERROR.equals(e.getMessage())) {
|
||||
throw importParameterNameFailure("ANY", path, e);
|
||||
@@ -3583,7 +3592,8 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
|
||||
private void applyOperation(String region, String apiId, String resourceId, String httpMethod,
|
||||
Operation operation, OpenAPI openAPI, Map<String, String> schemeToAuthorizerId,
|
||||
Map<String, String> schemeToAuthType, Map<String, String> validatorNameToId,
|
||||
Map<String, String> schemeToAuthType, Set<String> apiKeySchemes,
|
||||
Map<String, String> validatorNameToId,
|
||||
String defaultValidatorId) {
|
||||
// Create the method
|
||||
Map<String, Object> methodRequest = new HashMap<>();
|
||||
@@ -3591,12 +3601,14 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
// to a method authorizationType (CUSTOM/AWS_IAM/COGNITO_USER_POOLS) + authorizerId.
|
||||
List<SecurityRequirement> secReqs = operation.getSecurity() != null
|
||||
? operation.getSecurity() : openAPI.getSecurity();
|
||||
boolean apiKeyRequired = secReqs != null && !secReqs.isEmpty()
|
||||
&& secReqs.stream().allMatch(requirement -> requirement.keySet().stream()
|
||||
.anyMatch(apiKeySchemes::contains));
|
||||
String authType = "NONE";
|
||||
String authorizerId = null;
|
||||
List<String> authorizationScopes = List.of();
|
||||
if (secReqs != null) {
|
||||
// AWS resolves the OR-list of security requirements to the first declared
|
||||
// authorizer scheme (a method has exactly one authorizer), so stop at the first match.
|
||||
// An API Gateway method can reference only one authorizer, so preserve the first match.
|
||||
resolveAuth:
|
||||
for (SecurityRequirement secReq : secReqs) {
|
||||
for (String schemeName : secReq.keySet()) {
|
||||
@@ -3615,6 +3627,7 @@ public class ApiGatewayService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
methodRequest.put("authorizationType", authType);
|
||||
methodRequest.put("apiKeyRequired", apiKeyRequired);
|
||||
if (authorizerId != null) {
|
||||
methodRequest.put("authorizerId", authorizerId);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user