Skip to content

Commit 2d4cfbd

Browse files
committed
feat(migrate): perform data operations to migrate legacy CyberArk PAM usage
1 parent d3fe406 commit 2d4cfbd

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

cmd/migrate.go

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
// "github.com/Keyfactor/keyfactor-go-client-sdk/v24/api/keyfactor/v2"
2323
"github.com/Keyfactor/keyfactor-go-client-sdk/v2/api/keyfactor"
24+
"github.com/Keyfactor/keyfactor-go-client/v3/api"
2425
"github.com/rs/zerolog/log"
2526
"github.com/spf13/cobra"
2627
)
@@ -217,9 +218,15 @@ var migratePamCmd = &cobra.Command{
217218
InstanceLevel: &falsevalue,
218219
},
219220
}
220-
// TODO: might need to explicit filter for CyberArk expected params, i.e. not map over Safe
221+
221222
// 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+
}
223230
}
224231
}
225232

@@ -241,16 +248,31 @@ var migratePamCmd = &cobra.Command{
241248
// providertypeparam should be set to all matching values from GET TYPES
242249
// ignoring datatype
243250

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 ^^^")
247265

248266
// foreach store GUID pass in as a parameter-----
249267
// GET Store by GUID (instance GUID matches Store Id GUID)
250268
// output some store info to confirm
251269

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+
}
254276

255277
jobject, _ = json.MarshalIndent(certStore, "", " ")
256278
fmt.Println(string(jobject))
@@ -277,11 +299,11 @@ var migratePamCmd = &cobra.Command{
277299

278300
// check if Pam Secret is using our migrating provider
279301
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 {
280305
// reformat to required POST format for properties
281306
formattedSecret["Value"] = reformatPamSecretForPost(propSecret)
282-
} else {
283-
// Pam Secret that Needs to be migrated
284-
formattedSecret["Value"] = buildMigratedPamSecret(propSecret, fromProviderLevelParamValues, 0)
285307
}
286308
} else {
287309
// non-managed secret i.e. a KF-encrypted secret, or no value
@@ -293,32 +315,42 @@ var migratePamCmd = &cobra.Command{
293315

294316
// update Properties object with newly formatted secret, compliant with POST requirements
295317
certStore.Properties[propName] = formattedSecret
296-
297-
jobject, _ = json.MarshalIndent(certStore.Properties, "", " ")
298-
fmt.Println(string(jobject))
299-
fmt.Println("^^^ SECRETS REFORMATTED ^^^")
300318
}
301319
}
302320

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)
304329

305330
// 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
322354
},
323355
}
324356

@@ -386,7 +418,7 @@ func buildMigratedPamSecret(secretProp map[string]interface{}, fromProviderLevel
386418
// TODO: this logic needs to not be hard-coded, and evaluated for actual migrations other than legacy CyberArk
387419
reformattedParams["Safe"] = fromProviderLevelValues["Safe"]
388420

389-
migrated["Properties"] = reformattedParams
421+
migrated["Parameters"] = reformattedParams
390422

391423
return migrated
392424
}

0 commit comments

Comments
 (0)