forked from hpe-storage/common-host-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nimble_provider.go
220 lines (189 loc) · 6.27 KB
/
nimble_provider.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2019 Hewlett Packard Enterprise Development LP
package provider
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/hpe-storage/common-host-libs/cert"
"github.com/hpe-storage/common-host-libs/connectivity"
"github.com/hpe-storage/common-host-libs/linux"
log "github.com/hpe-storage/common-host-libs/logger"
"github.com/hpe-storage/common-host-libs/util"
)
// struct to map all blocked keys per option
type optionLimit struct {
option string
blockedKeys []string
}
const (
nimbleProviderPort = "8443"
// DefaultContainerProviderVersion indicates default provider api version
DefaultContainerProviderVersion = "0.0"
)
// LoginRequest : container provider login Request
type LoginRequest struct {
Username string `json:"UserName,omitempty"`
Password string `json:"Password,omitempty"`
Cert string `json:"Cert,omitempty"`
}
// LoginResponse : container provider login response
type LoginResponse struct {
Err string `json:"Err,omitempty"`
}
// IsNimblePlugin returns true if plugin type is for nimble platform
func IsNimblePlugin() bool {
if os.Getenv("PLUGIN_TYPE") == "nimble" {
return true
}
return false
}
func getBasicContainerProviderClient(ipAddress string) *connectivity.Client {
containerproviderLoginURI := fmt.Sprintf("https://%s:8443/container-provider", ipAddress)
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
transport := &http.Transport{TLSClientConfig: tlsConfig}
containerProviderLoginClient := connectivity.NewHTTPSClient(containerproviderLoginURI, transport)
return containerProviderLoginClient
}
//AddRemoveCertContainerProvider :
func AddRemoveCertContainerProvider(containerProviderURI string, ipAddress string, hostCert string, username string, password string) error {
log.Tracef(">>>>> AddRemoveCertContainerProvider called with %s %s", containerProviderURI, ipAddress)
defer log.Trace("<<<<< AddRemoveCertContainerProvider")
containerProviderLoginClient := getBasicContainerProviderClient(ipAddress)
request := &LoginRequest{Username: username, Password: password, Cert: hostCert}
response := &LoginResponse{}
_, err := containerProviderLoginClient.DoJSON(&connectivity.Request{Action: "POST", Path: containerProviderURI, Payload: &request, Response: &response, ResponseError: nil})
if err != nil {
log.Error(err.Error())
return err
}
if response.Err != "" {
log.Errorf("Failure while attempting %s to container provider: %s", containerProviderURI, response.Err)
return errors.New("failed to configure certificate on container provider: " + response.Err)
}
return nil
}
// get client for nimble container provider
func getNimbleContainerProviderClient() (*connectivity.Client, error) {
log.Trace(">>>>> getNimbleContainerProviderClient")
defer log.Trace("<<<<< getNimbleContainerProviderClient")
providerURI, err := GetProviderURI("", nimbleProviderPort, "/container-provider")
if err != nil {
return nil, err
}
is, _, err := util.FileExists(HostCertFile)
if !is {
log.Errorf("host cert file %s doesnt exist", HostCertFile)
return nil, err
}
is, _, err = util.FileExists(HostKeyFile)
if !is {
log.Errorf("host key file %s doesnt exist", HostKeyFile)
return nil, err
}
is, _, err = util.FileExists(ServerCertFile)
if !is {
log.Errorf("host key file %s doesnt exist", ServerCertFile)
return nil, err
}
cert, err := tls.LoadX509KeyPair(HostCertFile, HostKeyFile)
if err != nil {
log.Error("unable to load client cert", err.Error())
return nil, err
}
//Load CA cert which is the server cert in our case
caCert, err := ioutil.ReadFile(ServerCertFile)
if err != nil {
log.Trace("unable to load server cert", err.Error())
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate()
// Setup HTTPS client
transport := &http.Transport{TLSClientConfig: tlsConfig}
containerProviderClient := connectivity.NewHTTPSClientWithTimeout(providerURI, transport,
providerClientTimeout)
return containerProviderClient, nil
}
// LoginAndCreateCerts :
func LoginAndCreateCerts(ipAddress string, username string, password string, isv2 bool) error {
log.Tracef(">>>>> LoginAndCreateCerts called with ipAddress(%s) username(%s)", ipAddress, username)
defer log.Trace("<<<<< LoginAndCreateCerts")
// get array certificate
groupCert, err := cert.GetCertFromGroup(ipAddress, nimbleProviderPort)
if err != nil {
log.Error("LoginAndCreateCerts err", err.Error())
return err
}
serverCertPem, err := cert.ConvertCertToPem(groupCert)
if err != nil {
return err
}
//write array cert to dockerplugin.ServerCertFile
err = cert.WriteCertPemToFile(serverCertPem, ServerCertFile)
if err != nil {
return err
}
isHostCertsPresent := checkIfHostCertsExist()
// reuse the host certs if they are present
if !isHostCertsPresent {
err := createAndAddHostCerts(ipAddress, username, password, HostKeyFile, HostCertFile)
if err != nil {
return err
}
}
return nil
}
func checkIfHostCertsExist() bool {
log.Trace(">>>>> checkIfHostCertsExist")
defer log.Trace("<<<<< checkIfHostCertsExist")
is, _, _ := util.FileExists(HostKeyFile)
if !is {
log.Debugf("host key file %s doesnt exist", HostKeyFile)
return is
}
is, _, _ = util.FileExists(HostCertFile)
if !is {
log.Debugf("host key file %s doesnt exist", HostCertFile)
return is
}
log.Debugf("hostCertificate exists at %s. Reusing them to connect to the array", HostCertFile)
return true
}
func createAndAddHostCerts(ipAddress, username, password, hostKeyFile, hostCertFile string) error {
// get common name as hostname
cn, err := linux.GetHostNameAndDomain()
if err != nil {
return err
}
// generate host key and certificate
_, hostKey, hostCert, err := cert.GenerateCert(cn[0])
if err != nil {
return err
}
// write the hostKey and hostCert in PEM format
err = cert.WriteCertPemToFile(hostKey, hostKeyFile)
if err != nil {
return err
}
err = cert.WriteCertPemToFile(hostCert, hostCertFile)
if err != nil {
return err
}
// invoke dockerplugin.NimbleLoginURI end point of container provider to add certificate
err = AddRemoveCertContainerProvider(NimbleLoginURI, ipAddress, hostCert, username, password)
if err != nil {
return err
}
return nil
}