-
Notifications
You must be signed in to change notification settings - Fork 3.6k
JWT for internal communication #2032
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
presto-main/src/main/java/io/prestosql/server/InternalAuthenticationManager.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,116 @@ | ||
| /* | ||
| * 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 io.prestosql.server; | ||
|
|
||
| import com.google.common.hash.Hashing; | ||
| import io.airlift.http.client.HttpRequestFilter; | ||
| import io.airlift.http.client.Request; | ||
| import io.airlift.log.Logger; | ||
| import io.airlift.node.NodeInfo; | ||
| import io.jsonwebtoken.Claims; | ||
| import io.jsonwebtoken.Jws; | ||
| import io.jsonwebtoken.JwtException; | ||
| import io.jsonwebtoken.JwtParser; | ||
| import io.jsonwebtoken.Jwts; | ||
| import io.jsonwebtoken.SignatureAlgorithm; | ||
| import io.prestosql.server.security.InternalPrincipal; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| import java.security.Principal; | ||
| import java.time.ZonedDateTime; | ||
| import java.util.Date; | ||
| import java.util.Optional; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static io.airlift.http.client.Request.Builder.fromRequest; | ||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class InternalAuthenticationManager | ||
| implements HttpRequestFilter | ||
| { | ||
| private static final Logger log = Logger.get(InternalAuthenticationManager.class); | ||
|
|
||
| private static final String PRESTO_INTERNAL_BEARER = "X-Presto-Internal-Bearer"; | ||
|
|
||
| private final Optional<JwtParser> jwtParser; | ||
| private final Optional<Supplier<String>> jwtGenerator; | ||
|
|
||
| @Inject | ||
| public InternalAuthenticationManager(InternalCommunicationConfig internalCommunicationConfig, NodeInfo nodeInfo) | ||
|
dain marked this conversation as resolved.
Outdated
|
||
| { | ||
| this(requireNonNull(internalCommunicationConfig, "internalCommunicationConfig is null").getSharedSecret(), nodeInfo.getNodeId()); | ||
| } | ||
|
|
||
| public InternalAuthenticationManager(Optional<String> sharedSecret, String nodeId) | ||
| { | ||
| if (sharedSecret.isPresent()) { | ||
| byte[] hmac = Hashing.sha256().hashString(sharedSecret.get(), UTF_8).asBytes(); | ||
| this.jwtParser = Optional.of(Jwts.parser().setSigningKey(hmac)); | ||
| this.jwtGenerator = Optional.of(() -> generateJwt(hmac, nodeId)); | ||
| } | ||
| else { | ||
| this.jwtParser = Optional.empty(); | ||
| this.jwtGenerator = Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| private static String generateJwt(byte[] hmac, String nodeId) | ||
| { | ||
| return Jwts.builder() | ||
| .signWith(SignatureAlgorithm.HS256, hmac) | ||
| .setSubject(nodeId) | ||
| .setExpiration(Date.from(ZonedDateTime.now().plusMinutes(5).toInstant())) | ||
| .compact(); | ||
| } | ||
|
|
||
| public boolean isInternalRequest(HttpServletRequest request) | ||
| { | ||
| return request.getHeader(PRESTO_INTERNAL_BEARER) != null; | ||
| } | ||
|
|
||
| public Principal authenticateInternalRequest(HttpServletRequest request) | ||
| { | ||
| if (!jwtParser.isPresent()) { | ||
| log.error("Internal authentication in not configured"); | ||
| return null; | ||
|
dain marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| String internalBarer = request.getHeader(PRESTO_INTERNAL_BEARER); | ||
| try { | ||
| Jws<Claims> claimsJws = jwtParser.get().parseClaimsJws(internalBarer); | ||
| String subject = claimsJws.getBody().getSubject(); | ||
| return new InternalPrincipal(subject); | ||
| } | ||
| catch (JwtException e) { | ||
| log.error(e, "Internal authentication failed"); | ||
| return null; | ||
| } | ||
| catch (RuntimeException e) { | ||
| throw new RuntimeException("Authentication error", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Request filterRequest(Request request) | ||
| { | ||
| return jwtGenerator.map(Supplier::get) | ||
| .map(jwt -> fromRequest(request) | ||
| .addHeader(PRESTO_INTERNAL_BEARER, jwt) | ||
| .build()) | ||
| .orElse(request); | ||
|
electrum marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
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
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
61 changes: 61 additions & 0 deletions
61
presto-main/src/main/java/io/prestosql/server/security/InternalPrincipal.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,61 @@ | ||
| /* | ||
| * 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 io.prestosql.server.security; | ||
|
|
||
| import java.security.Principal; | ||
| import java.util.Objects; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public final class InternalPrincipal | ||
|
electrum marked this conversation as resolved.
Outdated
|
||
| implements Principal | ||
| { | ||
| private final String name; | ||
|
|
||
| public InternalPrincipal(String name) | ||
| { | ||
| this.name = requireNonNull(name, "name is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) | ||
| { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| InternalPrincipal that = (InternalPrincipal) o; | ||
| return Objects.equals(name, that.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() | ||
| { | ||
| return Objects.hash(name); | ||
| } | ||
| } | ||
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
67 changes: 67 additions & 0 deletions
67
presto-main/src/test/java/io/prestosql/server/TestInternalCommunicationConfig.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,67 @@ | ||
| /* | ||
| * 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 io.prestosql.server; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static io.airlift.configuration.testing.ConfigAssertions.assertFullMapping; | ||
| import static io.airlift.configuration.testing.ConfigAssertions.assertRecordedDefaults; | ||
| import static io.airlift.configuration.testing.ConfigAssertions.recordDefaults; | ||
|
|
||
| public class TestInternalCommunicationConfig | ||
| { | ||
| @Test | ||
| public void testDefaults() | ||
| { | ||
| assertRecordedDefaults(recordDefaults(InternalCommunicationConfig.class) | ||
| .setSharedSecret(null) | ||
| .setHttpsRequired(false) | ||
| .setKeyStorePath(null) | ||
| .setKeyStorePassword(null) | ||
| .setTrustStorePath(null) | ||
| .setTrustStorePassword(null) | ||
| .setKerberosEnabled(false) | ||
| .setKerberosUseCanonicalHostname(true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExplicitPropertyMappings() | ||
| { | ||
| Map<String, String> properties = new ImmutableMap.Builder<String, String>() | ||
| .put("internal-communication.shared-secret", "secret") | ||
| .put("internal-communication.https.required", "true") | ||
| .put("internal-communication.https.keystore.path", "key-path") | ||
| .put("internal-communication.https.keystore.key", "key-key") | ||
| .put("internal-communication.https.truststore.path", "trust-path") | ||
| .put("internal-communication.https.truststore.key", "trust-key") | ||
| .put("internal-communication.kerberos.enabled", "true") | ||
| .put("internal-communication.kerberos.use-canonical-hostname", "false") | ||
| .build(); | ||
|
|
||
| InternalCommunicationConfig expected = new InternalCommunicationConfig() | ||
| .setSharedSecret("secret") | ||
| .setHttpsRequired(true) | ||
| .setKeyStorePath("key-path") | ||
| .setKeyStorePassword("key-key") | ||
| .setTrustStorePath("trust-path") | ||
| .setTrustStorePassword("trust-key") | ||
| .setKerberosEnabled(true) | ||
| .setKerberosUseCanonicalHostname(false); | ||
|
|
||
| assertFullMapping(properties, expected); | ||
| } | ||
| } |
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
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.