This repository has been archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
http_client.go
192 lines (136 loc) · 4.1 KB
/
http_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package avatica
import (
"bytes"
"database/sql/driver"
"fmt"
"io/ioutil"
"net/http"
"regexp"
avaticaMessage "github.com/Boostport/avatica/message"
"github.com/golang/protobuf/proto"
"github.com/hashicorp/go-cleanhttp"
"github.com/xinsnake/go-http-digest-auth-client"
"golang.org/x/net/context"
"golang.org/x/net/context/ctxhttp"
"gopkg.in/jcmturner/gokrb5.v4/client"
"gopkg.in/jcmturner/gokrb5.v4/config"
"gopkg.in/jcmturner/gokrb5.v4/credentials"
"gopkg.in/jcmturner/gokrb5.v4/keytab"
)
var (
badConnRe = regexp.MustCompile(`org\.apache\.calcite\.avatica\.NoSuchConnectionException`)
)
type httpClientAuthConfig struct {
authenticationType authentication
username string
password string
principal krb5Principal
keytab string
krb5Conf string
krb5CredentialCache string
}
// httpClient wraps the default http.Client to communicate with the Avatica server.
type httpClient struct {
host string
authConfig httpClientAuthConfig
httpClient *http.Client
kerberosClient client.Client
}
// NewHTTPClient creates a new httpClient from a host.
func NewHTTPClient(host string, authenticationConf httpClientAuthConfig) (*httpClient, error) {
hc := cleanhttp.DefaultPooledClient()
c := &httpClient{
host: host,
authConfig: authenticationConf,
httpClient: hc,
}
if authenticationConf.authenticationType == digest {
rt := digest_auth_client.NewTransport(authenticationConf.username, authenticationConf.password)
c.httpClient.Transport = &rt
} else if authenticationConf.authenticationType == spnego {
if authenticationConf.krb5CredentialCache != "" {
tc, err := credentials.LoadCCache(authenticationConf.krb5CredentialCache)
if err != nil {
return nil, fmt.Errorf("error reading kerberos ticket cache: %s", err)
}
kc, err := client.NewClientFromCCache(tc)
if err != nil {
return nil, fmt.Errorf("error creating kerberos client: %s", err)
}
c.kerberosClient = kc
} else {
cfg, err := config.Load(authenticationConf.krb5Conf)
if err != nil {
return nil, fmt.Errorf("error reading kerberos config: %s", err)
}
kt, err := keytab.Load(authenticationConf.keytab)
if err != nil {
return nil, fmt.Errorf("error reading kerberos keytab: %s", err)
}
kc := client.NewClientWithKeytab(authenticationConf.principal.username, authenticationConf.principal.realm, kt)
kc.WithConfig(cfg)
err = kc.Login()
if err != nil {
return nil, fmt.Errorf("error performing kerberos login with keytab: %s", err)
}
c.kerberosClient = kc
}
}
return c, nil
}
// post posts a protocol buffer message to the Avatica server.
func (c *httpClient) post(ctx context.Context, message proto.Message) (proto.Message, error) {
wrapped, err := proto.Marshal(message)
if err != nil {
return nil, err
}
wire := &avaticaMessage.WireMessage{
Name: classNameFromRequest(message),
WrappedMessage: wrapped,
}
body, err := proto.Marshal(wire)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", c.host, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-google-protobuf")
if c.authConfig.authenticationType == basic {
req.SetBasicAuth(c.authConfig.username, c.authConfig.password)
} else if c.authConfig.authenticationType == spnego {
c.kerberosClient.SetSPNEGOHeader(req, "")
}
res, err := ctxhttp.Do(ctx, c.httpClient, req)
if err != nil {
return nil, err
}
defer res.Body.Close()
response, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
result := &avaticaMessage.WireMessage{}
err = proto.Unmarshal(response, result)
if err != nil {
return nil, err
}
inner, err := responseFromClassName(result.Name)
if err != nil {
return nil, err
}
err = proto.Unmarshal(result.WrappedMessage, inner)
if err != nil {
return nil, err
}
if v, ok := inner.(*avaticaMessage.ErrorResponse); ok {
for _, exception := range v.Exceptions {
if badConnRe.MatchString(exception) {
return nil, driver.ErrBadConn
}
}
return nil, errorResponseToResponseError(v)
}
return inner, nil
}