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

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	artifactoryUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
     6  	"github.com/jfrog/jfrog-client-go/auth"
     7  	distributionServiceUtils "github.com/jfrog/jfrog-client-go/distribution/services/utils"
     8  	"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
     9  	"github.com/jfrog/jfrog-client-go/utils"
    10  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    11  	"github.com/jfrog/jfrog-client-go/utils/log"
    12  	"net/http"
    13  )
    14  
    15  type SignBundleService struct {
    16  	client      *jfroghttpclient.JfrogHttpClient
    17  	DistDetails auth.ServiceDetails
    18  }
    19  
    20  func NewSignBundleService(client *jfroghttpclient.JfrogHttpClient) *SignBundleService {
    21  	return &SignBundleService{client: client}
    22  }
    23  
    24  func (sb *SignBundleService) GetDistDetails() auth.ServiceDetails {
    25  	return sb.DistDetails
    26  }
    27  
    28  func (sb *SignBundleService) SignReleaseBundle(signBundleParams SignBundleParams) (*utils.Sha256Summary, error) {
    29  	signBundleBody := &SignBundleBody{
    30  		StoringRepository: signBundleParams.StoringRepository,
    31  	}
    32  	return sb.execSignReleaseBundle(signBundleParams.Name, signBundleParams.Version, signBundleParams.GpgPassphrase, signBundleBody)
    33  }
    34  
    35  func (sb *SignBundleService) execSignReleaseBundle(name, version, gpgPassphrase string, signBundleBody *SignBundleBody) (*utils.Sha256Summary, error) {
    36  	summary := utils.NewSha256Summary()
    37  	httpClientsDetails := sb.DistDetails.CreateHttpClientDetails()
    38  	content, err := json.Marshal(signBundleBody)
    39  	if err != nil {
    40  		return summary, errorutils.CheckError(err)
    41  	}
    42  	url := sb.DistDetails.GetUrl() + "api/v1/release_bundle/" + name + "/" + version + "/sign"
    43  	artifactoryUtils.SetContentType("application/json", &httpClientsDetails.Headers)
    44  	distributionServiceUtils.AddGpgPassphraseHeader(gpgPassphrase, &httpClientsDetails.Headers)
    45  	resp, body, err := sb.client.SendPost(url, content, &httpClientsDetails)
    46  	if err != nil {
    47  		return summary, err
    48  	}
    49  	if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
    50  		return summary, err
    51  	}
    52  	summary.SetSucceeded(true)
    53  	summary.SetSha256(resp.Header.Get("X-Checksum-Sha256"))
    54  
    55  	log.Debug("Distribution response:", resp.Status)
    56  	log.Debug(utils.IndentJson(body))
    57  	return summary, errorutils.CheckError(err)
    58  }
    59  
    60  type SignBundleBody struct {
    61  	StoringRepository string `json:"storing_repository,omitempty"`
    62  }
    63  
    64  type SignBundleParams struct {
    65  	Name              string
    66  	Version           string
    67  	StoringRepository string
    68  	GpgPassphrase     string
    69  }
    70  
    71  func NewSignBundleParams(name, version string) SignBundleParams {
    72  	return SignBundleParams{
    73  		Name:    name,
    74  		Version: version,
    75  	}
    76  }