server.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  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. * http://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. // This binary can only run on Google Cloud Platform (GCP).
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "net"
  24. "strings"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/credentials/alts"
  27. "google.golang.org/grpc/grpclog"
  28. "google.golang.org/grpc/interop"
  29. "google.golang.org/grpc/tap"
  30. testgrpc "google.golang.org/grpc/interop/grpc_testing"
  31. )
  32. const (
  33. udsAddrPrefix = "unix:"
  34. )
  35. var (
  36. hsAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address")
  37. serverAddr = flag.String("server_address", ":8080", "The address on which the server is listening. Only two types of addresses are supported, 'host:port' and 'unix:/path'.")
  38. logger = grpclog.Component("interop")
  39. )
  40. func main() {
  41. flag.Parse()
  42. // If the server address starts with `unix:`, then we have a UDS address.
  43. network := "tcp"
  44. address := *serverAddr
  45. if strings.HasPrefix(address, udsAddrPrefix) {
  46. network = "unix"
  47. address = strings.TrimPrefix(address, udsAddrPrefix)
  48. }
  49. lis, err := net.Listen(network, address)
  50. if err != nil {
  51. logger.Fatalf("gRPC Server: failed to start the server at %v: %v", address, err)
  52. }
  53. opts := alts.DefaultServerOptions()
  54. if *hsAddr != "" {
  55. opts.HandshakerServiceAddress = *hsAddr
  56. }
  57. altsTC := alts.NewServerCreds(opts)
  58. grpcServer := grpc.NewServer(grpc.Creds(altsTC), grpc.InTapHandle(authz))
  59. testgrpc.RegisterTestServiceServer(grpcServer, interop.NewTestServer())
  60. grpcServer.Serve(lis)
  61. }
  62. // authz shows how to access client information at the server side to perform
  63. // application-layer authorization checks.
  64. func authz(ctx context.Context, info *tap.Info) (context.Context, error) {
  65. authInfo, err := alts.AuthInfoFromContext(ctx)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // Access all alts.AuthInfo data:
  70. logger.Infof("authInfo.ApplicationProtocol() = %v", authInfo.ApplicationProtocol())
  71. logger.Infof("authInfo.RecordProtocol() = %v", authInfo.RecordProtocol())
  72. logger.Infof("authInfo.SecurityLevel() = %v", authInfo.SecurityLevel())
  73. logger.Infof("authInfo.PeerServiceAccount() = %v", authInfo.PeerServiceAccount())
  74. logger.Infof("authInfo.LocalServiceAccount() = %v", authInfo.LocalServiceAccount())
  75. logger.Infof("authInfo.PeerRPCVersions() = %v", authInfo.PeerRPCVersions())
  76. logger.Infof("info.FullMethodName = %v", info.FullMethodName)
  77. return ctx, nil
  78. }