-
Notifications
You must be signed in to change notification settings - Fork 167
/
test-external-grpc.go
86 lines (74 loc) · 2 KB
/
test-external-grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cmd
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"google.golang.org/grpc/metadata"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
config2 "github.com/pydio/cells/v4/common/config"
"github.com/pydio/cells/v4/common/proto/tree"
)
var (
extAddr string
extToken string
)
var TestExternalGrpc = &cobra.Command{
Use: "grpc",
Short: "Sample GRPC request sent to web-facing interface",
RunE: func(cmd *cobra.Command, args []string) error {
creds, err := loadTLSCredentials()
if err != nil {
return err
}
conn, err := grpc.Dial(extAddr, grpc.WithTransportCredentials(creds))
if err != nil {
return err
}
cli := tree.NewNodeProviderClient(conn)
md := metadata.MD{}
md.Set("x-pydio-bearer", extToken)
og := metadata.NewOutgoingContext(context.Background(), md)
stream, e := cli.ListNodes(og, &tree.ListNodesRequest{Node: &tree.Node{Path: "common-files"}})
if e != nil {
return e
}
for {
rsp, er := stream.Recv()
if er != nil {
if er != io.EOF {
return er
}
break
}
fmt.Println(rsp.GetNode().GetPath(), rsp.GetNode().GetType(), rsp.GetNode().GetSize())
}
return nil
},
}
func loadTLSCredentials() (credentials.TransportCredentials, error) {
// Load certificate of the CA who signed server's certificate
pemServerCA, err := ioutil.ReadFile(filepath.Join(config2.ApplicationWorkingDir(), "certs", "rootCA.pem"))
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(pemServerCA) {
return nil, fmt.Errorf("failed to add server CA's certificate")
}
// Create the credentials and return it
config := &tls.Config{
RootCAs: certPool,
}
return credentials.NewTLS(config), nil
}
func init() {
TestExternalGrpc.Flags().StringVarP(&extAddr, "addr", "a", "local.pydio:8080", "Server address")
TestExternalGrpc.Flags().StringVarP(&extToken, "token", "t", "", "User Personal Token")
RootCmd.AddCommand(TestExternalGrpc)
}