Skip to content

Commit

Permalink
add hand-written client
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks committed Oct 18, 2024
1 parent 1721eb6 commit 948b6d9
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 1 deletion.
131 changes: 131 additions & 0 deletions cmd/stores-client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"log"
"os"

pb "github.com/bobadojo/go/pkg/stores/v1/storespb"
"golang.org/x/oauth2/google"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)

const defaultName = "world"

var (
addr = flag.String("addr", "127.0.0.1:50051", "Address of grpc server.")
key = flag.String("api-key", "", "API key.")
token = flag.String("token", "", "Authentication token.")
keyfile = flag.String("keyfile", "", "Path to a Google service account key file.")
audience = flag.String("audience", "", "Audience.")
insecure = flag.Bool("insecure", false, "Insecure connections.")
)

func main() {
if err := run(); err != nil {
log.Fatalf("%s", err)
}
}

func run() error {
flag.Parse()

// Set up a connection to the server.
var conn *grpc.ClientConn
var err error
if *insecure {
conn, err = grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
} else {
proxyCA := "roots.pem" // CA cert that signed the proxy
f, err := os.ReadFile(proxyCA)
if err != nil {
log.Fatalf("%v", err)
}
p := x509.NewCertPool()
p.AppendCertsFromPEM(f)
tlsConfig := &tls.Config{
RootCAs: p,
InsecureSkipVerify: true,
}

conn, err = grpc.Dial(*addr, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
//conn, err := grpc.Dial(os.Getenv("hello.endpoints.agentio.cloud.goog:443"), grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{InsecureSkipVerify: false})))
//conn, err := grpc.Dial("hello.endpoints.agentio.cloud.goog:443", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
}
defer conn.Close()
c := pb.NewStoresClient(conn)

if *keyfile != "" {
log.Printf("Authenticating using Google service account key in %s", *keyfile)
keyBytes, err := ioutil.ReadFile(*keyfile)
if err != nil {
log.Fatalf("Unable to read service account key file %s: %v", *keyfile, err)
}

tokenSource, err := google.JWTAccessTokenSourceFromJSON(keyBytes, *audience)
if err != nil {
log.Fatalf("Error building JWT access token source: %v", err)
}
jwt, err := tokenSource.Token()
if err != nil {
return nil
}
*token = jwt.AccessToken
// NOTE: the generated JWT token has a 1h TTL.
// Make sure to refresh the token before it expires by calling TokenSource.Token() for each outgoing requests.
// Calls to this particular implementation of TokenSource.Token() are cheap.
}

ctx := context.Background()
if *key != "" {
log.Printf("Using API key, len=%d", len(*key))
ctx = metadata.AppendToOutgoingContext(ctx, "x-api-key", *key)
}
if *token != "" {
log.Printf("Using authentication token: %s", *token)
ctx = metadata.AppendToOutgoingContext(ctx, "Authorization", fmt.Sprintf("Bearer %s", *token))
}

// Contact the server and print out its response.
r, err := c.FindStores(ctx, &pb.FindStoresRequest{
Bounds: &pb.BoundingBox{
Max: &pb.Location{
Latitude: 26.2,
Longitude: -80,
},
Min: &pb.Location{
Latitude: 26.1,
Longitude: -81,
},
},
})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Stores: %d", len(r.Stores))
for i, s := range r.Stores {
log.Printf("%v", s)

r2, err := c.GetStore(ctx, &pb.GetStoreRequest{
Name: s.Name,
})
if err != nil {
return err
}
log.Printf("%d: %+v", i, r2)
}
return nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ go 1.22.5
require (
github.com/bobadojo/go v0.0.3
github.com/tidwall/rtree v1.10.0
golang.org/x/oauth2 v0.22.0
google.golang.org/grpc v1.67.1
)

require (
cloud.google.com/go/compute/metadata v0.5.0 // indirect
github.com/tidwall/geoindex v1.7.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cloud.google.com/go/compute/metadata v0.5.0 h1:Zr0eK8JbFv6+Wi4ilXAR8FJ3wyNdpxHKJNPos6LTZOY=
cloud.google.com/go/compute/metadata v0.5.0/go.mod h1:aHnloV2TPI38yx4s9+wAZhHykWvVCfu7hQbF+9CWoiY=
github.com/bobadojo/go v0.0.3 h1:Hr6oaMplnK7Mv1j4nsfChFeZ0ApRdMngIqWp/MtmU5E=
github.com/bobadojo/go v0.0.3/go.mod h1:E9iGT/RP9PCYAlJ9ORDwHt5TP3BwZ0AssCz0O8aHsMU=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
Expand All @@ -12,6 +14,8 @@ github.com/tidwall/rtree v1.10.0 h1:+EcI8fboEaW1L3/9oW/6AMoQ8HiEIHyR7bQOGnmz4Mg=
github.com/tidwall/rtree v1.10.0/go.mod h1:iDJQ9NBRtbfKkzZu02za+mIlaP+bjYPnunbSNidpbCQ=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA=
golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
Expand Down
13 changes: 12 additions & 1 deletion kubernetes/managed-espv2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ spec:
tls:
- hosts:
- stores.timbx.me
- stores.endpoints.bobadojo.cloud.goog
secretName: stores-tls
rules:
- host: stores.timbx.me
Expand All @@ -79,5 +80,15 @@ spec:
backend:
service:
name: stores
port:
port:
number: 80
- host: stores.endpoints.bobadojo.cloud.goog
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: stores
port:
number: 80

0 comments on commit 948b6d9

Please sign in to comment.