Skip to content

Commit ecb60cb

Browse files
committed
fix: preserve JSON secret values in store CSV import
1 parent 414394b commit ecb60cb

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

cmd/storesBulkOperations.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,9 @@ func getJsonForRequest(headerRow []string, row []string) *gabs.Container {
11541154
reqJson := gabs.New()
11551155
for hIdx, header := range headerRow {
11561156
log.Debug().Msgf("Processing header '%s'", header)
1157-
if strings.ToUpper(row[hIdx]) == "TRUE" {
1157+
if shouldTreatCSVValueAsSecretString(header) && row[hIdx] != "" {
1158+
reqJson.Set(row[hIdx], strings.Split(header, ".")...)
1159+
} else if strings.ToUpper(row[hIdx]) == "TRUE" {
11581160
reqJson.Set(true, strings.Split(header, ".")...)
11591161
} else if strings.ToUpper(row[hIdx]) == "FALSE" {
11601162
reqJson.Set(false, strings.Split(header, ".")...)
@@ -1176,6 +1178,15 @@ func getJsonForRequest(headerRow []string, row []string) *gabs.Container {
11761178
return reqJson
11771179
}
11781180

1181+
func shouldTreatCSVValueAsSecretString(header string) bool {
1182+
switch header {
1183+
case "Properties.ServerUsername", "Properties.ServerPassword", "Password":
1184+
return true
1185+
default:
1186+
return strings.HasSuffix(header, ".SecretValue")
1187+
}
1188+
}
1189+
11791190
func writeCsvFile(outpath string, rows [][]string) error {
11801191
log.Debug().Msgf("Writing CSV file '%s'", outpath)
11811192
csvFile, err := os.Create(outpath)

cmd/stores_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,19 @@ func Test_FormatProperties_FormatsManagedPamSecretPropertiesForPost(t *testing.T
356356
assert.Equal(t, " ", params["StaticSecretFieldName"])
357357
}
358358

359+
func Test_GetJsonForRequest_TreatsJsonSecretValuesAsStrings(t *testing.T) {
360+
header := []string{"Properties.ServerPassword", "Properties.ServerUsername.SecretValue"}
361+
row := []string{
362+
`{"kind":"Config","apiVersion":"v1"}`,
363+
`{"username":"kubeconfig"}`,
364+
}
365+
366+
reqJson := getJsonForRequest(header, row)
367+
368+
assert.Equal(t, row[0], reqJson.S("Properties", "ServerPassword").Data())
369+
assert.Equal(t, row[1], reqJson.S("Properties", "ServerUsername", "SecretValue").Data())
370+
}
371+
359372
func testExportStore(t *testing.T, storeTypeName string) (string, []string) {
360373
var (
361374
output string

0 commit comments

Comments
 (0)