trim values from envrionment files (#13991)

trim values to remove any spaces, newlines
from the files while importing credentials
and other values.
This commit is contained in:
Harshavardhana 2021-12-25 22:02:54 -08:00 committed by GitHub
parent b883803b21
commit c980804514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 2 deletions

View File

@ -19,6 +19,7 @@ package cmd
import ( import (
"bufio" "bufio"
"bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
@ -456,7 +457,8 @@ func (e envKV) String() string {
} }
func parsEnvEntry(envEntry string) (envKV, error) { func parsEnvEntry(envEntry string) (envKV, error) {
if strings.TrimSpace(envEntry) == "" { envEntry = strings.TrimSpace(envEntry)
if envEntry == "" {
// Skip all empty lines // Skip all empty lines
return envKV{ return envKV{
Skip: true, Skip: true,
@ -529,7 +531,7 @@ func readFromSecret(sp string) (string, error) {
} }
return "", err return "", err
} }
return string(credBuf), nil return string(bytes.TrimSpace(credBuf)), nil
} }
func loadEnvVarsFromFiles() { func loadEnvVarsFromFiles() {

View File

@ -24,6 +24,49 @@ import (
"testing" "testing"
) )
func Test_readFromSecret(t *testing.T) {
testCases := []struct {
content string
expectedErr bool
expectedValue string
}{
{
"value\n",
false,
"value",
},
{
" \t\n Hello, Gophers \n\t\r\n",
false,
"Hello, Gophers",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run("", func(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "testfile")
if err != nil {
t.Error(err)
}
tmpfile.WriteString(testCase.content)
tmpfile.Sync()
tmpfile.Close()
value, err := readFromSecret(tmpfile.Name())
if err != nil && !testCase.expectedErr {
t.Error(err)
}
if err == nil && testCase.expectedErr {
t.Error(errors.New("expected error, found success"))
}
if value != testCase.expectedValue {
t.Errorf("Expected %s, got %s", testCase.expectedValue, value)
}
})
}
}
func Test_minioEnvironFromFile(t *testing.T) { func Test_minioEnvironFromFile(t *testing.T) {
testCases := []struct { testCases := []struct {
content string content string