/
mtls_certificates.go
215 lines (186 loc) · 7.01 KB
/
mtls_certificates.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
package khulnasoft
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
// MTLSAssociation represents the metadata for an existing association
// between a user-uploaded mTLS certificate and a Khulnasoft service.
type MTLSAssociation struct {
Service string `json:"service"`
Status string `json:"status"`
}
// MTLSAssociationResponse represents the response from the retrieval endpoint
// for mTLS certificate associations.
type MTLSAssociationResponse struct {
Response
Result []MTLSAssociation `json:"result"`
}
// MTLSCertificate represents the metadata for a user-uploaded mTLS
// certificate.
type MTLSCertificate struct {
ID string `json:"id"`
Name string `json:"name"`
Issuer string `json:"issuer"`
Signature string `json:"signature"`
SerialNumber string `json:"serial_number"`
Certificates string `json:"certificates"`
CA bool `json:"ca"`
UploadedOn time.Time `json:"uploaded_on"`
UpdatedAt time.Time `json:"updated_at"`
ExpiresOn time.Time `json:"expires_on"`
}
// MTLSCertificateResponse represents the response from endpoints relating to
// retrieving, creating, and deleting an mTLS certificate.
type MTLSCertificateResponse struct {
Response
Result MTLSCertificate `json:"result"`
}
// MTLSCertificatesResponse represents the response from the mTLS certificate
// list endpoint.
type MTLSCertificatesResponse struct {
Response
Result []MTLSCertificate `json:"result"`
ResultInfo `json:"result_info"`
}
// MTLSCertificateParams represents the data related to the mTLS certificate
// being uploaded. Name is an optional field.
type CreateMTLSCertificateParams struct {
Name string `json:"name"`
Certificates string `json:"certificates"`
PrivateKey string `json:"private_key"`
CA bool `json:"ca"`
}
type ListMTLSCertificatesParams struct {
PaginationOptions
Limit int `url:"limit,omitempty"`
Offset int `url:"offset,omitempty"`
Name string `url:"name,omitempty"`
CA bool `url:"ca,omitempty"`
}
type ListMTLSCertificateAssociationsParams struct {
CertificateID string
}
var (
ErrMissingCertificateID = errors.New("missing required certificate ID")
)
// ListMTLSCertificates returns a list of all user-uploaded mTLS certificates.
//
// API reference: https://api.khulnasoft.com/#mtls-certificate-management-list-mtls-certificates
func (api *API) ListMTLSCertificates(ctx context.Context, rc *ResourceContainer, params ListMTLSCertificatesParams) ([]MTLSCertificate, ResultInfo, error) {
if rc.Level != AccountRouteLevel {
return []MTLSCertificate{}, ResultInfo{}, ErrRequiredAccountLevelResourceContainer
}
if rc.Identifier == "" {
return []MTLSCertificate{}, ResultInfo{}, ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s/mtls_certificates", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, params)
if err != nil {
return []MTLSCertificate{}, ResultInfo{}, err
}
var r MTLSCertificatesResponse
if err := json.Unmarshal(res, &r); err != nil {
return []MTLSCertificate{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, r.ResultInfo, err
}
// GetMTLSCertificate returns the metadata associated with a user-uploaded mTLS
// certificate.
//
// API reference: https://api.khulnasoft.com/#mtls-certificate-management-get-mtls-certificate
func (api *API) GetMTLSCertificate(ctx context.Context, rc *ResourceContainer, certificateID string) (MTLSCertificate, error) {
if rc.Level != AccountRouteLevel {
return MTLSCertificate{}, ErrRequiredAccountLevelResourceContainer
}
if rc.Identifier == "" {
return MTLSCertificate{}, ErrMissingAccountID
}
if certificateID == "" {
return MTLSCertificate{}, ErrMissingCertificateID
}
uri := fmt.Sprintf("/accounts/%s/mtls_certificates/%s", rc.Identifier, certificateID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return MTLSCertificate{}, err
}
var r MTLSCertificateResponse
if err := json.Unmarshal(res, &r); err != nil {
return MTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// ListMTLSCertificateAssociations returns a list of all existing associations
// between the mTLS certificate and Khulnasoft services.
//
// API reference: https://api.khulnasoft.com/#mtls-certificate-management-list-mtls-certificate-associations
func (api *API) ListMTLSCertificateAssociations(ctx context.Context, rc *ResourceContainer, params ListMTLSCertificateAssociationsParams) ([]MTLSAssociation, error) {
if rc.Level != AccountRouteLevel {
return []MTLSAssociation{}, ErrRequiredAccountLevelResourceContainer
}
if rc.Identifier == "" {
return []MTLSAssociation{}, ErrMissingAccountID
}
if params.CertificateID == "" {
return []MTLSAssociation{}, ErrMissingCertificateID
}
uri := fmt.Sprintf("/accounts/%s/mtls_certificates/%s/associations", rc.Identifier, params.CertificateID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []MTLSAssociation{}, err
}
var r MTLSAssociationResponse
if err := json.Unmarshal(res, &r); err != nil {
return []MTLSAssociation{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// CreateMTLSCertificate will create the provided certificate for use with mTLS
// enabled Khulnasoft services.
//
// API reference: https://api.khulnasoft.com/#mtls-certificate-management-upload-mtls-certificate
func (api *API) CreateMTLSCertificate(ctx context.Context, rc *ResourceContainer, params CreateMTLSCertificateParams) (MTLSCertificate, error) {
if rc.Level != AccountRouteLevel {
return MTLSCertificate{}, ErrRequiredAccountLevelResourceContainer
}
if rc.Identifier == "" {
return MTLSCertificate{}, ErrMissingAccountID
}
uri := fmt.Sprintf("/accounts/%s/mtls_certificates", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return MTLSCertificate{}, err
}
var r MTLSCertificateResponse
if err := json.Unmarshal(res, &r); err != nil {
return MTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// DeleteMTLSCertificate will delete the specified mTLS certificate.
//
// API reference: https://api.khulnasoft.com/#mtls-certificate-management-delete-mtls-certificate
func (api *API) DeleteMTLSCertificate(ctx context.Context, rc *ResourceContainer, certificateID string) (MTLSCertificate, error) {
if rc.Level != AccountRouteLevel {
return MTLSCertificate{}, ErrRequiredAccountLevelResourceContainer
}
if rc.Identifier == "" {
return MTLSCertificate{}, ErrMissingAccountID
}
if certificateID == "" {
return MTLSCertificate{}, ErrMissingCertificateID
}
uri := fmt.Sprintf("/accounts/%s/mtls_certificates/%s", rc.Identifier, certificateID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return MTLSCertificate{}, err
}
var r MTLSCertificateResponse
if err := json.Unmarshal(res, &r); err != nil {
return MTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}