feat(endpoint): add endpoint parser (#1273)
* feat(endpoint): add endpoint parser * fix parseTarget ut * fix insecure testingpull/1275/head
parent
8f4e78b47d
commit
bc35f20228
@ -1,64 +0,0 @@ |
||||
|
||||
-----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 |
||||
DFErW7XHKHe8vKXuRLG5k3xZBiH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||||
-----END CERTIFICATE----- |
||||
-----BEGIN CERTIFICATE----- |
||||
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 |
||||
djovWE7E6ic= |
||||
-----END CERTIFICATE----- |
@ -1,81 +0,0 @@ |
||||
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) |
||||
} |
@ -1,75 +0,0 @@ |
||||
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) |
||||
} |
||||
} |
@ -1,65 +0,0 @@ |
||||
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,132 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/tls" |
||||
"crypto/x509" |
||||
"fmt" |
||||
"io/ioutil" |
||||
"testing" |
||||
"time" |
||||
|
||||
etcdregistry "github.com/go-kratos/etcd/registry" |
||||
"github.com/go-kratos/kratos/examples/helloworld/helloworld" |
||||
pb "github.com/go-kratos/kratos/examples/helloworld/helloworld" |
||||
"github.com/go-kratos/kratos/v2" |
||||
"github.com/go-kratos/kratos/v2/registry" |
||||
"github.com/go-kratos/kratos/v2/transport/grpc" |
||||
"github.com/go-kratos/kratos/v2/transport/http" |
||||
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("Welcome %+v!", in.Name)}, nil |
||||
} |
||||
|
||||
func startServer(r registry.Registrar, c *tls.Config) (app *kratos.App, err error) { |
||||
httpSrv := http.NewServer(http.TLSConfig(c)) |
||||
grpcSrv := grpc.NewServer(grpc.TLSConfig(c)) |
||||
|
||||
s := &server{} |
||||
pb.RegisterGreeterServer(grpcSrv, s) |
||||
pb.RegisterGreeterHTTPServer(httpSrv, s) |
||||
|
||||
app = kratos.New( |
||||
kratos.Name("helloworld"), |
||||
kratos.Server( |
||||
httpSrv, |
||||
grpcSrv, |
||||
), |
||||
kratos.Registrar(r), |
||||
kratos.RegistrarTimeout(5*time.Second), |
||||
) |
||||
go func() { |
||||
err = app.Run() |
||||
}() |
||||
time.Sleep(time.Second) |
||||
return |
||||
} |
||||
|
||||
func callGRPC(t *testing.T, r registry.Discovery, c *tls.Config) { |
||||
conn, err := grpc.Dial( |
||||
context.Background(), |
||||
grpc.WithEndpoint("discovery:///helloworld"), |
||||
grpc.WithTLSConfig(c), |
||||
grpc.WithDiscovery(r), |
||||
) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer conn.Close() |
||||
client := helloworld.NewGreeterClient(conn) |
||||
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
t.Logf("[grpc] SayHello %+v\n", reply) |
||||
} |
||||
|
||||
func callHTTP(t *testing.T, r registry.Discovery, c *tls.Config) { |
||||
conn, err := http.NewClient( |
||||
context.Background(), |
||||
http.WithEndpoint("discovery:///helloworld"), |
||||
http.WithTLSConfig(c), |
||||
http.WithDiscovery(r), |
||||
http.WithBlock(), |
||||
) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer conn.Close() |
||||
client := helloworld.NewGreeterHTTPClient(conn) |
||||
reply, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "kratos"}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
t.Logf("[http] SayHello %+v\n", reply) |
||||
} |
||||
|
||||
func TestETCD(t *testing.T) { |
||||
client, err := etcd.New(etcd.Config{ |
||||
Endpoints: []string{"127.0.0.1:2379"}, |
||||
}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
b, err := ioutil.ReadFile("./cert/server.crt") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
cp := x509.NewCertPool() |
||||
if !cp.AppendCertsFromPEM(b) { |
||||
t.Fatal(err) |
||||
} |
||||
cert, err := tls.LoadX509KeyPair("./cert/server.crt", "./cert/server.key") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
tlsConf := &tls.Config{ |
||||
ServerName: "www.kratos.com", |
||||
RootCAs: cp, |
||||
Certificates: []tls.Certificate{cert}, |
||||
} |
||||
r := etcdregistry.New(client) |
||||
srv, err := startServer(r, nil) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
srvTLS, err := startServer(r, tlsConf) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
callHTTP(t, r, tlsConf) |
||||
callGRPC(t, r, tlsConf) |
||||
srv.Stop() |
||||
srvTLS.Stop() |
||||
} |
Loading…
Reference in new issue