insecure.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. *
  3. * Copyright 2020 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. // Package insecure provides an implementation of the
  19. // credentials.TransportCredentials interface which disables transport security.
  20. package insecure
  21. import (
  22. "context"
  23. "net"
  24. "google.golang.org/grpc/credentials"
  25. )
  26. // NewCredentials returns a credentials which disables transport security.
  27. //
  28. // Note that using this credentials with per-RPC credentials which require
  29. // transport security is incompatible and will cause grpc.Dial() to fail.
  30. func NewCredentials() credentials.TransportCredentials {
  31. return insecureTC{}
  32. }
  33. // insecureTC implements the insecure transport credentials. The handshake
  34. // methods simply return the passed in net.Conn and set the security level to
  35. // NoSecurity.
  36. type insecureTC struct{}
  37. func (insecureTC) ClientHandshake(ctx context.Context, _ string, conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
  38. return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil
  39. }
  40. func (insecureTC) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
  41. return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil
  42. }
  43. func (insecureTC) Info() credentials.ProtocolInfo {
  44. return credentials.ProtocolInfo{SecurityProtocol: "insecure"}
  45. }
  46. func (insecureTC) Clone() credentials.TransportCredentials {
  47. return insecureTC{}
  48. }
  49. func (insecureTC) OverrideServerName(string) error {
  50. return nil
  51. }
  52. // info contains the auth information for an insecure connection.
  53. // It implements the AuthInfo interface.
  54. type info struct {
  55. credentials.CommonAuthInfo
  56. }
  57. // AuthType returns the type of info as a string.
  58. func (info) AuthType() string {
  59. return "insecure"
  60. }
  61. // insecureBundle implements an insecure bundle.
  62. // An insecure bundle provides a thin wrapper around insecureTC to support
  63. // the credentials.Bundle interface.
  64. type insecureBundle struct{}
  65. // NewBundle returns a bundle with disabled transport security and no per rpc credential.
  66. func NewBundle() credentials.Bundle {
  67. return insecureBundle{}
  68. }
  69. // NewWithMode returns a new insecure Bundle. The mode is ignored.
  70. func (insecureBundle) NewWithMode(string) (credentials.Bundle, error) {
  71. return insecureBundle{}, nil
  72. }
  73. // PerRPCCredentials returns an nil implementation as insecure
  74. // bundle does not support a per rpc credential.
  75. func (insecureBundle) PerRPCCredentials() credentials.PerRPCCredentials {
  76. return nil
  77. }
  78. // TransportCredentials returns the underlying insecure transport credential.
  79. func (insecureBundle) TransportCredentials() credentials.TransportCredentials {
  80. return NewCredentials()
  81. }