@@ -99,7 +99,7 @@ func serializeStoreFromTypeDef(storeTypeName string, input string) (string, erro
9999
100100var importStoresCmd = & cobra.Command {
101101 Use : "import" ,
102- Short : "Import a file with certificate store parameters and create them in keyfactor ." ,
102+ Short : "Import a file with certificate store definitions and create them in Keyfactor Command ." ,
103103 Long : `Tools for generating import templates and importing certificate stores` ,
104104}
105105
@@ -117,6 +117,19 @@ 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" )
123+
124+ if serverUsername == "" {
125+ serverUsername = os .Getenv (EnvStoresImportCSVServerUsername )
126+ }
127+ if serverPassword == "" {
128+ serverPassword = os .Getenv (EnvStoresImportCSVServerPassword )
129+ }
130+ if storePassword == "" {
131+ storePassword = os .Getenv (EnvStoresImportCSVStorePassword )
132+ }
120133
121134 //// Flag Checks
122135 //inputErr := storeTypeIdentifierFlagCheck(cmd)
@@ -259,6 +272,19 @@ var storesCreateFromCSVCmd = &cobra.Command{
259272
260273 errorCount := 0
261274
275+ if ! noPrompt {
276+ promptCreds := promptForInteractiveYesNo ("Input default credentials to use for certificate stores?" )
277+ if promptCreds {
278+ outputResult ("NOTE: Credentials provided in file will take precedence over prompts." , outputFormat )
279+ serverUsername = promptForInteractiveParameter ("ServerUsername" , serverUsername )
280+ log .Debug ().Str ("serverUsername" , serverUsername ).Msg ("ServerUsername" )
281+ serverPassword = promptForInteractivePassword ("ServerPassword" , serverPassword )
282+ log .Debug ().Str ("serverPassword" , hashSecretValue (serverPassword )).Msg ("ServerPassword" )
283+ storePassword = promptForInteractivePassword ("StorePassword" , storePassword )
284+ log .Debug ().Str ("storePassword" , hashSecretValue (storePassword )).Msg ("StorePassword" )
285+ }
286+ }
287+
262288 log .Info ().Msgf ("Processing CSV rows from file '%s'" , filePath )
263289 for idx , row := range inFile {
264290 log .Debug ().Msgf ("Processing row '%d'" , idx )
@@ -270,7 +296,6 @@ var storesCreateFromCSVCmd = &cobra.Command{
270296 continue
271297 }
272298 reqJson := getJsonForRequest (headerRow , row )
273-
274299 reqJson = formatProperties (reqJson , reqPropertiesForStoreType )
275300
276301 reqJson .Set (intID , "CertStoreType" )
@@ -286,7 +311,31 @@ var storesCreateFromCSVCmd = &cobra.Command{
286311 // parse properties
287312 var createStoreReqParameters api.CreateStoreFctArgs
288313 props := unmarshalPropertiesString (reqJson .S ("Properties" ).String ())
314+
315+ //check if ServerUsername is present in the properties
316+ _ , uOk := props ["ServerUsername" ]
317+ if ! uOk && serverUsername != "" {
318+ props ["ServerUsername" ] = serverUsername
319+ }
320+
321+ _ , pOk := props ["ServerPassword" ]
322+ if ! pOk && serverPassword != "" {
323+ props ["ServerPassword" ] = serverPassword
324+ }
325+
326+ rowStorePassword := reqJson .S ("Password" ).String ()
289327 reqJson .Delete ("Properties" ) // todo: why is this deleting the properties from the request json?
328+ var passwdParams * api.StorePasswordConfig
329+ if rowStorePassword != "" {
330+ reqJson .Delete ("Password" )
331+ passwdParams = & api.StorePasswordConfig {
332+ Value : & rowStorePassword ,
333+ }
334+ } else {
335+ passwdParams = & api.StorePasswordConfig {
336+ Value : & storePassword ,
337+ }
338+ }
290339 mJSON := reqJson .String ()
291340 conversionError := json .Unmarshal ([]byte (mJSON ), & createStoreReqParameters )
292341
@@ -299,10 +348,10 @@ var storesCreateFromCSVCmd = &cobra.Command{
299348 return conversionError
300349 }
301350
351+ createStoreReqParameters .Password = passwdParams
302352 createStoreReqParameters .Properties = props
303353 log .Debug ().Msgf ("Request parameters: %v" , createStoreReqParameters )
304354
305- // make request.
306355 log .Info ().Msgf ("Calling Command to create store from row '%d'" , idx )
307356 res , err := kfClient .CreateStore (& createStoreReqParameters )
308357
@@ -335,6 +384,7 @@ var storesCreateFromCSVCmd = &cobra.Command{
335384 Int ("totalSuccess" , totalSuccess ).Send ()
336385
337386 log .Info ().Msgf ("Writing results to file '%s'" , outPath )
387+
338388 //writeCsvFile(outPath, originalMap)
339389 mapToCSV (inputMap , outPath )
340390 log .Info ().Int ("totalRows" , totalRows ).
@@ -1111,6 +1161,39 @@ func init() {
11111161 - 1 ,
11121162 "The ID of the cert store type for the stores." ,
11131163 )
1164+ storesCreateFromCSVCmd .Flags ().StringVarP (
1165+ & storeTypeName ,
1166+ "server-username" ,
1167+ "u" ,
1168+ "" ,
1169+ "The username Keyfactor Command will use to use connect to the certificate store host. " +
1170+ "This field can be specified in the CSV file in the column `Properties.ServerUsername`. " +
1171+ "This value can also be sourced from the environmental variable `KFUTIL_CSV_SERVER_USERNAME`. " +
1172+ "*NOTE* a value provided in the CSV file will override any other input value" ,
1173+ )
1174+ storesCreateFromCSVCmd .Flags ().StringVarP (
1175+ & storeTypeName ,
1176+ "server-password" ,
1177+ "p" ,
1178+ "" ,
1179+ "The password Keyfactor Command will use to use connect to the certificate store host. " +
1180+ "This field can be specified in the CSV file in the column `Properties.ServerPassword`. " +
1181+ "This value can also be sourced from the environmental variable `KFUTIL_CSV_SERVER_PASSWORD`. " +
1182+ "*NOTE* a value provided in the CSV file will override any other input value" ,
1183+ )
1184+ storesCreateFromCSVCmd .Flags ().StringVarP (
1185+ & storeTypeName ,
1186+ "store-password" ,
1187+ "s" ,
1188+ "" ,
1189+ "The credential information Keyfactor Command will use to access the certificates in a specific certificate" +
1190+ " store (the store password). This is different from credential information Keyfactor Command uses to" +
1191+ " access a certificate store host." +
1192+ " This field can be specified in the CSV file in the column `Password`. This value can also be sourced from" +
1193+ " the environmental variable `KFUTIL_CSV_STORE_PASSWORD`. *NOTE* a value provided in the CSV file will" +
1194+ " override any other input value" ,
1195+ )
1196+
11141197 storesCreateFromCSVCmd .Flags ().StringVarP (& file , "file" , "f" , "" , "CSV file containing cert stores to create." )
11151198 storesCreateFromCSVCmd .MarkFlagRequired ("file" )
11161199 storesCreateFromCSVCmd .Flags ().BoolP ("dry-run" , "d" , false , "Do not import, just check for necessary fields." )
0 commit comments