Skip to content

Commit 3a53e44

Browse files
committed
feat(migrate): add check command for finding active pam usage
1 parent 955598e commit 3a53e44

File tree

1 file changed

+127
-3
lines changed

1 file changed

+127
-3
lines changed

cmd/migrate.go

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"errors"
2121
"fmt"
22+
"strings"
2223

2324
// "github.com/Keyfactor/keyfactor-go-client-sdk/v24/api/keyfactor/v2"
2425
"github.com/Keyfactor/keyfactor-go-client-sdk/v2/api/keyfactor"
@@ -35,6 +36,99 @@ var migrateCmd = &cobra.Command{
3536
to new Extension implementations that have definitions that differ from prior releases.`,
3637
}
3738

39+
var migrateCheckCmd = &cobra.Command{
40+
Use: "check",
41+
Short: "Check usage of a feature to migrate. Currently only PAM is supported.",
42+
Long: "Check usage of a feature to migrate. Currently only PAM is supported",
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
cmd.SilenceUsage = true
45+
isExperimental := true
46+
47+
// load specified flags
48+
fromCheck, _ := cmd.Flags().GetString("from") // name of entity, e.g. PAM Provider
49+
pamCheck, _ := cmd.Flags().GetBool("pam-usage")
50+
51+
if pamCheck == false {
52+
return errors.New("Flag --pam-usage was not specified, but this is the only currently supported use case.")
53+
}
54+
55+
// Debug + expEnabled checks
56+
informDebug(debugFlag)
57+
debugErr := warnExperimentalFeature(expEnabled, isExperimental)
58+
if debugErr != nil {
59+
return debugErr
60+
}
61+
62+
// Log flags
63+
log.Info().Str("from", fromCheck).
64+
Bool("pam-usage", pamCheck).
65+
Msg("migrate PAM Provider")
66+
67+
sdkClient, err := initGenClient(false)
68+
if err != nil {
69+
return err
70+
}
71+
72+
// get all secret GUIDs for PAM Provider
73+
found, pamProvider, err := getExistingPamProvider(sdkClient, fromCheck)
74+
75+
activePamSecretGuids := map[string]bool{}
76+
for _, param := range pamProvider.ProviderTypeParamValues {
77+
if param.InstanceGuid != nil {
78+
// enter every instance guid as a key with value true
79+
// represents an active Secret being managed in this pam provider
80+
// the same key will be set multiple times for each parameter for a particular Secret, but this should be no issue
81+
activePamSecretGuids[*param.InstanceGuid] = true
82+
}
83+
}
84+
85+
if err != nil {
86+
log.Error().Err(err).Send()
87+
return err
88+
}
89+
90+
if found == false {
91+
return errors.New("Named entity in 'from' argument was not found, no check can be run.")
92+
}
93+
94+
legacyClient, err := initClient(false)
95+
if err != nil {
96+
return err
97+
}
98+
99+
// get all certificate stores
100+
certStoreList, err := legacyClient.ListCertificateStores(nil)
101+
102+
if err != nil {
103+
log.Error().Err(err).Send()
104+
return err
105+
}
106+
107+
certStoreGuids := map[string]bool{}
108+
// loop through every found certificate store
109+
for _, store := range *certStoreList {
110+
// get properties field, as this will contain the Secret GUID for one of our active Instances if the PAM provider is in use
111+
storeProperties := store.PropertiesString
112+
113+
// loop through all found Instance GUIDs of the PAM Provider
114+
// if the GUID is present in the Properties field, add this Store ID to the list to return
115+
for instanceGuid, _ := range activePamSecretGuids {
116+
if strings.Contains(storeProperties, instanceGuid) {
117+
certStoreGuids[store.Id] = true
118+
}
119+
}
120+
}
121+
122+
// print out list of Cert Store GUIDs
123+
fmt.Println("\nThe following Cert Store Ids are using the PAM Provider with name '" + fromCheck + "'\n")
124+
for storeId, _ := range certStoreGuids {
125+
fmt.Println(storeId)
126+
}
127+
128+
return nil
129+
},
130+
}
131+
38132
var migratePamCmd = &cobra.Command{
39133
Use: "pam",
40134
Short: "Migrate existing PAM Provider usage to a new PAM Provider",
@@ -303,12 +397,18 @@ func getExistingPamProvider(sdkClient *keyfactor.APIClient, name string) (bool,
303397
return false, pamProvider, returnHttpErr(httpResponse, err)
304398
}
305399

306-
if len(foundProvider) != 1 {
400+
if len(foundProvider) > 1 {
307401
logMsg = "More than one PAM Provider returned for the same name. This is not supported behavior."
308402
log.Error().Msg(logMsg)
309403
return false, pamProvider, errors.New(logMsg)
310404
}
311405

406+
if len(foundProvider) == 0 {
407+
logMsg = "No PAM Provider was found with the given name."
408+
log.Warn().Msg(logMsg)
409+
return false, pamProvider, nil
410+
}
411+
312412
return true, foundProvider[0], nil
313413
}
314414

@@ -465,13 +565,37 @@ func buildMigratedPamSecret(secretProp map[string]interface{}, fromProviderLevel
465565
}
466566

467567
func init() {
568+
RootCmd.AddCommand(migrateCmd)
569+
570+
// migrate check
571+
var pamCheck bool
572+
var fromCheck string
573+
574+
migrateCmd.AddCommand(migrateCheckCmd)
575+
576+
migrateCheckCmd.Flags().BoolVar(
577+
&pamCheck,
578+
"pam-usage",
579+
true,
580+
"Specify this flag to check usage of a PAM Provider named with the 'from' argument. Returns a list of Certificate Store GUIDs using that provider.",
581+
)
582+
583+
migrateCheckCmd.Flags().StringVarP(
584+
&fromCheck,
585+
"from",
586+
"f",
587+
"",
588+
"The name of the KF entity to search for usage of. Behavior will be different depending on type of check specified.",
589+
)
590+
591+
migrateCheckCmd.MarkFlagRequired("from")
592+
593+
// migrate pam
468594
var from string
469595
var to string
470596
var appendName string
471597
var store string
472598

473-
RootCmd.AddCommand(migrateCmd)
474-
475599
migrateCmd.AddCommand(migratePamCmd)
476600

477601
migratePamCmd.Flags().StringVarP(

0 commit comments

Comments
 (0)