Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.

Commit 7c34802

Browse files
committed
Add sign method
1 parent 7dd120c commit 7c34802

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

pki/services_service.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ type IssueResponse struct {
5050
Auth *string `json:"auth"`
5151
}
5252

53+
// SignRequest
54+
type SignRequest struct {
55+
CSR string `json:"csr" validation:"required"`
56+
CommonName string `json:"common_name" validation:"required"`
57+
AltNames string `json:"alt_names"`
58+
OtherSans string `json:"other_sans"`
59+
IPSans string `json:"ip_sans"`
60+
URISans string `json:"uri_sans"`
61+
TTL string `json:"ttl,omitempty"`
62+
Format string `json:"format" validation:"required" enum:"pem|der|pem_bundle"`
63+
ExcludeCNFromSans bool `json:"exclude_cn_from_sans"`
64+
}
65+
5366
// ServiceOptions
5467
type ServiceOptions struct {
5568
}
@@ -160,6 +173,29 @@ func (d *IssueData) GetPrivateKey() (interface{}, error) {
160173
return nil, ErrInvalidPrivateKey
161174
}
162175

176+
// Sign
177+
func (c *ServicesService) Sign(logicalPath, roleName string, signRequest SignRequest, options ...OptionFunc) (*IssueResponse, *Response, error) {
178+
if err := c.validate.Struct(signRequest); err != nil {
179+
return nil, nil, err
180+
}
181+
req, err := c.client.NewServiceRequest(http.MethodPost, "core/pki/api/"+logicalPath+"/sign/"+roleName, &signRequest, options)
182+
if err != nil {
183+
return nil, nil, err
184+
}
185+
var responseStruct struct {
186+
IssueResponse
187+
ErrorResponse
188+
}
189+
resp, err := c.client.Do(req, &responseStruct)
190+
if err != nil {
191+
return nil, nil, err
192+
}
193+
if resp == nil {
194+
return nil, nil, ErrEmptyResult
195+
}
196+
return &responseStruct.IssueResponse, resp, nil
197+
}
198+
163199
// IssueCertificate
164200
func (c *ServicesService) IssueCertificate(logicalPath, roleName string, request CertificateRequest, options ...OptionFunc) (*IssueResponse, *Response, error) {
165201
req, err := c.client.NewServiceRequest(http.MethodPost, "core/pki/api/"+logicalPath+"/issue/"+roleName, &request, options)

pki/services_service_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ kljJ1cnVriYSyGoStCTCep8b4zDjl3KTdu2cGU4tUZIif6E2DruBZJ8=
152152
}
153153
}
154154

155-
func TestIssueCertificate(t *testing.T) {
155+
func TestIssueAndSignCertificates(t *testing.T) {
156156
teardown := setup(t)
157157
defer teardown()
158158

@@ -196,6 +196,7 @@ func TestIssueCertificate(t *testing.T) {
196196
role := "ec384"
197197
muxPKI.HandleFunc("/core/pki/api/"+logicalPath+"/cert/"+serial, returnCert(logicalPath, role))
198198
muxPKI.HandleFunc("/core/pki/api/"+logicalPath+"/issue/"+role, returnCert(logicalPath, role))
199+
muxPKI.HandleFunc("/core/pki/api/"+logicalPath+"/sign/"+role, returnCert(logicalPath, role))
199200
cert, resp, err := pkiClient.Services.IssueCertificate(logicalPath, role, pki.CertificateRequest{
200201
CommonName: "test",
201202
TTL: "4355h",
@@ -237,6 +238,21 @@ func TestIssueCertificate(t *testing.T) {
237238
if !assert.NotNil(t, cert) {
238239
return
239240
}
241+
242+
cert, resp, err = pkiClient.Services.Sign(logicalPath, role, pki.SignRequest{
243+
CSR: "",
244+
CommonName: "1e100.io",
245+
Format: "pem",
246+
})
247+
if !assert.Nil(t, err) {
248+
return
249+
}
250+
if !assert.NotNil(t, resp) {
251+
return
252+
}
253+
if !assert.NotNil(t, cert) {
254+
return
255+
}
240256
}
241257

242258
func TestServicesErrors(t *testing.T) {

0 commit comments

Comments
 (0)