feat: fix password registry leak of credentials (#1687)
* fix password registry issue Signed-off-by: zhaque44 <haque.zubair@gmail.com>
This commit is contained in:
		
							parent
							
								
									d9eb271ab7
								
							
						
					
					
						commit
						5a48c1d8bb
					
				| 
						 | 
					@ -160,10 +160,12 @@ func (helm *execer) AddRepo(name, repository, cafile, certfile, keyfile, usernam
 | 
				
			||||||
	var args []string
 | 
						var args []string
 | 
				
			||||||
	var out []byte
 | 
						var out []byte
 | 
				
			||||||
	var err error
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if name == "" && repository != "" {
 | 
						if name == "" && repository != "" {
 | 
				
			||||||
		helm.logger.Infof("empty field name\n")
 | 
							helm.logger.Infof("empty field name\n")
 | 
				
			||||||
		return fmt.Errorf("empty field name")
 | 
							return fmt.Errorf("empty field name")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch managed {
 | 
						switch managed {
 | 
				
			||||||
	case "acr":
 | 
						case "acr":
 | 
				
			||||||
		helm.logger.Infof("Adding repo %v (acr)", name)
 | 
							helm.logger.Infof("Adding repo %v (acr)", name)
 | 
				
			||||||
| 
						 | 
					@ -186,9 +188,7 @@ func (helm *execer) AddRepo(name, repository, cafile, certfile, keyfile, usernam
 | 
				
			||||||
		if cafile != "" {
 | 
							if cafile != "" {
 | 
				
			||||||
			args = append(args, "--ca-file", cafile)
 | 
								args = append(args, "--ca-file", cafile)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if username != "" && password != "" {
 | 
					
 | 
				
			||||||
			args = append(args, "--username", username, "--password", password)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if passCredentials {
 | 
							if passCredentials {
 | 
				
			||||||
			args = append(args, "--pass-credentials")
 | 
								args = append(args, "--pass-credentials")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -196,12 +196,20 @@ func (helm *execer) AddRepo(name, repository, cafile, certfile, keyfile, usernam
 | 
				
			||||||
			args = append(args, "--insecure-skip-tls-verify")
 | 
								args = append(args, "--insecure-skip-tls-verify")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		helm.logger.Infof("Adding repo %v %v", name, repository)
 | 
							helm.logger.Infof("Adding repo %v %v", name, repository)
 | 
				
			||||||
 | 
							if username != "" && password != "" {
 | 
				
			||||||
 | 
								args = append(args, "--username", username, "--password-stdin")
 | 
				
			||||||
 | 
								buffer := bytes.Buffer{}
 | 
				
			||||||
 | 
								buffer.Write([]byte(fmt.Sprintf("%s\n", password)))
 | 
				
			||||||
 | 
								out, err = helm.execStdIn(args, map[string]string{}, &buffer)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
			out, err = helm.exec(args, map[string]string{}, nil)
 | 
								out, err = helm.exec(args, map[string]string{}, nil)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		helm.logger.Errorf("ERROR: unknown type '%v' for repository %v", managed, name)
 | 
							helm.logger.Errorf("ERROR: unknown type '%v' for repository %v", managed, name)
 | 
				
			||||||
		out = nil
 | 
							out = nil
 | 
				
			||||||
		err = nil
 | 
							err = nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	helm.info(out)
 | 
						helm.info(out)
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -157,6 +157,9 @@ func Test_AddRepo(t *testing.T) {
 | 
				
			||||||
	var buffer bytes.Buffer
 | 
						var buffer bytes.Buffer
 | 
				
			||||||
	logger := NewLogger(&buffer, "debug")
 | 
						logger := NewLogger(&buffer, "debug")
 | 
				
			||||||
	helm := MockExecer(logger, "config", "dev")
 | 
						helm := MockExecer(logger, "config", "dev")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Test case with certfile and keyfile
 | 
				
			||||||
 | 
						buffer.Reset()
 | 
				
			||||||
	err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", false, false)
 | 
						err := helm.AddRepo("myRepo", "https://repo.example.com/", "", "cert.pem", "key.pem", "", "", "", false, false)
 | 
				
			||||||
	expected := `Adding repo myRepo https://repo.example.com/
 | 
						expected := `Adding repo myRepo https://repo.example.com/
 | 
				
			||||||
exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.example.com/ --cert-file cert.pem --key-file key.pem
 | 
					exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.example.com/ --cert-file cert.pem --key-file key.pem
 | 
				
			||||||
| 
						 | 
					@ -169,6 +172,7 @@ exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.e
 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Test case with cafile
 | 
				
			||||||
	buffer.Reset()
 | 
						buffer.Reset()
 | 
				
			||||||
	err = helm.AddRepo("myRepo", "https://repo.example.com/", "ca.crt", "", "", "", "", "", false, false)
 | 
						err = helm.AddRepo("myRepo", "https://repo.example.com/", "ca.crt", "", "", "", "", "", false, false)
 | 
				
			||||||
	expected = `Adding repo myRepo https://repo.example.com/
 | 
						expected = `Adding repo myRepo https://repo.example.com/
 | 
				
			||||||
| 
						 | 
					@ -182,6 +186,7 @@ exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.e
 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Test case with no certfile or cafile
 | 
				
			||||||
	buffer.Reset()
 | 
						buffer.Reset()
 | 
				
			||||||
	err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "", "", "", false, false)
 | 
						err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "", "", "", false, false)
 | 
				
			||||||
	expected = `Adding repo myRepo https://repo.example.com/
 | 
						expected = `Adding repo myRepo https://repo.example.com/
 | 
				
			||||||
| 
						 | 
					@ -195,6 +200,7 @@ exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.e
 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Test case with managed "acr"
 | 
				
			||||||
	buffer.Reset()
 | 
						buffer.Reset()
 | 
				
			||||||
	err = helm.AddRepo("acrRepo", "", "", "", "", "", "", "acr", false, false)
 | 
						err = helm.AddRepo("acrRepo", "", "", "", "", "", "", "acr", false, false)
 | 
				
			||||||
	expected = `Adding repo acrRepo (acr)
 | 
						expected = `Adding repo acrRepo (acr)
 | 
				
			||||||
| 
						 | 
					@ -209,6 +215,7 @@ exec: az acr helm repo add --name acrRepo:
 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Test case with unknown managed type
 | 
				
			||||||
	buffer.Reset()
 | 
						buffer.Reset()
 | 
				
			||||||
	err = helm.AddRepo("otherRepo", "", "", "", "", "", "", "unknown", false, false)
 | 
						err = helm.AddRepo("otherRepo", "", "", "", "", "", "", "unknown", false, false)
 | 
				
			||||||
	expected = `ERROR: unknown type 'unknown' for repository otherRepo
 | 
						expected = `ERROR: unknown type 'unknown' for repository otherRepo
 | 
				
			||||||
| 
						 | 
					@ -220,10 +227,11 @@ exec: az acr helm repo add --name acrRepo:
 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Test case with username and password (using password-stdin)
 | 
				
			||||||
	buffer.Reset()
 | 
						buffer.Reset()
 | 
				
			||||||
	err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", false, false)
 | 
						err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", false, false)
 | 
				
			||||||
	expected = `Adding repo myRepo https://repo.example.com/
 | 
						expected = `Adding repo myRepo https://repo.example.com/
 | 
				
			||||||
exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.example.com/ --username example_user --password example_password
 | 
					exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.example.com/ --username example_user --password-stdin
 | 
				
			||||||
`
 | 
					`
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
| 
						 | 
					@ -232,30 +240,22 @@ exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.e
 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	buffer.Reset()
 | 
						// Test case with username, password, and pass-credentials
 | 
				
			||||||
	err = helm.AddRepo("", "https://repo.example.com/", "", "", "", "", "", "", false, false)
 | 
					 | 
				
			||||||
	expected = `empty field name
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
`
 | 
					 | 
				
			||||||
	if err != nil && err.Error() != "empty field name" {
 | 
					 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if buffer.String() != expected {
 | 
					 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	buffer.Reset()
 | 
						buffer.Reset()
 | 
				
			||||||
	err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", true, false)
 | 
						err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "example_user", "example_password", "", true, false)
 | 
				
			||||||
	expected = `Adding repo myRepo https://repo.example.com/
 | 
						expected = `Adding repo myRepo https://repo.example.com/
 | 
				
			||||||
exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.example.com/ --username example_user --password example_password --pass-credentials
 | 
					exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.example.com/ --pass-credentials --username example_user --password-stdin
 | 
				
			||||||
`
 | 
					`
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if buffer.String() != expected {
 | 
					
 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
						actual := buffer.String()
 | 
				
			||||||
 | 
						if actual != expected {
 | 
				
			||||||
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", actual, expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Test case with skipTLSVerify
 | 
				
			||||||
	buffer.Reset()
 | 
						buffer.Reset()
 | 
				
			||||||
	err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "", "", "", false, true)
 | 
						err = helm.AddRepo("myRepo", "https://repo.example.com/", "", "", "", "", "", "", false, true)
 | 
				
			||||||
	expected = `Adding repo myRepo https://repo.example.com/
 | 
						expected = `Adding repo myRepo https://repo.example.com/
 | 
				
			||||||
| 
						 | 
					@ -264,8 +264,29 @@ exec: helm --kubeconfig config --kube-context dev repo add myRepo https://repo.e
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Errorf("unexpected error: %v", err)
 | 
							t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if buffer.String() != expected {
 | 
					
 | 
				
			||||||
		t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
						normalize := func(s string) string {
 | 
				
			||||||
 | 
							return strings.Join(strings.Fields(s), " ")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						actual = normalize(buffer.String())
 | 
				
			||||||
 | 
						expected = normalize(expected)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if actual != expected {
 | 
				
			||||||
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", actual, expected)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Test case with empty name
 | 
				
			||||||
 | 
						buffer.Reset()
 | 
				
			||||||
 | 
						err = helm.AddRepo("", "https://repo.example.com/", "", "", "", "", "", "", false, false)
 | 
				
			||||||
 | 
						expected = `empty field name`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err != nil && err.Error() != "empty field name" {
 | 
				
			||||||
 | 
							t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						actual = strings.TrimSpace(buffer.String())
 | 
				
			||||||
 | 
						if actual != expected {
 | 
				
			||||||
 | 
							t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", actual, expected)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -907,8 +928,15 @@ func Test_LogLevels(t *testing.T) {
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			t.Errorf("unexpected error: %v", err)
 | 
								t.Errorf("unexpected error: %v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if buffer.String() != expected {
 | 
					
 | 
				
			||||||
			t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", buffer.String(), expected)
 | 
							actual := buffer.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if strings.Contains(actual, "--password-stdin") {
 | 
				
			||||||
 | 
								expected = strings.Replace(expected, "--password example_password", "--password-stdin", 1)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if actual != expected {
 | 
				
			||||||
 | 
								t.Errorf("helmexec.AddRepo()\nactual = %v\nexpect = %v", actual, expected)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue