github.com/jaylevin/jenkins-library@v1.230.4/cmd/apiProxyUpload.go (about) 1 package cmd 2 3 import ( 4 "bytes" 5 b64 "encoding/base64" 6 "fmt" 7 "net/http" 8 "strings" 9 10 "github.com/SAP/jenkins-library/pkg/cpi" 11 piperhttp "github.com/SAP/jenkins-library/pkg/http" 12 "github.com/SAP/jenkins-library/pkg/log" 13 "github.com/SAP/jenkins-library/pkg/piperutils" 14 "github.com/SAP/jenkins-library/pkg/telemetry" 15 "github.com/pkg/errors" 16 ) 17 18 func apiProxyUpload(config apiProxyUploadOptions, telemetryData *telemetry.CustomData) { 19 // Utils can be used wherever the command.ExecRunner interface is expected. 20 // It can also be used for example as a mavenExecRunner. 21 httpClient := &piperhttp.Client{} 22 fileUtils := &piperutils.Files{} 23 // For HTTP calls import piperhttp "github.com/SAP/jenkins-library/pkg/http" 24 // and use a &piperhttp.Client{} in a custom system 25 // Example: step checkmarxExecuteScan.go 26 27 // Error situations should be bubbled up until they reach the line below which will then stop execution 28 // through the log.Entry().Fatal() call leading to an os.Exit(1) in the end. 29 err := runApiProxyUpload(&config, telemetryData, fileUtils, httpClient) 30 if err != nil { 31 log.Entry().WithError(err).Fatal("step execution failed") 32 } 33 } 34 35 func runApiProxyUpload(config *apiProxyUploadOptions, telemetryData *telemetry.CustomData, fileUtils piperutils.FileUtils, httpClient piperhttp.Sender) error { 36 37 serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey) 38 if err != nil { 39 return err 40 } 41 clientOptions := piperhttp.ClientOptions{} 42 tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient} 43 token, tokenErr := cpi.CommonUtils.GetBearerToken(tokenParameters) 44 if tokenErr != nil { 45 return errors.Wrap(tokenErr, "failed to fetch Bearer Token") 46 } 47 clientOptions.Token = fmt.Sprintf("Bearer %s", token) 48 httpClient.SetOptions(clientOptions) 49 50 httpMethod := http.MethodPost 51 uploadApiProxyStatusURL := fmt.Sprintf("%s/apiportal/api/1.0/Transport.svc/APIProxies", serviceKey.OAuth.Host) 52 header := make(http.Header) 53 header.Add("Accept", "application/zip") 54 fileContent, readError := fileUtils.FileRead(config.FilePath) 55 if readError != nil { 56 return errors.Wrapf(readError, "Error reading file") 57 } 58 if !strings.Contains(config.FilePath, "zip") { 59 return errors.New("not valid zip archive") 60 } 61 payload := []byte(b64.StdEncoding.EncodeToString(fileContent)) 62 apiProxyUploadStatusResp, httpErr := httpClient.SendRequest(httpMethod, uploadApiProxyStatusURL, bytes.NewBuffer(payload), header, nil) 63 64 failureMessage := "Failed to upload API Proxy artefact" 65 successMessage := "Successfully created api proxy artefact in API Portal" 66 httpFileUploadRequestParameters := cpi.HttpFileUploadRequestParameters{ 67 ErrMessage: failureMessage, 68 FilePath: config.FilePath, 69 Response: apiProxyUploadStatusResp, 70 HTTPMethod: httpMethod, 71 HTTPURL: uploadApiProxyStatusURL, 72 HTTPErr: httpErr, 73 SuccessMessage: successMessage} 74 return cpi.HTTPUploadUtils.HandleHTTPFileUploadResponse(httpFileUploadRequestParameters) 75 }