github.com/SimplyVC/oasis_api_server/src@v0.0.0-20220105202803-ad2c5a67840e/rpc/rpc.go (about) 1 package rpc 2 3 import ( 4 "crypto/tls" 5 "crypto/x509" 6 "fmt" 7 "io/ioutil" 8 9 "google.golang.org/grpc" 10 "google.golang.org/grpc/credentials" 11 12 cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc" 13 "github.com/oasisprotocol/oasis-core/go/common/identity" 14 consensus "github.com/oasisprotocol/oasis-core/go/consensus/api" 15 control "github.com/oasisprotocol/oasis-core/go/control/api" 16 registry "github.com/oasisprotocol/oasis-core/go/registry/api" 17 scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api" 18 sentry "github.com/oasisprotocol/oasis-core/go/sentry/api" 19 staking "github.com/oasisprotocol/oasis-core/go/staking/api" 20 ) 21 22 // SentryClient - initiate new sentry client 23 func SentryClient(address string, tlsPath string) (*grpc.ClientConn, 24 sentry.Backend, error) { 25 26 conn, err := ConnectTLS(address, tlsPath) 27 if err != nil { 28 conn.Close() 29 return nil, nil, fmt.Errorf("Failed to establish Sentry "+ 30 "Connection with node %s", address) 31 } 32 33 client := sentry.NewSentryClient(conn) 34 return conn, client, nil 35 } 36 37 // SchedulerClient - initiate new scheduler client 38 func SchedulerClient(address string) (*grpc.ClientConn, scheduler.Backend, 39 error) { 40 41 conn, err := Connect(address) 42 if err != nil { 43 return nil, nil, fmt.Errorf("Failed to establish Scheduler "+ 44 "Client Connection with node %s", address) 45 } 46 47 client := scheduler.NewSchedulerClient(conn) 48 return conn, client, nil 49 } 50 51 // NodeControllerClient - initiate new registry client 52 func NodeControllerClient(address string) (*grpc.ClientConn, 53 control.NodeController, error) { 54 55 conn, err := Connect(address) 56 if err != nil { 57 return nil, nil, fmt.Errorf("Failed to establish "+ 58 "NodeController Connection with node %s", address) 59 } 60 61 client := control.NewNodeControllerClient(conn) 62 return conn, client, nil 63 } 64 65 // RegistryClient - initiate new registry client 66 func RegistryClient(address string) (*grpc.ClientConn, 67 registry.Backend, error) { 68 69 conn, err := Connect(address) 70 if err != nil { 71 return nil, nil, fmt.Errorf("Failed to establish Registry "+ 72 "Client Connection with node %s", address) 73 } 74 75 client := registry.NewRegistryClient(conn) 76 return conn, client, nil 77 } 78 79 // ConsensusClient - initiate new consensus client 80 func ConsensusClient(address string) (*grpc.ClientConn, 81 consensus.ClientBackend, error) { 82 conn, err := Connect(address) 83 if err != nil { 84 return nil, nil, fmt.Errorf("failed to establish connection "+ 85 "with node %s", address) 86 } 87 88 client := consensus.NewConsensusClient(conn) 89 return conn, client, nil 90 } 91 92 // StakingClient - initiate new staking client 93 func StakingClient(address string) (*grpc.ClientConn, staking.Backend, error) { 94 conn, err := Connect(address) 95 if err != nil { 96 return nil, nil, fmt.Errorf("failed to establish connection "+ 97 "with node %s", address) 98 } 99 100 client := staking.NewStakingClient(conn) 101 return conn, client, nil 102 } 103 104 // ConnectTLS connects to server using TLS Certificate 105 func ConnectTLS(address string, tlsPath string) (*grpc.ClientConn, error) { 106 107 // Open and read tls file containing connection information 108 b, err := ioutil.ReadFile(tlsPath) 109 if err != nil { 110 return nil, err 111 } 112 113 // Add Credentials to a certificate pool 114 certPool := x509.NewCertPool() 115 if !certPool.AppendCertsFromPEM(b) { 116 return nil, fmt.Errorf("credentials: failed to append " + 117 "certificates") 118 } 119 120 // Create new TLS credentials 121 creds := credentials.NewTLS(&tls.Config{ 122 RootCAs: certPool, 123 ServerName: identity.CommonName, 124 }) 125 126 // Add Credentials to grpc options to be used for TLS Connection 127 opts := grpc.WithTransportCredentials(creds) 128 conn, err := cmnGrpc.Dial( 129 address, 130 opts, 131 ) 132 if err != nil { 133 return nil, err 134 } 135 136 return conn, nil 137 } 138 139 // Connect - connect to grpc 140 // Add grpc.WithBlock() and grpc.WithTimeout() 141 // to have dial to constantly try and establish connection 142 func Connect(address string) (*grpc.ClientConn, error) { 143 opts := []grpc.DialOption{grpc.WithInsecure()} 144 opts = append(opts, grpc.WithDefaultCallOptions( 145 grpc.WaitForReady(false))) 146 147 conn, err := cmnGrpc.Dial( 148 address, 149 opts..., 150 ) 151 if err != nil { 152 return nil, err 153 } 154 155 return conn, nil 156 }