Skip to content
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

grpc: optional interface to provide channel authority #6752

Merged
merged 6 commits into from Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 6 additions & 19 deletions clientconn.go
Expand Up @@ -1980,28 +1980,15 @@ func (cc *ClientConn) determineAuthority() error {
return fmt.Errorf("ClientConn's authority from transport creds %q and dial option %q don't match", authorityFromCreds, authorityFromDialOption)
}

endpoint := cc.parsedTarget.Endpoint()
target := cc.target
switch {
case authorityFromDialOption != "":
if authorityFromDialOption != "" {
cc.authority = authorityFromDialOption
case authorityFromCreds != "":
} else if authorityFromCreds != "" {
cc.authority = authorityFromCreds
case strings.HasPrefix(target, "unix:") || strings.HasPrefix(target, "unix-abstract:"):
// TODO: remove when the unix resolver implements optional interface to
// return channel authority.
cc.authority = "localhost"
case strings.HasPrefix(endpoint, ":"):
} else if auth, isAuthOverrider := cc.resolverBuilder.(resolver.AuthorityOverrider); isAuthOverrider {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go style: please name this ok instead of isAuthOverrider.

cc.authority = auth.OverrideAuthority(cc.parsedTarget)
} else if endpoint := cc.parsedTarget.Endpoint(); strings.HasPrefix(endpoint, ":") {
easwars marked this conversation as resolved.
Show resolved Hide resolved
cc.authority = "localhost" + endpoint
default:
// TODO: Define an optional interface on the resolver builder to return
// the channel authority given the user's dial target. For resolvers
// which don't implement this interface, we will use the endpoint from
// "scheme://authority/endpoint" as the default authority.
// Escape the endpoint to handle use cases where the endpoint
// might not be a valid authority by default.
// For example an endpoint which has multiple paths like
// 'a/b/c', which is not a valid authority by default.
} else {
cc.authority = encodeAuthority(endpoint)
}
channelz.Infof(logger, cc.channelzID, "Channel authority set to %q", cc.authority)
Expand Down
4 changes: 4 additions & 0 deletions internal/resolver/unix/unix.go
Expand Up @@ -61,6 +61,10 @@ func (b *builder) Scheme() string {
return b.scheme
}

func (b *builder) OverrideAuthority(t resolver.Target) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since t is unused, you can omit it entirely here.

return "localhost"
}

type nopResolver struct {
}

Expand Down
9 changes: 9 additions & 0 deletions resolver/resolver.go
Expand Up @@ -319,3 +319,12 @@ type Resolver interface {
// Close closes the resolver.
Close()
}

// AuthorityOverrider is implemented by Builders that wish to override the
// default authority for the ClientConn.
// By default, the authority used is target.Endpoint()
easwars marked this conversation as resolved.
Show resolved Hide resolved
type AuthorityOverrider interface {
// OverrideAuthority returns the authority to use for a ClientConn with the
// given target. It must not perform I/O or any other blocking operations.
easwars marked this conversation as resolved.
Show resolved Hide resolved
OverrideAuthority(Target) string
}