Skip to content

Commit 02ac064

Browse files
committed
fix(certs): Add ChangeCertificateOwnerRole
1 parent bb177bc commit 02ac064

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

v3/api/certificate.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,68 @@ func (c *Client) RecoverCertificate(
768768
return priv, leaf, chain, nil
769769
}
770770

771+
// ChangeCertificateOwnerRole changes the certificate's owner. Users must be in the current owner's role and the new owner's role.
772+
// If removing the owner, leave both NewRoleId and NewRoleName empty in the request.
773+
// Calls PUT /Certificates/{id}/Owner endpoint.
774+
func (c *Client) ChangeCertificateOwnerRole(
775+
certificateId int,
776+
req *OwnerRequest,
777+
params ...*CertificateOwnerChangeParams,
778+
) error {
779+
log.Printf("[INFO] Changing owner of certificate with ID %d in Keyfactor", certificateId)
780+
781+
// Validate certificate ID
782+
if certificateId <= 0 {
783+
return errors.New("certificate ID must be a positive integer")
784+
}
785+
786+
// Set Keyfactor-specific headers
787+
headers := &apiHeaders{
788+
Headers: []StringTuple{
789+
{"x-keyfactor-api-version", "1"},
790+
{"x-keyfactor-requested-with", "APIClient"},
791+
{"Content-Type", "application/json"},
792+
},
793+
}
794+
795+
// Build URL with query parameters
796+
endpoint := fmt.Sprintf("Certificates/%d/Owner", certificateId)
797+
var queryParams []string
798+
799+
if len(params) > 0 && params[0] != nil {
800+
param := params[0]
801+
if param.CollectionId != nil {
802+
queryParams = append(queryParams, fmt.Sprintf("collectionId=%d", *param.CollectionId))
803+
}
804+
if param.ContainerId != nil {
805+
queryParams = append(queryParams, fmt.Sprintf("containerId=%d", *param.ContainerId))
806+
}
807+
}
808+
809+
if len(queryParams) > 0 {
810+
endpoint += "?" + strings.Join(queryParams, "&")
811+
}
812+
813+
keyfactorAPIStruct := &request{
814+
Method: "PUT",
815+
Endpoint: endpoint,
816+
Headers: headers,
817+
Payload: req,
818+
}
819+
820+
resp, err := c.sendRequest(keyfactorAPIStruct)
821+
if err != nil {
822+
return err
823+
}
824+
825+
// Check if the response indicates success (204 No Content expected)
826+
if resp.StatusCode != http.StatusNoContent {
827+
return fmt.Errorf("failed to change certificate owner: HTTP %d", resp.StatusCode)
828+
}
829+
830+
return nil
831+
}
832+
771833
// createSubject builds the certificate subject string from a passed CertificateSubject argument.
772834
func createSubject(cs CertificateSubject) (string, error) {
773835
var subject string

v3/api/certificate_models.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,15 @@ type SubjectAltNameElements struct {
340340
type downloadCertificateResponse struct {
341341
Content string `json:"Content"`
342342
}
343+
344+
// OwnerRequest represents the request structure for changing certificate ownership
345+
type OwnerRequest struct {
346+
NewRoleId *int `json:"NewRoleId,omitempty"`
347+
NewRoleName *string `json:"NewRoleName,omitempty"`
348+
}
349+
350+
// CertificateOwnerChangeParams represents the parameters for changing certificate ownership
351+
type CertificateOwnerChangeParams struct {
352+
CollectionId *int `json:"collectionId,omitempty"`
353+
ContainerId *int `json:"containerId,omitempty"`
354+
}

0 commit comments

Comments
 (0)