@@ -3,14 +3,10 @@ package service
3
3
import (
4
4
"context"
5
5
"crypto/tls"
6
- "crypto/x509"
7
- "fmt"
8
- "io/ioutil"
9
6
"math"
10
7
"os"
11
8
"os/user"
12
9
"path"
13
- "path/filepath"
14
10
"strings"
15
11
16
12
"github.com/codelingo/lingo/app/util"
@@ -39,13 +35,10 @@ const (
39
35
// passed as arguments.
40
36
func GrpcConnection (client , server string , insecureAllowed bool ) (* grpc.ClientConn , error ) {
41
37
var grpcAddr string
42
- var err error
43
- var isTLS bool
44
- var cert * x509.Certificate
38
+ isTLS := ! insecureAllowed
45
39
46
40
switch client {
47
41
case LocalClient :
48
- isTLS = true
49
42
pCfg , err := config .Platform ()
50
43
if err != nil {
51
44
return nil , errors .Trace (err )
@@ -71,61 +64,24 @@ func GrpcConnection(client, server string, insecureAllowed bool) (*grpc.ClientCo
71
64
grpcAddr = "localhost:8002"
72
65
}
73
66
74
- if isTLS {
75
- // TODO: host may be insecure and will fail here; prompt for insecure or require flag
76
-
77
- util .Logger .Debug ("getting tls cert from cache..." )
78
- cert , err = getCertFromCache (grpcAddr )
79
- if err != nil {
80
- // TODO(waigani) check error
81
- // return nil, errors.Trace(err)
82
-
83
- // if cert hasn't been cached, get a new one which caches it under the hood
84
- util .Logger .Debug ("no cert found, creating new one..." )
85
- if cert , err = newCert (grpcAddr ); err != nil && ! insecureAllowed {
86
- return nil , errors .Trace (err )
87
- }
88
- }
89
- }
90
-
91
- conn , err := dial (grpcAddr , cert , insecureAllowed )
67
+ conn , err := dial (grpcAddr , isTLS )
92
68
if err != nil {
93
- if cert == nil {
94
- return nil , errors .Trace (err )
95
- }
96
-
97
- // TODO(waigani) check error
98
-
99
- // if cert is stale, get a new one
100
- util .Logger .Debug ("dial up failed with given cert, creating new cert..." )
101
- if cert , err = newCert (grpcAddr ); err != nil {
102
- return nil , errors .Trace (err )
103
- }
104
-
105
- if conn , err = dial (grpcAddr , cert , insecureAllowed ); err != nil {
106
- return nil , errors .Trace (err )
107
- }
108
-
69
+ return nil , errors .Trace (err )
109
70
}
71
+
110
72
util .Logger .Debug ("...got answer from grpc server." )
111
73
112
74
return conn , nil
113
75
}
114
76
115
- func dial (target string , cert * x509.Certificate , insecureAllowed bool ) (* grpc.ClientConn , error ) {
116
- tlsOpt := grpc .WithInsecure ()
117
- if cert != nil {
118
- creds , err := credsFromCert (cert )
119
- if err != nil {
120
- if insecureAllowed {
121
- util .Logger .Warn ("failed secure, trying insecure" )
122
- tlsOpt = grpc .WithInsecure ()
123
- } else {
124
- return nil , errors .Trace (err )
125
- }
126
- } else {
127
- tlsOpt = grpc .WithTransportCredentials (creds )
128
- }
77
+ func dial (target string , isTLS bool ) (* grpc.ClientConn , error ) {
78
+
79
+ var tlsOpt grpc.DialOption
80
+ if ! isTLS {
81
+ tlsOpt = grpc .WithInsecure ()
82
+ } else {
83
+ creds := credentials .NewTLS (& tls.Config {})
84
+ tlsOpt = grpc .WithTransportCredentials (creds )
129
85
}
130
86
131
87
util .Logger .Debug ("dialing grpc server..." )
@@ -135,84 +91,6 @@ func dial(target string, cert *x509.Certificate, insecureAllowed bool) (*grpc.Cl
135
91
))
136
92
}
137
93
138
- func newCert (host string ) (* x509.Certificate , error ) {
139
- cert , err := certFromHost (host )
140
- if err != nil {
141
- return nil , errors .Trace (err )
142
- }
143
-
144
- if err := cacheRawCert (host , cert .Raw ); err != nil {
145
- return nil , errors .Trace (err )
146
- }
147
-
148
- return cert , nil
149
- }
150
-
151
- func credsFromCert (cert * x509.Certificate ) (credentials.TransportCredentials , error ) {
152
- cp := x509 .NewCertPool ()
153
- cp .AddCert (cert )
154
- return credentials .NewTLS (& tls.Config {ServerName : "" , RootCAs : cp }), nil
155
- }
156
-
157
- func getCertFromCache (host string ) (* x509.Certificate , error ) {
158
-
159
- certP , err := certPath (host )
160
- if err != nil {
161
- return nil , errors .Trace (err )
162
- }
163
-
164
- rawCert , err := ioutil .ReadFile (certP )
165
- if err != nil {
166
- return nil , errors .Trace (err )
167
- }
168
-
169
- return x509 .ParseCertificate (rawCert )
170
-
171
- }
172
-
173
- func certPath (host string ) (string , error ) {
174
- homePath , err := util .LingoHome ()
175
- if err != nil {
176
- return "" , errors .Trace (err )
177
- }
178
-
179
- env , err := util .GetEnv ()
180
- if err != nil {
181
- return "" , errors .Trace (err )
182
- }
183
-
184
- return path .Join (homePath , fmt .Sprintf ("certs/%s/%s.cert" , env , host )), nil
185
-
186
- }
187
-
188
- func cacheRawCert (host string , rawCert []byte ) error {
189
- certP , err := certPath (host )
190
- if err != nil {
191
- return errors .Trace (err )
192
- }
193
-
194
- if err := os .MkdirAll (filepath .Dir (certP ), 0755 ); err != nil {
195
- return errors .Trace (err )
196
- }
197
-
198
- return errors .Trace (ioutil .WriteFile (certP , rawCert , 0755 ))
199
- }
200
-
201
- // credsFromHost retrieves the public certificate from the given host and returns the transport credentials.
202
- func certFromHost (host string ) (* x509.Certificate , error ) {
203
- conn , err := tls .Dial ("tcp" , host , nil )
204
- if err != nil {
205
- return nil , errors .Trace (err )
206
- }
207
- defer conn .Close ()
208
- err = conn .Handshake ()
209
- if err != nil {
210
- return nil , errors .Trace (err )
211
- }
212
-
213
- return conn .ConnectionState ().PeerCertificates [0 ], nil
214
- }
215
-
216
94
func ListLexicons (ctx context.Context ) ([]string , error ) {
217
95
conn , err := GrpcConnection (LocalClient , PlatformServer , false )
218
96
if err != nil {
0 commit comments