@@ -21,6 +21,7 @@ import (
21
21
22
22
// "github.com/Keyfactor/keyfactor-go-client-sdk/v24/api/keyfactor/v2"
23
23
"github.com/Keyfactor/keyfactor-go-client-sdk/v2/api/keyfactor"
24
+ "github.com/Keyfactor/keyfactor-go-client/v3/api"
24
25
"github.com/rs/zerolog/log"
25
26
"github.com/spf13/cobra"
26
27
)
@@ -217,9 +218,15 @@ var migratePamCmd = &cobra.Command{
217
218
InstanceLevel : & falsevalue ,
218
219
},
219
220
}
220
- // TODO: might need to explicit filter for CyberArk expected params, i.e. not map over Safe
221
+
221
222
// append filled out provider type parameter object, which contains the Provider-level parameter values
222
- migrationPamProvider .ProviderTypeParamValues = append (migrationPamProvider .ProviderTypeParamValues , providerLevelParameter )
223
+ // migrationPamProvider.ProviderTypeParamValues = append(migrationPamProvider.ProviderTypeParamValues, providerLevelParameter)
224
+
225
+ // TODO: need to explicit filter for CyberArk expected params, i.e. not map over Safe
226
+ // this needs to be done programatically for other provider types
227
+ if paramName == "AppId" {
228
+ migrationPamProvider .ProviderTypeParamValues = append (migrationPamProvider .ProviderTypeParamValues , providerLevelParameter )
229
+ }
223
230
}
224
231
}
225
232
@@ -241,16 +248,31 @@ var migratePamCmd = &cobra.Command{
241
248
// providertypeparam should be set to all matching values from GET TYPES
242
249
// ignoring datatype
243
250
244
- //
245
- // TODO: POST PAM PROVIDER
246
- //
251
+ createdPamProvider , httpResponse , rErr := sdkClient .PAMProviderApi .PAMProviderCreatePamProvider (context .Background ()).
252
+ Provider (migrationPamProvider ).
253
+ XKeyfactorRequestedWith (XKeyfactorRequestedWith ).XKeyfactorApiVersion (XKeyfactorApiVersion ).
254
+ Execute ()
255
+
256
+ if rErr != nil {
257
+ log .Error ().Err (rErr ).Send ()
258
+ return returnHttpErr (httpResponse , rErr )
259
+ }
260
+
261
+ fmt .Println ("vvv CREATED MIGRATION PAM PROVIDER vvv" )
262
+ jobject , _ = json .MarshalIndent (createdPamProvider , "" , " " )
263
+ fmt .Println (string (jobject ))
264
+ fmt .Println ("^^^ CREATED MIGRATION PAM PROVIDER ^^^" )
247
265
248
266
// foreach store GUID pass in as a parameter-----
249
267
// GET Store by GUID (instance GUID matches Store Id GUID)
250
268
// output some store info to confirm
251
269
252
- // TODO: assign error and check
253
- certStore , _ := legacyClient .GetCertificateStoreByID (storeUsingPam )
270
+ // TODO: use updated client when API endpoint available
271
+ certStore , rErr := legacyClient .GetCertificateStoreByID (storeUsingPam )
272
+ if rErr != nil {
273
+ log .Error ().Err (rErr ).Send ()
274
+ return rErr
275
+ }
254
276
255
277
jobject , _ = json .MarshalIndent (certStore , "" , " " )
256
278
fmt .Println (string (jobject ))
@@ -277,11 +299,11 @@ var migratePamCmd = &cobra.Command{
277
299
278
300
// check if Pam Secret is using our migrating provider
279
301
if * fromPamProvider .Id == int32 (propSecret ["ProviderId" ].(float64 )) {
302
+ // Pam Secret that Needs to be migrated
303
+ formattedSecret ["Value" ] = buildMigratedPamSecret (propSecret , fromProviderLevelParamValues , * createdPamProvider .Id )
304
+ } else {
280
305
// reformat to required POST format for properties
281
306
formattedSecret ["Value" ] = reformatPamSecretForPost (propSecret )
282
- } else {
283
- // Pam Secret that Needs to be migrated
284
- formattedSecret ["Value" ] = buildMigratedPamSecret (propSecret , fromProviderLevelParamValues , 0 )
285
307
}
286
308
} else {
287
309
// non-managed secret i.e. a KF-encrypted secret, or no value
@@ -293,32 +315,42 @@ var migratePamCmd = &cobra.Command{
293
315
294
316
// update Properties object with newly formatted secret, compliant with POST requirements
295
317
certStore .Properties [propName ] = formattedSecret
296
-
297
- jobject , _ = json .MarshalIndent (certStore .Properties , "" , " " )
298
- fmt .Println (string (jobject ))
299
- fmt .Println ("^^^ SECRETS REFORMATTED ^^^" )
300
318
}
301
319
}
302
320
303
- return nil
321
+ jobject , _ = json .MarshalIndent (certStore .Properties , "" , " " )
322
+ fmt .Println (string (jobject ))
323
+ fmt .Println ("^^^ SECRETS REFORMATTED ^^^" )
324
+
325
+ // propertiesAsString, _ := json.Marshal(certStore.Properties)
326
+ // jsonProps := string(propertiesAsString)
327
+ // escapedProps := strings.ReplaceAll(jsonProps, "\"", "\\\"")
328
+ // fmt.Println(escapedProps)
304
329
305
330
// update property object
306
- // foreach ProviderTypeParameterValues
307
- // where ProviderTypeParam.Name = first map key (map is map[fieldname]map[InstanceGuid]value)
308
- // create new PAM value for this secret
309
- // json object:
310
- // value: {
311
- // provider: integer id of new provider
312
- // Parameters: {
313
- // fieldname: new value
314
- // }}
315
- //
316
- // leave all other fields untouched
317
- // IMPORTANT: other secret fields need to match value:{secretvalue:"*****" or secretvalue:null}
318
-
319
- // marshal json back to string for Properties field
320
- // make sure quotes are escaped
321
- // submit PUT for updating Store definition
331
+ // set required fields, and new Properties
332
+ updateStoreArgs := api.UpdateStoreFctArgs {
333
+ Id : certStore .Id ,
334
+ ClientMachine : certStore .ClientMachine ,
335
+ StorePath : certStore .StorePath ,
336
+ AgentId : certStore .AgentId ,
337
+ Properties : certStore .Properties ,
338
+ Password : & certStore .Password ,
339
+ }
340
+
341
+ // TODO: use updated client when API endpoint available
342
+ updatedStore , rErr := legacyClient .UpdateStore (& updateStoreArgs )
343
+
344
+ if rErr != nil {
345
+ log .Error ().Err (rErr ).Send ()
346
+ return rErr
347
+ }
348
+
349
+ jobject , _ = json .MarshalIndent (updatedStore , "" , " " )
350
+ fmt .Println (string (jobject ))
351
+ fmt .Println ("^^^ UPDATED STORE ^^^" )
352
+
353
+ return nil
322
354
},
323
355
}
324
356
@@ -386,7 +418,7 @@ func buildMigratedPamSecret(secretProp map[string]interface{}, fromProviderLevel
386
418
// TODO: this logic needs to not be hard-coded, and evaluated for actual migrations other than legacy CyberArk
387
419
reformattedParams ["Safe" ] = fromProviderLevelValues ["Safe" ]
388
420
389
- migrated ["Properties " ] = reformattedParams
421
+ migrated ["Parameters " ] = reformattedParams
390
422
391
423
return migrated
392
424
}
0 commit comments