forked from smira/go-kmip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
167 lines (133 loc) · 3.66 KB
/
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
package kmip
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import (
"crypto/tls"
"time"
"github.com/pkg/errors"
)
// Client implements basic KMIP client
//
// Client is not safe for concurrent use
type Client struct {
// Server endpoint as "host:port"
Endpoint string
// TLS client config
TLSConfig *tls.Config
// KMIP version to use
//
// Defaults to DefaultSupportedVersions[0] if not set
Version ProtocolVersion
// Network timeouts
ReadTimeout, WriteTimeout time.Duration
conn *tls.Conn
e *Encoder
d *Decoder
}
// Connect establishes connection with the server
func (c *Client) Connect() error {
var err error
if c.conn, err = tls.Dial("tcp", c.Endpoint, c.TLSConfig); err != nil {
return errors.Wrap(err, "error dialing connection")
}
if c.ReadTimeout != 0 {
_ = c.conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
}
if c.WriteTimeout != 0 {
_ = c.conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
}
if err = c.conn.Handshake(); err != nil {
return errors.Wrap(err, "error running tls handshake")
}
var zeroVersion ProtocolVersion
if c.Version == zeroVersion {
c.Version = DefaultSupportedVersions[0]
}
c.e = NewEncoder(c.conn)
c.d = NewDecoder(c.conn)
return nil
}
// Close connection to the server
func (c *Client) Close() error {
if c.conn == nil {
return nil
}
err := c.conn.Close()
c.conn = nil
return err
}
// DiscoverVersions with the server
func (c *Client) DiscoverVersions(versions []ProtocolVersion) (serverVersions []ProtocolVersion, err error) {
var resp interface{}
resp, err = c.Send(OPERATION_DISCOVER_VERSIONS,
DiscoverVersionsRequest{
ProtocolVersions: versions,
})
if err != nil {
return
}
serverVersions = resp.(DiscoverVersionsResponse).ProtocolVersions
return
}
// Send request to server and deliver response/error back
//
// Request payload should be passed as req, and response payload will be
// returned back as resp. Operation will be sent as a batch with single
// item.
//
// Send is a generic method, it's better to implement specific methods for
// each operation (use DiscoverVersions as example).
func (c *Client) Send(operation Enum, req interface{}) (resp interface{}, err error) {
if c.conn == nil {
err = errors.New("not connected")
return
}
request := &Request{
Header: RequestHeader{
Version: c.Version,
BatchCount: 1,
},
BatchItems: []RequestBatchItem{
{
Operation: operation,
RequestPayload: req,
},
},
}
if c.WriteTimeout != 0 {
_ = c.conn.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
}
err = c.e.Encode(request)
if err != nil {
err = errors.Wrap(err, "error writing request")
return
}
if c.ReadTimeout != 0 {
_ = c.conn.SetReadDeadline(time.Now().Add(c.ReadTimeout))
}
var response Response
err = c.d.Decode(&response)
if err != nil {
err = errors.Wrap(err, "error reading response")
return
}
if response.Header.BatchCount != 1 {
err = errors.Errorf("unexepcted response batch count: %d", response.Header.BatchCount)
return
}
if len(response.BatchItems) != 1 {
err = errors.Errorf("unexpected response batch items: %d", len(response.BatchItems))
return
}
if response.BatchItems[0].Operation != operation {
err = errors.Errorf("unexpected response operation: %d", response.BatchItems[0].Operation)
return
}
if response.BatchItems[0].ResultStatus == RESULT_STATUS_SUCCESS {
resp = response.BatchItems[0].ResponsePayload
return
}
err = wrapError(errors.New(response.BatchItems[0].ResultMessage), response.BatchItems[0].ResultReason)
return
}