...

Source file src/github.com/google/s2a-go/example/client/client.go

Documentation: github.com/google/s2a-go/example/client

     1  /*
     2   *
     3   * Copyright 2022 Google LLC
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     https://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package main establishes a connection with an Echo service.
    20  package main
    21  
    22  import (
    23  	"context"
    24  	"flag"
    25  	"log"
    26  	"time"
    27  
    28  	"github.com/google/s2a-go"
    29  	"google.golang.org/grpc"
    30  
    31  	pb "github.com/google/s2a-go/example/proto/echo_go_proto"
    32  )
    33  
    34  const (
    35  	defaultTimeout = 10.0 * time.Second
    36  )
    37  
    38  var (
    39  	serverAddr = flag.String("server_addr", "0.0.0.0:8080", "Echo service address.")
    40  	s2aAddr    = flag.String("s2a_addr", "0.0.0.0:61365", "S2A service address.")
    41  )
    42  
    43  func runClient(serverAddr *string) {
    44  	creds, err := s2a.NewClientCreds(&s2a.ClientOptions{
    45  		S2AAddress:       *s2aAddr,
    46  		VerificationMode: s2a.ConnectToGoogle,
    47  		LocalIdentity:    s2a.NewHostname("test_rsa_client_identity"),
    48  	})
    49  	if err != nil {
    50  		log.Fatalf("NewClientCreds() failed: %v", err)
    51  	}
    52  	opts := []grpc.DialOption{
    53  		grpc.WithTransportCredentials(creds),
    54  		grpc.WithReturnConnectionError(),
    55  		grpc.WithDisableRetry(),
    56  		grpc.WithBlock(),
    57  	}
    58  	conn, err := grpc.Dial(*serverAddr, opts...)
    59  	if err != nil {
    60  		log.Fatalf("Client: failed to connect: %v", err)
    61  	}
    62  	defer conn.Close()
    63  	c := pb.NewEchoClient(conn)
    64  	log.Printf("Client: connected to: %s", *serverAddr)
    65  	ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
    66  	defer cancel()
    67  	msg := "Hello S2Av2 user!"
    68  	r, err := c.Echo(ctx, &pb.EchoRequest{Msg: msg})
    69  	if err != nil {
    70  		log.Fatalf("Client: failed to send echo message: %v", err)
    71  	}
    72  	log.Printf("Client: received message from server: %s", r.GetMsg())
    73  }
    74  
    75  func main() {
    76  	runClient(serverAddr)
    77  }
    78  

View as plain text