Skip to content

Commit

Permalink
Merge pull request #44907 from mcruzdev/config-mapping-lambda
Browse files Browse the repository at this point in the history
Convert Lambda REST and HTTP to @ConfigMapping
  • Loading branch information
gsmet authored Dec 4, 2024
2 parents bedb080 + 9b2367c commit eab567b
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 52 deletions.
3 changes: 0 additions & 3 deletions extensions/amazon-lambda-http/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setupCDI(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
@BuildStep
public void setupSecurity(BuildProducer<AdditionalBeanBuildItem> additionalBeans,
LambdaHttpBuildTimeConfig config) {
if (!config.enableSecurity)
if (!config.enableSecurity())
return;

AdditionalBeanBuildItem.Builder builder = AdditionalBeanBuildItem.builder().setUnremovable();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.quarkus.amazon.lambda.http.deployment;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot
public class LambdaHttpBuildTimeConfig {
@ConfigMapping(prefix = "quarkus.lambda-http")
public interface 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;
@WithDefault("false")
boolean enableSecurity();
}
1 change: 0 additions & 1 deletion extensions/amazon-lambda-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<module>runtime</module>
<module>http-event-server</module>
<module>deployment</module>

<module>maven-archetype</module>
</modules>

Expand Down
3 changes: 0 additions & 3 deletions extensions/amazon-lambda-http/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public long getIssuedAtTime() {
@Override
public Set<String> getGroups() {
if (groups == null) {
if (jwt.getClaims().containsKey(LambdaHttpRecorder.config.cognitoRoleClaim)) {
String claim = jwt.getClaims().get(LambdaHttpRecorder.config.cognitoRoleClaim);
if (jwt.getClaims().containsKey(LambdaHttpRecorder.config.cognitoRoleClaim())) {
String claim = jwt.getClaims().get(LambdaHttpRecorder.config.cognitoRoleClaim());
Matcher matcher = LambdaHttpRecorder.groupPattern.matcher(claim);
groups = new HashSet<>();
while (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Class<DefaultLambdaAuthenticationRequest> getRequestType() {
public Uni<SecurityIdentity> authenticate(DefaultLambdaAuthenticationRequest request,
AuthenticationRequestContext context) {
APIGatewayV2HTTPEvent event = request.getEvent();
SecurityIdentity identity = authenticate(event, LambdaHttpRecorder.config.mapCognitoToRoles);
SecurityIdentity identity = authenticate(event, LambdaHttpRecorder.config.mapCognitoToRoles());
if (identity == null) {
return Uni.createFrom().optional(Optional.empty());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
package io.quarkus.amazon.lambda.http;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public class LambdaHttpConfig {
@ConfigMapping(prefix = "quarkus.lambda-http")
public interface LambdaHttpConfig {

/**
* If true, Quarkus will map claims from Cognito to Quarkus security roles.
* The "cognito:groups" claim will be used by default. Change cognitoRoleClaim
* config value to change the claim source.
*
* <p>
* True by default
*/
@ConfigItem(defaultValue = "true")
public boolean mapCognitoToRoles;
@WithDefault("true")
boolean mapCognitoToRoles();

/**
* Cognito claim that contains roles you want to map. Defaults to "cognito:groups"
*/
@ConfigItem(defaultValue = "cognito:groups")
public String cognitoRoleClaim;
@WithDefault("cognito:groups")
String cognitoRoleClaim();

/**
* Regular expression to locate role values within a Cognito claim string.
* By default it looks for space delimited strings enclosed in brackets
* By default, it looks for space delimited strings enclosed in brackets
* "[^\[\] \t]+"
*/
@ConfigItem(defaultValue = "[^\\[\\] \\t]+")
public String cognitoClaimMatcher;
@WithDefault(value = "[^\\[\\] \\t]+")
String cognitoClaimMatcher();

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LambdaHttpRecorder {

public void setConfig(LambdaHttpConfig c) {
config = c;
String pattern = c.cognitoClaimMatcher;
String pattern = c.cognitoClaimMatcher();
groupPattern = Pattern.compile(pattern);
}
}
3 changes: 0 additions & 3 deletions extensions/amazon-lambda-rest/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setupCDI(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
@BuildStep
public void setupSecurity(BuildProducer<AdditionalBeanBuildItem> additionalBeans,
LambdaHttpBuildTimeConfig config) {
if (!config.enableSecurity)
if (!config.enableSecurity())
return;

AdditionalBeanBuildItem.Builder builder = AdditionalBeanBuildItem.builder().setUnremovable();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.quarkus.amazon.lambda.http.deployment;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot
public class LambdaHttpBuildTimeConfig {
@ConfigMapping(prefix = "quarkus.lambda-http")
public interface 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;
@WithDefault("false")
boolean enableSecurity();
}
3 changes: 0 additions & 3 deletions extensions/amazon-lambda-rest/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public long getIssuedAtTime() {
@Override
public Set<String> getGroups() {
if (groups == null) {
String grpClaim = claims.getClaim(LambdaHttpRecorder.config.cognitoRoleClaim);
String grpClaim = claims.getClaim(LambdaHttpRecorder.config.cognitoRoleClaim());
if (grpClaim != null) {
Matcher matcher = LambdaHttpRecorder.groupPattern.matcher(grpClaim);
groups = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Class<DefaultLambdaAuthenticationRequest> getRequestType() {
public Uni<SecurityIdentity> authenticate(DefaultLambdaAuthenticationRequest request,
AuthenticationRequestContext context) {
AwsProxyRequest event = request.getEvent();
SecurityIdentity identity = authenticate(event, LambdaHttpRecorder.config.mapCognitoToRoles);
SecurityIdentity identity = authenticate(event, LambdaHttpRecorder.config.mapCognitoToRoles());
if (identity == null) {
return Uni.createFrom().optional(Optional.empty());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package io.quarkus.amazon.lambda.http;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public class LambdaHttpConfig {
@ConfigMapping(prefix = "quarkus.lambda-http")
public interface LambdaHttpConfig {

/**
* If true, runtime will search Cognito JWT claims for "cognito:groups"
* and add them as Quarkus security roles.
*
* True by default
*/
@ConfigItem(defaultValue = "true")
public boolean mapCognitoToRoles;
@WithDefault("true")
boolean mapCognitoToRoles();

/**
* Cognito claim that contains roles you want to map. Defaults to "cognito:groups"
*/
@ConfigItem(defaultValue = "cognito:groups")
public String cognitoRoleClaim;
@WithDefault("cognito:groups")
String cognitoRoleClaim();

/**
* Regular expression to locate role values within a Cognito claim string.
* By default it looks for space delimited strings enclosed in brackets
* By default, it looks for space delimited strings enclosed in brackets
* "[^\[\] \t]+"
*/
@ConfigItem(defaultValue = "[^\\[\\] \\t]+")
public String cognitoClaimMatcher;
@WithDefault("[^\\[\\] \\t]+")
String cognitoClaimMatcher();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LambdaHttpRecorder {

public void setConfig(LambdaHttpConfig c) {
config = c;
String pattern = c.cognitoClaimMatcher;
String pattern = c.cognitoClaimMatcher();
groupPattern = Pattern.compile(pattern);
}
}
3 changes: 0 additions & 3 deletions extensions/amazon-lambda/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit eab567b

Please sign in to comment.