-
Notifications
You must be signed in to change notification settings - Fork 4
/
serverapi.go
137 lines (109 loc) · 4.57 KB
/
serverapi.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
// Copyright 2019 Thales eSecurity
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package estclient
import (
"crypto"
"crypto/x509"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/thales-e-security/estclient/apiclient"
"github.com/thales-e-security/estclient/apiclient/operation"
)
// A serverAPI instance represents and EST server.
type serverAPI interface {
// CACerts retrieves the CA certificates from the EST server. The result is the raw response from the server.
CACerts() (string, error)
// SimpleEnroll triggers the simple enroll endpoint of the EST server. certRequest should be a base64-encoded
// DER-format PKCS#10 certificate request. The result is the raw response from the server.
SimpleEnroll(authData AuthData, certRequest string) (string, error)
// SimpleReEnroll triggers the simple re-enroll endpoint of the EST server. certRequest should be a base64-encoded
// DER-format PKCS#10 certificate request. The result is the raw response from the server.
SimpleReEnroll(authData AuthData, certRequest string) (string, error)
}
// An apiBuilder creates serverAPI instances.
type apiBuilder interface {
// Build creates a serverAPI instance, optionally using the supplied private key and certificate
// for client authentication.
Build(currentKey crypto.PrivateKey, currentCert *x509.Certificate) (serverAPI, error)
}
type swaggerAPIBuilder struct {
options ClientOptions
host string
}
func (s swaggerAPIBuilder) Build(currentKey crypto.PrivateKey, currentCert *x509.Certificate) (serverAPI, error) {
o := httptransport.TLSClientOptions{
InsecureSkipVerify: s.options.InsecureSkipVerify,
LoadedCA: s.options.TLSTrustAnchor,
}
if currentKey != nil && currentCert != nil {
o.LoadedKey = currentKey
o.LoadedCertificate = currentCert
}
tlsClient, err := httptransport.TLSClient(o)
if err != nil {
return nil, errors.Wrap(err, "could not create TLS apiclient")
}
cfg := apiclient.DefaultTransportConfig()
rt := httptransport.NewWithClient(s.host, cfg.BasePath, cfg.Schemes, tlsClient)
// For now, we just process PKCS7 as text and base64-decode later
rt.Consumers["application/pkcs7-mime"] = runtime.TextConsumer()
// Ditto for PKCS10
rt.Producers["application/pkcs10"] = runtime.TextProducer()
return swaggerServerAPI{client: apiclient.New(rt, strfmt.Default).Operation}, nil
}
type swaggerServerAPI struct {
client *operation.Client
}
func (s swaggerServerAPI) CACerts() (string, error) {
params := operation.NewCacertsParams()
res, err := s.client.Cacerts(params)
if err != nil {
return "", err
}
return res.Payload, nil
}
func (s swaggerServerAPI) SimpleEnroll(authData AuthData, certRequest string) (string, error) {
var httpAuth runtime.ClientAuthInfoWriter
if authData.ID != nil && authData.Secret != nil {
httpAuth = httptransport.BasicAuth(*authData.ID, *authData.Secret)
}
params := operation.NewSimpleenrollParams()
params.Certrequest = certRequest
res, err := s.client.Simpleenroll(params, httpAuth)
if err != nil {
return "", err
}
return res.Payload, nil
}
func (s swaggerServerAPI) SimpleReEnroll(authData AuthData, certRequest string) (string, error) {
var httpAuth runtime.ClientAuthInfoWriter
if authData.ID != nil && authData.Secret != nil {
httpAuth = httptransport.BasicAuth(*authData.ID, *authData.Secret)
}
params := operation.NewSimplereenrollParams()
params.Certrequest = certRequest
res, err := s.client.Simplereenroll(params, httpAuth)
if err != nil {
return "", err
}
return res.Payload, nil
}