-
Notifications
You must be signed in to change notification settings - Fork 4.7k
internal/resolver: add target URI parsing with resolver validation #8876
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
Open
HueCodes
wants to merge
4
commits into
grpc:master
Choose a base branch
from
HueCodes:validate-target-uri
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3379b53
grpcutil: add ValidateTargetURI function
HueCodes 3a28eea
internal/resolver: add ParseTarget for target URI validation
HueCodes c2fdf5f
internal/resolver: simplify ParseTarget fallback logic
HueCodes 89ed8d1
internal/resolver: remove unused grpcutil/target.go, fix test names
HueCodes 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
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 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * 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 resolver | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/url" | ||
|
|
||
| "google.golang.org/grpc/resolver" | ||
| ) | ||
|
|
||
| // ParseTarget parses a gRPC target string into a resolver.Target, verifying | ||
| // that a resolver is registered for the parsed scheme using builder. | ||
| // | ||
| // If the target parses successfully and builder recognises the scheme, the | ||
| // parsed target is returned directly. When the scheme is unregistered, empty, | ||
| // or parsing fails, ParseTarget retries by prepending defaultScheme + ":///" | ||
| // if defaultScheme is non-empty. | ||
| // | ||
| // builder is a function that returns the resolver.Builder for a given scheme, | ||
| // or nil if no resolver is registered. Pass resolver.Get to use the global | ||
| // resolver registry, or a custom lookup function (e.g. cc.getResolver) to | ||
| // also consider resolvers registered via dial options. | ||
| func ParseTarget(target, defaultScheme string, builder func(string) resolver.Builder) (resolver.Target, error) { | ||
| u, err := url.Parse(target) | ||
| if err == nil && u.Scheme != "" && builder(u.Scheme) != nil { | ||
| return resolver.Target{URL: *u}, nil | ||
| } | ||
| // Parse error, empty scheme, or unregistered scheme: retry with the | ||
| // default scheme if one is provided. | ||
| if defaultScheme != "" && builder(defaultScheme) != nil { | ||
| canonicalTarget := defaultScheme + ":///" + target | ||
| if u2, err2 := url.Parse(canonicalTarget); err2 == nil { | ||
| return resolver.Target{URL: *u2}, nil | ||
| } | ||
| } | ||
| if err != nil { | ||
| return resolver.Target{}, fmt.Errorf("invalid target URI %q: %v", target, err) | ||
| } | ||
| if u.Scheme == "" { | ||
| return resolver.Target{}, fmt.Errorf("target URI %q has no scheme", target) | ||
| } | ||
| return resolver.Target{}, fmt.Errorf("no resolver registered for scheme %q in target %q", u.Scheme, target) | ||
| } |
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,195 @@ | ||
| /* | ||
| * | ||
| * Copyright 2026 gRPC authors. | ||
| * | ||
| * 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 resolver_test | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| iresolver "google.golang.org/grpc/internal/resolver" | ||
| _ "google.golang.org/grpc/internal/resolver/passthrough" // Register passthrough resolver. | ||
| "google.golang.org/grpc/resolver" | ||
| _ "google.golang.org/grpc/resolver/dns" // Register dns resolver. | ||
| ) | ||
|
|
||
| func TestParseTarget(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| target string | ||
| defaultScheme string | ||
| wantScheme string | ||
| wantErr bool | ||
| errContain string | ||
| }{ | ||
| { | ||
| name: "valid_dns_scheme", | ||
| target: "dns:///example.com:443", | ||
| wantScheme: "dns", | ||
| }, | ||
| { | ||
| name: "valid_passthrough_scheme", | ||
| target: "passthrough:///localhost:8080", | ||
| wantScheme: "passthrough", | ||
| }, | ||
| { | ||
| name: "valid_dns_scheme_with_default", | ||
| target: "dns:///example.com:443", | ||
| defaultScheme: "dns", | ||
| wantScheme: "dns", | ||
| }, | ||
| { | ||
| name: "missing_scheme_falls_back_to_default", | ||
| target: "/path/to/socket", | ||
| defaultScheme: "passthrough", | ||
| wantScheme: "passthrough", | ||
| }, | ||
| { | ||
| name: "missing_scheme_without_default", | ||
| target: "/path/to/socket", | ||
| wantErr: true, | ||
| errContain: "has no scheme", | ||
| }, | ||
| { | ||
| name: "host:port_retries_with_default_scheme", | ||
| target: "localhost:8080", | ||
| defaultScheme: "passthrough", | ||
| wantScheme: "passthrough", | ||
| }, | ||
| { | ||
| name: "host:port_without_default", | ||
| target: "localhost:8080", | ||
| wantErr: true, | ||
| errContain: "no resolver registered for scheme", | ||
| }, | ||
| { | ||
| name: "unregistered_scheme", | ||
| target: "unknown:///example.com:443", | ||
| wantErr: true, | ||
| errContain: "no resolver registered for scheme", | ||
| }, | ||
| { | ||
| name: "unregistered_scheme_falls_back_to_default", | ||
| target: "unknown:///foo", | ||
| defaultScheme: "passthrough", | ||
| wantScheme: "passthrough", | ||
| }, | ||
| { | ||
| name: "invalid_URI", | ||
| target: "dns:///example\x00.com", | ||
| wantErr: true, | ||
| errContain: "invalid target URI", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := iresolver.ParseTarget(tt.target, tt.defaultScheme, resolver.Get) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("ParseTarget(%q, %q) error = %v, wantErr %v", tt.target, tt.defaultScheme, err, tt.wantErr) | ||
| return | ||
| } | ||
| if tt.wantErr { | ||
| if tt.errContain != "" && !strings.Contains(err.Error(), tt.errContain) { | ||
| t.Errorf("ParseTarget(%q, %q) error = %q, want it to contain %q", tt.target, tt.defaultScheme, err, tt.errContain) | ||
| } | ||
| return | ||
| } | ||
| if got.URL.Scheme != tt.wantScheme { | ||
| t.Errorf("ParseTarget(%q, %q).URL.Scheme = %q, want %q", tt.target, tt.defaultScheme, got.URL.Scheme, tt.wantScheme) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestParseTargetWithCustomBuilder(t *testing.T) { | ||
| // A registry that only recognises "passthrough". This mirrors the | ||
| // cc.getResolver pattern in ClientConn, which may include resolvers | ||
| // registered via dial options that are invisible to resolver.Get. | ||
| passthroughOnly := func(scheme string) resolver.Builder { | ||
| if scheme == "passthrough" { | ||
| return resolver.Get("passthrough") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| target string | ||
| defaultScheme string | ||
| wantScheme string | ||
| wantErr bool | ||
| errContain string | ||
| }{ | ||
| { | ||
| name: "known_scheme_resolves", | ||
| target: "passthrough:///service:8080", | ||
| wantScheme: "passthrough", | ||
| }, | ||
| { | ||
| name: "dns_not_in_custom_registry", | ||
| target: "dns:///example.com:443", | ||
| wantErr: true, | ||
| errContain: "no resolver registered for scheme", | ||
| }, | ||
| { | ||
| name: "unregistered_scheme_falls_back_to_custom_default", | ||
| target: "dns:///example.com:443", | ||
| defaultScheme: "passthrough", | ||
| wantScheme: "passthrough", | ||
| }, | ||
| { | ||
| // Opaque URI (host:port form) falls back to the default scheme. | ||
| name: "host:port_falls_back_to_custom_default", | ||
| target: "service:8080", | ||
| defaultScheme: "passthrough", | ||
| wantScheme: "passthrough", | ||
| }, | ||
| { | ||
| name: "missing_scheme_without_default", | ||
| target: "/path", | ||
| wantErr: true, | ||
| errContain: "has no scheme", | ||
| }, | ||
| { | ||
| name: "missing_scheme_uses_default", | ||
| target: "/path", | ||
| defaultScheme: "passthrough", | ||
| wantScheme: "passthrough", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := iresolver.ParseTarget(tt.target, tt.defaultScheme, passthroughOnly) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("ParseTarget(%q, %q) error = %v, wantErr %v", tt.target, tt.defaultScheme, err, tt.wantErr) | ||
| return | ||
| } | ||
| if tt.wantErr { | ||
| if tt.errContain != "" && !strings.Contains(err.Error(), tt.errContain) { | ||
| t.Errorf("ParseTarget(%q, %q) error = %q, want it to contain %q", tt.target, tt.defaultScheme, err, tt.errContain) | ||
| } | ||
| return | ||
| } | ||
| if got.URL.Scheme != tt.wantScheme { | ||
| t.Errorf("ParseTarget(%q, %q).URL.Scheme = %q, want %q", tt.target, tt.defaultScheme, got.URL.Scheme, tt.wantScheme) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.
Can we move the computation of the default scheme higher up and pass the default scheme to the first call to
iresolver.ParseTarget? That should allow us to get rid of the secondParseTargetbelow.