Repository navigation
[Go] Support non-default universes for GCS and Dataflow clients #40686
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
7 commits
Select commit
Hold shift + click to select a range
5a7acef
[Go] Support GCS endpoint override and non-default universes
shunping f89e7d9
[Go] Support non-default universes in the Dataflow client
shunping 9072f14
Add CHANGES.md
shunping acbfd09
[Go] Simplify universe domain handling and dedup tests
shunping 7e75a53
Merge branch 'master' into tpc-5
shunping f5afce3
Use t.Context() in new tests
shunping a724989
Add a comment to explain the loop in test.
shunping 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
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,120 @@ | ||
| // 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. | ||
|
|
||
| package gcpopts | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime" | ||
| "golang.org/x/oauth2/google" | ||
| ) | ||
|
|
||
| func TestGetUniverseDomain(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| env string | ||
| credsUD string | ||
| credsErr error | ||
| want string | ||
| wantCalls int // credential lookups across two calls | ||
| }{ | ||
| { | ||
| name: "env var takes precedence", | ||
| env: "env.example.com", | ||
| credsUD: "creds.example.com", | ||
| want: "env.example.com", | ||
| }, | ||
| { | ||
| name: "falls back to credentials and caches", | ||
| credsUD: "creds.example.com", | ||
| want: "creds.example.com", | ||
| wantCalls: 1, | ||
| }, | ||
| { | ||
| name: "credential errors are not cached", | ||
| credsErr: errors.New("no credentials"), | ||
| want: "", | ||
| wantCalls: 2, | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| resetUniverseDomainCache(t) | ||
| t.Setenv(universeDomainEnvVar, test.env) | ||
| calls := 0 | ||
| orig := findDefaultCredentials | ||
| findDefaultCredentials = func(context.Context, ...string) (*google.Credentials, error) { | ||
| calls++ | ||
| if test.credsErr != nil { | ||
| return nil, test.credsErr | ||
| } | ||
| return &google.Credentials{ | ||
| UniverseDomainProvider: func() (string, error) { return test.credsUD, nil }, | ||
| }, nil | ||
| } | ||
| t.Cleanup(func() { findDefaultCredentials = orig }) | ||
|
|
||
| // Call twice so wantCalls can verify caching: a successful lookup | ||
| // is reused by the second call, while a failed one is retried. | ||
| for i := 0; i < 2; i++ { | ||
|
Contributor
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. Add a comment why we're doing repetitions here please.
Collaborator
Author
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. Done. |
||
| if got := GetUniverseDomain(t.Context()); got != test.want { | ||
| t.Errorf("GetUniverseDomain() = %q, want %q", got, test.want) | ||
| } | ||
| } | ||
| if calls != test.wantCalls { | ||
| t.Errorf("credentials looked up %d times, want %d", calls, test.wantCalls) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // resetUniverseDomainCache clears the cached universe domain and restores it | ||
| // when the test finishes. | ||
| func resetUniverseDomainCache(t *testing.T) { | ||
| t.Helper() | ||
| reset := func() { | ||
| cachedUniverseDomain.mu.Lock() | ||
| cachedUniverseDomain.resolved = false | ||
| cachedUniverseDomain.value = "" | ||
| cachedUniverseDomain.mu.Unlock() | ||
| } | ||
| reset() | ||
| t.Cleanup(reset) | ||
| } | ||
|
|
||
| func TestGetGcsEndpoint(t *testing.T) { | ||
| const flagValue = "https://flag.example.com/storage/v1/" | ||
| const optionValue = "https://option.example.com/storage/v1/" | ||
|
|
||
| origFlag := *GcsEndpoint | ||
| t.Cleanup(func() { | ||
| *GcsEndpoint = origFlag | ||
| runtime.GlobalOptions.Set("gcs_endpoint", "") | ||
| }) | ||
| // On workers, the value arrives as a pipeline option rather than a flag. | ||
| runtime.GlobalOptions.Set("gcs_endpoint", optionValue) | ||
|
|
||
| *GcsEndpoint = "" | ||
| if got := GetGcsEndpoint(); got != optionValue { | ||
| t.Errorf("GetGcsEndpoint() with flag unset = %q, want pipeline option %q", got, optionValue) | ||
| } | ||
| *GcsEndpoint = flagValue | ||
| if got := GetGcsEndpoint(); got != flagValue { | ||
| t.Errorf("GetGcsEndpoint() with flag set = %q, want flag %q", got, flagValue) | ||
| } | ||
| } | ||
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
91 changes: 91 additions & 0 deletions
91
sdks/go/pkg/beam/runners/dataflow/dataflowlib/client_test.go
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,91 @@ | ||
| // 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. | ||
|
|
||
| package dataflowlib | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "encoding/json" | ||
| "encoding/pem" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNewClient_BasePath(t *testing.T) { | ||
| // Fake service account credentials in a non-default universe. The auth | ||
| // library parses the key when the client is created (non-default universes | ||
| // use self-signed JWTs), so it must be a valid key. No token is fetched | ||
| // until a request is made. | ||
| key, err := rsa.GenerateKey(rand.Reader, 2048) | ||
| if err != nil { | ||
| t.Fatalf("Failed generating key: %v", err) | ||
| } | ||
| der, err := x509.MarshalPKCS8PrivateKey(key) | ||
| if err != nil { | ||
| t.Fatalf("Failed marshaling key: %v", err) | ||
| } | ||
| content, err := json.Marshal(map[string]string{ | ||
| "type": "service_account", | ||
| "project_id": "test-project", | ||
| "client_email": "test@test-project.iam.gserviceaccount.com", | ||
| "private_key_id": "test-key-id", | ||
| "private_key": string(pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der})), | ||
| "universe_domain": "creds.example.com", | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Failed marshaling credentials: %v", err) | ||
| } | ||
| credFile := filepath.Join(t.TempDir(), "creds.json") | ||
| if err := os.WriteFile(credFile, content, 0600); err != nil { | ||
| t.Fatalf("Failed writing fake credential file: %v", err) | ||
| } | ||
| t.Setenv("GOOGLE_APPLICATION_CREDENTIALS", credFile) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| endpoint string | ||
| env string | ||
| want string | ||
| }{ | ||
| { | ||
| // The client library doesn't read the credentials' universe domain, | ||
| // so this verifies that NewClient passes it through. | ||
| name: "universe domain from credentials", | ||
| want: "https://dataflow.creds.example.com/", | ||
| }, | ||
| { | ||
| name: "endpoint takes precedence over universe domain", | ||
| endpoint: "http://localhost:8080/", | ||
| env: "creds.example.com", | ||
| want: "http://localhost:8080/", | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| t.Setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", test.env) | ||
|
|
||
| client, err := NewClient(t.Context(), test.endpoint) | ||
| if err != nil { | ||
| t.Fatalf("NewClient(%q) failed: %v", test.endpoint, err) | ||
| } | ||
| if got := client.BasePath; got != test.want { | ||
| t.Errorf("NewClient(%q).BasePath = %q, want %q", test.endpoint, got, test.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
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.
Beautiful use of an anonymous struct type.