-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Support username/password GRPC auth #4406
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
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
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
47 changes: 47 additions & 0 deletions
47
java/grpc-client/src/main/java/io/vitess/client/grpc/StaticAuthCredentials.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,47 @@ | ||
| package io.vitess.client.grpc; | ||
|
|
||
| import io.grpc.Attributes; | ||
| import io.grpc.CallCredentials; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.MethodDescriptor; | ||
| import io.grpc.Status; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.Executor; | ||
|
|
||
| /** | ||
| * {@link CallCredentials} that applies plain username and password. | ||
| */ | ||
| public class StaticAuthCredentials implements CallCredentials { | ||
|
|
||
| private static final Metadata.Key<String> USERNAME = | ||
| Metadata.Key.of("username", Metadata.ASCII_STRING_MARSHALLER); | ||
| private static final Metadata.Key<String> PASSWORD = | ||
| Metadata.Key.of("password", Metadata.ASCII_STRING_MARSHALLER); | ||
|
|
||
| private final String username; | ||
| private final String password; | ||
|
|
||
| public StaticAuthCredentials(String username, String password) { | ||
| this.username = Objects.requireNonNull(username); | ||
| this.password = Objects.requireNonNull(password); | ||
| } | ||
|
|
||
| @Override | ||
| public void applyRequestMetadata(MethodDescriptor<?, ?> method, Attributes attrs, | ||
| Executor executor, MetadataApplier applier) { | ||
| executor.execute(() -> { | ||
| try { | ||
| Metadata headers = new Metadata(); | ||
| headers.put(USERNAME, username); | ||
| headers.put(PASSWORD, password); | ||
| applier.apply(headers); | ||
| } catch (Throwable e) { | ||
| applier.fail(Status.UNAUTHENTICATED.withCause(e)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void thisUsesUnstableApi() { | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
java/grpc-client/src/test/java/io/client/grpc/GrpcClientStaticAuthTest.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,81 @@ | ||
| package io.client.grpc; | ||
|
|
||
| import com.google.common.base.Throwables; | ||
| import io.grpc.Status; | ||
| import io.grpc.StatusRuntimeException; | ||
| import io.vitess.client.Context; | ||
| import io.vitess.client.RpcClient; | ||
| import io.vitess.client.RpcClientTest; | ||
| import io.vitess.client.grpc.GrpcClientFactory; | ||
| import io.vitess.client.grpc.StaticAuthCredentials; | ||
| import io.vitess.proto.Vtgate.GetSrvKeyspaceRequest; | ||
| import java.net.ServerSocket; | ||
| import java.net.URI; | ||
| import java.util.Arrays; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.joda.time.Duration; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Assert; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class GrpcClientStaticAuthTest extends RpcClientTest { | ||
| private static Process vtgateclienttest; | ||
| private static int port; | ||
|
|
||
| @BeforeClass | ||
| public static void setUpBeforeClass() throws Exception { | ||
| String vtRoot = System.getenv("VTROOT"); | ||
| if (vtRoot == null) { | ||
| throw new RuntimeException("cannot find env variable VTROOT; make sure to source dev.env"); | ||
| } | ||
| URI staticAuthFile = GrpcClientStaticAuthTest.class.getResource("grpc_static_auth.json").toURI(); | ||
|
|
||
| ServerSocket socket = new ServerSocket(0); | ||
| port = socket.getLocalPort(); | ||
| socket.close(); | ||
|
|
||
| vtgateclienttest = new ProcessBuilder(Arrays.asList( | ||
| vtRoot + "/bin/vtgateclienttest", | ||
| "-logtostderr", | ||
| "-grpc_port", Integer.toString(port), | ||
| "-service_map", "grpc-vtgateservice", | ||
| "-grpc_auth_mode", "static", | ||
| "-grpc_auth_static_password_file", staticAuthFile.getPath() | ||
| )).inheritIO().start(); | ||
|
|
||
| Context ctx = Context.getDefault().withDeadlineAfter(Duration.millis(5000L)); | ||
| client = new GrpcClientFactory() | ||
| .setCallCredentials(new StaticAuthCredentials("test-username", "test-password")) | ||
| .create(ctx, "localhost:" + port); | ||
ys8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @AfterClass | ||
| public static void tearDownAfterClass() throws Exception { | ||
| if (client != null) { | ||
| client.close(); | ||
| } | ||
| if (vtgateclienttest != null) { | ||
| vtgateclienttest.destroy(); | ||
| vtgateclienttest.waitFor(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testWrongPassword() throws Exception { | ||
| RpcClient client = new GrpcClientFactory() | ||
| .setCallCredentials(new StaticAuthCredentials("test-username", "WRONG-password")) | ||
| .create(Context.getDefault(), "localhost:" + port); | ||
| try { | ||
| client.getSrvKeyspace(Context.getDefault(), GetSrvKeyspaceRequest.getDefaultInstance()).get(); | ||
| Assert.fail(); | ||
| } catch (ExecutionException e) { | ||
| StatusRuntimeException cause = (StatusRuntimeException) Throwables.getRootCause(e); | ||
| Assert.assertSame(cause.getStatus().getCode(), Status.Code.PERMISSION_DENIED); | ||
| } | ||
| client.close(); | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
java/grpc-client/src/test/resources/io/client/grpc/grpc_static_auth.json
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,6 @@ | ||
| [ | ||
| { | ||
| "username": "test-username", | ||
| "password": "test-password" | ||
| } | ||
| ] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can call one constructor from another so that only one constructor contains the logic for initialization.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it first. It didn't look too clean to me. Do you prefer that way? (In fact, having CallCredentials parameter as Nullabe in this case, an extra constructor is technically no longer needed besides the constructor can be more confusing from caller's point of view.)