Skip to content

Commit 0596632

Browse files
committed
feat(store-types): Add ability to use integration-manifest.json from custom Keyfactor git repo, default to kfutil repo.
Signed-off-by: spbsoluble <[email protected]>
1 parent 9e407af commit 0596632

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+127
-70
lines changed

cmd/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ const (
2121
DefaultAPIPath = "KeyfactorAPI"
2222
DefaultConfigFileName = "command_config.json"
2323
DefaultStoreTypesFileName = "store_types.json"
24+
DefaultGitRepo = "kfutil"
25+
DefaultGitRef = "main"
2426
FailedAuthMsg = "Login failed!"
2527
SuccessfulAuthMsg = "Login successful!"
2628
XKeyfactorRequestedWith = "APIClient"
2729
XKeyfactorApiVersion = "1"
2830
FlagGitRef = "git-ref"
31+
FlagGitRepo = "repo"
2932
FlagFromFile = "from-file"
3033
DebugFuncEnter = "entered: %s"
3134
DebugFuncExit = "exiting: %s"

cmd/storeTypes.go

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ var storesTypeCreateCmd = &cobra.Command{
9191
cmd.SilenceUsage = true
9292
// Specific flags
9393
gitRef, _ := cmd.Flags().GetString(FlagGitRef)
94+
gitRepo, _ := cmd.Flags().GetString(FlagGitRepo)
9495
creatAll, _ := cmd.Flags().GetBool("all")
95-
validStoreTypes := getValidStoreTypes("", gitRef)
96+
validStoreTypes := getValidStoreTypes("", gitRef, gitRepo)
9697
storeType, _ := cmd.Flags().GetString("name")
9798
listTypes, _ := cmd.Flags().GetBool("list")
9899
storeTypeConfigFile, _ := cmd.Flags().GetString("from-file")
@@ -110,7 +111,10 @@ var storesTypeCreateCmd = &cobra.Command{
110111

111112
// CLI Logic
112113
if gitRef == "" {
113-
gitRef = "main"
114+
gitRef = DefaultGitRef
115+
}
116+
if gitRepo == "" {
117+
gitRepo = DefaultGitRepo
114118
}
115119
storeTypeIsValid := false
116120

@@ -119,6 +123,7 @@ var storesTypeCreateCmd = &cobra.Command{
119123
Str("storeTypeConfigFile", storeTypeConfigFile).
120124
Bool("creatAll", creatAll).
121125
Str("gitRef", gitRef).
126+
Str("gitRepo", gitRepo).
122127
Strs("validStoreTypes", validStoreTypes).
123128
Msg("create command flags")
124129

@@ -183,7 +188,7 @@ var storesTypeCreateCmd = &cobra.Command{
183188
} else {
184189
typesToCreate = validStoreTypes
185190
}
186-
storeTypeConfig, stErr := readStoreTypesConfig("", gitRef, offline)
191+
storeTypeConfig, stErr := readStoreTypesConfig("", gitRef, gitRepo, offline)
187192
if stErr != nil {
188193
log.Error().Err(stErr).Send()
189194
return stErr
@@ -263,7 +268,7 @@ var storesTypeDeleteCmd = &cobra.Command{
263268
validStoreTypesResp, vstErr := kfClient.ListCertificateStoreTypes()
264269
if vstErr != nil {
265270
log.Error().Err(vstErr).Msg("unable to list certificate store types")
266-
validStoreTypes = getValidStoreTypes("", gitRef)
271+
validStoreTypes = getValidStoreTypes("", gitRef, DefaultGitRepo)
267272
} else {
268273
for _, v := range *validStoreTypesResp {
269274
validStoreTypes = append(validStoreTypes, v.ShortName)
@@ -360,6 +365,7 @@ var fetchStoreTypesCmd = &cobra.Command{
360365
cmd.SilenceUsage = true
361366
// Specific flags
362367
gitRef, _ := cmd.Flags().GetString(FlagGitRef)
368+
gitRepo, _ := cmd.Flags().GetString(FlagGitRepo)
363369

364370
// Debug + expEnabled checks
365371
isExperimental := false
@@ -372,7 +378,7 @@ var fetchStoreTypesCmd = &cobra.Command{
372378
if gitRef == "" {
373379
gitRef = "main"
374380
}
375-
templates, err := readStoreTypesConfig("", gitRef, offline)
381+
templates, err := readStoreTypesConfig("", gitRef, gitRepo, offline)
376382
if err != nil {
377383
log.Error().Err(err).Send()
378384
return err
@@ -480,15 +486,26 @@ func formatStoreTypes(sTypesList *[]interface{}) (map[string]interface{}, error)
480486
return output, nil
481487
}
482488

483-
func getStoreTypesInternet(gitRef string) (map[string]interface{}, error) {
489+
func getStoreTypesInternet(gitRef string, repo string) (map[string]interface{}, error) {
484490
//resp, err := http.Get("https://raw.githubusercontent.com/keyfactor/kfutil/main/store_types.json")
485491
//resp, err := http.Get("https://raw.githubusercontent.com/keyfactor/kfctl/master/storetypes/storetypes.json")
486492

487-
baseUrl := "https://raw.githubusercontent.com/Keyfactor/kfutil/%s/store_types.json"
493+
baseUrl := "https://raw.githubusercontent.com/Keyfactor/%s/%s/%s"
488494
if gitRef == "" {
489-
gitRef = "main"
495+
gitRef = DefaultGitRef
496+
}
497+
if repo == "" {
498+
repo = DefaultGitRepo
499+
}
500+
501+
var fileName string
502+
if repo == "kfutil" {
503+
fileName = "store_types.json"
504+
} else {
505+
fileName = "integration-manifest.json"
490506
}
491-
url := fmt.Sprintf(baseUrl, gitRef)
507+
508+
url := fmt.Sprintf(baseUrl, repo, gitRef, fileName)
492509
log.Debug().
493510
Str("url", url).
494511
Msg("Getting store types from internet")
@@ -513,7 +530,23 @@ func getStoreTypesInternet(gitRef string) (map[string]interface{}, error) {
513530
var result []interface{}
514531
jErr := json.Unmarshal(body, &result)
515532
if jErr != nil {
516-
return nil, jErr
533+
log.Warn().Err(jErr).Msg("Unable to decode JSON file, attempting to parse an integration manifest")
534+
// Attempt to parse as an integration manifest
535+
var manifest IntegrationManifest
536+
log.Debug().Msg("Decoding JSON file as integration manifest")
537+
// Reset the file pointer
538+
539+
mErr := json.Unmarshal(body, &manifest)
540+
if mErr != nil {
541+
return nil, jErr
542+
}
543+
log.Debug().Msg("Decoded JSON file as integration manifest")
544+
sTypes := manifest.About.Orchestrator.StoreTypes
545+
output := make(map[string]interface{})
546+
for _, st := range sTypes {
547+
output[st.ShortName] = st
548+
}
549+
return output, nil
517550
}
518551
output, sErr := formatStoreTypes(&result)
519552
if sErr != nil {
@@ -525,18 +558,20 @@ func getStoreTypesInternet(gitRef string) (map[string]interface{}, error) {
525558

526559
}
527560

528-
func getValidStoreTypes(fp string, gitRef string) []string {
561+
func getValidStoreTypes(fp string, gitRef string, gitRepo string) []string {
529562
log.Debug().
530563
Str("file", fp).
531564
Str("gitRef", gitRef).
565+
Str("gitRepo", gitRepo).
532566
Bool("offline", offline).
533567
Msg(DebugFuncEnter)
534568

535569
log.Debug().
536570
Str("file", fp).
537571
Str("gitRef", gitRef).
572+
Str("gitRepo", gitRepo).
538573
Msg("Reading store types config.")
539-
validStoreTypes, rErr := readStoreTypesConfig(fp, gitRef, offline)
574+
validStoreTypes, rErr := readStoreTypesConfig(fp, gitRef, gitRepo, offline)
540575
if rErr != nil {
541576
log.Error().Err(rErr).Msg("unable to read store types")
542577
return nil
@@ -549,7 +584,7 @@ func getValidStoreTypes(fp string, gitRef string) []string {
549584
return validStoreTypesList
550585
}
551586

552-
func readStoreTypesConfig(fp, gitRef string, offline bool) (map[string]interface{}, error) {
587+
func readStoreTypesConfig(fp, gitRef string, gitRepo string, offline bool) (map[string]interface{}, error) {
553588
log.Debug().Str("file", fp).Str("gitRef", gitRef).Msg("Entering readStoreTypesConfig")
554589

555590
var (
@@ -560,7 +595,7 @@ func readStoreTypesConfig(fp, gitRef string, offline bool) (map[string]interface
560595
log.Debug().Msg("Reading store types config from file")
561596
} else {
562597
log.Debug().Msg("Reading store types config from internet")
563-
sTypes, stErr = getStoreTypesInternet(gitRef)
598+
sTypes, stErr = getStoreTypesInternet(gitRef, gitRepo)
564599
}
565600

566601
if stErr != nil || sTypes == nil || len(sTypes) == 0 {
@@ -600,11 +635,11 @@ func readStoreTypesConfig(fp, gitRef string, offline bool) (map[string]interface
600635
}
601636

602637
func init() {
603-
defaultGitRef := "main"
604638
offline = true // temporarily set to true as it runs before the flag is set
605639
debugFlag = false // temporarily set to false as it runs before the flag is set
606640
var gitRef string
607-
validTypesString := strings.Join(getValidStoreTypes("", defaultGitRef), ", ")
641+
var gitRepo string
642+
validTypesString := strings.Join(getValidStoreTypes("", DefaultGitRef, DefaultGitRepo), ", ")
608643
offline = false //revert this so that flag is not set to true by default
609644
RootCmd.AddCommand(storeTypesCmd)
610645

@@ -618,6 +653,14 @@ func init() {
618653
"The git branch or tag to reference when pulling store-types from the internet.",
619654
)
620655

656+
fetchStoreTypesCmd.Flags().StringVarP(
657+
&gitRepo,
658+
FlagGitRepo,
659+
"r",
660+
DefaultGitRepo,
661+
"The repository to pull store-type definitions from.",
662+
)
663+
621664
// LIST command
622665
storeTypesCmd.AddCommand(storesTypesListCmd)
623666

@@ -653,6 +696,14 @@ func init() {
653696
"main",
654697
"The git branch or tag to reference when pulling store-types from the internet.",
655698
)
699+
storesTypeCreateCmd.Flags().StringVarP(
700+
&gitRepo,
701+
FlagGitRepo,
702+
"r",
703+
DefaultGitRepo,
704+
"The repository to pull store-types definitions from.",
705+
)
706+
656707
storesTypeCreateCmd.Flags().BoolVarP(&createAll, "all", "a", false, "Create all store types.")
657708

658709
// UPDATE command

cmd/storeTypes_get.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ type StoreTypesGetOptions struct {
187187
storeTypeName string
188188
genericFormat bool
189189
gitRef string
190+
gitRepo string
190191
storeTypeInterface interface{}
191192
outputType string
192193
outputToIntegrationManifest bool
@@ -243,7 +244,7 @@ func (f *StoreTypesGetOptions) Validate() error {
243244
// Check inputs and prompt if necessary
244245
// The f.storeTypeInterface is used to pass the store type to the API
245246
if f.storeTypeID < 0 && f.storeTypeName == "" {
246-
validStoreTypes := getValidStoreTypes("", f.gitRef)
247+
validStoreTypes := getValidStoreTypes("", f.gitRef, DefaultGitRepo)
247248
prompt := &survey.Select{
248249
Message: "Choose a store type:",
249250
Options: validStoreTypes,

cmd/storesBulkOperations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func formatProperties(json *gabs.Container, reqPropertiesForStoreType []string)
7777

7878
func serializeStoreFromTypeDef(storeTypeName string, input string) (string, error) {
7979
// check if storetypename is an integer
80-
storeTypes, _ := readStoreTypesConfig("", "", offline)
80+
storeTypes, _ := readStoreTypesConfig("", DefaultGitRef, DefaultGitRepo, offline)
8181
log.Debug().
8282
Str("storeTypeName", storeTypeName).
8383
Msg("checking if storeTypeName is an integer")
@@ -399,7 +399,7 @@ Store type IDs can be found by running the "store-types" command.`,
399399
validStoreTypesResp, vstErr := kfClient.ListCertificateStoreTypes()
400400
if vstErr != nil {
401401
log.Error().Err(vstErr).Msg("unable to list certificate store types")
402-
validStoreTypes = getValidStoreTypes("", "main")
402+
validStoreTypes = getValidStoreTypes("", DefaultGitRef, DefaultGitRepo)
403403
} else {
404404
for _, v := range *validStoreTypesResp {
405405
validStoreTypes = append(validStoreTypes, v.ShortName)

docs/kfutil.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ A CLI wrapper around the Keyfactor Platform API.
4545
* [kfutil stores](kfutil_stores.md) - Keyfactor certificate stores APIs and utilities.
4646
* [kfutil version](kfutil_version.md) - Shows version of kfutil
4747

48-
###### Auto generated by spf13/cobra on 20-Nov-2024
48+
###### Auto generated by spf13/cobra on 9-Dec-2024

docs/kfutil_completion.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ See each sub-command's help for details on how to use the generated script.
4444
* [kfutil completion powershell](kfutil_completion_powershell.md) - Generate the autocompletion script for powershell
4545
* [kfutil completion zsh](kfutil_completion_zsh.md) - Generate the autocompletion script for zsh
4646

47-
###### Auto generated by spf13/cobra on 20-Nov-2024
47+
###### Auto generated by spf13/cobra on 9-Dec-2024

docs/kfutil_completion_bash.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ kfutil completion bash
6363

6464
* [kfutil completion](kfutil_completion.md) - Generate the autocompletion script for the specified shell
6565

66-
###### Auto generated by spf13/cobra on 20-Nov-2024
66+
###### Auto generated by spf13/cobra on 9-Dec-2024

docs/kfutil_completion_fish.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ kfutil completion fish [flags]
5454

5555
* [kfutil completion](kfutil_completion.md) - Generate the autocompletion script for the specified shell
5656

57-
###### Auto generated by spf13/cobra on 20-Nov-2024
57+
###### Auto generated by spf13/cobra on 9-Dec-2024

docs/kfutil_completion_powershell.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ kfutil completion powershell [flags]
5151

5252
* [kfutil completion](kfutil_completion.md) - Generate the autocompletion script for the specified shell
5353

54-
###### Auto generated by spf13/cobra on 20-Nov-2024
54+
###### Auto generated by spf13/cobra on 9-Dec-2024

docs/kfutil_completion_zsh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ kfutil completion zsh [flags]
6565

6666
* [kfutil completion](kfutil_completion.md) - Generate the autocompletion script for the specified shell
6767

68-
###### Auto generated by spf13/cobra on 20-Nov-2024
68+
###### Auto generated by spf13/cobra on 9-Dec-2024

0 commit comments

Comments
 (0)