fix vsphere postprocessor password log filtering, write tests

This commit is contained in:
Megan Marsh 2020-07-24 15:09:49 -07:00
parent b3c3e3ed63
commit 6383e6cbbf
2 changed files with 32 additions and 3 deletions

View File

@ -193,12 +193,14 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact
func filterLog(s string, u *url.URL) string {
password, passwordSet := u.User.Password()
if passwordSet && password != "" {
return strings.Replace(s, password, "<password>", -1)
}
if !passwordSet || password == "" {
return s
}
encodedUserPassword := strings.Split(u.User.String(), ":")
encodedPassword := encodedUserPassword[len(encodedUserPassword)-1]
return strings.Replace(s, encodedPassword, "<password>", -1)
}
func (p *PostProcessor) BuildArgs(source, ovftool_uri string) ([]string, error) {
args := []string{

View File

@ -101,3 +101,30 @@ func TestGenerateURI_PasswordEscapes(t *testing.T) {
}
}
}
func TestFilterLogs(t *testing.T) {
// Password is encoded, and contains a colon
ovftool_uri := fmt.Sprintf("vi://hostname/Datacenter/host/cluster")
u, _ := url.Parse(ovftool_uri)
u.User = url.UserPassword("us:ername", "P@ssW:rd")
logstring := "vi://us%3Aername:P%40ssW%3Ard@hostname/Datacenter/host/cluster"
outstring := filterLog(logstring, u)
expected := "vi://us%3Aername:<password>@hostname/Datacenter/host/cluster"
if outstring != expected {
t.Fatalf("Should have successfully filtered encoded password. Expected: %s; recieved: %s", expected, outstring)
}
// There is no password
u.User = url.UserPassword("us:ername", "")
logstring = "vi://us%3Aername:@hostname/Datacenter/host/cluster"
outstring = filterLog(logstring, u)
expected = "vi://us%3Aername:@hostname/Datacenter/host/cluster"
if outstring != expected {
t.Fatalf("Should have ignored password filtering since it was not set. Expected: %s; recieved: %s", expected, outstring)
}
}