1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-16 05:16:08 +02:00

fortifyExecuteScan: Make URL parameters more robust (#1900)

This commit is contained in:
Stephan Aßmus 2020-08-11 18:07:06 +02:00 committed by GitHub
parent 771bfd0cf2
commit 5338ea1476
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 7 deletions

View File

@ -76,6 +76,10 @@ type SystemInstance struct {
// NewSystemInstance - creates an returns a new SystemInstance
func NewSystemInstance(serverURL, apiEndpoint, authToken string, timeout time.Duration) *SystemInstance {
// If serverURL ends in a trailing slash, UploadResultFile() will construct a URL with two or more
// consecutive slashes and actually fail with a 503. https://github.com/SAP/jenkins-library/issues/1826
// Also, since the step outputs a lot of URLs to the log, those will look nicer without redundant slashes.
serverURL = strings.TrimRight(serverURL, "/")
format := strfmt.Default
dateTimeFormat := models.Iso8601MilliDateTime{}
format.Add("datetime", &dateTimeFormat, models.IsDateTime)
@ -90,6 +94,9 @@ func NewSystemInstance(serverURL, apiEndpoint, authToken string, timeout time.Du
func createTransportConfig(serverURL, apiEndpoint string) *ff.TransportConfig {
scheme, host := splitSchemeAndHost(serverURL)
host, hostEndpoint := splitHostAndEndpoint(host)
// Cleaning up any slashes here is mostly for cleaner log-output.
hostEndpoint = strings.TrimRight(hostEndpoint, "/")
apiEndpoint = strings.Trim(apiEndpoint, "/")
return &ff.TransportConfig{
Host: host,
Schemes: []string{scheme},

View File

@ -39,7 +39,13 @@ func spinUpServer(f func(http.ResponseWriter, *http.Request)) (*SystemInstance,
func TestCreateTransportConfig(t *testing.T) {
t.Run("Valid URL", func(t *testing.T) {
config := createTransportConfig("http://some.fortify.host.com/ssc", "api/v2")
config := createTransportConfig("http://some.fortify.host.com/ssc", "/api/v2")
assert.Equal(t, []string{"http"}, config.Schemes)
assert.Equal(t, "some.fortify.host.com", config.Host)
assert.Equal(t, "ssc/api/v2", config.BasePath)
})
t.Run("Slashes are trimmed", func(t *testing.T) {
config := createTransportConfig("http://some.fortify.host.com/ssc//", "//api/v2/")
assert.Equal(t, []string{"http"}, config.Schemes)
assert.Equal(t, "some.fortify.host.com", config.Host)
assert.Equal(t, "ssc/api/v2", config.BasePath)
@ -59,12 +65,19 @@ func TestCreateTransportConfig(t *testing.T) {
}
func TestNewSystemInstance(t *testing.T) {
sys := NewSystemInstance("https://some.fortify.host.com/ssc", "api/v1", "akjhskjhks", 10*time.Second)
assert.IsType(t, ff.Fortify{}, *sys.client, "Expected to get a Fortify client instance")
assert.IsType(t, piperHttp.Client{}, *sys.httpClient, "Expected to get a HTTP client instance")
assert.IsType(t, logrus.Entry{}, *sys.logger, "Expected to get a logrus entry instance")
assert.Equal(t, 10*time.Second, sys.timeout, "Expected different timeout value")
assert.Equal(t, "akjhskjhks", sys.token, "Expected different token value")
t.Run("fields are initialized", func(t *testing.T) {
sys := NewSystemInstance("https://some.fortify.host.com/ssc", "api/v1", "akjhskjhks", 10*time.Second)
assert.IsType(t, ff.Fortify{}, *sys.client, "Expected to get a Fortify client instance")
assert.IsType(t, piperHttp.Client{}, *sys.httpClient, "Expected to get a HTTP client instance")
assert.IsType(t, logrus.Entry{}, *sys.logger, "Expected to get a logrus entry instance")
assert.Equal(t, 10*time.Second, sys.timeout, "Expected different timeout value")
assert.Equal(t, "akjhskjhks", sys.token, "Expected different token value")
assert.Equal(t, "https://some.fortify.host.com/ssc", sys.serverURL)
})
t.Run("SSC URL is trimmed", func(t *testing.T) {
sys := NewSystemInstance("https://some.fortify.host.com/ssc/", "api/v1", "akjhskjhks", 10*time.Second)
assert.Equal(t, "https://some.fortify.host.com/ssc", sys.serverURL)
})
}
func TestGetProjectByName(t *testing.T) {