Repository navigation
Add an Amazon SES email sender using the SES v2 API and IAM credentials #52361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
70 changes: 70 additions & 0 deletions
70
services/src/main/java/org/keycloak/email/aws/AwsHttpRequest.java
This file contains hidden or 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,70 @@ | ||
| /* | ||
| * Copyright 2026 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.email.aws; | ||
|
|
||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * One outbound HTTP call, described in full before it is executed. | ||
| * <p> | ||
| * The request is a value object on purpose: SigV4 signs a byte-exact rendering of method, URI, | ||
| * headers and body, so the bytes that are signed and the bytes that reach the socket must come from | ||
| * the same immutable object. Anything that lets a layer below add or rewrite a signed header | ||
| * invalidates the signature. | ||
| * | ||
| * @param method HTTP method, uppercase | ||
| * @param uri absolute request URI; must carry no query string for SES | ||
| * @param headers headers to place on the request, insertion-ordered | ||
| * @param body request body, empty (never {@code null}) for bodyless requests | ||
| * @param connectTimeoutMillis TCP connect timeout | ||
| * @param readTimeoutMillis socket read timeout | ||
| */ | ||
| public record AwsHttpRequest(String method, URI uri, Map<String, String> headers, byte[] body, | ||
| int connectTimeoutMillis, int readTimeoutMillis) { | ||
|
|
||
| public static final byte[] NO_BODY = new byte[0]; | ||
|
|
||
| public AwsHttpRequest { | ||
| headers = Collections.unmodifiableMap(new LinkedHashMap<>(headers)); | ||
| body = body == null ? NO_BODY : body.clone(); | ||
| } | ||
|
|
||
| public static AwsHttpRequest get(URI uri, Map<String, String> headers, int connectTimeoutMillis, int readTimeoutMillis) { | ||
| return new AwsHttpRequest("GET", uri, headers, NO_BODY, connectTimeoutMillis, readTimeoutMillis); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] body() { | ||
| return body.clone(); | ||
| } | ||
|
|
||
| /** The body as text, for the JSON/XML responses of the credential endpoints. */ | ||
| public String bodyAsString() { | ||
| return new String(body, StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| public AwsHttpRequest withHeaders(Map<String, String> additional) { | ||
| Map<String, String> merged = new LinkedHashMap<>(headers); | ||
| merged.putAll(additional); | ||
| return new AwsHttpRequest(method, uri, merged, body, connectTimeoutMillis, readTimeoutMillis); | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
services/src/main/java/org/keycloak/email/aws/AwsHttpResponse.java
This file contains hidden or 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,62 @@ | ||
| /* | ||
| * Copyright 2026 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.email.aws; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * The response of an {@link AwsHttpTransport} exchange. | ||
| * | ||
| * @param status HTTP status code | ||
| * @param headers response headers; look them up through {@link #header(String)}, never directly — | ||
| * AWS documents {@code x-amzn-ErrorType} in one casing and sends another | ||
| * @param body response body, empty when there is none | ||
| */ | ||
| public record AwsHttpResponse(int status, Map<String, String> headers, byte[] body) { | ||
|
|
||
| public AwsHttpResponse { | ||
| headers = Collections.unmodifiableMap(new LinkedHashMap<>(headers)); | ||
| body = body == null ? new byte[0] : body.clone(); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] body() { | ||
| return body.clone(); | ||
| } | ||
|
|
||
| public String bodyAsString() { | ||
| return new String(body, StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| public boolean isSuccessful() { | ||
| return status >= 200 && status < 300; | ||
| } | ||
|
|
||
| /** Case-insensitive header lookup, as HTTP requires and AWS's own casing demands. */ | ||
| public String header(String name) { | ||
| for (Map.Entry<String, String> entry : headers.entrySet()) { | ||
| if (entry.getKey().equalsIgnoreCase(name)) { | ||
| return entry.getValue(); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
services/src/main/java/org/keycloak/email/aws/AwsHttpTransport.java
This file contains hidden or 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,38 @@ | ||
| /* | ||
| * Copyright 2026 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.email.aws; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Executes an {@link AwsHttpRequest} exactly as described. | ||
| * <p> | ||
| * "Exactly" is the whole contract. An implementation must not add, remove, reorder or rewrite any | ||
| * header the caller set, must not follow redirects, and must not itself retry a request whose bytes | ||
| * already reached the wire: the first two would break the SigV4 signature, the third would send a | ||
| * transactional email twice, because SES {@code SendEmail} has no idempotency token. | ||
| * <p> | ||
| * The last one is only as strong as the client underneath. {@code KeycloakHttpTransport} runs on the | ||
| * server's shared Apache client, which installs a retry handler of its own and turns it into a | ||
| * resend-after-send when {@code spi-connections-http-client-default-max-retries} is set; that cannot | ||
| * be opted out of per request, so the factory warns at startup when it sees it. | ||
| */ | ||
| public interface AwsHttpTransport { | ||
|
|
||
| AwsHttpResponse exchange(AwsHttpRequest request) throws IOException; | ||
| } |
56 changes: 56 additions & 0 deletions
56
services/src/main/java/org/keycloak/email/aws/AwsRegion.java
This file contains hidden or 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,56 @@ | ||
| /* | ||
| * Copyright 2026 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.keycloak.email.aws; | ||
|
|
||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * The two things this provider needs to know about an AWS region name: whether it is one, and which | ||
| * DNS suffix its partition uses. | ||
| * <p> | ||
| * Validation is not defensive programming for its own sake. A region name is interpolated into the | ||
| * hostname of the SES and STS endpoints, and both of those requests carry credentials — the STS one | ||
| * carries a service-account token in its body. A region read from the environment as | ||
| * {@code attacker.example/collect} would otherwise build | ||
| * {@code https://sts.attacker.example/collect.amazonaws.com/} and post that token to a host of | ||
| * someone else's choosing. | ||
| */ | ||
| public final class AwsRegion { | ||
|
|
||
| /** {@code eu-central-1}, {@code us-gov-west-1}, {@code cn-north-1} — and nothing that could smuggle a host or a path. */ | ||
| private static final Pattern VALID = Pattern.compile("[a-z0-9]+(-[a-z0-9]+)+"); | ||
|
|
||
| private static final String DEFAULT_DNS_SUFFIX = "amazonaws.com"; | ||
| private static final String CHINA_DNS_SUFFIX = "amazonaws.com.cn"; | ||
|
|
||
| private AwsRegion() { | ||
| } | ||
|
|
||
| public static boolean isValid(String region) { | ||
| return region != null && VALID.matcher(region).matches(); | ||
| } | ||
|
|
||
| /** | ||
| * The DNS suffix of the region's partition. China is the partition that differs for the endpoints | ||
| * this provider builds; the AWS GovCloud and the standard partition share {@code amazonaws.com}, | ||
| * and the isolated partitions are not reachable from a server that could run this code anyway. | ||
| */ | ||
| public static String dnsSuffix(String region) { | ||
| return region != null && region.startsWith("cn-") ? CHINA_DNS_SUFFIX : DEFAULT_DNS_SUFFIX; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.