...
1
18
19
20
21
22
23
24
25
26
27
28
29
30 package local
31
32 import (
33 "context"
34 "fmt"
35 "net"
36 "strings"
37
38 "google.golang.org/grpc/credentials"
39 )
40
41
42
43 type info struct {
44 credentials.CommonAuthInfo
45 }
46
47
48 func (info) AuthType() string {
49 return "local"
50 }
51
52
53 type localTC struct {
54 info credentials.ProtocolInfo
55 }
56
57 func (c *localTC) Info() credentials.ProtocolInfo {
58 return c.info
59 }
60
61
62
63 func getSecurityLevel(network, addr string) (credentials.SecurityLevel, error) {
64 switch {
65
66 case strings.HasPrefix(addr, "127."), strings.HasPrefix(addr, "[::1]:"):
67 return credentials.NoSecurity, nil
68
69 case network == "pipe" && strings.HasPrefix(addr, `\\.\pipe\`):
70 return credentials.NoSecurity, nil
71
72 case network == "unix":
73 return credentials.PrivacyAndIntegrity, nil
74
75 default:
76 return credentials.InvalidSecurityLevel, fmt.Errorf("local credentials rejected connection to non-local address %q", addr)
77 }
78 }
79
80 func (*localTC) ClientHandshake(ctx context.Context, authority string, conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
81 secLevel, err := getSecurityLevel(conn.RemoteAddr().Network(), conn.RemoteAddr().String())
82 if err != nil {
83 return nil, nil, err
84 }
85 return conn, info{credentials.CommonAuthInfo{SecurityLevel: secLevel}}, nil
86 }
87
88 func (*localTC) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
89 secLevel, err := getSecurityLevel(conn.RemoteAddr().Network(), conn.RemoteAddr().String())
90 if err != nil {
91 return nil, nil, err
92 }
93 return conn, info{credentials.CommonAuthInfo{SecurityLevel: secLevel}}, nil
94 }
95
96
97 func NewCredentials() credentials.TransportCredentials {
98 return &localTC{
99 info: credentials.ProtocolInfo{
100 SecurityProtocol: "local",
101 },
102 }
103 }
104
105
106 func (c *localTC) Clone() credentials.TransportCredentials {
107 return &localTC{info: c.info}
108 }
109
110
111
112 func (c *localTC) OverrideServerName(serverNameOverride string) error {
113 c.info.ServerName = serverNameOverride
114 return nil
115 }
116
View as plain text