Skip to content

Commit a9e3b2d

Browse files
authored
Merge pull request #190 from norman465/update_grpc_example
Update gRPC example to support transport credentials
2 parents ea01433 + 1a863ab commit a9e3b2d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

grpc/client/main.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"crypto/x509"
56
"encoding/json"
67
"fmt"
78
"log"
@@ -13,6 +14,7 @@ import (
1314
"github.com/gorilla/mux"
1415
ec "github.com/qu1queee/CodeEngine/grpc/ecommerce"
1516
"google.golang.org/grpc"
17+
"google.golang.org/grpc/credentials"
1618
"google.golang.org/grpc/credentials/insecure"
1719
)
1820

@@ -98,10 +100,28 @@ func BuyHandler(w http.ResponseWriter, r *http.Request, groceryClient ec.Grocery
98100
}
99101

100102
func main() {
101-
localEndpoint := os.Getenv("LOCAL_ENDPOINT_WITH_PORT")
103+
endpoint := os.Getenv("ENDPOINT_WITH_PORT")
104+
if endpoint == "" {
105+
log.Fatalf("no endpoint set: %s", endpoint)
106+
}
107+
certPool, err := x509.SystemCertPool()
108+
if err != nil {
109+
log.Fatalf("failed to get cert pool: %v", err)
110+
}
111+
creds := credentials.NewClientTLSFromCert(certPool, "")
112+
insArg := os.Getenv("INSECURE")
113+
if insArg != "" {
114+
unencrypted, err := strconv.ParseBool(insArg)
115+
if err != nil {
116+
log.Fatalf("could not parse %v: ", err)
117+
}
118+
if unencrypted {
119+
creds = insecure.NewCredentials()
120+
}
121+
}
102122

103-
fmt.Printf("using local endpoint: %s\n", localEndpoint)
104-
conn, err := grpc.Dial(localEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
123+
fmt.Printf("using endpoint: %s\n", endpoint)
124+
conn, err := grpc.Dial(endpoint, grpc.WithTransportCredentials(creds))
105125
if err != nil {
106126
log.Fatalf("failed to connect: %v", err)
107127
}

grpc/run

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ echo "[INFO] Local endpoint is: ${SERVER_INTERNAL_ENDPOINT}"
2929

3030
# Create the client server app
3131
echo "[INFO] Creating CE client/server application ${CLIENT_APP_NAME}"
32-
ibmcloud ce app create --name "${CLIENT_APP_NAME}" --image icr.io/codeengine/grpc-client --min-scale 0 --env LOCAL_ENDPOINT_WITH_PORT="${SERVER_INTERNAL_ENDPOINT}:80"
32+
ibmcloud ce app create --name "${CLIENT_APP_NAME}" --image icr.io/codeengine/grpc-client --min-scale 0 --env INSECURE=true --env ENDPOINT_WITH_PORT="${SERVER_INTERNAL_ENDPOINT}:80"
3333

3434
# Get the client server public endpoint
3535
echo "[INFO] Retrieving client/server public endpoint"

0 commit comments

Comments
 (0)