DefaultGrpcDialOptions returns a set of sensible default list of grpc.DialOption values. It includes transport credentials and configures per-RPC credentials using an authorizer, if one is configured.
(hostport string, s UsesAuthorizer, additionalOpts ...grpc.DialOption)
| 106 | // DefaultGrpcDialOptions returns a set of sensible default list of grpc.DialOption values. It includes |
| 107 | // transport credentials and configures per-RPC credentials using an authorizer, if one is configured. |
| 108 | func DefaultGrpcDialOptions(hostport string, s UsesAuthorizer, additionalOpts ...grpc.DialOption) (opts []grpc.DialOption) { |
| 109 | var ( |
| 110 | port string |
| 111 | host string |
| 112 | err error |
| 113 | ) |
| 114 | |
| 115 | host, port, err = net.SplitHostPort(hostport) |
| 116 | |
| 117 | // TODO(oxisto): make a better distinction, for now this is ok |
| 118 | if err == nil && port == "443" { |
| 119 | // Use default TLS configuration using the system cert store |
| 120 | opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ |
| 121 | ServerName: host, |
| 122 | MinVersion: tls.VersionTLS12, |
| 123 | MaxVersion: 0, |
| 124 | }))) |
| 125 | } else { |
| 126 | opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 127 | } |
| 128 | |
| 129 | // In practice, we should always have an authorizer, so we could fail early here. However, |
| 130 | // if the server-side has not enabled the auth middleware (for example in testing), it is perfectly |
| 131 | // fine to at least attempt to run it without one. If the server-side has enabled auth middleware |
| 132 | // and does not receive any client credentials, the actual RPC call will then fail later. |
| 133 | if s != nil { |
| 134 | if authorizer := s.Authorizer(); authorizer != nil { |
| 135 | opts = append(opts, grpc.WithPerRPCCredentials(authorizer)) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Apply any additional options that we might have |
| 140 | opts = append(opts, additionalOpts...) |
| 141 | |
| 142 | return opts |
| 143 | } |
no test coverage detected