Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

grpc-auth-support.md 3.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Authentication
  2. As outlined in the [gRPC authentication guide](https://grpc.io/docs/guides/auth.html) there are a number of different mechanisms for asserting identity between an client and server. We'll present some code-samples here demonstrating how to provide TLS support encryption and identity assertions as well as passing OAuth2 tokens to services that support it.
  3. # Enabling TLS on a gRPC client
  4. ```Go
  5. conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
  6. ```
  7. # Enabling TLS on a gRPC server
  8. ```Go
  9. creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
  10. if err != nil {
  11. log.Fatalf("Failed to generate credentials %v", err)
  12. }
  13. lis, err := net.Listen("tcp", ":0")
  14. server := grpc.NewServer(grpc.Creds(creds))
  15. ...
  16. server.Serve(lis)
  17. ```
  18. # OAuth2
  19. For an example of how to configure client and server to use OAuth2 tokens, see
  20. [here](https://github.com/grpc/grpc-go/tree/master/examples/features/authentication).
  21. ## Validating a token on the server
  22. Clients may use
  23. [metadata.MD](https://godoc.org/google.golang.org/grpc/metadata#MD)
  24. to store tokens and other authentication-related data. To gain access to the
  25. `metadata.MD` object, a server may use
  26. [metadata.FromIncomingContext](https://godoc.org/google.golang.org/grpc/metadata#FromIncomingContext).
  27. With a reference to `metadata.MD` on the server, one needs to simply lookup the
  28. `authorization` key. Note, all keys stored within `metadata.MD` are normalized
  29. to lowercase. See [here](https://godoc.org/google.golang.org/grpc/metadata#New).
  30. It is possible to configure token validation for all RPCs using an interceptor.
  31. A server may configure either a
  32. [grpc.UnaryInterceptor](https://godoc.org/google.golang.org/grpc#UnaryInterceptor)
  33. or a
  34. [grpc.StreamInterceptor](https://godoc.org/google.golang.org/grpc#StreamInterceptor).
  35. ## Adding a token to all outgoing client RPCs
  36. To send an OAuth2 token with each RPC, a client may configure the
  37. `grpc.DialOption`
  38. [grpc.WithPerRPCCredentials](https://godoc.org/google.golang.org/grpc#WithPerRPCCredentials).
  39. Alternatively, a client may also use the `grpc.CallOption`
  40. [grpc.PerRPCCredentials](https://godoc.org/google.golang.org/grpc#PerRPCCredentials)
  41. on each invocation of an RPC.
  42. To create a `credentials.PerRPCCredentials`, use
  43. [oauth.NewOauthAccess](https://godoc.org/google.golang.org/grpc/credentials/oauth#NewOauthAccess).
  44. Note, the OAuth2 implementation of `grpc.PerRPCCredentials` requires a client to use
  45. [grpc.WithTransportCredentials](https://godoc.org/google.golang.org/grpc#WithTransportCredentials)
  46. to prevent any insecure transmission of tokens.
  47. # Authenticating with Google
  48. ## Google Compute Engine (GCE)
  49. ```Go
  50. conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
  51. ```
  52. ## JWT
  53. ```Go
  54. jwtCreds, err := oauth.NewServiceAccountFromFile(*serviceAccountKeyFile, *oauthScope)
  55. if err != nil {
  56. log.Fatalf("Failed to create JWT credentials: %v", err)
  57. }
  58. conn, err := grpc.Dial(serverAddr, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")), grpc.WithPerRPCCredentials(jwtCreds))
  59. ```