-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AWS Lambda HTTP Security Integration
sam local docs convert lambda security to auth mechanism default providers add mechanism if empty credential types use QuarkusPrincipal
- Loading branch information
1 parent
bce6a9e
commit 60bd78a
Showing
44 changed files
with
1,171 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...ent/src/main/java/io/quarkus/amazon/lambda/http/deployment/LambdaHttpBuildTimeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.quarkus.amazon.lambda.http.deployment; | ||
|
||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
|
||
@ConfigRoot | ||
public class LambdaHttpBuildTimeConfig { | ||
/** | ||
* Enable security mechanisms to process lambda and AWS based security (i.e. Cognito, IAM) from | ||
* the http event sent from API Gateway | ||
*/ | ||
@ConfigItem(defaultValue = "false") | ||
public boolean enableSecurity; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...zon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/CognitoPrincipal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.amazon.lambda.http; | ||
|
||
import java.security.Principal; | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent; | ||
|
||
/** | ||
* Represents a Cognito JWT used to authenticate request | ||
* | ||
* Will only be allocated if requestContext.authorizer.jwt.claims.cognito:username is set | ||
* in the http event sent by API Gateway | ||
*/ | ||
public class CognitoPrincipal implements Principal { | ||
private APIGatewayV2HTTPEvent.RequestContext.Authorizer.JWT jwt; | ||
private String name; | ||
|
||
public CognitoPrincipal(APIGatewayV2HTTPEvent.RequestContext.Authorizer.JWT jwt) { | ||
this.jwt = jwt; | ||
this.name = jwt.getClaims().get("cognito:username"); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public APIGatewayV2HTTPEvent.RequestContext.Authorizer.JWT getClaims() { | ||
return jwt; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...azon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/CustomPrincipal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.amazon.lambda.http; | ||
|
||
import java.security.Principal; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents a custom principal sent by API Gateway i.e. a Lambda authorizer | ||
* | ||
* Will only be allocated if requestContext.authorizer.lambda.principalId is set | ||
* in the http event sent by API Gateway | ||
* | ||
*/ | ||
public class CustomPrincipal implements Principal { | ||
private String name; | ||
private Map<String, Object> claims; | ||
|
||
public CustomPrincipal(String name, Map<String, Object> claims) { | ||
this.claims = claims; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Map<String, Object> getClaims() { | ||
return claims; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ntime/src/main/java/io/quarkus/amazon/lambda/http/DefaultLambdaAuthenticationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.amazon.lambda.http; | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent; | ||
|
||
import io.quarkus.security.identity.request.BaseAuthenticationRequest; | ||
|
||
/** | ||
* This will execute if and only if there is no identity after invoking a LambdaAuthenticationRequest | ||
*/ | ||
final public class DefaultLambdaAuthenticationRequest extends BaseAuthenticationRequest { | ||
private APIGatewayV2HTTPEvent event; | ||
|
||
public DefaultLambdaAuthenticationRequest(APIGatewayV2HTTPEvent event) { | ||
this.event = event; | ||
} | ||
|
||
public APIGatewayV2HTTPEvent getEvent() { | ||
return event; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...tp/runtime/src/main/java/io/quarkus/amazon/lambda/http/DefaultLambdaIdentityProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.quarkus.amazon.lambda.http; | ||
|
||
import java.security.Principal; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent; | ||
|
||
import io.quarkus.security.identity.AuthenticationRequestContext; | ||
import io.quarkus.security.identity.IdentityProvider; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.runtime.QuarkusPrincipal; | ||
import io.quarkus.security.runtime.QuarkusSecurityIdentity; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@ApplicationScoped | ||
final public class DefaultLambdaIdentityProvider implements IdentityProvider<DefaultLambdaAuthenticationRequest> { | ||
|
||
@Override | ||
public Class<DefaultLambdaAuthenticationRequest> getRequestType() { | ||
return DefaultLambdaAuthenticationRequest.class; | ||
} | ||
|
||
@Override | ||
public Uni<SecurityIdentity> authenticate(DefaultLambdaAuthenticationRequest request, | ||
AuthenticationRequestContext context) { | ||
APIGatewayV2HTTPEvent event = request.getEvent(); | ||
SecurityIdentity identity = authenticate(event); | ||
if (identity == null) { | ||
return Uni.createFrom().optional(Optional.empty()); | ||
} | ||
return Uni.createFrom().item(identity); | ||
} | ||
|
||
/** | ||
* Create a SecurityIdentity with a principal derived from APIGatewayV2HTTPEvent. | ||
* Looks for Cognito JWT, IAM, or Custom Lambda metadata for principal name | ||
* | ||
* @param event | ||
* @return | ||
*/ | ||
public static SecurityIdentity authenticate(APIGatewayV2HTTPEvent event) { | ||
Principal principal = getPrincipal(event); | ||
if (principal == null) { | ||
return null; | ||
} | ||
QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(); | ||
builder.setPrincipal(principal); | ||
return builder.build(); | ||
} | ||
|
||
protected static Principal getPrincipal(APIGatewayV2HTTPEvent request) { | ||
final Map<String, String> systemEnvironment = System.getenv(); | ||
final boolean isSamLocal = Boolean.parseBoolean(systemEnvironment.get("AWS_SAM_LOCAL")); | ||
final APIGatewayV2HTTPEvent.RequestContext requestContext = request.getRequestContext(); | ||
if (isSamLocal && (requestContext == null || requestContext.getAuthorizer() == null)) { | ||
final String forcedUserName = systemEnvironment.get("QUARKUS_AWS_LAMBDA_FORCE_USER_NAME"); | ||
if (forcedUserName != null && !forcedUserName.isEmpty()) { | ||
return new QuarkusPrincipal(forcedUserName); | ||
} | ||
} else { | ||
if (requestContext != null) { | ||
final APIGatewayV2HTTPEvent.RequestContext.Authorizer authorizer = requestContext.getAuthorizer(); | ||
if (authorizer != null) { | ||
if (authorizer.getJwt() != null) { | ||
final APIGatewayV2HTTPEvent.RequestContext.Authorizer.JWT jwt = authorizer.getJwt(); | ||
final Map<String, String> claims = jwt.getClaims(); | ||
if (claims != null && claims.containsKey("cognito:username")) { | ||
return new CognitoPrincipal(jwt); | ||
} | ||
} else if (authorizer.getIam() != null) { | ||
if (authorizer.getIam().getUserId() != null) { | ||
return new IAMPrincipal(authorizer.getIam()); | ||
} | ||
} else if (authorizer.getLambda() != null) { | ||
Object tmp = authorizer.getLambda().get("principalId"); | ||
if (tmp != null && tmp instanceof String) { | ||
String username = (String) tmp; | ||
return new CustomPrincipal(username, authorizer.getLambda()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
.../amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/IAMPrincipal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.amazon.lambda.http; | ||
|
||
import java.security.Principal; | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent; | ||
|
||
/** | ||
* Used if IAM is used for authentication. | ||
* | ||
* Will only be allocated if requestContext.authorizer.iam.userId is set | ||
* in the http event sent by API Gateway | ||
*/ | ||
public class IAMPrincipal implements Principal { | ||
private String name; | ||
private APIGatewayV2HTTPEvent.RequestContext.IAM iam; | ||
|
||
public IAMPrincipal(APIGatewayV2HTTPEvent.RequestContext.IAM iam) { | ||
this.iam = iam; | ||
this.name = iam.getUserId(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public APIGatewayV2HTTPEvent.RequestContext.IAM getIam() { | ||
return iam; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaAuthenticationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.quarkus.amazon.lambda.http; | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent; | ||
|
||
import io.quarkus.security.identity.request.BaseAuthenticationRequest; | ||
|
||
public class LambdaAuthenticationRequest extends BaseAuthenticationRequest { | ||
private APIGatewayV2HTTPEvent event; | ||
|
||
public LambdaAuthenticationRequest(APIGatewayV2HTTPEvent event) { | ||
this.event = event; | ||
} | ||
|
||
public APIGatewayV2HTTPEvent getEvent() { | ||
return event; | ||
} | ||
} |
Oops, something went wrong.