From ff3a9d5c6a109bfeb78b929c642835fb81c96867 Mon Sep 17 00:00:00 2001 From: asmith236 Date: Wed, 8 Feb 2023 09:38:29 -0700 Subject: [PATCH 1/8] modified API function calls to work with new wrapper --- cmd/inventory.go | 14 +++++++------- cmd/rot.go | 3 +-- cmd/stores.go | 6 ++---- go.mod | 3 ++- go.sum | 14 ++++++++++++++ 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/cmd/inventory.go b/cmd/inventory.go index 47068e8..ce0719b 100644 --- a/cmd/inventory.go +++ b/cmd/inventory.go @@ -80,7 +80,7 @@ var inventoryClearCmd = &cobra.Command{ sTypeLookup := make(map[string]bool) if !allStores { - allStoresResponse, _ := kfClient.ListCertificateStores(nil) + allStoresResponse, _ := kfClient.ListCertificateStores() //nil for _, store := range *allStoresResponse { sTypeName, stErr := kfClient.GetCertificateStoreTypeById(store.CertStoreType) sTypeLookup[sTypeName.ShortName] = true @@ -93,7 +93,7 @@ var inventoryClearCmd = &cobra.Command{ } } } else { - allStoresResp, fErr := kfClient.ListCertificateStores(nil) + allStoresResp, fErr := kfClient.ListCertificateStores() if fErr != nil { fmt.Printf("Error listing certificate stores: %s\n", fErr) log.Fatal(fErr) @@ -265,7 +265,7 @@ attempt to add all the certificate(s) meeting the specified criteria to all stor sTypeLookup := make(map[string]bool) if !allStores { - allStoresResponse, _ := kfClient.ListCertificateStores(nil) + allStoresResponse, _ := kfClient.ListCertificateStores() for _, store := range *allStoresResponse { sTypeName, stErr := kfClient.GetCertificateStoreTypeById(store.CertStoreType) sTypeLookup[sTypeName.ShortName] = true @@ -277,7 +277,7 @@ attempt to add all the certificate(s) meeting the specified criteria to all stor } } } else { - allStoresResp, fErr := kfClient.ListCertificateStores(nil) + allStoresResp, fErr := kfClient.ListCertificateStores() if fErr != nil { fmt.Printf("Error getting listing certificate stores: %s", fErr) log.Fatal(fErr) @@ -418,7 +418,7 @@ var inventoryRemoveCmd = &cobra.Command{ sTypeLookup := make(map[string]bool) if !allStores { - allStoresResponse, _ := kfClient.ListCertificateStores(nil) + allStoresResponse, _ := kfClient.ListCertificateStores() for _, store := range *allStoresResponse { sTypeName, stErr := kfClient.GetCertificateStoreTypeById(store.CertStoreType) sTypeLookup[sTypeName.ShortName] = true @@ -431,7 +431,7 @@ var inventoryRemoveCmd = &cobra.Command{ } } } else { - allStoresResp, fErr := kfClient.ListCertificateStores(nil) + allStoresResp, fErr := kfClient.ListCertificateStores() if fErr != nil { fmt.Printf("Error listing certificate stores: %s\n", fErr) log.Fatal(fErr) @@ -540,7 +540,7 @@ var inventoryShowCmd = &cobra.Command{ for _, s := range storeIDs { params["Id"] = append(params["Id"].([]string), s) } - stResp, err := kfClient.ListCertificateStores(¶ms) + stResp, err := kfClient.ListCertificateStores() if err != nil { fmt.Println("Error, unable to list certificate stores. ", err) log.Printf("[ERROR] Unable to list certificate stores: %s\n", err) diff --git a/cmd/rot.go b/cmd/rot.go index fb1c6f8..667acd8 100644 --- a/cmd/rot.go +++ b/cmd/rot.go @@ -879,8 +879,7 @@ the utility will first generate an audit report and then execute the add/remove if stID >= 0 || s == "all" { log.Printf("[DEBUG] Store type ID: %d\n", stID) - params := map[string]interface{}{} - stores, sErr := kfClient.ListCertificateStores(¶ms) + stores, sErr := kfClient.ListCertificateStores() if sErr != nil { fmt.Printf("[ERROR] getting certificate stores of type '%s': %s\n", s, sErr) log.Fatalf("[ERROR] getting certificate stores of type '%s': %s", s, sErr) diff --git a/cmd/stores.go b/cmd/stores.go index 794018c..2df2687 100644 --- a/cmd/stores.go +++ b/cmd/stores.go @@ -9,10 +9,9 @@ package cmd import ( "encoding/json" "fmt" + "github.com/spf13/cobra" "io" "log" - - "github.com/spf13/cobra" ) // storesCmd represents the stores command @@ -32,8 +31,7 @@ var storesListCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { log.SetOutput(io.Discard) kfClient, _ := initClient() - params := map[string]interface{}{} - stores, err := kfClient.ListCertificateStores(¶ms) + stores, err := kfClient.ListCertificateStores() if err != nil { log.Printf("Error: %s", err) } diff --git a/go.mod b/go.mod index bcfc8ca..fb1fd6e 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,13 @@ go 1.19 require ( github.com/Jeffail/gabs/v2 v2.6.1 - github.com/Keyfactor/keyfactor-go-client v1.3.1 + github.com/Keyfactor/keyfactor-go-client v1.2.1-0.20230207180411-19f63f6d48b8 github.com/spf13/cobra v1.6.1 golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 ) require ( + github.com/Keyfactor/keyfactor-go-client-sdk v0.0.0-20230207174129-9d99e523eb15 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect diff --git a/go.sum b/go.sum index 92aadea..cb1aceb 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,18 @@ github.com/Jeffail/gabs/v2 v2.6.1 h1:wwbE6nTQTwIMsMxzi6XFQQYRZ6wDc1mSdxoAN+9U4Gk= github.com/Jeffail/gabs/v2 v2.6.1/go.mod h1:xCn81vdHKxFUuWWAaD5jCTQDNPBMh5pPs9IJ+NcziBI= +github.com/Keyfactor/keyfactor-go-client v1.2.1-0.20230207180411-19f63f6d48b8 h1:+5bZhfUjt+3t9mwLGkGqdBI5kxB85UHsx9dqbTPqfXU= +github.com/Keyfactor/keyfactor-go-client v1.2.1-0.20230207180411-19f63f6d48b8/go.mod h1:qG9ZJTt0o5YPV2+QW4Gb9Dub9dnwQvx+G48QUVfjKRA= github.com/Keyfactor/keyfactor-go-client v1.3.1 h1:PACXm/8ZtxGzSSZ0d+043ntMbHOMS7bi46zX2fn8Yq4= github.com/Keyfactor/keyfactor-go-client v1.3.1/go.mod h1:u1M1AjcwiO/Tbvc7EsNl9YTy757hO5wmey1/W/7Qkbs= +github.com/Keyfactor/keyfactor-go-client-sdk v0.0.0-20230207174129-9d99e523eb15 h1:tOBT7/oLUtjcFCRDLsQAIv2oSj+59O6PUSveIb5gkZQ= +github.com/Keyfactor/keyfactor-go-client-sdk v0.0.0-20230207174129-9d99e523eb15/go.mod h1:vDv1/UDRv3BQHyThh1ibGlOpxsN73FK9KkLVSPnWUFA= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spbsoluble/go-pkcs12 v0.3.1 h1:3DWrjdP3HOeYW6aTUSO9pqqAgRL8VKZLqvD5PGkLVMo= @@ -14,6 +21,12 @@ github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 h1:CCriYyAfq1Br1aIYettdHZTy8mBTIPo7We18TuO/bak= go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352/go.mod h1:SNgMg+EgDFwmvSmLRTNKC5fegJjB7v23qTQ0XLGUNHk= golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o= @@ -29,5 +42,6 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 9799eaf0e371a88973b221b5a1cae1f8539078d8 Mon Sep 17 00:00:00 2001 From: asmith236 Date: Mon, 20 Feb 2023 14:46:29 -0700 Subject: [PATCH 2/8] add migrate export commands --- cmd/migrate.go | 250 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 13 ++- 2 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 cmd/migrate.go diff --git a/cmd/migrate.go b/cmd/migrate.go new file mode 100644 index 0000000..4aae46f --- /dev/null +++ b/cmd/migrate.go @@ -0,0 +1,250 @@ +package cmd + +import ( + "context" + "encoding/json" + "fmt" + keyfactor_command_client_api "github.com/Keyfactor/keyfactor-go-client-sdk" + "github.com/Keyfactor/keyfactor-go-client/api" + "github.com/spf13/cobra" + "log" + "os" +) + +type outJson struct { + Collections []keyfactor_command_client_api.ModelsCertificateQuery `json:"Collections"` + MetadataFields []keyfactor_command_client_api.ModelsMetadataFieldTypeModel `json:"MetadataFields"` + ExpirationAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertDefinitionResponse `json:"ExpirationAlerts"` + IssuedCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertDefinitionResponse `json:"IssuedCertAlerts"` + DeniedCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertDefinitionResponse `json:"DeniedCertAlerts"` + PendingCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertDefinitionResponse `json:"PendingCertAlerts"` + Networks []keyfactor_command_client_api.KeyfactorApiModelsSslNetworkQueryResponse `json:"Networks"` + WorkflowDefinitions []keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionQueryResponse `json:"WorkflowDefinitions"` + BuiltInReports []keyfactor_command_client_api.ModelsReport `json:"BuiltInReports"` + CustomReports []keyfactor_command_client_api.ModelsCustomReport `json:"CustomReports"` + SecurityRoles api.GetSecurityRolesResponse `json:"SecurityRoles"` +} + +func exportToJSON(out outJson, exportPath string) { + mOut, jErr := json.MarshalIndent(out, "", " ") + if jErr != nil { + fmt.Printf("Error processing JSON object. %s\n", jErr) + log.Fatalf("[ERROR]: %s", jErr) + } + wErr := os.WriteFile(exportPath, mOut, 0666) + if wErr != nil { + fmt.Printf("Error writing files to %s: %s\n", exportPath, wErr) + log.Fatalf("[ERROR]: %s", wErr) + } else { + fmt.Printf("Content successfully written to %s", exportPath) + } +} + +var migrateCmd = &cobra.Command{ + Use: "migrate", + Short: "Keyfactor instance migrate utilities.", + Long: `A collection of APIs and utilities for migrating Keyfactor instance data.`, +} + +// exportCmd represents the export command +var exportCmd = &cobra.Command{ + Use: "export", + Short: "Keyfactor instance export utilities.", + Long: `A collection of APIs and utilities for exporting Keyfactor instance data.`, + Run: func(cmd *cobra.Command, args []string) { + // initialize each entry as an empty list in the event it is not requested by the flags + out := outJson{ + Collections: []keyfactor_command_client_api.ModelsCertificateQuery{}, + MetadataFields: []keyfactor_command_client_api.ModelsMetadataFieldTypeModel{}, + ExpirationAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertDefinitionResponse{}, + IssuedCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertDefinitionResponse{}, + DeniedCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertDefinitionResponse{}, + PendingCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertDefinitionResponse{}, + Networks: []keyfactor_command_client_api.KeyfactorApiModelsSslNetworkQueryResponse{}, + WorkflowDefinitions: []keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionQueryResponse{}, + BuiltInReports: []keyfactor_command_client_api.ModelsReport{}, + CustomReports: []keyfactor_command_client_api.ModelsCustomReport{}, + SecurityRoles: api.GetSecurityRolesResponse{}, + } + exportPath := cmd.Flag("file").Value.String() + if cmd.Flag("collections").Value.String() == "true" { + out.Collections = getCollections() + } + if cmd.Flag("metadata").Value.String() == "true" { + out.MetadataFields = getMetadata() + } + if cmd.Flag("expiration-alerts").Value.String() == "true" { + out.ExpirationAlerts = getExpirationAlerts() + } + if cmd.Flag("issued-alerts").Value.String() == "true" { + out.IssuedCertAlerts = getIssuedAlerts() + } + if cmd.Flag("denied-alerts").Value.String() == "true" { + out.DeniedCertAlerts = getDeniedAlerts() + } + if cmd.Flag("pending-alerts").Value.String() == "true" { + out.PendingCertAlerts = getPendingAlerts() + } + if cmd.Flag("networks").Value.String() == "true" { + out.Networks = getSslNetworks() + } + if cmd.Flag("workflow-definitions").Value.String() == "true" { + out.WorkflowDefinitions = getWorkflowDefinitions() + } + if cmd.Flag("reports").Value.String() == "true" { + out.BuiltInReports, out.CustomReports = getReports() + } + if cmd.Flag("security-roles").Value.String() == "true" { + out.SecurityRoles = getRoles() + } + exportToJSON(out, exportPath) + }, +} + +func getCollections() []keyfactor_command_client_api.ModelsCertificateQuery { + kfClient := initGenClient() + collections, _, reqErr := kfClient.CertificateCollectionApi.CertificateCollectionGetCollections(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to get collections %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return collections +} + +func getMetadata() []keyfactor_command_client_api.ModelsMetadataFieldTypeModel { + kfClient := initGenClient() + metadata, _, reqErr := kfClient.MetadataFieldApi.MetadataFieldGetAllMetadataFields(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to get metadata %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return metadata +} + +func getExpirationAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertDefinitionResponse { + kfClient := initGenClient() + expirAlerts, _, reqErr := kfClient.ExpirationAlertApi.ExpirationAlertGetExpirationAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to get expiration alerts %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return expirAlerts +} + +func getIssuedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertDefinitionResponse { + kfClient := initGenClient() + issuedAlerts, _, reqErr := kfClient.IssuedAlertApi.IssuedAlertGetIssuedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to get issued cert alerts %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return issuedAlerts +} + +func getDeniedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertDefinitionResponse { + kfClient := initGenClient() + deniedAlerts, _, reqErr := kfClient.DeniedAlertApi.DeniedAlertGetDeniedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to get denied cert alerts %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return deniedAlerts +} + +func getPendingAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertDefinitionResponse { + kfClient := initGenClient() + pendingAlerts, _, reqErr := kfClient.PendingAlertApi.PendingAlertGetPendingAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to get pending cert alerts %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return pendingAlerts +} + +func getSslNetworks() []keyfactor_command_client_api.KeyfactorApiModelsSslNetworkQueryResponse { + kfClient := initGenClient() + networks, _, reqErr := kfClient.SslApi.SslGetNetworks(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to get SSL networks %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return networks +} + +func getWorkflowDefinitions() []keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionQueryResponse { + kfClient := initGenClient() + workflowDefs, _, reqErr := kfClient.WorkflowDefinitionApi.WorkflowDefinitionQuery(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to get workflow definitions %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return workflowDefs +} + +func getReports() ([]keyfactor_command_client_api.ModelsReport, []keyfactor_command_client_api.ModelsCustomReport) { + kfClient := initGenClient() + //Gets all built-in reports + bReports, _, bErr := kfClient.ReportsApi.ReportsQueryReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if bErr != nil { + fmt.Printf("Error, unable to get built-in reports %s\n", bErr) + log.Fatalf("Error: %s", bErr) + } + //Gets all custom reports + cReports, _, cErr := kfClient.ReportsApi.ReportsQueryCustomReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if cErr != nil { + fmt.Printf("Error, unable to get custom reports %s\n", cErr) + log.Fatalf("Error: %s", cErr) + } + return bReports, cReports +} + +func getRoles() api.GetSecurityRolesResponse { + kfClient, _ := initClient() + roles, reqErr := kfClient.GetSecurityRoles() + if reqErr != nil { + fmt.Printf("Error, unable to get roles %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } + return roles +} + +func init() { + var exportPath string + var fCollections bool + var fMetadata bool + var fExpirationAlerts bool + var fIssuedAlerts bool + var fDeniedAlerts bool + var fPendingAlerts bool + var fNetworks bool + var fWorkflowDefinitions bool + var fReports bool + var fSecurityRoles bool + + RootCmd.AddCommand(migrateCmd) + migrateCmd.AddCommand(exportCmd) + + exportCmd.Flags().StringVarP(&exportPath, "file", "f", "", "export JSON to a specified filepath") + exportCmd.MarkFlagRequired("file") + + exportCmd.Flags().BoolVarP(&fCollections, "collections", "c", false, "export collections to JSON file") + exportCmd.Flags().Lookup("collections").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fMetadata, "metadata", "m", false, "export metadata to JSON file") + exportCmd.Flags().Lookup("metadata").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fExpirationAlerts, "expiration-alerts", "e", false, "export expiration cert alerts to JSON file") + exportCmd.Flags().Lookup("expiration-alerts").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fIssuedAlerts, "issued-alerts", "i", false, "export issued cert alerts to JSON file") + exportCmd.Flags().Lookup("issued-alerts").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fDeniedAlerts, "denied-alerts", "d", false, "export denied cert alerts to JSON file") + exportCmd.Flags().Lookup("denied-alerts").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fPendingAlerts, "pending-alerts", "p", false, "export pending cert alerts to JSON file") + exportCmd.Flags().Lookup("pending-alerts").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fNetworks, "networks", "n", false, "export SSL networks to JSON file") + exportCmd.Flags().Lookup("networks").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fWorkflowDefinitions, "workflow-definitions", "w", false, "export workflow definitions to JSON file") + exportCmd.Flags().Lookup("workflow-definitions").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fReports, "reports", "r", false, "export reports to JSON file") + exportCmd.Flags().Lookup("reports").NoOptDefVal = "true" + exportCmd.Flags().BoolVarP(&fSecurityRoles, "security-roles", "s", false, "export security roles to JSON file") + exportCmd.Flags().Lookup("security-roles").NoOptDefVal = "true" +} diff --git a/cmd/root.go b/cmd/root.go index 769ee20..a4af17e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,6 +8,7 @@ package cmd import ( "fmt" + keyfactor_command_client_api "github.com/Keyfactor/keyfactor-go-client-sdk" "github.com/Keyfactor/keyfactor-go-client/api" "github.com/spf13/cobra" "io" @@ -16,6 +17,9 @@ import ( "time" ) +var xKeyfactorRequestedWith = "APIClient" +var xKeyfactorApiVersion = "1" + func initClient() (*api.Client, error) { log.SetOutput(io.Discard) var clientAuth api.AuthConfig @@ -45,7 +49,14 @@ func initClient() (*api.Client, error) { fmt.Printf("Error connecting to Keyfactor: %s\n", err) log.Fatalf("[ERROR] creating Keyfactor client: %s", err) } - return c, err + + return c, nil +} + +func initGenClient() *keyfactor_command_client_api.APIClient { + configuration := keyfactor_command_client_api.NewConfiguration() + c := keyfactor_command_client_api.NewAPIClient(configuration) + return c } // RootCmd represents the base command when called without any subcommands From d958f352fcf700ff79ebf1451e93f49839225238 Mon Sep 17 00:00:00 2001 From: asmith236 Date: Tue, 21 Feb 2023 18:24:53 -0700 Subject: [PATCH 3/8] add migrate import commands --- cmd/{migrate.go => export.go} | 9 +- cmd/import.go | 330 ++++++++++++++++++++++++++++++++++ cmd/stores_create.go | 8 +- 3 files changed, 335 insertions(+), 12 deletions(-) rename cmd/{migrate.go => export.go} (97%) create mode 100644 cmd/import.go diff --git a/cmd/migrate.go b/cmd/export.go similarity index 97% rename from cmd/migrate.go rename to cmd/export.go index 4aae46f..5828e0d 100644 --- a/cmd/migrate.go +++ b/cmd/export.go @@ -40,12 +40,6 @@ func exportToJSON(out outJson, exportPath string) { } } -var migrateCmd = &cobra.Command{ - Use: "migrate", - Short: "Keyfactor instance migrate utilities.", - Long: `A collection of APIs and utilities for migrating Keyfactor instance data.`, -} - // exportCmd represents the export command var exportCmd = &cobra.Command{ Use: "export", @@ -221,8 +215,7 @@ func init() { var fReports bool var fSecurityRoles bool - RootCmd.AddCommand(migrateCmd) - migrateCmd.AddCommand(exportCmd) + RootCmd.AddCommand(exportCmd) exportCmd.Flags().StringVarP(&exportPath, "file", "f", "", "export JSON to a specified filepath") exportCmd.MarkFlagRequired("file") diff --git a/cmd/import.go b/cmd/import.go new file mode 100644 index 0000000..0dee7a0 --- /dev/null +++ b/cmd/import.go @@ -0,0 +1,330 @@ +package cmd + +import ( + "context" + "encoding/json" + "fmt" + keyfactor_command_client_api "github.com/Keyfactor/keyfactor-go-client-sdk" + "github.com/Keyfactor/keyfactor-go-client/api" + "github.com/spf13/cobra" + "io" + "log" + "os" +) + +// TODO print actual error messages +var importCmd = &cobra.Command{ + Use: "import", + Short: "Keyfactor instance import utilities.", + Long: `A collection of APIs and utilities for importing Keyfactor instance data.`, + Run: func(cmd *cobra.Command, args []string) { + exportPath := cmd.Flag("file").Value.String() + jsonFile, oErr := os.Open(exportPath) + if oErr != nil { + fmt.Printf("Error opening exported file: %s\n", oErr) + log.Fatalf("Error: %s", oErr) + } + defer jsonFile.Close() + var out outJson + bJson, _ := io.ReadAll(jsonFile) + jErr := json.Unmarshal(bJson, &out) + if jErr != nil { + fmt.Printf("Error reading exported file: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + kfClient := initGenClient() + if len(out.Collections) != 0 { + importCollections(out.Collections, kfClient) + } + if len(out.MetadataFields) != 0 { + importMetadataFields(out.MetadataFields, kfClient) + } + if len(out.ExpirationAlerts) != 0 { + importExpirationAlerts(out.ExpirationAlerts, kfClient) + } + if len(out.IssuedCertAlerts) != 0 { + importIssuedCertAlerts(out.IssuedCertAlerts, kfClient) + } + if len(out.DeniedCertAlerts) != 0 { + importDeniedCertAlerts(out.DeniedCertAlerts, kfClient) + } + if len(out.PendingCertAlerts) != 0 { + importPendingCertAlerts(out.PendingCertAlerts, kfClient) + } + if len(out.Networks) != 0 { + importNetworks(out.Networks, kfClient) + } + if len(out.WorkflowDefinitions) != 0 { + importWorkflowDefinitions(out.WorkflowDefinitions, kfClient) + } + if len(out.BuiltInReports) != 0 { + importBuiltInReports(out.BuiltInReports, kfClient) + } + if len(out.CustomReports) != 0 { + importCustomReports(out.CustomReports, kfClient) + } + if len(out.SecurityRoles) != 0 { + kfClient, _ := initClient() + importSecurityRoles(out.SecurityRoles, kfClient) + } + }, +} + +func importCollections(collections []keyfactor_command_client_api.ModelsCertificateQuery, kfClient *keyfactor_command_client_api.APIClient) { + //TODO error message if collection name already exists + for _, collection := range collections { + cJson, _ := json.Marshal(collection) + var collectionReq keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest + jErr := json.Unmarshal(cJson, &collectionReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + collectionReq.Query = collection.Content + _, httpResp, reqErr := kfClient.CertificateCollectionApi.CertificateCollectionCreateCollection(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith). + Request(collectionReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create collection: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(collectionReq.Name) + fmt.Println("Added", string(name), "to collections") + } + } +} + +func importMetadataFields(metadataFields []keyfactor_command_client_api.ModelsMetadataFieldTypeModel, kfClient *keyfactor_command_client_api.APIClient) { + for _, metadata := range metadataFields { + mJson, _ := json.Marshal(metadata) + var metadataReq keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest + jErr := json.Unmarshal(mJson, &metadataReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, httpResp, reqErr := kfClient.MetadataFieldApi.MetadataFieldCreateMetadataField(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).MetadataFieldType(metadataReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create metadata field type: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(metadataReq.Name) + fmt.Println("Added", string(name), "to metadata field types.") + } + } +} + +func importExpirationAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertDefinitionResponse, kfClient *keyfactor_command_client_api.APIClient) { + //TODO do I need to check that it corresponds to the correct collection? Ask JD + for _, alert := range alerts { + aJson, _ := json.Marshal(alert) + var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest + jErr := json.Unmarshal(aJson, &alertReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + alertReq.CertificateQueryId = alert.CertificateQuery.Id + _, httpResp, reqErr := kfClient.ExpirationAlertApi.ExpirationAlertAddExpirationAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alertReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create expiration alert: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(alertReq.DisplayName) + fmt.Println("Added", string(name), "to expiration alerts.") + } + } +} + +func importIssuedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertDefinitionResponse, kfClient *keyfactor_command_client_api.APIClient) { + for _, alert := range alerts { + aJson, _ := json.Marshal(alert) + var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest + jErr := json.Unmarshal(aJson, &alertReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, httpResp, reqErr := kfClient.IssuedAlertApi.IssuedAlertAddIssuedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alertReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create issued cert alert: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(alertReq.DisplayName) + fmt.Println("Added", string(name), "to issued cert alerts.") + } + } +} + +func importDeniedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertDefinitionResponse, kfClient *keyfactor_command_client_api.APIClient) { + for _, alert := range alerts { + aJson, _ := json.Marshal(alert) + var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest + jErr := json.Unmarshal(aJson, &alertReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, httpResp, reqErr := kfClient.DeniedAlertApi.DeniedAlertAddDeniedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alertReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create denied cert alert: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(alertReq.DisplayName) + fmt.Println("Added", string(name), "to denied cert alerts.") + } + } +} + +func importPendingCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertDefinitionResponse, kfClient *keyfactor_command_client_api.APIClient) { + for _, alert := range alerts { + aJson, _ := json.Marshal(alert) + var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest + jErr := json.Unmarshal(aJson, &alertReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, httpResp, reqErr := kfClient.PendingAlertApi.PendingAlertAddPendingAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alertReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create pending cert alert: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(alertReq.DisplayName) + fmt.Println("Added", string(name), "to pending cert alerts.") + } + } +} + +func importNetworks(networks []keyfactor_command_client_api.KeyfactorApiModelsSslNetworkQueryResponse, kfClient *keyfactor_command_client_api.APIClient) { + for _, network := range networks { + nJson, _ := json.Marshal(network) + var networkReq keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest + jErr := json.Unmarshal(nJson, &networkReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, httpResp, reqErr := kfClient.SslApi.SslCreateNetwork(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Network(networkReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create SSL network: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(networkReq.Name) + fmt.Println("Added", string(name), "to SSL networks.") + } + } +} + +func importWorkflowDefinitions(workflowDefs []keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionQueryResponse, kfClient *keyfactor_command_client_api.APIClient) { + for _, workflowDef := range workflowDefs { + wJson, _ := json.Marshal(workflowDef) + var workflowDefReq keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionCreateRequest + jErr := json.Unmarshal(wJson, &workflowDefReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, httpResp, reqErr := kfClient.WorkflowDefinitionApi.WorkflowDefinitionCreateNewDefinition(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(workflowDefReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create a new workflow definition: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(workflowDefReq.DisplayName) + fmt.Println("Added", string(name), "to workflow definitions.") + } + } +} + +// identified built-in reports where UsesCollections is false +func checkBuiltInReportDiffs(exportedReport keyfactor_command_client_api.ModelsReport) *int32 { + bReports, _ := getReports() + //check if built in report was modified from default in exported instance; if modified, update built-in report in new instance + for _, bReport := range bReports { + iJson, _ := json.Marshal(bReport.DisplayName) + eJson, _ := json.Marshal(exportedReport.DisplayName) + var usesCollectionBool = "false" + if string(iJson) == string(eJson) { + usesCollectionJson, _ := json.Marshal(exportedReport.UsesCollection) + if string(usesCollectionJson) == usesCollectionBool { + return bReport.Id + } else { + return nil + } + } + } + return nil +} + +// only imports built in reports where UsesCollections is false +func importBuiltInReports(reports []keyfactor_command_client_api.ModelsReport, kfClient *keyfactor_command_client_api.APIClient) { + //TODO PUT /Reports, only for reports that don't use a collection, ask JD + for _, report := range reports { + newReportId := checkBuiltInReportDiffs(report) + if newReportId != nil { + rJson, _ := json.Marshal(report) + var reportReq keyfactor_command_client_api.ModelsReportRequestModel + jErr := json.Unmarshal(rJson, &reportReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, httpResp, reqErr := kfClient.ReportsApi.ReportsUpdateReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(reportReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to update built-in report: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(report.DisplayName) + fmt.Println("Updated", string(name), "in built-in reports.") + } + } + } +} + +func importCustomReports(reports []keyfactor_command_client_api.ModelsCustomReport, kfClient *keyfactor_command_client_api.APIClient) { + for _, report := range reports { + rJson, _ := json.Marshal(report) + var reportReq keyfactor_command_client_api.ModelsCustomReportCreationRequest + jErr := json.Unmarshal(rJson, &reportReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, httpResp, reqErr := kfClient.ReportsApi.ReportsCreateCustomReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(reportReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + if reqErr != nil { + fmt.Printf("Error, unable to create custom report: %s\n", httpResp.Body) + log.Fatalf("Error: %s", httpResp.Body) + } else { + name, _ := json.Marshal(reportReq.DisplayName) + fmt.Println("Added", string(name), "to custom reports.") + } + } +} + +func importSecurityRoles(roles api.GetSecurityRolesResponse, kfClient *api.Client) { + for _, role := range roles { + rJson, _ := json.Marshal(role) + var roleReq api.CreateSecurityRoleArg + jErr := json.Unmarshal(rJson, &roleReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + _, reqErr := kfClient.CreateSecurityRole(&roleReq) + if reqErr != nil { + fmt.Printf("Error, unable to create security role: %s\n", reqErr) + log.Fatalf("Error: %s", reqErr) + } else { + name, _ := json.Marshal(roleReq.Name) + fmt.Println("Added", string(name), "to security roles.") + } + } +} + +func init() { + var exportPath string + + RootCmd.AddCommand(importCmd) + + importCmd.Flags().StringVarP(&exportPath, "file", "f", "", "export JSON to a specified filepath") + importCmd.MarkFlagRequired("file") +} diff --git a/cmd/stores_create.go b/cmd/stores_create.go index 2c36246..6bbd910 100644 --- a/cmd/stores_create.go +++ b/cmd/stores_create.go @@ -49,7 +49,7 @@ var ( } ) -var importCmd = &cobra.Command{ +var importStoresCmd = &cobra.Command{ Use: "import", Short: "Import a file with certificate store parameters and create them in keyfactor.", Long: `Tools for generating import templates and importing certificate stores`, @@ -409,9 +409,9 @@ var ( ) func init() { - storesCmd.AddCommand(importCmd) - importCmd.AddCommand(storesCreateTemplateCmd) - importCmd.AddCommand(storesCreateCmd) + storesCmd.AddCommand(importStoresCmd) + importStoresCmd.AddCommand(storesCreateTemplateCmd) + importStoresCmd.AddCommand(storesCreateCmd) storesCreateTemplateCmd.Flags().StringVarP(&storeTypeName, "store-type-name", "n", "", "The name of the cert store type for the template. Use if store-type-id is unknown.") storesCreateTemplateCmd.Flags().IntVarP(&storeTypeId, "store-type-id", "i", -1, "The ID of the cert store type for the template.") From 49f17c1e1285722af6eb3c33ddded18f02411f9b Mon Sep 17 00:00:00 2001 From: asmith236 Date: Tue, 28 Feb 2023 18:22:45 -0700 Subject: [PATCH 4/8] refactored to store exported info without Ids --- cmd/export.go | 259 +++++++++++++++++++++++++++++++++++++++++--------- cmd/import.go | 225 +++++++++++++++---------------------------- 2 files changed, 292 insertions(+), 192 deletions(-) diff --git a/cmd/export.go b/cmd/export.go index 5828e0d..717b533 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -9,20 +9,52 @@ import ( "github.com/spf13/cobra" "log" "os" + "strconv" ) +type exportModelsReport struct { + Id *int32 `json:"-"` + Scheduled *int32 `json:"Scheduled,omitempty"` + DisplayName *string `json:"DisplayName,omitempty"` + Description *string `json:"Description,omitempty"` + ReportPath *string `json:"ReportPath,omitempty"` + VersionNumber *string `json:"VersionNumber,omitempty"` + Categories *string `json:"Categories,omitempty"` + ShortName *string `json:"ShortName,omitempty"` + InNavigator *bool `json:"InNavigator,omitempty"` + Favorite *bool `json:"Favorite,omitempty"` + RemoveDuplicates *bool `json:"RemoveDuplicates,omitempty"` + UsesCollection *bool `json:"UsesCollection,omitempty"` + ReportParameter []keyfactor_command_client_api.ModelsReportParameters `json:"ReportParameter,omitempty"` + Schedules []keyfactor_command_client_api.ModelsReportSchedule `json:"Schedules,omitempty"` + AcceptedScheduleFormats []string `json:"AcceptedScheduleFormats,omitempty"` +} + +type exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest struct { + // Display name of the Definition + DisplayName *string `json:"DisplayName,omitempty"` + // Description of the Definition + Description *string `json:"Description,omitempty"` + // Key to be used to look up definition when starting a new workflow. For enrollment workflowTypes, this should be a template + Key *string `json:"Key,omitempty"` + //Name of Template corresponding to key value + KeyName *string `json:"KeyName,omitempty"` + // The Type of Workflow + WorkflowType *string `json:"WorkflowType,omitempty"` +} + type outJson struct { - Collections []keyfactor_command_client_api.ModelsCertificateQuery `json:"Collections"` - MetadataFields []keyfactor_command_client_api.ModelsMetadataFieldTypeModel `json:"MetadataFields"` - ExpirationAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertDefinitionResponse `json:"ExpirationAlerts"` - IssuedCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertDefinitionResponse `json:"IssuedCertAlerts"` - DeniedCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertDefinitionResponse `json:"DeniedCertAlerts"` - PendingCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertDefinitionResponse `json:"PendingCertAlerts"` - Networks []keyfactor_command_client_api.KeyfactorApiModelsSslNetworkQueryResponse `json:"Networks"` - WorkflowDefinitions []keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionQueryResponse `json:"WorkflowDefinitions"` - BuiltInReports []keyfactor_command_client_api.ModelsReport `json:"BuiltInReports"` - CustomReports []keyfactor_command_client_api.ModelsCustomReport `json:"CustomReports"` - SecurityRoles api.GetSecurityRolesResponse `json:"SecurityRoles"` + Collections []keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest `json:"Collections"` + MetadataFields []keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest `json:"MetadataFields"` + ExpirationAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest `json:"ExpirationAlerts"` + IssuedCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest `json:"IssuedCertAlerts"` + DeniedCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest `json:"DeniedCertAlerts"` + PendingCertAlerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest `json:"PendingCertAlerts"` + Networks []keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest `json:"Networks"` + WorkflowDefinitions []exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest `json:"WorkflowDefinitions"` + BuiltInReports []exportModelsReport `json:"BuiltInReports"` + CustomReports []keyfactor_command_client_api.ModelsCustomReportCreationRequest `json:"CustomReports"` + SecurityRoles []api.CreateSecurityRoleArg `json:"SecurityRoles"` } func exportToJSON(out outJson, exportPath string) { @@ -48,17 +80,17 @@ var exportCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // initialize each entry as an empty list in the event it is not requested by the flags out := outJson{ - Collections: []keyfactor_command_client_api.ModelsCertificateQuery{}, - MetadataFields: []keyfactor_command_client_api.ModelsMetadataFieldTypeModel{}, - ExpirationAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertDefinitionResponse{}, - IssuedCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertDefinitionResponse{}, - DeniedCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertDefinitionResponse{}, - PendingCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertDefinitionResponse{}, - Networks: []keyfactor_command_client_api.KeyfactorApiModelsSslNetworkQueryResponse{}, - WorkflowDefinitions: []keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionQueryResponse{}, - BuiltInReports: []keyfactor_command_client_api.ModelsReport{}, - CustomReports: []keyfactor_command_client_api.ModelsCustomReport{}, - SecurityRoles: api.GetSecurityRolesResponse{}, + Collections: []keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest{}, + MetadataFields: []keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest{}, + ExpirationAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest{}, + IssuedCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest{}, + DeniedCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest{}, + PendingCertAlerts: []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest{}, + Networks: []keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest{}, + WorkflowDefinitions: []exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest{}, + BuiltInReports: []exportModelsReport{}, + CustomReports: []keyfactor_command_client_api.ModelsCustomReportCreationRequest{}, + SecurityRoles: []api.CreateSecurityRoleArg{}, } exportPath := cmd.Flag("file").Value.String() if cmd.Flag("collections").Value.String() == "true" { @@ -95,87 +127,188 @@ var exportCmd = &cobra.Command{ }, } -func getCollections() []keyfactor_command_client_api.ModelsCertificateQuery { +func getCollections() []keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest { kfClient := initGenClient() collections, _, reqErr := kfClient.CertificateCollectionApi.CertificateCollectionGetCollections(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { fmt.Printf("Error, unable to get collections %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return collections + var lCollectionReq []keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest + for _, collection := range collections { + cJson, _ := json.Marshal(collection) + var collectionReq keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest + jErr := json.Unmarshal(cJson, &collectionReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + collectionReq.Query = collection.Content + collectionReq.Id = nil + lCollectionReq = append(lCollectionReq, collectionReq) + } + return lCollectionReq } -func getMetadata() []keyfactor_command_client_api.ModelsMetadataFieldTypeModel { +func getMetadata() []keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest { kfClient := initGenClient() metadata, _, reqErr := kfClient.MetadataFieldApi.MetadataFieldGetAllMetadataFields(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { fmt.Printf("Error, unable to get metadata %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return metadata + var lMetadataReq []keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest + for _, metadataItem := range metadata { + mJson, _ := json.Marshal(metadataItem) + var metadataReq keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest + jErr := json.Unmarshal(mJson, &metadataReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + metadataItem.Id = nil + lMetadataReq = append(lMetadataReq, metadataReq) + } + return lMetadataReq } -func getExpirationAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertDefinitionResponse { +func getExpirationAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest { kfClient := initGenClient() - expirAlerts, _, reqErr := kfClient.ExpirationAlertApi.ExpirationAlertGetExpirationAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + alerts, _, reqErr := kfClient.ExpirationAlertApi.ExpirationAlertGetExpirationAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { fmt.Printf("Error, unable to get expiration alerts %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return expirAlerts + var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest + for _, alert := range alerts { + mJson, _ := json.Marshal(alert) + var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest + jErr := json.Unmarshal(mJson, &alertReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + lAlertReq = append(lAlertReq, alertReq) + } + return lAlertReq } -func getIssuedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertDefinitionResponse { +func getIssuedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest { kfClient := initGenClient() - issuedAlerts, _, reqErr := kfClient.IssuedAlertApi.IssuedAlertGetIssuedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + alerts, _, reqErr := kfClient.IssuedAlertApi.IssuedAlertGetIssuedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { fmt.Printf("Error, unable to get issued cert alerts %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return issuedAlerts + var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest + for _, alert := range alerts { + mJson, _ := json.Marshal(alert) + var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest + jErr := json.Unmarshal(mJson, &alertReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + alertReq.TemplateId = nil + lAlertReq = append(lAlertReq, alertReq) + } + return lAlertReq } -func getDeniedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertDefinitionResponse { +func getDeniedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest { kfClient := initGenClient() - deniedAlerts, _, reqErr := kfClient.DeniedAlertApi.DeniedAlertGetDeniedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + alerts, _, reqErr := kfClient.DeniedAlertApi.DeniedAlertGetDeniedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { fmt.Printf("Error, unable to get denied cert alerts %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return deniedAlerts + var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest + for _, alert := range alerts { + mJson, _ := json.Marshal(alert) + var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest + jErr := json.Unmarshal(mJson, &alertReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + alertReq.TemplateId = nil + lAlertReq = append(lAlertReq, alertReq) + } + return lAlertReq } -func getPendingAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertDefinitionResponse { +func getPendingAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest { kfClient := initGenClient() - pendingAlerts, _, reqErr := kfClient.PendingAlertApi.PendingAlertGetPendingAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + alerts, _, reqErr := kfClient.PendingAlertApi.PendingAlertGetPendingAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { fmt.Printf("Error, unable to get pending cert alerts %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return pendingAlerts + var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest + for _, alert := range alerts { + mJson, _ := json.Marshal(alert) + var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest + jErr := json.Unmarshal(mJson, &alertReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + alertReq.TemplateId = nil + lAlertReq = append(lAlertReq, alertReq) + } + return lAlertReq } -func getSslNetworks() []keyfactor_command_client_api.KeyfactorApiModelsSslNetworkQueryResponse { +func getSslNetworks() []keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest { kfClient := initGenClient() networks, _, reqErr := kfClient.SslApi.SslGetNetworks(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { fmt.Printf("Error, unable to get SSL networks %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return networks + var lNetworkReq []keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest + for _, network := range networks { + mJson, _ := json.Marshal(network) + var networkReq keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest + jErr := json.Unmarshal(mJson, &networkReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + lNetworkReq = append(lNetworkReq, networkReq) + } + return lNetworkReq } -func getWorkflowDefinitions() []keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionQueryResponse { +func getWorkflowDefinitions() []exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest { kfClient := initGenClient() workflowDefs, _, reqErr := kfClient.WorkflowDefinitionApi.WorkflowDefinitionQuery(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { fmt.Printf("Error, unable to get workflow definitions %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return workflowDefs + var lWorkflowReq []exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest + for _, workflowDef := range workflowDefs { + mJson, _ := json.Marshal(workflowDef) + var workflowReq exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest + jErr := json.Unmarshal(mJson, &workflowReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + if workflowDef.Key != nil { + key, _ := strconv.ParseInt(*workflowDef.Key, 10, 64) + key32 := int32(key) + template, _, _ := kfClient.TemplateApi.TemplateGetTemplate(context.Background(), key32).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + workflowReq.KeyName = template.TemplateName + } + workflowReq.Key = nil + lWorkflowReq = append(lWorkflowReq, workflowReq) + } + return lWorkflowReq } -func getReports() ([]keyfactor_command_client_api.ModelsReport, []keyfactor_command_client_api.ModelsCustomReport) { +func getReports() ([]exportModelsReport, []keyfactor_command_client_api.ModelsCustomReportCreationRequest) { kfClient := initGenClient() //Gets all built-in reports bReports, _, bErr := kfClient.ReportsApi.ReportsQueryReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() @@ -183,23 +316,57 @@ func getReports() ([]keyfactor_command_client_api.ModelsReport, []keyfactor_comm fmt.Printf("Error, unable to get built-in reports %s\n", bErr) log.Fatalf("Error: %s", bErr) } + var lbReportsReq []exportModelsReport + for _, bReport := range bReports { + mJson, _ := json.Marshal(bReport) + var newbReport exportModelsReport + jErr := json.Unmarshal(mJson, &newbReport) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + newbReport.Id = nil + lbReportsReq = append(lbReportsReq, newbReport) + } //Gets all custom reports cReports, _, cErr := kfClient.ReportsApi.ReportsQueryCustomReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if cErr != nil { fmt.Printf("Error, unable to get custom reports %s\n", cErr) log.Fatalf("Error: %s", cErr) } - return bReports, cReports + var lcReportReq []keyfactor_command_client_api.ModelsCustomReportCreationRequest + for _, cReport := range cReports { + mJson, _ := json.Marshal(cReport) + var cReportReq keyfactor_command_client_api.ModelsCustomReportCreationRequest + jErr := json.Unmarshal(mJson, &cReportReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + lcReportReq = append(lcReportReq, cReportReq) + } + return lbReportsReq, lcReportReq } -func getRoles() api.GetSecurityRolesResponse { +func getRoles() []api.CreateSecurityRoleArg { kfClient, _ := initClient() roles, reqErr := kfClient.GetSecurityRoles() if reqErr != nil { fmt.Printf("Error, unable to get roles %s\n", reqErr) log.Fatalf("Error: %s", reqErr) } - return roles + var lRoleReq []api.CreateSecurityRoleArg + for _, role := range roles { + mJson, _ := json.Marshal(role) + var cRoleReq api.CreateSecurityRoleArg + jErr := json.Unmarshal(mJson, &cRoleReq) + if jErr != nil { + fmt.Printf("Error: %s\n", jErr) + log.Fatalf("Error: %s", jErr) + } + lRoleReq = append(lRoleReq, cRoleReq) + } + return lRoleReq } func init() { diff --git a/cmd/import.go b/cmd/import.go index 0dee7a0..5d65841 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -12,7 +12,7 @@ import ( "os" ) -// TODO print actual error messages +// TODO import flags --all (export too) var importCmd = &cobra.Command{ Use: "import", Short: "Keyfactor instance import utilities.", @@ -39,9 +39,6 @@ var importCmd = &cobra.Command{ if len(out.MetadataFields) != 0 { importMetadataFields(out.MetadataFields, kfClient) } - if len(out.ExpirationAlerts) != 0 { - importExpirationAlerts(out.ExpirationAlerts, kfClient) - } if len(out.IssuedCertAlerts) != 0 { importIssuedCertAlerts(out.IssuedCertAlerts, kfClient) } @@ -70,152 +67,97 @@ var importCmd = &cobra.Command{ }, } -func importCollections(collections []keyfactor_command_client_api.ModelsCertificateQuery, kfClient *keyfactor_command_client_api.APIClient) { - //TODO error message if collection name already exists +func importCollections(collections []keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest, kfClient *keyfactor_command_client_api.APIClient) { for _, collection := range collections { - cJson, _ := json.Marshal(collection) - var collectionReq keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest - jErr := json.Unmarshal(cJson, &collectionReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - collectionReq.Query = collection.Content _, httpResp, reqErr := kfClient.CertificateCollectionApi.CertificateCollectionCreateCollection(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith). - Request(collectionReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + Request(collection).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(collection.Name) if reqErr != nil { - fmt.Printf("Error, unable to create collection: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error! Unable to create collection %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(collectionReq.Name) + name, _ := json.Marshal(collection.Name) fmt.Println("Added", string(name), "to collections") } } } -func importMetadataFields(metadataFields []keyfactor_command_client_api.ModelsMetadataFieldTypeModel, kfClient *keyfactor_command_client_api.APIClient) { +func importMetadataFields(metadataFields []keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest, kfClient *keyfactor_command_client_api.APIClient) { for _, metadata := range metadataFields { - mJson, _ := json.Marshal(metadata) - var metadataReq keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest - jErr := json.Unmarshal(mJson, &metadataReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - _, httpResp, reqErr := kfClient.MetadataFieldApi.MetadataFieldCreateMetadataField(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).MetadataFieldType(metadataReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + _, httpResp, reqErr := kfClient.MetadataFieldApi.MetadataFieldCreateMetadataField(context.Background()). + XKeyfactorRequestedWith(xKeyfactorRequestedWith).MetadataFieldType(metadata). + XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(metadata.Name) if reqErr != nil { - fmt.Printf("Error, unable to create metadata field type: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error! Unable to create metadata field type %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(metadataReq.Name) fmt.Println("Added", string(name), "to metadata field types.") } } } -func importExpirationAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertDefinitionResponse, kfClient *keyfactor_command_client_api.APIClient) { - //TODO do I need to check that it corresponds to the correct collection? Ask JD - for _, alert := range alerts { - aJson, _ := json.Marshal(alert) - var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest - jErr := json.Unmarshal(aJson, &alertReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - alertReq.CertificateQueryId = alert.CertificateQuery.Id - _, httpResp, reqErr := kfClient.ExpirationAlertApi.ExpirationAlertAddExpirationAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alertReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() - if reqErr != nil { - fmt.Printf("Error, unable to create expiration alert: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) - } else { - name, _ := json.Marshal(alertReq.DisplayName) - fmt.Println("Added", string(name), "to expiration alerts.") - } - } -} - -func importIssuedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertDefinitionResponse, kfClient *keyfactor_command_client_api.APIClient) { +func importIssuedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest, kfClient *keyfactor_command_client_api.APIClient) { for _, alert := range alerts { - aJson, _ := json.Marshal(alert) - var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest - jErr := json.Unmarshal(aJson, &alertReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - _, httpResp, reqErr := kfClient.IssuedAlertApi.IssuedAlertAddIssuedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alertReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + _, httpResp, reqErr := kfClient.IssuedAlertApi.IssuedAlertAddIssuedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(alert.DisplayName) if reqErr != nil { - fmt.Printf("Error, unable to create issued cert alert: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error! Unable to create issued cert alert %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(alertReq.DisplayName) fmt.Println("Added", string(name), "to issued cert alerts.") } } } -func importDeniedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertDefinitionResponse, kfClient *keyfactor_command_client_api.APIClient) { +func importDeniedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest, kfClient *keyfactor_command_client_api.APIClient) { for _, alert := range alerts { - aJson, _ := json.Marshal(alert) - var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest - jErr := json.Unmarshal(aJson, &alertReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - _, httpResp, reqErr := kfClient.DeniedAlertApi.DeniedAlertAddDeniedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alertReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + _, httpResp, reqErr := kfClient.DeniedAlertApi.DeniedAlertAddDeniedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(alert.DisplayName) if reqErr != nil { - fmt.Printf("Error, unable to create denied cert alert: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error, unable to create denied cert alert %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(alertReq.DisplayName) fmt.Println("Added", string(name), "to denied cert alerts.") } } } -func importPendingCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertDefinitionResponse, kfClient *keyfactor_command_client_api.APIClient) { +func importPendingCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest, kfClient *keyfactor_command_client_api.APIClient) { for _, alert := range alerts { - aJson, _ := json.Marshal(alert) - var alertReq keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest - jErr := json.Unmarshal(aJson, &alertReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - _, httpResp, reqErr := kfClient.PendingAlertApi.PendingAlertAddPendingAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alertReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + _, httpResp, reqErr := kfClient.PendingAlertApi.PendingAlertAddPendingAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(alert.DisplayName) if reqErr != nil { - fmt.Printf("Error, unable to create pending cert alert: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error, unable to create pending cert alert %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(alertReq.DisplayName) fmt.Println("Added", string(name), "to pending cert alerts.") } } } -func importNetworks(networks []keyfactor_command_client_api.KeyfactorApiModelsSslNetworkQueryResponse, kfClient *keyfactor_command_client_api.APIClient) { +func importNetworks(networks []keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest, kfClient *keyfactor_command_client_api.APIClient) { for _, network := range networks { - nJson, _ := json.Marshal(network) - var networkReq keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest - jErr := json.Unmarshal(nJson, &networkReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - _, httpResp, reqErr := kfClient.SslApi.SslCreateNetwork(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Network(networkReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + _, httpResp, reqErr := kfClient.SslApi.SslCreateNetwork(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Network(network).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(network.Name) if reqErr != nil { - fmt.Printf("Error, unable to create SSL network: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error, unable to create SSL network %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(networkReq.Name) fmt.Println("Added", string(name), "to SSL networks.") } } } -func importWorkflowDefinitions(workflowDefs []keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionQueryResponse, kfClient *keyfactor_command_client_api.APIClient) { +func findMatchingTempIds(exportedWorkflowDef exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest, kfClient *keyfactor_command_client_api.APIClient) *string { + importInstanceTemplates, _, _ := kfClient.TemplateApi.TemplateGetTemplates(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + for _, template := range importInstanceTemplates { + importInstTempNameJson, _ := json.Marshal(template.TemplateName) + exportInstTempNameJson, _ := json.Marshal(exportedWorkflowDef.KeyName) + if string(importInstTempNameJson) == string(exportInstTempNameJson) { + importInstMatchingTemplateId, _ := json.Marshal(template.Id) + importInstMatchingTemplateIdStr := string(importInstMatchingTemplateId) + return &importInstMatchingTemplateIdStr + } + } + return nil +} + +func importWorkflowDefinitions(workflowDefs []exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest, kfClient *keyfactor_command_client_api.APIClient) { for _, workflowDef := range workflowDefs { wJson, _ := json.Marshal(workflowDef) var workflowDefReq keyfactor_command_client_api.KeyfactorApiModelsWorkflowsDefinitionCreateRequest @@ -224,31 +166,39 @@ func importWorkflowDefinitions(workflowDefs []keyfactor_command_client_api.Keyfa fmt.Printf("Error: %s\n", jErr) log.Fatalf("Error: %s", jErr) } + newTemplateId := findMatchingTempIds(workflowDef, kfClient) + if newTemplateId != nil { + workflowDefReq.Key = newTemplateId + } _, httpResp, reqErr := kfClient.WorkflowDefinitionApi.WorkflowDefinitionCreateNewDefinition(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(workflowDefReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(workflowDef.DisplayName) if reqErr != nil { - fmt.Printf("Error, unable to create a new workflow definition: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error! Unable to create a new workflow definition %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(workflowDefReq.DisplayName) fmt.Println("Added", string(name), "to workflow definitions.") } } } -// identified built-in reports where UsesCollections is false -func checkBuiltInReportDiffs(exportedReport keyfactor_command_client_api.ModelsReport) *int32 { - bReports, _ := getReports() +func checkBuiltInReportDiffs(exportedReport exportModelsReport, kfClient *keyfactor_command_client_api.APIClient) *int32 { + importInstanceReports, _, _ := kfClient.ReportsApi.ReportsQueryReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() //check if built in report was modified from default in exported instance; if modified, update built-in report in new instance - for _, bReport := range bReports { - iJson, _ := json.Marshal(bReport.DisplayName) - eJson, _ := json.Marshal(exportedReport.DisplayName) + for _, report := range importInstanceReports { + importInstDispNameJson, _ := json.Marshal(report.DisplayName) + exportInstDispNameJson, _ := json.Marshal(exportedReport.DisplayName) + importInstInNavJson, _ := json.Marshal(report.InNavigator) + exportInstInNavJson, _ := json.Marshal(exportedReport.InNavigator) + importInstFavJson, _ := json.Marshal(report.Favorite) + exportInstFavJson, _ := json.Marshal(exportedReport.Favorite) + importInstRemDupJson, _ := json.Marshal(report.RemoveDuplicates) + exportInstRemDupJson, _ := json.Marshal(exportedReport.RemoveDuplicates) var usesCollectionBool = "false" - if string(iJson) == string(eJson) { - usesCollectionJson, _ := json.Marshal(exportedReport.UsesCollection) - if string(usesCollectionJson) == usesCollectionBool { - return bReport.Id - } else { - return nil + if string(importInstDispNameJson) == string(exportInstDispNameJson) { + if (string(importInstFavJson) != string(exportInstFavJson)) || (string(importInstInNavJson) != string(exportInstInNavJson)) || (string(importInstRemDupJson) != string(exportInstRemDupJson)) { + usesCollectionJson, _ := json.Marshal(exportedReport.UsesCollection) + if string(usesCollectionJson) == usesCollectionBool { + return report.Id + } } } } @@ -256,10 +206,9 @@ func checkBuiltInReportDiffs(exportedReport keyfactor_command_client_api.ModelsR } // only imports built in reports where UsesCollections is false -func importBuiltInReports(reports []keyfactor_command_client_api.ModelsReport, kfClient *keyfactor_command_client_api.APIClient) { - //TODO PUT /Reports, only for reports that don't use a collection, ask JD +func importBuiltInReports(reports []exportModelsReport, kfClient *keyfactor_command_client_api.APIClient) { for _, report := range reports { - newReportId := checkBuiltInReportDiffs(report) + newReportId := checkBuiltInReportDiffs(report, kfClient) if newReportId != nil { rJson, _ := json.Marshal(report) var reportReq keyfactor_command_client_api.ModelsReportRequestModel @@ -268,53 +217,37 @@ func importBuiltInReports(reports []keyfactor_command_client_api.ModelsReport, k fmt.Printf("Error: %s\n", jErr) log.Fatalf("Error: %s", jErr) } + reportReq.Id = newReportId _, httpResp, reqErr := kfClient.ReportsApi.ReportsUpdateReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(reportReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(report.DisplayName) if reqErr != nil { - fmt.Printf("Error, unable to update built-in report: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error! Unable to update built-in report %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(report.DisplayName) fmt.Println("Updated", string(name), "in built-in reports.") } } } } -func importCustomReports(reports []keyfactor_command_client_api.ModelsCustomReport, kfClient *keyfactor_command_client_api.APIClient) { +func importCustomReports(reports []keyfactor_command_client_api.ModelsCustomReportCreationRequest, kfClient *keyfactor_command_client_api.APIClient) { for _, report := range reports { - rJson, _ := json.Marshal(report) - var reportReq keyfactor_command_client_api.ModelsCustomReportCreationRequest - jErr := json.Unmarshal(rJson, &reportReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - _, httpResp, reqErr := kfClient.ReportsApi.ReportsCreateCustomReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(reportReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + _, httpResp, reqErr := kfClient.ReportsApi.ReportsCreateCustomReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(report).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() + name, _ := json.Marshal(report.DisplayName) if reqErr != nil { - fmt.Printf("Error, unable to create custom report: %s\n", httpResp.Body) - log.Fatalf("Error: %s", httpResp.Body) + fmt.Printf("Error! Unable to create custom report %s: %s\n", string(name), httpResp.Body) } else { - name, _ := json.Marshal(reportReq.DisplayName) fmt.Println("Added", string(name), "to custom reports.") } } } -func importSecurityRoles(roles api.GetSecurityRolesResponse, kfClient *api.Client) { +func importSecurityRoles(roles []api.CreateSecurityRoleArg, kfClient *api.Client) { for _, role := range roles { - rJson, _ := json.Marshal(role) - var roleReq api.CreateSecurityRoleArg - jErr := json.Unmarshal(rJson, &roleReq) - if jErr != nil { - fmt.Printf("Error: %s\n", jErr) - log.Fatalf("Error: %s", jErr) - } - _, reqErr := kfClient.CreateSecurityRole(&roleReq) + _, reqErr := kfClient.CreateSecurityRole(&role) + name, _ := json.Marshal(role.Name) if reqErr != nil { - fmt.Printf("Error, unable to create security role: %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("Error! Unable to create security role %s: %s\n", string(name), reqErr) } else { - name, _ := json.Marshal(roleReq.Name) fmt.Println("Added", string(name), "to security roles.") } } From c7395bf698ccddddb9da8b2bd3d210521d359768 Mon Sep 17 00:00:00 2001 From: asmith236 Date: Tue, 28 Feb 2023 18:40:18 -0700 Subject: [PATCH 5/8] add new flags to export and import --- cmd/export.go | 78 ++++++++++++++++++++++++++++------------------- cmd/import.go | 84 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 106 insertions(+), 56 deletions(-) diff --git a/cmd/export.go b/cmd/export.go index 717b533..8f119ff 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -12,6 +12,19 @@ import ( "strconv" ) +var exportPath string +var fCollections bool +var fMetadata bool +var fExpirationAlerts bool +var fIssuedAlerts bool +var fDeniedAlerts bool +var fPendingAlerts bool +var fNetworks bool +var fWorkflowDefinitions bool +var fReports bool +var fSecurityRoles bool +var fAll bool + type exportModelsReport struct { Id *int32 `json:"-"` Scheduled *int32 `json:"Scheduled,omitempty"` @@ -93,35 +106,48 @@ var exportCmd = &cobra.Command{ SecurityRoles: []api.CreateSecurityRoleArg{}, } exportPath := cmd.Flag("file").Value.String() - if cmd.Flag("collections").Value.String() == "true" { + if cmd.Flag("all").Value.String() == "true" { out.Collections = getCollections() - } - if cmd.Flag("metadata").Value.String() == "true" { out.MetadataFields = getMetadata() - } - if cmd.Flag("expiration-alerts").Value.String() == "true" { out.ExpirationAlerts = getExpirationAlerts() - } - if cmd.Flag("issued-alerts").Value.String() == "true" { out.IssuedCertAlerts = getIssuedAlerts() - } - if cmd.Flag("denied-alerts").Value.String() == "true" { out.DeniedCertAlerts = getDeniedAlerts() - } - if cmd.Flag("pending-alerts").Value.String() == "true" { out.PendingCertAlerts = getPendingAlerts() - } - if cmd.Flag("networks").Value.String() == "true" { out.Networks = getSslNetworks() - } - if cmd.Flag("workflow-definitions").Value.String() == "true" { out.WorkflowDefinitions = getWorkflowDefinitions() - } - if cmd.Flag("reports").Value.String() == "true" { out.BuiltInReports, out.CustomReports = getReports() - } - if cmd.Flag("security-roles").Value.String() == "true" { out.SecurityRoles = getRoles() + } else { + if cmd.Flag("collections").Value.String() == "true" { + out.Collections = getCollections() + } + if cmd.Flag("metadata").Value.String() == "true" { + out.MetadataFields = getMetadata() + } + if cmd.Flag("expiration-alerts").Value.String() == "true" { + out.ExpirationAlerts = getExpirationAlerts() + } + if cmd.Flag("issued-alerts").Value.String() == "true" { + out.IssuedCertAlerts = getIssuedAlerts() + } + if cmd.Flag("denied-alerts").Value.String() == "true" { + out.DeniedCertAlerts = getDeniedAlerts() + } + if cmd.Flag("pending-alerts").Value.String() == "true" { + out.PendingCertAlerts = getPendingAlerts() + } + if cmd.Flag("networks").Value.String() == "true" { + out.Networks = getSslNetworks() + } + if cmd.Flag("workflow-definitions").Value.String() == "true" { + out.WorkflowDefinitions = getWorkflowDefinitions() + } + if cmd.Flag("reports").Value.String() == "true" { + out.BuiltInReports, out.CustomReports = getReports() + } + if cmd.Flag("security-roles").Value.String() == "true" { + out.SecurityRoles = getRoles() + } } exportToJSON(out, exportPath) }, @@ -370,23 +396,13 @@ func getRoles() []api.CreateSecurityRoleArg { } func init() { - var exportPath string - var fCollections bool - var fMetadata bool - var fExpirationAlerts bool - var fIssuedAlerts bool - var fDeniedAlerts bool - var fPendingAlerts bool - var fNetworks bool - var fWorkflowDefinitions bool - var fReports bool - var fSecurityRoles bool - RootCmd.AddCommand(exportCmd) exportCmd.Flags().StringVarP(&exportPath, "file", "f", "", "export JSON to a specified filepath") exportCmd.MarkFlagRequired("file") + exportCmd.Flags().BoolVarP(&fAll, "all", "a", false, "export all exportable data to JSON file") + exportCmd.Flags().Lookup("all").NoOptDefVal = "true" exportCmd.Flags().BoolVarP(&fCollections, "collections", "c", false, "export collections to JSON file") exportCmd.Flags().Lookup("collections").NoOptDefVal = "true" exportCmd.Flags().BoolVarP(&fMetadata, "metadata", "m", false, "export metadata to JSON file") diff --git a/cmd/import.go b/cmd/import.go index 5d65841..587e94f 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -12,7 +12,6 @@ import ( "os" ) -// TODO import flags --all (export too) var importCmd = &cobra.Command{ Use: "import", Short: "Keyfactor instance import utilities.", @@ -33,36 +32,49 @@ var importCmd = &cobra.Command{ log.Fatalf("Error: %s", jErr) } kfClient := initGenClient() - if len(out.Collections) != 0 { + oldKfClient, _ := initClient() + if cmd.Flag("all").Value.String() == "true" { importCollections(out.Collections, kfClient) - } - if len(out.MetadataFields) != 0 { importMetadataFields(out.MetadataFields, kfClient) - } - if len(out.IssuedCertAlerts) != 0 { importIssuedCertAlerts(out.IssuedCertAlerts, kfClient) - } - if len(out.DeniedCertAlerts) != 0 { importDeniedCertAlerts(out.DeniedCertAlerts, kfClient) - } - if len(out.PendingCertAlerts) != 0 { importPendingCertAlerts(out.PendingCertAlerts, kfClient) - } - if len(out.Networks) != 0 { importNetworks(out.Networks, kfClient) - } - if len(out.WorkflowDefinitions) != 0 { importWorkflowDefinitions(out.WorkflowDefinitions, kfClient) - } - if len(out.BuiltInReports) != 0 { importBuiltInReports(out.BuiltInReports, kfClient) - } - if len(out.CustomReports) != 0 { importCustomReports(out.CustomReports, kfClient) - } - if len(out.SecurityRoles) != 0 { - kfClient, _ := initClient() - importSecurityRoles(out.SecurityRoles, kfClient) + importSecurityRoles(out.SecurityRoles, oldKfClient) + } else { + if len(out.Collections) != 0 && cmd.Flag("collections").Value.String() == "true" { + importCollections(out.Collections, kfClient) + } + if len(out.MetadataFields) != 0 && cmd.Flag("metadata").Value.String() == "true" { + importMetadataFields(out.MetadataFields, kfClient) + } + if len(out.IssuedCertAlerts) != 0 && cmd.Flag("issued-alerts").Value.String() == "true" { + importIssuedCertAlerts(out.IssuedCertAlerts, kfClient) + } + if len(out.DeniedCertAlerts) != 0 && cmd.Flag("denied-alerts").Value.String() == "true" { + importDeniedCertAlerts(out.DeniedCertAlerts, kfClient) + } + if len(out.PendingCertAlerts) != 0 && cmd.Flag("pending-alerts").Value.String() == "true" { + importPendingCertAlerts(out.PendingCertAlerts, kfClient) + } + if len(out.Networks) != 0 && cmd.Flag("networks").Value.String() == "true" { + importNetworks(out.Networks, kfClient) + } + if len(out.WorkflowDefinitions) != 0 && cmd.Flag("workflow-definitions").Value.String() == "true" { + importWorkflowDefinitions(out.WorkflowDefinitions, kfClient) + } + if len(out.BuiltInReports) != 0 && cmd.Flag("reports").Value.String() == "true" { + importBuiltInReports(out.BuiltInReports, kfClient) + } + if len(out.CustomReports) != 0 && cmd.Flag("reports").Value.String() == "true" { + importCustomReports(out.CustomReports, kfClient) + } + if len(out.SecurityRoles) != 0 && cmd.Flag("security-roles").Value.String() == "true" { + importSecurityRoles(out.SecurityRoles, oldKfClient) + } } }, } @@ -254,10 +266,32 @@ func importSecurityRoles(roles []api.CreateSecurityRoleArg, kfClient *api.Client } func init() { - var exportPath string - RootCmd.AddCommand(importCmd) - importCmd.Flags().StringVarP(&exportPath, "file", "f", "", "export JSON to a specified filepath") + importCmd.Flags().StringVarP(&exportPath, "file", "f", "", "import JSON to a specified filepath") importCmd.MarkFlagRequired("file") + + importCmd.Flags().BoolVarP(&fAll, "all", "a", false, "import all importable data to JSON file") + importCmd.Flags().Lookup("all").NoOptDefVal = "true" + + importCmd.Flags().BoolVarP(&fCollections, "collections", "c", false, "import collections to JSON file") + importCmd.Flags().Lookup("collections").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fMetadata, "metadata", "m", false, "import metadata to JSON file") + importCmd.Flags().Lookup("metadata").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fExpirationAlerts, "expiration-alerts", "e", false, "import expiration cert alerts to JSON file") + importCmd.Flags().Lookup("expiration-alerts").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fIssuedAlerts, "issued-alerts", "i", false, "import issued cert alerts to JSON file") + importCmd.Flags().Lookup("issued-alerts").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fDeniedAlerts, "denied-alerts", "d", false, "import denied cert alerts to JSON file") + importCmd.Flags().Lookup("denied-alerts").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fPendingAlerts, "pending-alerts", "p", false, "import pending cert alerts to JSON file") + importCmd.Flags().Lookup("pending-alerts").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fNetworks, "networks", "n", false, "import SSL networks to JSON file") + importCmd.Flags().Lookup("networks").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fWorkflowDefinitions, "workflow-definitions", "w", false, "import workflow definitions to JSON file") + importCmd.Flags().Lookup("workflow-definitions").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fReports, "reports", "r", false, "import reports to JSON file") + importCmd.Flags().Lookup("reports").NoOptDefVal = "true" + importCmd.Flags().BoolVarP(&fSecurityRoles, "security-roles", "s", false, "import security roles to JSON file") + importCmd.Flags().Lookup("security-roles").NoOptDefVal = "true" } From a3f3d7818d208b3678a124c8edfc4a0760ec0a02 Mon Sep 17 00:00:00 2001 From: asmith236 Date: Tue, 28 Feb 2023 20:30:54 -0700 Subject: [PATCH 6/8] prettified comments --- cmd/export.go | 33 +++++++++++---------------------- cmd/import.go | 32 ++++++++++++++++++++++---------- cmd/root.go | 3 +++ 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/cmd/export.go b/cmd/export.go index 8f119ff..4d985d8 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -157,8 +157,7 @@ func getCollections() []keyfactor_command_client_api.KeyfactorApiModelsCertifica kfClient := initGenClient() collections, _, reqErr := kfClient.CertificateCollectionApi.CertificateCollectionGetCollections(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { - fmt.Printf("Error, unable to get collections %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get collections %s%s\n", colorRed, reqErr, colorWhite) } var lCollectionReq []keyfactor_command_client_api.KeyfactorApiModelsCertificateCollectionsCertificateCollectionCreateRequest for _, collection := range collections { @@ -180,8 +179,7 @@ func getMetadata() []keyfactor_command_client_api.KeyfactorApiModelsMetadataFiel kfClient := initGenClient() metadata, _, reqErr := kfClient.MetadataFieldApi.MetadataFieldGetAllMetadataFields(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { - fmt.Printf("Error, unable to get metadata %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get metadata %s%s\n", colorRed, reqErr, colorWhite) } var lMetadataReq []keyfactor_command_client_api.KeyfactorApiModelsMetadataFieldMetadataFieldCreateRequest for _, metadataItem := range metadata { @@ -202,8 +200,7 @@ func getExpirationAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAler kfClient := initGenClient() alerts, _, reqErr := kfClient.ExpirationAlertApi.ExpirationAlertGetExpirationAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { - fmt.Printf("Error, unable to get expiration alerts %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get expiration alerts %s%s\n", colorRed, reqErr, colorWhite) } var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsExpirationExpirationAlertCreationRequest for _, alert := range alerts { @@ -223,8 +220,7 @@ func getIssuedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsIs kfClient := initGenClient() alerts, _, reqErr := kfClient.IssuedAlertApi.IssuedAlertGetIssuedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { - fmt.Printf("Error, unable to get issued cert alerts %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get issued cert alerts %s%s\n", colorRed, reqErr, colorWhite) } var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsIssuedIssuedAlertCreationRequest for _, alert := range alerts { @@ -245,8 +241,7 @@ func getDeniedAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsDe kfClient := initGenClient() alerts, _, reqErr := kfClient.DeniedAlertApi.DeniedAlertGetDeniedAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { - fmt.Printf("Error, unable to get denied cert alerts %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get denied cert alerts %s%s\n", colorRed, reqErr, colorWhite) } var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsDeniedDeniedAlertCreationRequest for _, alert := range alerts { @@ -267,8 +262,7 @@ func getPendingAlerts() []keyfactor_command_client_api.KeyfactorApiModelsAlertsP kfClient := initGenClient() alerts, _, reqErr := kfClient.PendingAlertApi.PendingAlertGetPendingAlerts(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { - fmt.Printf("Error, unable to get pending cert alerts %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get pending cert alerts %s%s\n", colorRed, reqErr, colorWhite) } var lAlertReq []keyfactor_command_client_api.KeyfactorApiModelsAlertsPendingPendingAlertCreationRequest for _, alert := range alerts { @@ -289,8 +283,7 @@ func getSslNetworks() []keyfactor_command_client_api.KeyfactorApiModelsSslCreate kfClient := initGenClient() networks, _, reqErr := kfClient.SslApi.SslGetNetworks(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { - fmt.Printf("Error, unable to get SSL networks %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get SSL networks %s%s\n", colorRed, reqErr, colorWhite) } var lNetworkReq []keyfactor_command_client_api.KeyfactorApiModelsSslCreateNetworkRequest for _, network := range networks { @@ -310,8 +303,7 @@ func getWorkflowDefinitions() []exportKeyfactorApiModelsWorkflowsDefinitionCreat kfClient := initGenClient() workflowDefs, _, reqErr := kfClient.WorkflowDefinitionApi.WorkflowDefinitionQuery(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if reqErr != nil { - fmt.Printf("Error, unable to get workflow definitions %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get workflow definitions %s%s\n", colorRed, reqErr, colorWhite) } var lWorkflowReq []exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest for _, workflowDef := range workflowDefs { @@ -339,8 +331,7 @@ func getReports() ([]exportModelsReport, []keyfactor_command_client_api.ModelsCu //Gets all built-in reports bReports, _, bErr := kfClient.ReportsApi.ReportsQueryReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if bErr != nil { - fmt.Printf("Error, unable to get built-in reports %s\n", bErr) - log.Fatalf("Error: %s", bErr) + fmt.Printf("%s Error! Unable to get built-in reports %s%s\n", colorRed, bErr, colorWhite) } var lbReportsReq []exportModelsReport for _, bReport := range bReports { @@ -357,8 +348,7 @@ func getReports() ([]exportModelsReport, []keyfactor_command_client_api.ModelsCu //Gets all custom reports cReports, _, cErr := kfClient.ReportsApi.ReportsQueryCustomReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() if cErr != nil { - fmt.Printf("Error, unable to get custom reports %s\n", cErr) - log.Fatalf("Error: %s", cErr) + fmt.Printf("%s Error! Unable to get custom reports %s%s\n", colorRed, cErr, colorWhite) } var lcReportReq []keyfactor_command_client_api.ModelsCustomReportCreationRequest for _, cReport := range cReports { @@ -378,8 +368,7 @@ func getRoles() []api.CreateSecurityRoleArg { kfClient, _ := initClient() roles, reqErr := kfClient.GetSecurityRoles() if reqErr != nil { - fmt.Printf("Error, unable to get roles %s\n", reqErr) - log.Fatalf("Error: %s", reqErr) + fmt.Printf("%s Error! Unable to get security roles %s%s\n", colorRed, reqErr, colorWhite) } var lRoleReq []api.CreateSecurityRoleArg for _, role := range roles { diff --git a/cmd/import.go b/cmd/import.go index 587e94f..37826bc 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -12,6 +12,18 @@ import ( "os" ) +type Body struct { + ErrorCode string + Message string +} + +func parseError(error io.ReadCloser) string { + bytes, _ := io.ReadAll(error) + var newError Body + json.Unmarshal(bytes, &newError) + return newError.Message +} + var importCmd = &cobra.Command{ Use: "import", Short: "Keyfactor instance import utilities.", @@ -85,7 +97,7 @@ func importCollections(collections []keyfactor_command_client_api.KeyfactorApiMo Request(collection).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(collection.Name) if reqErr != nil { - fmt.Printf("Error! Unable to create collection %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to create collection %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { name, _ := json.Marshal(collection.Name) fmt.Println("Added", string(name), "to collections") @@ -100,7 +112,7 @@ func importMetadataFields(metadataFields []keyfactor_command_client_api.Keyfacto XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(metadata.Name) if reqErr != nil { - fmt.Printf("Error! Unable to create metadata field type %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to create metadata field type %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { fmt.Println("Added", string(name), "to metadata field types.") } @@ -112,7 +124,7 @@ func importIssuedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiMo _, httpResp, reqErr := kfClient.IssuedAlertApi.IssuedAlertAddIssuedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(alert.DisplayName) if reqErr != nil { - fmt.Printf("Error! Unable to create issued cert alert %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to create issued cert alert %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { fmt.Println("Added", string(name), "to issued cert alerts.") } @@ -124,7 +136,7 @@ func importDeniedCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiMo _, httpResp, reqErr := kfClient.DeniedAlertApi.DeniedAlertAddDeniedAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(alert.DisplayName) if reqErr != nil { - fmt.Printf("Error, unable to create denied cert alert %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to create denied cert alert %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { fmt.Println("Added", string(name), "to denied cert alerts.") } @@ -136,7 +148,7 @@ func importPendingCertAlerts(alerts []keyfactor_command_client_api.KeyfactorApiM _, httpResp, reqErr := kfClient.PendingAlertApi.PendingAlertAddPendingAlert(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Req(alert).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(alert.DisplayName) if reqErr != nil { - fmt.Printf("Error, unable to create pending cert alert %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to create pending cert alert %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { fmt.Println("Added", string(name), "to pending cert alerts.") } @@ -148,7 +160,7 @@ func importNetworks(networks []keyfactor_command_client_api.KeyfactorApiModelsSs _, httpResp, reqErr := kfClient.SslApi.SslCreateNetwork(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Network(network).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(network.Name) if reqErr != nil { - fmt.Printf("Error, unable to create SSL network %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to create SSL network %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { fmt.Println("Added", string(name), "to SSL networks.") } @@ -185,7 +197,7 @@ func importWorkflowDefinitions(workflowDefs []exportKeyfactorApiModelsWorkflowsD _, httpResp, reqErr := kfClient.WorkflowDefinitionApi.WorkflowDefinitionCreateNewDefinition(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(workflowDefReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(workflowDef.DisplayName) if reqErr != nil { - fmt.Printf("Error! Unable to create a new workflow definition %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to create workflow definition %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { fmt.Println("Added", string(name), "to workflow definitions.") } @@ -233,7 +245,7 @@ func importBuiltInReports(reports []exportModelsReport, kfClient *keyfactor_comm _, httpResp, reqErr := kfClient.ReportsApi.ReportsUpdateReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(reportReq).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(report.DisplayName) if reqErr != nil { - fmt.Printf("Error! Unable to update built-in report %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to update built-in report %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { fmt.Println("Updated", string(name), "in built-in reports.") } @@ -246,7 +258,7 @@ func importCustomReports(reports []keyfactor_command_client_api.ModelsCustomRepo _, httpResp, reqErr := kfClient.ReportsApi.ReportsCreateCustomReport(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).Request(report).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() name, _ := json.Marshal(report.DisplayName) if reqErr != nil { - fmt.Printf("Error! Unable to create custom report %s: %s\n", string(name), httpResp.Body) + fmt.Printf("%s Error! Unable to create custom report %s - %s%s\n", colorRed, string(name), parseError(httpResp.Body), colorWhite) } else { fmt.Println("Added", string(name), "to custom reports.") } @@ -258,7 +270,7 @@ func importSecurityRoles(roles []api.CreateSecurityRoleArg, kfClient *api.Client _, reqErr := kfClient.CreateSecurityRole(&role) name, _ := json.Marshal(role.Name) if reqErr != nil { - fmt.Printf("Error! Unable to create security role %s: %s\n", string(name), reqErr) + fmt.Printf("%s Error! Unable to create security role %s - %s%s\n", colorRed, string(name), reqErr, colorWhite) } else { fmt.Println("Added", string(name), "to security roles.") } diff --git a/cmd/root.go b/cmd/root.go index a4af17e..bf15e43 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,6 +17,9 @@ import ( "time" ) +var colorRed = "\033[31m" +var colorWhite = "\033[37m" + var xKeyfactorRequestedWith = "APIClient" var xKeyfactorApiVersion = "1" From f8783490daadea7b4a81affa2d92648e665b2f1b Mon Sep 17 00:00:00 2001 From: Sean <1661003+spbsoluble@users.noreply.github.com> Date: Wed, 1 Mar 2023 08:32:28 -0800 Subject: [PATCH 7/8] fix: update mod using `keyfactor-go-client-v1.4.0` --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index fb1fd6e..a88f3dd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/Jeffail/gabs/v2 v2.6.1 - github.com/Keyfactor/keyfactor-go-client v1.2.1-0.20230207180411-19f63f6d48b8 + github.com/Keyfactor/keyfactor-go-client v1.4.0 github.com/spf13/cobra v1.6.1 golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 ) From 2fce868b1b917a1da1cad7cb3b11c434ebc40fc2 Mon Sep 17 00:00:00 2001 From: asmith236 Date: Wed, 1 Mar 2023 19:19:27 -0700 Subject: [PATCH 8/8] added export and import functions to README --- README.md | 35 ++++++++++++++++++++++++++++++++++- cmd/import.go | 6 ++++-- docs/kfutil_export.md | 30 ++++++++++++++++++++++++++++++ docs/kfutil_import.md | 30 ++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 docs/kfutil_export.md create mode 100644 docs/kfutil_import.md diff --git a/README.md b/README.md index 846f666..130ef03 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,40 @@ kfutil stores inventory remove \ --cn ``` +### Export Instance Data + +For full documentation, see [export](docs/kfutil_export.md). + +Export select instance data to JSON file: + +```bash +# export only collections, metadata, and roles +kfutil export --collections --metadata --roles --file +``` + +Export all exportable instance data to JSON file: + +```bash +kfutil export --all --file +``` + +### Import Instance Data + +For full documentation, see [import](docs/kfutil_import.md). + +Import select instance data from exported JSON file: + +```bash +# export only collections, metadata, and roles +kfutil import --collections --metadata --roles --file +``` + +Import all importable instance data from exported JSON file: + +```bash +kfutil import --all --file +``` + ## Development This CLI developed using [cobra](https://umarcor.github.io/cobra/) @@ -379,4 +413,3 @@ alternatively you can specify the parent command ```bash cobra-cli add -p 'Cmd' ``` - diff --git a/cmd/import.go b/cmd/import.go index 37826bc..90ee726 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -167,7 +167,8 @@ func importNetworks(networks []keyfactor_command_client_api.KeyfactorApiModelsSs } } -func findMatchingTempIds(exportedWorkflowDef exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest, kfClient *keyfactor_command_client_api.APIClient) *string { +// identify matching templates between instances by name, then return the template Id of the matching template in the import instance +func findMatchingTemplates(exportedWorkflowDef exportKeyfactorApiModelsWorkflowsDefinitionCreateRequest, kfClient *keyfactor_command_client_api.APIClient) *string { importInstanceTemplates, _, _ := kfClient.TemplateApi.TemplateGetTemplates(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() for _, template := range importInstanceTemplates { importInstTempNameJson, _ := json.Marshal(template.TemplateName) @@ -190,7 +191,7 @@ func importWorkflowDefinitions(workflowDefs []exportKeyfactorApiModelsWorkflowsD fmt.Printf("Error: %s\n", jErr) log.Fatalf("Error: %s", jErr) } - newTemplateId := findMatchingTempIds(workflowDef, kfClient) + newTemplateId := findMatchingTemplates(workflowDef, kfClient) if newTemplateId != nil { workflowDefReq.Key = newTemplateId } @@ -204,6 +205,7 @@ func importWorkflowDefinitions(workflowDefs []exportKeyfactorApiModelsWorkflowsD } } +// check for built-in report discrepancies between instances, return the report id of reports that need to be updated in import instance func checkBuiltInReportDiffs(exportedReport exportModelsReport, kfClient *keyfactor_command_client_api.APIClient) *int32 { importInstanceReports, _, _ := kfClient.ReportsApi.ReportsQueryReports(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute() //check if built in report was modified from default in exported instance; if modified, update built-in report in new instance diff --git a/docs/kfutil_export.md b/docs/kfutil_export.md new file mode 100644 index 0000000..5e38448 --- /dev/null +++ b/docs/kfutil_export.md @@ -0,0 +1,30 @@ +## kfutil export + +Keyfactor export utilities. + +### Synopsis + +A collections of APIs and utilities for exporting Keyfactor instance data. + +### Options + +``` + -a, --all export all exportable data to JSON file + -c, --collections export collections to JSON file + -d, --denied-alerts export denied cert alerts to JSON file + -e, --expiration-alerts export expiration cert alerts to JSON file + -f, --file string export JSON to a specified filepath + -h, --help help for export + -i, --issued-alerts export issued cert alerts to JSON file + -m, --metadata export metadata to JSON file + -n, --networks export SSL networks to JSON file + -p, --pending-alerts export pending cert alerts to JSON file + -r, --reports export reports to JSON file + -s, --security-roles export security roles to JSON file + -w, --workflow-definitions export workflow definitions to JSON file +``` + +### SEE ALSO + +* [kfutil](kfutil.md) - Keyfactor CLI utilities + diff --git a/docs/kfutil_import.md b/docs/kfutil_import.md new file mode 100644 index 0000000..fa8e1ce --- /dev/null +++ b/docs/kfutil_import.md @@ -0,0 +1,30 @@ +## kfutil import + +Keyfactor import utilities. + +### Synopsis + +A collections of APIs and utilities for importing Keyfactor instance data. + +### Options + +``` + -a, --all import all importable data to JSON file + -c, --collections import collections to JSON file + -d, --denied-alerts import denied cert alerts to JSON file + -e, --expiration-alerts import expiration cert alerts to JSON file + -f, --file string import JSON to a specified filepath + -h, --help help for import + -i, --issued-alerts import issued cert alerts to JSON file + -m, --metadata import metadata to JSON file + -n, --networks import SSL networks to JSON file + -p, --pending-alerts import pending cert alerts to JSON file + -r, --reports import reports to JSON file + -s, --security-roles import security roles to JSON file + -w, --workflow-definitions import workflow definitions to JSON file +``` + +### SEE ALSO + +* [kfutil](kfutil.md) - Keyfactor CLI utilities +