-
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 1 commit
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,72 @@ | ||
| /* | ||
| * | ||
| * 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. | ||
| // | ||
| // Hierarchical URIs (scheme://authority/path) with a non-empty scheme are | ||
| // validated directly: if builder returns nil for the scheme an error is | ||
| // returned immediately with no fallback. | ||
| // | ||
| // For opaque URIs (e.g. "host:port" where url.URL.Opaque is non-empty), | ||
| // 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 != "" { | ||
| if builder(u.Scheme) != nil { | ||
| // Recognised scheme (hierarchical or opaque form) — use as-is. | ||
| return resolver.Target{URL: *u}, nil | ||
| } | ||
| if u.Opaque == "" { | ||
| // Unregistered scheme in hierarchical URI form (scheme://...): the | ||
| // caller explicitly chose this scheme; do not silently fall back. | ||
| return resolver.Target{}, fmt.Errorf("no resolver registered for scheme %q in target %q", u.Scheme, target) | ||
| } | ||
|
||
| // Opaque URI (e.g. "host:port") with unregistered scheme: treat the | ||
| // same as an empty-scheme URI and fall through to the retry below. | ||
| } | ||
| // Parse error, empty scheme, or opaque URI with unregistered scheme: | ||
| // 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,201 @@ | ||
| /* | ||
| * | ||
| * 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", | ||
| }, | ||
| { | ||
| // Explicit hierarchical URI with unknown scheme is rejected even when | ||
| // a default is provided. Only opaque URIs (host:port) fall back. | ||
| name: "unregistered explicit scheme is rejected", | ||
| 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", | ||
| }, | ||
| { | ||
| // Explicit hierarchical URI: dns is not in the custom registry, and | ||
| // unlike an opaque "host:port" URI, explicit schemes are not retried. | ||
| name: "unregistered explicit scheme rejected even with default", | ||
| 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.