@@ -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
@@ -109,14 +109,32 @@ var storesCreateFromCSVCmd = &cobra.Command{
109109 Long : `Certificate stores: Will parse a CSV and attempt to create a certificate store for each row with the provided parameters.
110110'store-type-name' OR 'store-type-id' are required.
111111'file' is the path to the file to be imported.
112- 'resultspath' is where the import results will be written to.` ,
112+ 'resultspath' is where the import results will be written to.
113+ *NOTE*: If you do not wish to include credentials in your CSV file they can be provided one of three ways:
114+ - via the --server-username --server-password and --store-password flags
115+ - via environment variables: KFUTIL_CSV_SERVER_USERNAME, KFUTIL_CSV_SERVER_PASSWORD, KFUTIL_CSV_STORE_PASSWORD
116+ - via interactive prompts
117+ ` ,
113118 RunE : func (cmd * cobra.Command , args []string ) error {
114119 // Specific flags
115120 storeTypeName , _ := cmd .Flags ().GetString ("store-type-name" )
116121 storeTypeID , _ := cmd .Flags ().GetInt ("store-type-id" )
117122 filePath , _ := cmd .Flags ().GetString ("file" )
118123 outPath , _ := cmd .Flags ().GetString ("results-path" )
119124 dryRun , _ := cmd .Flags ().GetBool ("dry-run" )
125+ serverUsername , _ := cmd .Flags ().GetString ("server-username" )
126+ serverPassword , _ := cmd .Flags ().GetString ("server-password" )
127+ storePassword , _ := cmd .Flags ().GetString ("store-password" )
128+
129+ if serverUsername == "" {
130+ serverUsername = os .Getenv (EnvStoresImportCSVServerUsername )
131+ }
132+ if serverPassword == "" {
133+ serverPassword = os .Getenv (EnvStoresImportCSVServerPassword )
134+ }
135+ if storePassword == "" {
136+ storePassword = os .Getenv (EnvStoresImportCSVStorePassword )
137+ }
120138
121139 //// Flag Checks
122140 //inputErr := storeTypeIdentifierFlagCheck(cmd)
@@ -259,6 +277,19 @@ var storesCreateFromCSVCmd = &cobra.Command{
259277
260278 errorCount := 0
261279
280+ if ! noPrompt {
281+ promptCreds := promptForInteractiveYesNo ("Input default credentials to use for certificate stores?" )
282+ if promptCreds {
283+ outputResult ("NOTE: Credentials provided in file will take precedence over prompts." , outputFormat )
284+ serverUsername = promptForInteractiveParameter ("ServerUsername" , serverUsername )
285+ log .Debug ().Str ("serverUsername" , serverUsername ).Msg ("ServerUsername" )
286+ serverPassword = promptForInteractivePassword ("ServerPassword" , serverPassword )
287+ log .Debug ().Str ("serverPassword" , hashSecretValue (serverPassword )).Msg ("ServerPassword" )
288+ storePassword = promptForInteractivePassword ("StorePassword" , storePassword )
289+ log .Debug ().Str ("storePassword" , hashSecretValue (storePassword )).Msg ("StorePassword" )
290+ }
291+ }
292+
262293 log .Info ().Msgf ("Processing CSV rows from file '%s'" , filePath )
263294 for idx , row := range inFile {
264295 log .Debug ().Msgf ("Processing row '%d'" , idx )
@@ -270,7 +301,6 @@ var storesCreateFromCSVCmd = &cobra.Command{
270301 continue
271302 }
272303 reqJson := getJsonForRequest (headerRow , row )
273-
274304 reqJson = formatProperties (reqJson , reqPropertiesForStoreType )
275305
276306 reqJson .Set (intID , "CertStoreType" )
@@ -286,7 +316,31 @@ var storesCreateFromCSVCmd = &cobra.Command{
286316 // parse properties
287317 var createStoreReqParameters api.CreateStoreFctArgs
288318 props := unmarshalPropertiesString (reqJson .S ("Properties" ).String ())
319+
320+ //check if ServerUsername is present in the properties
321+ _ , uOk := props ["ServerUsername" ]
322+ if ! uOk && serverUsername != "" {
323+ props ["ServerUsername" ] = serverUsername
324+ }
325+
326+ _ , pOk := props ["ServerPassword" ]
327+ if ! pOk && serverPassword != "" {
328+ props ["ServerPassword" ] = serverPassword
329+ }
330+
331+ rowStorePassword := reqJson .S ("Password" ).String ()
289332 reqJson .Delete ("Properties" ) // todo: why is this deleting the properties from the request json?
333+ var passwdParams * api.StorePasswordConfig
334+ if rowStorePassword != "" {
335+ reqJson .Delete ("Password" )
336+ passwdParams = & api.StorePasswordConfig {
337+ Value : & rowStorePassword ,
338+ }
339+ } else {
340+ passwdParams = & api.StorePasswordConfig {
341+ Value : & storePassword ,
342+ }
343+ }
290344 mJSON := reqJson .String ()
291345 conversionError := json .Unmarshal ([]byte (mJSON ), & createStoreReqParameters )
292346
@@ -299,10 +353,10 @@ var storesCreateFromCSVCmd = &cobra.Command{
299353 return conversionError
300354 }
301355
356+ createStoreReqParameters .Password = passwdParams
302357 createStoreReqParameters .Properties = props
303358 log .Debug ().Msgf ("Request parameters: %v" , createStoreReqParameters )
304359
305- // make request.
306360 log .Info ().Msgf ("Calling Command to create store from row '%d'" , idx )
307361 res , err := kfClient .CreateStore (& createStoreReqParameters )
308362
@@ -335,6 +389,7 @@ var storesCreateFromCSVCmd = &cobra.Command{
335389 Int ("totalSuccess" , totalSuccess ).Send ()
336390
337391 log .Info ().Msgf ("Writing results to file '%s'" , outPath )
392+
338393 //writeCsvFile(outPath, originalMap)
339394 mapToCSV (inputMap , outPath )
340395 log .Info ().Int ("totalRows" , totalRows ).
@@ -1111,6 +1166,39 @@ func init() {
11111166 - 1 ,
11121167 "The ID of the cert store type for the stores." ,
11131168 )
1169+ storesCreateFromCSVCmd .Flags ().StringVarP (
1170+ & storeTypeName ,
1171+ "server-username" ,
1172+ "u" ,
1173+ "" ,
1174+ "The username Keyfactor Command will use to use connect to the certificate store host. " +
1175+ "This field can be specified in the CSV file in the column `Properties.ServerUsername`. " +
1176+ "This value can also be sourced from the environmental variable `KFUTIL_CSV_SERVER_USERNAME`. " +
1177+ "*NOTE* a value provided in the CSV file will override any other input value" ,
1178+ )
1179+ storesCreateFromCSVCmd .Flags ().StringVarP (
1180+ & storeTypeName ,
1181+ "server-password" ,
1182+ "p" ,
1183+ "" ,
1184+ "The password Keyfactor Command will use to use connect to the certificate store host. " +
1185+ "This field can be specified in the CSV file in the column `Properties.ServerPassword`. " +
1186+ "This value can also be sourced from the environmental variable `KFUTIL_CSV_SERVER_PASSWORD`. " +
1187+ "*NOTE* a value provided in the CSV file will override any other input value" ,
1188+ )
1189+ storesCreateFromCSVCmd .Flags ().StringVarP (
1190+ & storeTypeName ,
1191+ "store-password" ,
1192+ "s" ,
1193+ "" ,
1194+ "The credential information Keyfactor Command will use to access the certificates in a specific certificate" +
1195+ " store (the store password). This is different from credential information Keyfactor Command uses to" +
1196+ " access a certificate store host." +
1197+ " This field can be specified in the CSV file in the column `Password`. This value can also be sourced from" +
1198+ " the environmental variable `KFUTIL_CSV_STORE_PASSWORD`. *NOTE* a value provided in the CSV file will" +
1199+ " override any other input value" ,
1200+ )
1201+
11141202 storesCreateFromCSVCmd .Flags ().StringVarP (& file , "file" , "f" , "" , "CSV file containing cert stores to create." )
11151203 storesCreateFromCSVCmd .MarkFlagRequired ("file" )
11161204 storesCreateFromCSVCmd .Flags ().BoolP ("dry-run" , "d" , false , "Do not import, just check for necessary fields." )
0 commit comments