Skip to content

Commit 3c12667

Browse files
committed
feat(stores): import csv add support for credential input via flags and/or user interactive prompt.
Signed-off-by: spbsoluble <1661003+spbsoluble@users.noreply.github.com>
1 parent b66dedd commit 3c12667

1 file changed

Lines changed: 58 additions & 5 deletions

File tree

cmd/storesBulkOperations.go

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ var storesCreateFromCSVCmd = &cobra.Command{
117117
filePath, _ := cmd.Flags().GetString("file")
118118
outPath, _ := cmd.Flags().GetString("results-path")
119119
dryRun, _ := cmd.Flags().GetBool("dry-run")
120+
serverUsername, _ := cmd.Flags().GetString("server-username")
121+
serverPassword, _ := cmd.Flags().GetString("server-password")
122+
storePassword, _ := cmd.Flags().GetString("store-password")
120123

121124
//// Flag Checks
122125
//inputErr := storeTypeIdentifierFlagCheck(cmd)
@@ -259,6 +262,19 @@ var storesCreateFromCSVCmd = &cobra.Command{
259262

260263
errorCount := 0
261264

265+
if !noPrompt {
266+
promptCreds := promptForInteractiveYesNo("Input default credentials to use for certificate stores?")
267+
if promptCreds {
268+
outputResult("NOTE: Credentials provided in file will take precedence over prompts.", outputFormat)
269+
serverUsername = promptForInteractiveParameter("ServerUsername", serverUsername)
270+
log.Debug().Str("serverUsername", serverUsername).Msg("ServerUsername")
271+
serverPassword = promptForInteractivePassword("ServerPassword", serverPassword)
272+
log.Debug().Str("serverPassword", hashSecretValue(serverPassword)).Msg("ServerPassword")
273+
storePassword = promptForInteractivePassword("StorePassword", storePassword)
274+
log.Debug().Str("storePassword", hashSecretValue(storePassword)).Msg("StorePassword")
275+
}
276+
}
277+
262278
log.Info().Msgf("Processing CSV rows from file '%s'", filePath)
263279
for idx, row := range inFile {
264280
log.Debug().Msgf("Processing row '%d'", idx)
@@ -270,7 +286,6 @@ var storesCreateFromCSVCmd = &cobra.Command{
270286
continue
271287
}
272288
reqJson := getJsonForRequest(headerRow, row)
273-
274289
reqJson = formatProperties(reqJson, reqPropertiesForStoreType)
275290

276291
reqJson.Set(intID, "CertStoreType")
@@ -286,13 +301,29 @@ var storesCreateFromCSVCmd = &cobra.Command{
286301
// parse properties
287302
var createStoreReqParameters api.CreateStoreFctArgs
288303
props := unmarshalPropertiesString(reqJson.S("Properties").String())
289-
storePasswd := reqJson.S("Password").String()
304+
305+
//check if ServerUsername is present in the properties
306+
_, uOk := props["ServerUsername"]
307+
if !uOk && serverUsername != "" {
308+
props["ServerUsername"] = serverUsername
309+
}
310+
311+
_, pOk := props["ServerPassword"]
312+
if !pOk && serverPassword != "" {
313+
props["ServerPassword"] = serverPassword
314+
}
315+
316+
rowStorePassword := reqJson.S("Password").String()
290317
reqJson.Delete("Properties") // todo: why is this deleting the properties from the request json?
291318
var passwdParams *api.StorePasswordConfig
292-
if storePasswd != "" {
319+
if rowStorePassword != "" {
293320
reqJson.Delete("Password")
294321
passwdParams = &api.StorePasswordConfig{
295-
Value: &storePasswd,
322+
Value: &rowStorePassword,
323+
}
324+
} else {
325+
passwdParams = &api.StorePasswordConfig{
326+
Value: &storePassword,
296327
}
297328
}
298329
mJSON := reqJson.String()
@@ -311,7 +342,6 @@ var storesCreateFromCSVCmd = &cobra.Command{
311342
createStoreReqParameters.Properties = props
312343
log.Debug().Msgf("Request parameters: %v", createStoreReqParameters)
313344

314-
// make request.
315345
log.Info().Msgf("Calling Command to create store from row '%d'", idx)
316346
res, err := kfClient.CreateStore(&createStoreReqParameters)
317347

@@ -344,6 +374,7 @@ var storesCreateFromCSVCmd = &cobra.Command{
344374
Int("totalSuccess", totalSuccess).Send()
345375

346376
log.Info().Msgf("Writing results to file '%s'", outPath)
377+
347378
//writeCsvFile(outPath, originalMap)
348379
mapToCSV(inputMap, outPath)
349380
log.Info().Int("totalRows", totalRows).
@@ -1120,6 +1151,28 @@ func init() {
11201151
-1,
11211152
"The ID of the cert store type for the stores.",
11221153
)
1154+
storesCreateFromCSVCmd.Flags().StringVarP(
1155+
&storeTypeName,
1156+
"server-username",
1157+
"u",
1158+
"",
1159+
"The username Keyfactor Command will use to use connect to the certificate store host. This field can be specified in the CSV file in the column `Properties.ServerUsername`.",
1160+
)
1161+
storesCreateFromCSVCmd.Flags().StringVarP(
1162+
&storeTypeName,
1163+
"server-password",
1164+
"p",
1165+
"",
1166+
"The password Keyfactor Command will use to use connect to the certificate store host. This field can be specified in the CSV file in the column `Properties.ServerPassword`.",
1167+
)
1168+
storesCreateFromCSVCmd.Flags().StringVarP(
1169+
&storeTypeName,
1170+
"store-password",
1171+
"s",
1172+
"",
1173+
"The credential information Keyfactor Command will use to access the certificates in a specific certificate store (the store password). This is different from credential information Keyfactor Command uses to access a certificate store host. This field can be specified in the CSV file in the column `Password`.",
1174+
)
1175+
11231176
storesCreateFromCSVCmd.Flags().StringVarP(&file, "file", "f", "", "CSV file containing cert stores to create.")
11241177
storesCreateFromCSVCmd.MarkFlagRequired("file")
11251178
storesCreateFromCSVCmd.Flags().BoolP("dry-run", "d", false, "Do not import, just check for necessary fields.")

0 commit comments

Comments
 (0)