-
Notifications
You must be signed in to change notification settings - Fork 53
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
feat(gax): add API key authentication to ClientSettings #3137
Merged
Changes from 10 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
558af8e
added setApiKey() method to client settings
ldetmer dad48d4
cleaned up and added logic for throwing error if both api key and cre…
ldetmer f6afbef
fixed formatting
ldetmer a516d95
fixed formatting
ldetmer f7ec0fa
wip
ldetmer db1674b
wip
ldetmer 9a13951
wip
ldetmer 62a3956
clean up
ldetmer f0a98e0
Merge branch 'main' into api-keys
ldetmer fa251cf
clean up
ldetmer 334a4e8
cleaned up tests/logic
ldetmer edb658f
cleaned up formatting
ldetmer 33f64a1
updated to use assertThrows
ldetmer c92322b
Merge branch 'main' into api-keys
ldetmer ae0f281
fixed imports
ldetmer 5e346e6
fixed imports
ldetmer 66cc0a7
updated logic to validate if multiple credentials are passed in via a…
ldetmer 69c57e9
formatting
ldetmer 023a4e4
cleanup
ldetmer d7c7a72
cleanup
ldetmer 0d48f41
cleanup
ldetmer bce5abb
added handling of deduping credential headers for GRPC calls + additi…
ldetmer d4670c5
lint fixes
ldetmer 1eda03f
lint fixes + additional showcase coverage
ldetmer 364acae
Merge branch 'main' into api-keys
ldetmer 50cbea1
cleaned up error checking in dedup + updated tests and java doc
ldetmer 351389c
lint fix
ldetmer ca19304
lint fix
ldetmer aa6a006
cleaned up java docs so stub settings and client settings are matching
ldetmer 0938405
lint fix
ldetmer a39bba0
fixed gdch IT tests
ldetmer f64279e
updated so credential deduping happens during the object build process
ldetmer 703139b
additional cleanup
ldetmer 245f8e2
lint
ldetmer 0dc642e
lint
ldetmer 60fbed4
updated to only dedup API key credential headers
ldetmer 68f38ae
language fixes
ldetmer d3492b3
lint
ldetmer 1330841
fixed changes to existing tests
ldetmer 768140d
fixed test modifiers
ldetmer bc798db
Merge branch 'main' into api-keys
ldetmer e20771d
no longer need to pre-load gdch creds as we're not deduping headers f…
ldetmer 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 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
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 |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
import static org.junit.jupiter.api.Assertions.assertNotSame; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
|
@@ -52,6 +53,7 @@ | |
import com.google.api.gax.rpc.testing.FakeClientSettings; | ||
import com.google.api.gax.rpc.testing.FakeStubSettings; | ||
import com.google.api.gax.rpc.testing.FakeTransportChannel; | ||
import com.google.auth.ApiKeyCredentials; | ||
import com.google.auth.Credentials; | ||
import com.google.auth.oauth2.ComputeEngineCredentials; | ||
import com.google.auth.oauth2.GdchCredentials; | ||
|
@@ -205,9 +207,6 @@ public TransportChannelProvider withPoolSize(int size) { | |
|
||
@Override | ||
public TransportChannel getTransportChannel() throws IOException { | ||
if (needsCredentials()) { | ||
throw new IllegalStateException("Needs Credentials"); | ||
} | ||
transport.setExecutor(executor); | ||
return transport; | ||
} | ||
|
@@ -1074,4 +1073,50 @@ public void testStreamWatchdogInterval_backportMethodsBehaveCorrectly() { | |
ct -> ct.getStreamWatchdogCheckIntervalDuration(), | ||
ct -> ct.getStreamWatchdogCheckInterval()); | ||
} | ||
|
||
@Test | ||
public void testSetApiKey_createsApiCredentials() throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: These test methods don't have to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah thanks! done |
||
String apiKey = "key"; | ||
FakeStubSettings.Builder builder = new FakeStubSettings.Builder(); | ||
InterceptingExecutor executor = new InterceptingExecutor(1); | ||
FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); | ||
FakeTransportProvider transportProvider = | ||
new FakeTransportProvider( | ||
transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); | ||
builder.setTransportChannelProvider(transportProvider); | ||
HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); | ||
Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); | ||
builder.setHeaderProvider(headerProvider); | ||
builder.setApiKey(apiKey); | ||
|
||
ClientContext context = ClientContext.create(builder.build()); | ||
|
||
FakeCallContext fakeCallContext = (FakeCallContext) context.getDefaultCallContext(); | ||
assertThat(fakeCallContext.getCredentials()).isInstanceOf(ApiKeyCredentials.class); | ||
} | ||
|
||
@Test | ||
void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exception { | ||
String apiKey = "key"; | ||
FakeStubSettings.Builder builder = new FakeStubSettings.Builder(); | ||
InterceptingExecutor executor = new InterceptingExecutor(1); | ||
FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); | ||
FakeTransportProvider transportProvider = | ||
new FakeTransportProvider( | ||
transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); | ||
builder.setTransportChannelProvider(transportProvider); | ||
HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); | ||
Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); | ||
builder.setHeaderProvider(headerProvider); | ||
builder.setApiKey(apiKey); | ||
builder.setCredentialsProvider(Mockito.mock(CredentialsProvider.class)); | ||
|
||
try { | ||
ClientContext.create(builder.build()); | ||
fail("No exception raised"); | ||
} catch (IllegalArgumentException e) { | ||
assert (e.getMessage() | ||
.contains("You can not provide both ApiKey and Credentials for a client.")); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
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.
Maybe extract the whole section to a method like
getCredentials
?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.
unfortunately there is some logic that updates Endpoint metadata that is also used in transport provider. Without a more indepth refactor I tried to consolidate as much as possible