@@ -19,6 +19,7 @@ import (
19
19
"encoding/json"
20
20
"errors"
21
21
"fmt"
22
+ "strings"
22
23
23
24
// "github.com/Keyfactor/keyfactor-go-client-sdk/v24/api/keyfactor/v2"
24
25
"github.com/Keyfactor/keyfactor-go-client-sdk/v2/api/keyfactor"
@@ -35,6 +36,99 @@ var migrateCmd = &cobra.Command{
35
36
to new Extension implementations that have definitions that differ from prior releases.` ,
36
37
}
37
38
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 ("\n The 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
+
38
132
var migratePamCmd = & cobra.Command {
39
133
Use : "pam" ,
40
134
Short : "Migrate existing PAM Provider usage to a new PAM Provider" ,
@@ -303,12 +397,18 @@ func getExistingPamProvider(sdkClient *keyfactor.APIClient, name string) (bool,
303
397
return false , pamProvider , returnHttpErr (httpResponse , err )
304
398
}
305
399
306
- if len (foundProvider ) != 1 {
400
+ if len (foundProvider ) > 1 {
307
401
logMsg = "More than one PAM Provider returned for the same name. This is not supported behavior."
308
402
log .Error ().Msg (logMsg )
309
403
return false , pamProvider , errors .New (logMsg )
310
404
}
311
405
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
+
312
412
return true , foundProvider [0 ], nil
313
413
}
314
414
@@ -465,13 +565,37 @@ func buildMigratedPamSecret(secretProp map[string]interface{}, fromProviderLevel
465
565
}
466
566
467
567
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
468
594
var from string
469
595
var to string
470
596
var appendName string
471
597
var store string
472
598
473
- RootCmd .AddCommand (migrateCmd )
474
-
475
599
migrateCmd .AddCommand (migratePamCmd )
476
600
477
601
migratePamCmd .Flags ().StringVarP (
0 commit comments