github.com/jfrog/jfrog-client-go@v1.40.2/distribution/services/setsigningkey.go (about)

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	artifactoryUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
     8  	"github.com/jfrog/jfrog-client-go/auth"
     9  	"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
    10  	"github.com/jfrog/jfrog-client-go/utils"
    11  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    12  	"github.com/jfrog/jfrog-client-go/utils/log"
    13  )
    14  
    15  type SetSigningKeyService struct {
    16  	client      *jfroghttpclient.JfrogHttpClient
    17  	DistDetails auth.ServiceDetails
    18  }
    19  
    20  func NewSetSigningKeyService(client *jfroghttpclient.JfrogHttpClient) *SetSigningKeyService {
    21  	return &SetSigningKeyService{client: client}
    22  }
    23  
    24  func (ssk *SetSigningKeyService) GetDistDetails() auth.ServiceDetails {
    25  	return ssk.DistDetails
    26  }
    27  
    28  func (ssk *SetSigningKeyService) SetSigningKey(signBundleParams SetSigningKeyParams) error {
    29  	body := &SetSigningKeyBody{
    30  		PublicKey:  signBundleParams.PublicKey,
    31  		PrivateKey: signBundleParams.PrivateKey,
    32  	}
    33  	return ssk.execSetSigningKey(body)
    34  }
    35  
    36  func (ssk *SetSigningKeyService) execSetSigningKey(setSigningKeyBody *SetSigningKeyBody) error {
    37  	httpClientsDetails := ssk.DistDetails.CreateHttpClientDetails()
    38  	content, err := json.Marshal(setSigningKeyBody)
    39  	if err != nil {
    40  		return errorutils.CheckError(err)
    41  	}
    42  	url := ssk.DistDetails.GetUrl() + "/api/v1/keys/pgp"
    43  	artifactoryUtils.SetContentType("application/json", &httpClientsDetails.Headers)
    44  	resp, body, err := ssk.client.SendPut(url, content, &httpClientsDetails)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
    49  		return err
    50  	}
    51  
    52  	log.Debug("Distribution response:", resp.Status)
    53  	log.Debug(utils.IndentJson(body))
    54  	return errorutils.CheckError(err)
    55  }
    56  
    57  type SetSigningKeyBody struct {
    58  	PublicKey  string `json:"public_key,omitempty"`
    59  	PrivateKey string `json:"private_key,omitempty"`
    60  }
    61  
    62  type SetSigningKeyParams struct {
    63  	PublicKey  string
    64  	PrivateKey string
    65  }
    66  
    67  func NewSetSigningKeyParams(publicKey, privateKey string) SetSigningKeyParams {
    68  	return SetSigningKeyParams{
    69  		PublicKey:  publicKey,
    70  		PrivateKey: privateKey,
    71  	}
    72  }