called the new methods, moved the new files, and added more test cases

This commit is contained in:
Bhaskar Brahma 2019-06-06 15:44:02 -07:00
Родитель 27a5ea8d47
Коммит dcb4322d9d
5 изменённых файлов: 61 добавлений и 34 удалений

Просмотреть файл

@ -10,6 +10,7 @@ import (
"github.com/Azure/custom-script-extension-linux/pkg/blobutil"
"github.com/Azure/custom-script-extension-linux/pkg/download"
"github.com/Azure/custom-script-extension-linux/pkg/preprocess"
"github.com/Azure/custom-script-extension-linux/pkg/urlutil"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"os"
@ -24,6 +25,10 @@ func downloadAndProcessURL(ctx *log.Context, url, downloadDir, storageAccountNam
return err
}
if !urlutil.IsValidUrl(url){
return fmt.Errorf("[url redacted] is not a valid url")
}
dl, err := getDownloader(url, storageAccountName, storageAccountKey)
if err != nil {
return err

Просмотреть файл

@ -1,31 +0,0 @@
package main
import (
"fmt"
"strings"
"testing"
)
func Test_urlparse01(t *testing.T) {
errorString := `Get https://aaaaaaaa.blob.core.windows.net/nodeagentpackage-version9-0-0-381/Ubuntu-16.04/batch_config-ubuntu-16.04-1.5.9.tar.gz?sv=2018-03-28&sr=b&sig=a%secret%2Bsecret&st=2019-05-17T01%3A25%3A42Z&se=2021-05-24T01%3A25%3A42Z&sp=r: dial tcp 13.68.165.64:443: i/o timeout`
inputErr := fmt.Errorf("%s", errorString)
outputErr := RemoveUrlFromErr(inputErr)
if strings.Contains(outputErr.Error(), "https://"){
t.Error("Url removal failed")
} else {
fmt.Print(outputErr.Error())
}
}
func Test_urlparse02(t *testing.T) {
errorString := `Gethttps://aaaaaaaa.blob.core.windows.net/nodeagentpackage-version9-0-0-381/Ubuntu-16.04/batch_config-ubuntu-16.04-1.5.9.tar.gz?sv=2018-03-28&sr=b&sig=a%secret%2Bsecret&st=2019-05-17T01%3A25%3A42Z&se=2021-05-24T01%3A25%3A42Z&sp=r: dial tcp 13.68.165.64:443: i/o timeout`
inputErr := fmt.Errorf("%s", errorString)
outputErr := RemoveUrlFromErr(inputErr)
if strings.Contains(outputErr.Error(), "https://"){
t.Error("Url removal failed")
} else {
fmt.Print(outputErr.Error())
}
}

Просмотреть файл

@ -2,6 +2,7 @@ package download
import (
"fmt"
"github.com/Azure/custom-script-extension-linux/pkg/urlutil"
"io"
"net"
"net/http"
@ -44,6 +45,7 @@ func Download(d Downloader) (io.ReadCloser, error) {
resp, err := httpClient.Do(req)
if err != nil {
err = urlutil.RemoveUrlFromErr(err)
return nil, errors.Wrapf(err, "http request failed")
}

Просмотреть файл

@ -1,4 +1,4 @@
package main
package urlutil
import (
"fmt"
@ -9,11 +9,18 @@ import (
func RemoveUrlFromErr(err error) error{
strSegments := strings.Split(err.Error(), " ")
for i, v := range strSegments{
u, parseError := url.Parse(v)
if parseError == nil && u.Scheme != "" && u.Host != "" && u.Path != ""{
if IsValidUrl(v){
// we found a url
strSegments[i] = "[uri redacted]"
}
}
return fmt.Errorf(strings.Join(strSegments, " "))
}
func IsValidUrl(urlstring string) bool {
u, parseError := url.Parse(urlstring)
if parseError == nil && u.Scheme != "" && u.Host != "" && u.Path != "" {
return true
}
return false
}

Просмотреть файл

@ -0,0 +1,44 @@
package urlutil
import (
"fmt"
"strings"
"testing"
)
func Test_urlparse01(t *testing.T) {
// error string as expected
errorString := `Get https://aaaaaaaa.blob.core.windows.net/nodeagentpackage-version9-0-0-381/Ubuntu-16.04/batch_config-ubuntu-16.04-1.5.9.tar.gz?sv=2018-03-28&sr=b&sig=a%secret%2Bsecret&st=2019-05-17T01%3A25%3A42Z&se=2021-05-24T01%3A25%3A42Z&sp=r: dial tcp 13.68.165.64:443: i/o timeout`
inputErr := fmt.Errorf("%s", errorString)
outputErr := RemoveUrlFromErr(inputErr)
if strings.Contains(outputErr.Error(), "https://") || !strings.Contains(outputErr.Error(), "[uri redacted]"){
t.Error("Url removal failed")
} else {
fmt.Println(outputErr.Error())
}
}
func Test_urlparse02(t *testing.T) {
// error string where scheme is not https because missing space between Get and https://
errorString := `Gethttps://aaaaaaaa.blob.core.windows.net/nodeagentpackage-version9-0-0-381/Ubuntu-16.04/batch_config-ubuntu-16.04-1.5.9.tar.gz?sv=2018-03-28&sr=b&sig=a%secret%2Bsecret&st=2019-05-17T01%3A25%3A42Z&se=2021-05-24T01%3A25%3A42Z&sp=r: dial tcp 13.68.165.64:443: i/o timeout`
inputErr := fmt.Errorf("%s", errorString)
outputErr := RemoveUrlFromErr(inputErr)
if strings.Contains(outputErr.Error(), "https://") || !strings.Contains(outputErr.Error(), "[uri redacted]"){
t.Error("Url removal failed")
} else {
fmt.Println(outputErr.Error())
}
}
func Test_urlparse03(t *testing.T) {
// error string where the uri isn' sparated by space with the rest of the error message
errorString := `Gethttps://aaaaaaaa.blob.core.windows.net/nodeagentpackage-version9-0-0-381/Ubuntu-16.04/batch_config-ubuntu-16.04-1.5.9.tar.gz?sv=2018-03-28&sr=b&sig=a%secret%2Bsecret&st=2019-05-17T01%3A25%3A42Z&se=2021-05-24T01%3A25%3A42Z&sp=r:dial tcp 13.68.165.64:443:i/o timeout`
inputErr := fmt.Errorf("%s", errorString)
outputErr := RemoveUrlFromErr(inputErr)
if strings.Contains(outputErr.Error(), "https://") || !strings.Contains(outputErr.Error(), "[uri redacted]"){
t.Error("Url removal failed")
} else {
fmt.Println(outputErr.Error())
}
}