Skip to content

Commit

Permalink
assert.True/False functions used instead of assert.Equal
Browse files Browse the repository at this point in the history
  • Loading branch information
aorcholski committed Apr 14, 2022
1 parent b6944fa commit 532208b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/webhook/validation/proxy_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (

const (
errorInvalidActiveGateProxyUrl = `The DynaKube's specification has an invalid Proxy URL value set. Make sure you correctly specify the URL in your custom resource.`
errorInvalidEvalCharacter = `The DynaKube's specification has an invalid Proxy password value set. Make sure you don't use forbidden characters: apostrophe, backtick, comma, ampersand, equals sign, plus sign.`
errorInvalidEvalCharacter = `The DynaKube's specification has an invalid Proxy password value set. Make sure you don't use forbidden characters: space, apostrophe, backtick, comma, ampersand, equals sign, plus sign, percent sign, backslash.`

errorMissingActiveGateProxySecret = `The Proxy secret indicated by the DynaKube specification doesn't exist.`

errorInvalidProxySecretFormat = `The Proxy secret indicated by the DynaKube specification has an invalid format. Make sure you correctly creates the secret.`

errorInvalidProxySecretUrl = `The Proxy secret indicated by the DynaKube specification has an invalid URL value set. Make sure you correctly specify the URL in the secret.`
errorInvalidProxySecretEvalCharacter = `The Proxy secret indicated by the DynaKube specification has an invalid Proxy password value set. Make sure you don't use forbidden characters: apostrophe, backtick, comma, ampersand, equals sign, plus sign.`
errorInvalidProxySecretEvalCharacter = `The Proxy secret indicated by the DynaKube specification has an invalid Proxy password value set. Make sure you don't use forbidden characters: space, apostrophe, backtick, comma, ampersand, equals sign, plus sign, percent sign, backslash.`
)

func invalidActiveGateProxyUrl(dv *dynakubeValidator, dynakube *dynatracev1beta1.DynaKube) string {
Expand Down Expand Up @@ -70,10 +70,10 @@ func isStringValidForAG(str string) bool {
// ` a b c d e f g h i j k l m n o
// p q r s t u v w x y z { | } ~

// '\'' '`' exceptions due to entrypoint.sh:readSecret:eval
// ',' exceptions due to Gateway reader of config files
// '&' '=' '+' exceptions due to entrypoint.sh:saveProxyConfiguration
// '\'' '`' exceptions due to entrypoint.sh:readSecret:eval
// ',' exceptions due to Gateway reader of config files
// '&' '=' '+' '%' '\' exceptions due to entrypoint.sh:saveProxyConfiguration

regex := regexp.MustCompile(`^[!"#$%()*\-./0-9:;<>?@A-Z\[\\\]^_a-z{|}~]+$`)
regex := regexp.MustCompile(`^[!"#$()*\-./0-9:;<>?@A-Z\[\]^_a-z{|}~]+$`)
return regex.MatchString(str)
}
38 changes: 21 additions & 17 deletions src/webhook/validation/proxy_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const (
testProxySecret = "proxysecret"

// invalidPlainTextProxyUrl contains forbidden apostrophe character
invalidPlainTextProxyUrl = "http://test:test'~!@#$%^*()_-|}{[]\":;?><./pass@proxy-service.dynatrace:3128"
invalidPlainTextProxyUrl = "http://test:password'!\"#$()*-./:;<>?@[]^_{|}~@proxy-service.dynatrace:3128"

// validEncodedProxyUrl contains no forbidden characters "http://test:test~!@#$%^*()_-|}{[]\":;?><./pass@proxy-service.dynatrace:3128"
validEncodedProxyUrl = "http://test:test~!%40%23%5E*()_-%7C%7D%7B%5B%5D%22%3A%3B%3F%3E%3C.%2Fpass@proxy-service.dynatrace:3128"
// validEncodedProxyUrl contains no forbidden characters "http://test:password!"#$()*-./:;<>?@[]^_{|}~@proxy-service.dynatrace:3128"
validEncodedProxyUrl = "http://test:password!%22%23%24()*-.%2F%3A%3B%3C%3E%3F%40%5B%5D%5E_%7B%7C%7D~@proxy-service.dynatrace:3128"
)

func TestInvalidActiveGateProxy(t *testing.T) {
Expand Down Expand Up @@ -136,29 +136,33 @@ func TestInvalidActiveGateProxy(t *testing.T) {
})

t.Run(`invalid proxy secret url - entrypoint.sh`, func(t *testing.T) {
assert.Equal(t, true, isStringValidForAG("password"))
assert.Equal(t, true, isStringValidForAG("test~!@#^*()_-|}{[]\":;?><./pass"))
assert.True(t, isStringValidForAG("password"))
assert.True(t, isStringValidForAG("test~!@#^*()_-|}{[]\":;?><./pass"))

// -[] have to be escaped in the regex
assert.Equal(t, true, isStringValidForAG("pass-word"))
assert.Equal(t, true, isStringValidForAG("pass[word"))
assert.Equal(t, true, isStringValidForAG("pass]word"))
assert.Equal(t, true, isStringValidForAG("pass$word"))
assert.True(t, isStringValidForAG("pass-word"))
assert.True(t, isStringValidForAG("pass[word"))
assert.True(t, isStringValidForAG("pass]word"))
assert.True(t, isStringValidForAG("pass$word"))

// apostrophe
assert.Equal(t, false, isStringValidForAG("pass'word"))
assert.False(t, isStringValidForAG("pass'word"))
// backtick
assert.Equal(t, false, isStringValidForAG("pass`word"))
assert.False(t, isStringValidForAG("pass`word"))
// comma
assert.Equal(t, false, isStringValidForAG("pass,word"))
assert.False(t, isStringValidForAG("pass,word"))
// ampersand
assert.Equal(t, false, isStringValidForAG("pass&word"))
assert.False(t, isStringValidForAG("pass&word"))
// equals sign
assert.Equal(t, false, isStringValidForAG("pass=word"))
// plus
assert.Equal(t, false, isStringValidForAG("pass+word"))
assert.False(t, isStringValidForAG("pass=word"))
// plus sign
assert.False(t, isStringValidForAG("pass+word"))
// percent sign
assert.False(t, isStringValidForAG("pass%word"))
// backslash
assert.False(t, isStringValidForAG("pass\\word"))

// UTF-8 single character - U+1F600 grinning face
assert.Equal(t, false, isStringValidForAG("\xF0\x9F\x98\x80"))
assert.False(t, isStringValidForAG("\xF0\x9F\x98\x80"))
})
}

0 comments on commit 532208b

Please sign in to comment.