-
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
base: master
Are you sure you want to change the base?
Changes from all commits
623f036
5ca5983
8ec2d85
383f46a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * | ||
| * 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. | ||
| // | ||
| // For hierarchical URIs (scheme://authority/path) with an unregistered scheme, | ||
| // ParseTarget returns an error immediately because the caller explicitly chose | ||
| // that scheme. For opaque URIs (e.g. host:port), empty-scheme URIs, and parse | ||
| // failures, 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 | ||
| } | ||
| // Hierarchical URI with an unregistered scheme: the caller explicitly | ||
| // chose this scheme, so do not silently fall back. | ||
| if err == nil && u.Scheme != "" && u.Opaque == "" { | ||
| return resolver.Target{}, fmt.Errorf("no resolver registered for scheme %q in target %q", u.Scheme, target) | ||
| } | ||
|
Comment on lines
+49
to
+53
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. Special handling for opaque URLs like this isn't present in the existing code. I assume this is required because the two places where the target is parsed behave differently:
If we pass @easwars, can you comment on whether it makes sense to unify these two usages? If so, what is the expected behavior? |
||
| // Parse error, empty scheme, or opaque URI (e.g. host:port): retry by | ||
| // prepending defaultScheme if one is provided. | ||
| if defaultScheme != "" { | ||
| if u2, err2 := url.Parse(defaultScheme + ":///" + target); err2 == nil && builder(u2.Scheme) != 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) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * | ||
| * 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_hierarchical_scheme_no_fallback", | ||
| target: "unknown:///foo", | ||
| defaultScheme: "passthrough", | ||
| wantErr: true, | ||
| errContain: "no resolver registered for scheme", | ||
| }, | ||
| { | ||
| 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_hierarchical_scheme_no_fallback", | ||
| target: "dns:///example.com:443", | ||
| defaultScheme: "passthrough", | ||
| wantErr: true, | ||
| errContain: "no resolver registered for scheme", | ||
| }, | ||
| { | ||
| // 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.