2020-05-13 14:51:48 +02:00
package cmd
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"path/filepath"
"strconv"
2020-08-26 14:09:00 +02:00
"strings"
2020-05-13 14:51:48 +02:00
"time"
2020-07-23 10:26:50 +02:00
"github.com/SAP/jenkins-library/pkg/abaputils"
2020-05-13 14:51:48 +02:00
"github.com/SAP/jenkins-library/pkg/command"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
)
2020-07-23 10:26:50 +02:00
func abapEnvironmentRunATCCheck ( options abapEnvironmentRunATCCheckOptions , telemetryData * telemetry . CustomData ) {
2020-05-13 14:51:48 +02:00
2020-07-23 10:26:50 +02:00
// Mapping for options
2020-11-04 13:33:31 +02:00
subOptions := convertATCOptions ( & options )
2020-05-13 14:51:48 +02:00
2020-07-31 14:43:23 +02:00
c := & command . Command { }
2020-05-13 14:51:48 +02:00
c . Stdout ( log . Entry ( ) . Writer ( ) )
c . Stderr ( log . Entry ( ) . Writer ( ) )
2020-07-31 14:43:23 +02:00
var autils = abaputils . AbapUtils {
Exec : c ,
}
var err error
2020-05-13 14:51:48 +02:00
client := piperhttp . Client { }
cookieJar , _ := cookiejar . New ( nil )
clientOptions := piperhttp . ClientOptions {
CookieJar : cookieJar ,
}
client . SetOptions ( clientOptions )
2020-07-23 10:26:50 +02:00
var details abaputils . ConnectionDetailsHTTP
2020-05-13 14:51:48 +02:00
//If Host flag is empty read ABAP endpoint from Service Key instead. Otherwise take ABAP system endpoint from config instead
if err == nil {
2020-07-31 14:43:23 +02:00
details , err = autils . GetAbapCommunicationArrangementInfo ( subOptions , "" )
2020-05-13 14:51:48 +02:00
}
var resp * http . Response
//Fetch Xcrsf-Token
if err == nil {
credentialsOptions := piperhttp . ClientOptions {
Username : details . User ,
Password : details . Password ,
CookieJar : cookieJar ,
}
client . SetOptions ( credentialsOptions )
details . XCsrfToken , err = fetchXcsrfToken ( "GET" , details , nil , & client )
}
if err == nil {
2020-08-26 14:09:00 +02:00
resp , err = triggerATCrun ( options , details , & client )
2020-05-13 14:51:48 +02:00
}
if err == nil {
2020-08-26 14:09:00 +02:00
err = handleATCresults ( resp , details , & client , options . AtcResultsFileName )
2020-05-13 14:51:48 +02:00
}
if err != nil {
log . Entry ( ) . WithError ( err ) . Fatal ( "step execution failed" )
}
2020-09-24 07:41:06 +02:00
log . Entry ( ) . Info ( "ATC run completed successfully. If there are any results from the respective run they will be listed in the logs above as well as being saved in the output .xml file" )
2020-05-13 14:51:48 +02:00
}
2020-08-26 14:09:00 +02:00
func handleATCresults ( resp * http . Response , details abaputils . ConnectionDetailsHTTP , client piperhttp . Sender , atcResultFileName string ) error {
2020-05-13 14:51:48 +02:00
var err error
2020-08-26 14:09:00 +02:00
var abapEndpoint string
abapEndpoint = details . URL
2020-05-13 14:51:48 +02:00
location := resp . Header . Get ( "Location" )
details . URL = abapEndpoint + location
location , err = pollATCRun ( details , nil , client )
if err == nil {
details . URL = abapEndpoint + location
resp , err = getResultATCRun ( "GET" , details , nil , client )
}
//Parse response
var body [ ] byte
if err == nil {
body , err = ioutil . ReadAll ( resp . Body )
}
if err == nil {
defer resp . Body . Close ( )
2020-08-26 14:09:00 +02:00
err = parseATCResult ( body , atcResultFileName )
2020-05-13 14:51:48 +02:00
}
if err != nil {
return fmt . Errorf ( "Handling ATC result failed: %w" , err )
}
return nil
}
2020-08-26 14:09:00 +02:00
func triggerATCrun ( config abapEnvironmentRunATCCheckOptions , details abaputils . ConnectionDetailsHTTP , client piperhttp . Sender ) ( * http . Response , error ) {
2020-05-13 14:51:48 +02:00
var atcConfigyamlFile [ ] byte
2020-08-26 14:09:00 +02:00
abapEndpoint := details . URL
2020-05-13 14:51:48 +02:00
filelocation , err := filepath . Glob ( config . AtcConfig )
//Parse YAML ATC run configuration as body for ATC run trigger
if err == nil {
2020-11-17 12:18:16 +02:00
filename , err := filepath . Abs ( filelocation [ 0 ] )
if err == nil {
atcConfigyamlFile , err = ioutil . ReadFile ( filename )
}
2020-05-13 14:51:48 +02:00
}
var ATCConfig ATCconfig
if err == nil {
var result [ ] byte
result , err = yaml . YAMLToJSON ( atcConfigyamlFile )
json . Unmarshal ( result , & ATCConfig )
}
2020-08-26 14:09:00 +02:00
var checkVariantString , packageString , softwareComponentString string
2020-05-13 14:51:48 +02:00
if err == nil {
2020-08-26 14:09:00 +02:00
checkVariantString , packageString , softwareComponentString , err = buildATCCheckBody ( ATCConfig )
2020-05-13 14:51:48 +02:00
}
//Trigger ATC run
var resp * http . Response
2020-08-26 14:09:00 +02:00
var bodyString = ` <?xml version="1.0" encoding="UTF-8"?><atc:runparameters xmlns:atc="http://www.sap.com/adt/atc" xmlns:obj="http://www.sap.com/adt/objectset" ` + checkVariantString + ` ><obj:objectSet> ` + softwareComponentString + packageString + ` </obj:objectSet></atc:runparameters> `
2020-11-02 15:17:13 +02:00
log . Entry ( ) . Debugf ( "Request Body: %s" , bodyString )
2020-05-13 14:51:48 +02:00
var body = [ ] byte ( bodyString )
if err == nil {
details . URL = abapEndpoint + "/sap/bc/adt/api/atc/runs?clientWait=false"
resp , err = runATC ( "POST" , details , body , client )
}
if err != nil {
2020-06-15 21:31:50 +02:00
return resp , fmt . Errorf ( "Triggering ATC run failed: %w" , err )
2020-05-13 14:51:48 +02:00
}
return resp , nil
}
2020-08-26 14:09:00 +02:00
func buildATCCheckBody ( ATCConfig ATCconfig ) ( checkVariantString string , packageString string , softwareComponentString string , err error ) {
2020-05-13 14:51:48 +02:00
if len ( ATCConfig . Objects . Package ) == 0 && len ( ATCConfig . Objects . SoftwareComponent ) == 0 {
2020-08-26 14:09:00 +02:00
return "" , "" , "" , fmt . Errorf ( "Error while parsing ATC run config. Please provide the packages and/or the software components to be checked! %w" , errors . New ( "No Package or Software Component specified. Please provide either one or both of them" ) )
2020-05-13 14:51:48 +02:00
}
2020-08-26 14:09:00 +02:00
//Build Check Variant and Configuration XML Body
if len ( ATCConfig . CheckVariant ) != 0 {
2020-10-27 10:56:00 +02:00
checkVariantString += ` checkVariant=" ` + ATCConfig . CheckVariant + ` " `
2020-11-02 15:17:13 +02:00
log . Entry ( ) . Infof ( "ATC Check Variant: %s" , ATCConfig . CheckVariant )
2020-08-26 14:09:00 +02:00
if len ( ATCConfig . Configuration ) != 0 {
checkVariantString += ` configuration=" ` + ATCConfig . Configuration + ` " `
}
2020-11-05 11:00:51 +02:00
} else {
const defaultCheckVariant = "SAP_CLOUD_PLATFORM_ATC_DEFAULT"
checkVariantString += ` checkVariant=" ` + defaultCheckVariant + ` " `
log . Entry ( ) . Infof ( "ATC Check Variant: %s" , checkVariantString )
2020-08-26 14:09:00 +02:00
}
2020-05-13 14:51:48 +02:00
//Build Package XML body
if len ( ATCConfig . Objects . Package ) != 0 {
packageString += "<obj:packages>"
for _ , s := range ATCConfig . Objects . Package {
packageString += ` <obj:package value=" ` + s . Name + ` " includeSubpackages=" ` + strconv . FormatBool ( s . IncludeSubpackages ) + ` "/> `
}
packageString += "</obj:packages>"
}
//Build SC XML body
if len ( ATCConfig . Objects . SoftwareComponent ) != 0 {
softwareComponentString += "<obj:softwarecomponents>"
for _ , s := range ATCConfig . Objects . SoftwareComponent {
softwareComponentString += ` <obj:softwarecomponent value=" ` + s . Name + ` "/> `
}
softwareComponentString += "</obj:softwarecomponents>"
}
2020-08-26 14:09:00 +02:00
return checkVariantString , packageString , softwareComponentString , nil
2020-05-13 14:51:48 +02:00
}
2020-08-26 14:09:00 +02:00
func parseATCResult ( body [ ] byte , atcResultFileName string ) ( err error ) {
2020-05-13 14:51:48 +02:00
if len ( body ) == 0 {
return fmt . Errorf ( "Parsing ATC result failed: %w" , errors . New ( "Body is empty, can't parse empty body" ) )
}
2020-11-02 15:17:13 +02:00
responseBody := string ( body )
log . Entry ( ) . Debugf ( "Response body: %s" , responseBody )
if strings . HasPrefix ( responseBody , "<html>" ) {
return errors . New ( "The Software Component could not be checked. Please make sure the respective Software Component has been cloned successfully on the system" )
}
2020-05-13 14:51:48 +02:00
parsedXML := new ( Result )
xml . Unmarshal ( [ ] byte ( body ) , & parsedXML )
2020-08-26 14:09:00 +02:00
if len ( parsedXML . Files ) == 0 {
log . Entry ( ) . Info ( "There were no results from this run, most likely the checked Software Components are empty or contain no ATC findings" )
}
2020-11-02 15:17:13 +02:00
2020-08-26 14:09:00 +02:00
err = ioutil . WriteFile ( atcResultFileName , body , 0644 )
2020-09-01 00:33:46 +02:00
if err == nil {
2020-05-13 14:51:48 +02:00
var reports [ ] piperutils . Path
2020-08-26 14:09:00 +02:00
reports = append ( reports , piperutils . Path { Target : atcResultFileName , Name : "ATC Results" , Mandatory : true } )
2020-05-13 14:51:48 +02:00
piperutils . PersistReportsAndLinks ( "abapEnvironmentRunATCCheck" , "" , reports , nil )
for _ , s := range parsedXML . Files {
for _ , t := range s . ATCErrors {
2021-01-28 14:06:13 +02:00
log . Entry ( ) . Infof ( "%s in file '%s': %s in line %s found by %s" , t . Severity , s . Key , t . Message , t . Line , t . Source )
2020-05-13 14:51:48 +02:00
}
}
}
if err != nil {
return fmt . Errorf ( "Writing results to XML file failed: %w" , err )
}
return nil
}
2020-07-23 10:26:50 +02:00
func runATC ( requestType string , details abaputils . ConnectionDetailsHTTP , body [ ] byte , client piperhttp . Sender ) ( * http . Response , error ) {
2020-05-13 14:51:48 +02:00
log . Entry ( ) . WithField ( "ABAP endpoint: " , details . URL ) . Info ( "Triggering ATC run" )
header := make ( map [ string ] [ ] string )
header [ "X-Csrf-Token" ] = [ ] string { details . XCsrfToken }
header [ "Content-Type" ] = [ ] string { "application/vnd.sap.atc.run.parameters.v1+xml; charset=utf-8;" }
req , err := client . SendRequest ( requestType , details . URL , bytes . NewBuffer ( body ) , header , nil )
if err != nil {
return req , fmt . Errorf ( "Triggering ATC run failed: %w" , err )
}
defer req . Body . Close ( )
return req , err
}
2020-07-23 10:26:50 +02:00
func fetchXcsrfToken ( requestType string , details abaputils . ConnectionDetailsHTTP , body [ ] byte , client piperhttp . Sender ) ( string , error ) {
2020-05-13 14:51:48 +02:00
2020-11-02 15:17:13 +02:00
log . Entry ( ) . WithField ( "ABAP Endpoint: " , details . URL ) . Debug ( "Fetching Xcrsf-Token" )
2020-05-13 14:51:48 +02:00
details . URL += "/sap/bc/adt/api/atc/runs/00000000000000000000000000000000"
details . XCsrfToken = "fetch"
header := make ( map [ string ] [ ] string )
header [ "X-Csrf-Token" ] = [ ] string { details . XCsrfToken }
header [ "Accept" ] = [ ] string { "application/vnd.sap.atc.run.v1+xml" }
req , err := client . SendRequest ( requestType , details . URL , bytes . NewBuffer ( body ) , header , nil )
if err != nil {
return "" , fmt . Errorf ( "Fetching Xcsrf-Token failed: %w" , err )
}
defer req . Body . Close ( )
2020-10-08 11:08:58 +02:00
// workaround until golang version 1.16 is used
time . Sleep ( 1 * time . Second )
2020-05-13 14:51:48 +02:00
token := req . Header . Get ( "X-Csrf-Token" )
return token , err
}
2020-07-23 10:26:50 +02:00
func pollATCRun ( details abaputils . ConnectionDetailsHTTP , body [ ] byte , client piperhttp . Sender ) ( string , error ) {
2020-05-13 14:51:48 +02:00
log . Entry ( ) . WithField ( "ABAP endpoint" , details . URL ) . Info ( "Polling ATC run status" )
for {
resp , err := getHTTPResponseATCRun ( "GET" , details , nil , client )
if err != nil {
return "" , fmt . Errorf ( "Getting HTTP response failed: %w" , err )
}
bodyText , err := ioutil . ReadAll ( resp . Body )
if err != nil {
return "" , fmt . Errorf ( "Reading response body failed: %w" , err )
}
x := new ( Run )
xml . Unmarshal ( bodyText , & x )
log . Entry ( ) . WithField ( "StatusCode" , resp . StatusCode ) . Info ( "Status: " + x . Status )
if x . Status == "Not Created" {
return "" , err
}
if x . Status == "Completed" {
return x . Link [ 0 ] . Key , err
}
if x . Status == "" {
return "" , fmt . Errorf ( "Could not get any response from ATC poll: %w" , errors . New ( "Status from ATC run is empty. Either it's not an ABAP system or ATC run hasn't started" ) )
}
time . Sleep ( 5 * time . Second )
}
}
2020-07-23 10:26:50 +02:00
func getHTTPResponseATCRun ( requestType string , details abaputils . ConnectionDetailsHTTP , body [ ] byte , client piperhttp . Sender ) ( * http . Response , error ) {
2020-05-13 14:51:48 +02:00
log . Entry ( ) . WithField ( "ABAP Endpoint: " , details . URL ) . Info ( "Polling ATC run status" )
header := make ( map [ string ] [ ] string )
header [ "Accept" ] = [ ] string { "application/vnd.sap.atc.run.v1+xml" }
req , err := client . SendRequest ( requestType , details . URL , bytes . NewBuffer ( body ) , header , nil )
if err != nil {
return req , fmt . Errorf ( "Getting ATC run status failed: %w" , err )
}
return req , err
}
2020-07-23 10:26:50 +02:00
func getResultATCRun ( requestType string , details abaputils . ConnectionDetailsHTTP , body [ ] byte , client piperhttp . Sender ) ( * http . Response , error ) {
2020-05-13 14:51:48 +02:00
log . Entry ( ) . WithField ( "ABAP Endpoint: " , details . URL ) . Info ( "Getting ATC results" )
header := make ( map [ string ] [ ] string )
header [ "x-csrf-token" ] = [ ] string { details . XCsrfToken }
header [ "Accept" ] = [ ] string { "application/vnd.sap.atc.checkstyle.v1+xml" }
req , err := client . SendRequest ( requestType , details . URL , bytes . NewBuffer ( body ) , header , nil )
if err != nil {
return req , fmt . Errorf ( "Getting ATC run results failed: %w" , err )
}
return req , err
}
2020-11-02 15:17:13 +02:00
func convertATCOptions ( options * abapEnvironmentRunATCCheckOptions ) abaputils . AbapEnvironmentOptions {
subOptions := abaputils . AbapEnvironmentOptions { }
subOptions . CfAPIEndpoint = options . CfAPIEndpoint
subOptions . CfServiceInstance = options . CfServiceInstance
subOptions . CfServiceKeyName = options . CfServiceKeyName
subOptions . CfOrg = options . CfOrg
subOptions . CfSpace = options . CfSpace
subOptions . Host = options . Host
subOptions . Password = options . Password
subOptions . Username = options . Username
return subOptions
}
2020-05-13 14:51:48 +02:00
//ATCconfig object for parsing yaml config of software components and packages
type ATCconfig struct {
2020-08-26 14:09:00 +02:00
CheckVariant string ` json:"checkvariant,omitempty" `
Configuration string ` json:"configuration,omitempty" `
Objects ATCObjects ` json:"atcobjects" `
2020-05-13 14:51:48 +02:00
}
//ATCObjects in form of packages and software components to be checked
type ATCObjects struct {
Package [ ] Package ` json:"package" `
SoftwareComponent [ ] SoftwareComponent ` json:"softwarecomponent" `
}
//Package for ATC run to be checked
type Package struct {
Name string ` json:"name" `
IncludeSubpackages bool ` json:"includesubpackage" `
}
//SoftwareComponent for ATC run to be checked
type SoftwareComponent struct {
Name string ` json:"name" `
}
//Run Object for parsing XML
type Run struct {
XMLName xml . Name ` xml:"run" `
Status string ` xml:"status,attr" `
Link [ ] Link ` xml:"link" `
}
//Link of XML object
type Link struct {
Key string ` xml:"href,attr" `
Value string ` xml:",chardata" `
}
//Result from ATC check for all files that were checked
type Result struct {
XMLName xml . Name ` xml:"checkstyle" `
Files [ ] File ` xml:"file" `
}
//File that contains ATC check with error for checked file
type File struct {
Key string ` xml:"name,attr" `
Value string ` xml:",chardata" `
ATCErrors [ ] ATCError ` xml:"error" `
}
//ATCError with message
type ATCError struct {
2021-01-28 14:06:13 +02:00
Text string ` xml:",chardata" `
Message string ` xml:"message,attr" `
Source string ` xml:"source,attr" `
Line string ` xml:"line,attr" `
Severity string ` xml:"severity,attr" `
2020-05-13 14:51:48 +02:00
}