grpc‘s secure discovery (#1270)
* add gprc secure discovery * add http insecure filterpull/1275/head
parent
a636fd52a4
commit
8f4e78b47d
@ -0,0 +1,64 @@ |
||||
|
||||
-----BEGIN CERTIFICATE----- |
||||
MIIFZTCCA02gAwIBAgIBATANBgkqhkiG9w0BAQUFADB8MQswCQYDVQQGEwJDTjEL |
||||
MAkGA1UECAwCU0gxCzAJBgNVBAcMAlNIMQ8wDQYDVQQKDAZrcmF0b3MxCzAJBgNV |
||||
BAsMAklUMRMwEQYDVQQDDAprcmF0b3MuY29tMSAwHgYJKoZIhvcNAQkBFhFrcmF0 |
||||
b3NAa3JhdG9zLmNvbTAeFw0yMTA3MjcxNTM1MjJaFw0yMjA3MjcxNTM1MjJaME0x |
||||
CzAJBgNVBAYTAkNOMQswCQYDVQQIDAJTSDELMAkGA1UEBwwCU0gxDzANBgNVBAoM |
||||
BmtyYXRvczETMBEGA1UEAwwKa3JhdG9zLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQAD |
||||
ggIPADCCAgoCggIBALfEWAuPqUHTwTiOT8dtiCM3aEm1D7I6K/PY2mSDMMOI4f5z |
||||
TYi7LsKLLItMQR44cEDein/kl0U0QNYJRHqVCr/3IXA4ds0maiq+npY/S2KABDiI |
||||
Z38TZ1PZ2bcD3Jb0o1gw0GcSokOGzVtuNKSiASc3D711AepGerChD5UrbixrZdg2 |
||||
wAeWZj39Tl93+zCcldVrCHMSM7LmDluHJ5KZ8T6auuK6ypQPVhqLz8VseHhB/IRw |
||||
s9/o9OlJ3kv4wB/CWlFIvU6ZGVnshZAGOk3Brq25kw600RRDr2MpxNhFY1Xvrt7D |
||||
tAJ7fK3/3VaV+I2C4OzzmztK7T1WlL8XqZj6I3t1ZCZryMvDIUPzC4mgiRqEJiF7 |
||||
VzuVm1sjyTKkD4oX6ZjJYXJ/6pbvd8+AexuwVAsQJKqaF1iQC9jThg55RUTkGo8F |
||||
DFErW7XHKHe8vKXuRLG5k3xZBiHK7gsVyHzx0ouuSuZMFHg7L9ACeeqtqxqKNd+0 |
||||
fIo4N0vNb3GEj+YaTLoadhDSBEsynyQNTfrDf+oFRmQUg3q0W4VJHJFhkmTHRRcq |
||||
Qj6xEAJDHC1xr8yr3jif9BKWG2+zEbvpiTcRXKhycv2OUI11dK72aMnOycusJjRe |
||||
8pOqcYhSVQZnz31WWlkmX9TRiQtEeUUkFCYAnIArSKm5rNwOddLCzC8Z4js1AgMB |
||||
AAGjITAfMB0GA1UdEQQWMBSCDCoua3JhdG9zLmNvbYcEfwAAATANBgkqhkiG9w0B |
||||
AQUFAAOCAgEAjr3SXzNOcN8+JQuroS6hKHadrcp6djepd3r5YKSEjKBNxVAU0gj6 |
||||
QGl0zjSqhxSFwN4wCqXU/4JJVOyAJCV+t992j5wNdaGI+Tcu4whK2LtPi0O68ttq |
||||
Nn5H/8bmotW0IZ/YDcq1V8EVWiTZPECk4QLx26S2sjG4HOKNUAs8o+PoUmQE5bKJ |
||||
XBFWmjsOfPnI0WBGnuCvGUw5wP1ipLiuK+OhoTNKA6SXPopm5KMDv7gjYPlcmPyI |
||||
sJcpve75m9EXQxrDvJtvws8MIZnkWvWi7bW6uQ7274S7YjMZL09/sQTolQOtwl59 |
||||
pvEbkQNPzgdvQYAfrlJjSBtbq3OtHF+j+p2K+7R0TY2F1OW3LeV8vkcq7IOt38Er |
||||
FK5feNEL3t9GlrF8ASmJp/JvhWQiJo5tZJxWZ68CjKfLmVd406ehsK5XNlHJemnl |
||||
hNpuAegeV6WDglvMNavvQCJfK5mKokn73HujtQveZ8vwPKV4f6Wg3sHJ2yyP7ou2 |
||||
2UZ10Qbp6UvFYfYuMvLuq8kznapWqQ0dIJzPDs+CzVtjk1eINUF9UjsSvqX4oCvy |
||||
+ZvaM9k4kU/fJMRkLstc/Dx2G13T/moI/l5sw0qFtck12zs/SQ7xgcW3b0ruGX9S |
||||
11opfl5R86GpXXbz+vNL9fWP+8cIvoGZK8RAC872bcMPEoJPjPIwAbI= |
||||
-----END CERTIFICATE----- |
||||
-----BEGIN CERTIFICATE----- |
||||
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 |
||||
djovWE7E6ic= |
||||
-----END CERTIFICATE----- |
@ -0,0 +1,81 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/tls" |
||||
"crypto/x509" |
||||
"github.com/go-kratos/kratos/v2/transport/http" |
||||
"io/ioutil" |
||||
"log" |
||||
"time" |
||||
|
||||
"github.com/go-kratos/etcd/registry" |
||||
"github.com/go-kratos/kratos/examples/helloworld/helloworld" |
||||
"github.com/go-kratos/kratos/v2/transport/grpc" |
||||
clientv3 "go.etcd.io/etcd/client/v3" |
||||
) |
||||
|
||||
func main() { |
||||
|
||||
b, err := ioutil.ReadFile("../cert/server.crt") |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
cp := x509.NewCertPool() |
||||
if !cp.AppendCertsFromPEM(b) { |
||||
panic(err) |
||||
} |
||||
tlsConf := &tls.Config{ServerName: "www.kratos.com", RootCAs: cp} |
||||
|
||||
cli, err := clientv3.New(clientv3.Config{ |
||||
Endpoints: []string{"127.0.0.1:2379"}, |
||||
}) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
r := registry.New(cli) |
||||
for { |
||||
callGRPC(r, tlsConf) |
||||
callHTTP(r, tlsConf) |
||||
time.Sleep(time.Second) |
||||
} |
||||
} |
||||
|
||||
func callGRPC(r *registry.Registry, tlsConf *tls.Config) { |
||||
conn, err := grpc.Dial( |
||||
context.Background(), |
||||
grpc.WithEndpoint("discovery:///helloworld"), |
||||
grpc.WithDiscovery(r), |
||||
grpc.WithTLSConfig(tlsConf), |
||||
) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
defer conn.Close() |
||||
client := helloworld.NewGreeterClient(conn) |
||||
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
log.Printf("[grpc] SayHello %+v\n", reply) |
||||
} |
||||
|
||||
func callHTTP(r *registry.Registry, tlsConf *tls.Config) { |
||||
conn, err := http.NewClient( |
||||
context.Background(), |
||||
http.WithEndpoint("discovery:///helloworld"), |
||||
http.WithDiscovery(r), |
||||
http.WithBlock(), |
||||
http.WithTLSConfig(tlsConf), |
||||
) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
defer conn.Close() |
||||
client := helloworld.NewGreeterHTTPClient(conn) |
||||
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
log.Printf("[http] SayHello %+v\n", reply) |
||||
} |
@ -0,0 +1,75 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/tls" |
||||
"fmt" |
||||
"github.com/go-kratos/kratos/v2/transport/http" |
||||
"log" |
||||
|
||||
"github.com/go-kratos/etcd/registry" |
||||
pb "github.com/go-kratos/kratos/examples/helloworld/helloworld" |
||||
"github.com/go-kratos/kratos/v2" |
||||
"github.com/go-kratos/kratos/v2/middleware/recovery" |
||||
"github.com/go-kratos/kratos/v2/transport/grpc" |
||||
etcd "go.etcd.io/etcd/client/v3" |
||||
) |
||||
|
||||
// server is used to implement helloworld.GreeterServer.
|
||||
type server struct { |
||||
pb.UnimplementedGreeterServer |
||||
} |
||||
|
||||
// SayHello implements helloworld.GreeterServer
|
||||
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { |
||||
return &pb.HelloReply{Message: fmt.Sprintf("use tls:Welcome %+v!", in.Name)}, nil |
||||
} |
||||
|
||||
func main() { |
||||
|
||||
cert, err := tls.LoadX509KeyPair("../cert/server.crt", "../cert/server.key") |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
tlsConf := &tls.Config{Certificates: []tls.Certificate{cert}} |
||||
|
||||
client, err := etcd.New(etcd.Config{ |
||||
Endpoints: []string{"127.0.0.1:2379"}, |
||||
}) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
|
||||
grpcSrv := grpc.NewServer( |
||||
grpc.Address(":9000"), |
||||
grpc.Middleware( |
||||
recovery.Recovery(), |
||||
), |
||||
grpc.TLSConfig(tlsConf), |
||||
) |
||||
|
||||
httpSrv := http.NewServer( |
||||
http.Address(":8000"), |
||||
http.Middleware( |
||||
recovery.Recovery(), |
||||
), |
||||
http.TLSConfig(tlsConf), |
||||
) |
||||
|
||||
s := &server{} |
||||
pb.RegisterGreeterServer(grpcSrv, s) |
||||
pb.RegisterGreeterHTTPServer(httpSrv, s) |
||||
|
||||
r := registry.New(client) |
||||
app := kratos.New( |
||||
kratos.Name("helloworld"), |
||||
kratos.Server( |
||||
grpcSrv, |
||||
httpSrv, |
||||
), |
||||
kratos.Registrar(r), |
||||
) |
||||
if err := app.Run(); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
} |
@ -0,0 +1,65 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"github.com/go-kratos/kratos/v2/transport/http" |
||||
"log" |
||||
|
||||
"github.com/go-kratos/etcd/registry" |
||||
pb "github.com/go-kratos/kratos/examples/helloworld/helloworld" |
||||
"github.com/go-kratos/kratos/v2" |
||||
"github.com/go-kratos/kratos/v2/middleware/recovery" |
||||
"github.com/go-kratos/kratos/v2/transport/grpc" |
||||
etcd "go.etcd.io/etcd/client/v3" |
||||
) |
||||
|
||||
// server is used to implement helloworld.GreeterServer.
|
||||
type server struct { |
||||
pb.UnimplementedGreeterServer |
||||
} |
||||
|
||||
// SayHello implements helloworld.GreeterServer
|
||||
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { |
||||
return &pb.HelloReply{Message: fmt.Sprintf("not use tls:Welcome %+v!", in.Name)}, nil |
||||
} |
||||
|
||||
func main() { |
||||
client, err := etcd.New(etcd.Config{ |
||||
Endpoints: []string{"127.0.0.1:2379"}, |
||||
}) |
||||
if err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
|
||||
grpcSrv := grpc.NewServer( |
||||
grpc.Address(":9001"), |
||||
grpc.Middleware( |
||||
recovery.Recovery(), |
||||
), |
||||
) |
||||
|
||||
httpSrv := http.NewServer( |
||||
http.Address(":8001"), |
||||
http.Middleware( |
||||
recovery.Recovery(), |
||||
), |
||||
) |
||||
|
||||
s := &server{} |
||||
pb.RegisterGreeterServer(grpcSrv, s) |
||||
pb.RegisterGreeterHTTPServer(httpSrv, s) |
||||
|
||||
r := registry.New(client) |
||||
app := kratos.New( |
||||
kratos.Name("helloworld"), |
||||
kratos.Server( |
||||
grpcSrv, |
||||
httpSrv, |
||||
), |
||||
kratos.Registrar(r), |
||||
) |
||||
if err := app.Run(); err != nil { |
||||
log.Fatal(err) |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
package endpoint |
||||
|
||||
import ( |
||||
"net/url" |
||||
"strconv" |
||||
) |
||||
|
||||
func NewEndpoint(scheme, host string, isSecure bool) *url.URL { |
||||
var query string |
||||
if isSecure { |
||||
query = "isSecure=true" |
||||
} |
||||
return &url.URL{Scheme: scheme, Host: host, RawQuery: query} |
||||
} |
||||
|
||||
func IsSecure(url *url.URL) bool { |
||||
ok, err := strconv.ParseBool(url.Query().Get("isSecure")) |
||||
if err != nil { |
||||
return false |
||||
} |
||||
return ok |
||||
} |
@ -0,0 +1,42 @@ |
||||
package endpoint |
||||
|
||||
import ( |
||||
"net/url" |
||||
"reflect" |
||||
"testing" |
||||
) |
||||
|
||||
func TestEndPoint(t *testing.T) { |
||||
type args struct { |
||||
url *url.URL |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want bool |
||||
}{ |
||||
// TODO: Add test cases.
|
||||
{ |
||||
name: "grpc://127.0.0.1?isSecure=false", |
||||
args: args{NewEndpoint("grpc", "127.0.0.1", false)}, |
||||
want: false, |
||||
}, |
||||
{ |
||||
name: "grpc://127.0.0.1?isSecure=true", |
||||
args: args{NewEndpoint("http", "127.0.0.1", true)}, |
||||
want: true, |
||||
}, |
||||
{ |
||||
name: "grpc://127.0.0.1", |
||||
args: args{NewEndpoint("grpc", "localhost", false)}, |
||||
want: false, |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if got := IsSecure(tt.args.url); !reflect.DeepEqual(got, tt.want) { |
||||
t.Errorf("GetQuery() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
Loading…
Reference in new issue