mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-18 05:18:24 +02:00
78a29d782b
* Switch to service key for CPI GetMplStatus Introduces read method for service key files, mock utils and tests. * Use secret text instead of file * Change serviceKey definition * Update cpiUpload to use Service Key retrieved the host and uaa information from service key * Update cpiDeploy to use service key retrieved the host and uaa information from service key * Update cpiServiceEndpoint to use Service Key retrieved the host and uaa information from service key * Update cpiDownload to use Service Key retrieved the host and uaa information from service key * Update cpiUpdateConfig to use Service Key retrieved the host and uaa information from service key * Refactor serviceKey var name * Fixed references to service key to follow the real format they should be accessed through oauth instead of uaa because of the format of the json * Rename ServiceKey to APIServiceKey To support having a different service key(and for readability), we need to change the name to API. * Add STAGES and STEPS yaml add in to each yaml file of cpi integration * Revert "Add STAGES and STEPS yaml" This reverts commit aa2665d158b0f864cfee95ff999a7dc8ea3477f1. * Change comments/formatting commonUtils Make comments more understandable and follow code climate suggestions * Change documentation files for steps remove OAuth and host and change credentials to be servicekey Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> Co-authored-by: Thorsten Duda <thorsten.duda@sap.com>
94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package cpi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/Jeffail/gabs/v2"
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
//CommonUtils for CPI
|
|
type CommonUtils interface {
|
|
GetBearerToken() (string, error)
|
|
}
|
|
|
|
//TokenParameters struct
|
|
type TokenParameters struct {
|
|
TokenURL, Username, Password string
|
|
Client piperhttp.Sender
|
|
}
|
|
|
|
// ServiceKey contains information about a CPI service key
|
|
type ServiceKey struct {
|
|
OAuth OAuth `json:"oauth"`
|
|
}
|
|
|
|
// OAuth is inside a CPI service key and contains more needed information
|
|
type OAuth struct {
|
|
Host string `json:"url"`
|
|
OAuthTokenProviderURL string `json:"tokenurl"`
|
|
ClientID string `json:"clientid"`
|
|
ClientSecret string `json:"clientsecret"`
|
|
}
|
|
|
|
// ReadCpiServiceKey unmarshalls the give json service key string.
|
|
func ReadCpiServiceKey(serviceKeyJSON string) (cpiServiceKey ServiceKey, err error) {
|
|
// parse
|
|
err = json.Unmarshal([]byte(serviceKeyJSON), &cpiServiceKey)
|
|
if err != nil {
|
|
err = errors.Wrap(err, "error unmarshalling serviceKey")
|
|
return
|
|
}
|
|
|
|
log.Entry().Info("CPI serviceKey read successfully")
|
|
return
|
|
}
|
|
|
|
// GetBearerToken -Provides the bearer token for making CPI OData calls
|
|
func (tokenParameters TokenParameters) GetBearerToken() (string, error) {
|
|
|
|
httpClient := tokenParameters.Client
|
|
|
|
clientOptions := piperhttp.ClientOptions{
|
|
Username: tokenParameters.Username,
|
|
Password: tokenParameters.Password,
|
|
}
|
|
httpClient.SetOptions(clientOptions)
|
|
|
|
header := make(http.Header)
|
|
header.Add("Accept", "application/json")
|
|
tokenFinalURL := fmt.Sprintf("%s?grant_type=client_credentials", tokenParameters.TokenURL)
|
|
method := "POST"
|
|
resp, httpErr := httpClient.SendRequest(method, tokenFinalURL, nil, header, nil)
|
|
if httpErr != nil {
|
|
return "", errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", method, tokenFinalURL)
|
|
}
|
|
if resp != nil && resp.Body != nil {
|
|
defer resp.Body.Close()
|
|
}
|
|
|
|
if resp == nil {
|
|
return "", errors.Errorf("did not retrieve a HTTP response")
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
return "", errors.Errorf("did not retrieve a valid HTTP response code: %v", httpErr)
|
|
}
|
|
|
|
bodyText, readErr := ioutil.ReadAll(resp.Body)
|
|
if readErr != nil {
|
|
return "", errors.Wrap(readErr, "HTTP response body could not be read")
|
|
}
|
|
jsonResponse, parsingErr := gabs.ParseJSON([]byte(bodyText))
|
|
if parsingErr != nil {
|
|
return "", errors.Wrapf(parsingErr, "HTTP response body could not be parsed as JSON: %v", string(bodyText))
|
|
}
|
|
token := jsonResponse.Path("access_token").Data().(string)
|
|
return token, nil
|
|
}
|