-
Notifications
You must be signed in to change notification settings - Fork 195
feat(csharp/src/Drivers/BigQuery): Add support for AAD/Entra authentication #2655
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
22 commits
Select commit
Hold shift + click to select a range
8f8bca6
pin
686e497
add an Entra test; include public projects even if there is an error
3d0152c
pin
50fc942
pin
23215ab
pin
8b3e7ee
run code cleanup tool
d27b188
mocking
020127b
entra and refresh working
ce87b47
fix license headers
5daef09
pin
d79a941
Merge https://github.com/davidhcoe/arrow-adbc into dev/bigquery-aad
12cebba
successfully resetting Entra token during long query
3eaaf82
polish retries
d943fec
refactor auth tests
4108b51
merge latest; update async methods
da4d42f
add support for SetOption
97ff5b7
clean up
782fea0
PR feedback
d556773
clean up
595adcb
more PR feedback
8d3cdc9
more clean up
befa2e2
fix delay
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,39 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.BigQuery | ||
| { | ||
| /// <summary> | ||
| /// The token response from BigQuery | ||
| /// </summary> | ||
| internal class BigQueryStsTokenResponse | ||
| { | ||
| [JsonPropertyName("access_token")] | ||
| public string? AccessToken { get; set; } | ||
|
|
||
| [JsonPropertyName("issued_token_type")] | ||
| public string? IssuedTokenType { get; set; } | ||
|
|
||
| [JsonPropertyName("token_type")] | ||
| public string? TokenType { get; set; } | ||
|
|
||
| [JsonPropertyName("expires_in")] | ||
| public int? ExpiresIn { get; set; } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| using System; | ||
| using Google; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.BigQuery | ||
| { | ||
| internal class BigQueryUtils | ||
| { | ||
| public static bool TokenRequiresUpdate(Exception ex) | ||
| { | ||
| bool result = false; | ||
|
|
||
| if (ex is GoogleApiException gaex && gaex.HttpStatusCode == System.Net.HttpStatusCode.Unauthorized) | ||
| { | ||
| result = true; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
| } |
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,40 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.BigQuery | ||
| { | ||
| /// <summary> | ||
| /// Common interface for a token protected resource. | ||
| /// </summary> | ||
| internal interface ITokenProtectedResource | ||
| { | ||
| /// <summary> | ||
| /// The function to call when updating the token. | ||
| /// </summary> | ||
| Func<Task>? UpdateToken { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Determines the token needs to be updated. | ||
| /// </summary> | ||
| /// <param name="ex">The exception that occurs.</param> | ||
| /// <returns>True/False indicating a refresh is needed.</returns> | ||
| bool TokenRequiresUpdate(Exception ex); | ||
| } | ||
| } |
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,82 @@ | ||
| | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.BigQuery | ||
| { | ||
| /// <summary> | ||
| /// Class that will retry calling a method with a backoff. | ||
| /// </summary> | ||
| internal class RetryManager | ||
| { | ||
| public static async Task<T> ExecuteWithRetriesAsync<T>( | ||
| ITokenProtectedResource tokenProtectedResource, | ||
| Func<Task<T>> action, | ||
| int maxRetries = 5, | ||
| int initialDelayMilliseconds = 200) | ||
| { | ||
| if (action == null) | ||
| { | ||
| throw new AdbcException("There is no method to retry", AdbcStatusCode.InvalidArgument); | ||
| } | ||
|
|
||
| int retryCount = 0; | ||
| int delay = initialDelayMilliseconds; | ||
|
|
||
| while (retryCount < maxRetries) | ||
| { | ||
| try | ||
| { | ||
| T result = await action(); | ||
| return result; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| retryCount++; | ||
| if (retryCount >= maxRetries) | ||
| { | ||
| if ((tokenProtectedResource?.UpdateToken != null)) | ||
| { | ||
| if (tokenProtectedResource?.TokenRequiresUpdate(ex) == true) | ||
| { | ||
| throw new AdbcException($"Cannot update access token after {maxRetries} tries", AdbcStatusCode.Unauthenticated, ex); | ||
| } | ||
| } | ||
|
|
||
| throw new AdbcException($"Cannot execute {action.Method.Name} after {maxRetries} tries", AdbcStatusCode.UnknownError, ex); | ||
| } | ||
|
|
||
| if ((tokenProtectedResource?.UpdateToken != null)) | ||
| { | ||
| if (tokenProtectedResource.TokenRequiresUpdate(ex) == true) | ||
| { | ||
| await tokenProtectedResource.UpdateToken(); | ||
| } | ||
| } | ||
|
|
||
| await Task.Delay(delay); | ||
| delay = Math.Min(2 * delay, 5000); | ||
| } | ||
| } | ||
|
|
||
| throw new AdbcException($"Could not successfully call {action.Method.Name}", AdbcStatusCode.UnknownError); | ||
| } | ||
| } | ||
| } |
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,60 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Google.Apis.Auth.OAuth2; | ||
| using Google.Cloud.BigQuery.Storage.V1; | ||
|
|
||
| namespace Apache.Arrow.Adbc.Drivers.BigQuery | ||
| { | ||
| /// <summary> | ||
| /// Manages a <see cref="BigQueryReadClient"/> that is protected by a token. | ||
| /// </summary> | ||
| internal class TokenProtectedReadClientManger : ITokenProtectedResource | ||
| { | ||
| BigQueryReadClient bigQueryReadClient; | ||
|
|
||
| public TokenProtectedReadClientManger(GoogleCredential credential) | ||
| { | ||
| UpdateCredential(credential); | ||
|
|
||
| if (bigQueryReadClient == null) | ||
| { | ||
| throw new InvalidOperationException("could not create a read client"); | ||
| } | ||
| } | ||
|
|
||
| public BigQueryReadClient ReadClient => bigQueryReadClient; | ||
|
|
||
| public void UpdateCredential(GoogleCredential? credential) | ||
| { | ||
| if (credential == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(credential)); | ||
| } | ||
|
|
||
| BigQueryReadClientBuilder readClientBuilder = new BigQueryReadClientBuilder(); | ||
| readClientBuilder.Credential = credential; | ||
| this.bigQueryReadClient = readClientBuilder.Build(); | ||
| } | ||
|
|
||
| public Func<Task>? UpdateToken { get; set; } | ||
|
|
||
| public bool TokenRequiresUpdate(Exception ex) => BigQueryUtils.TokenRequiresUpdate(ex); | ||
| } | ||
| } |
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
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.