Skip to content

Commit

Permalink
feat(store-types): Add ability to use integration-manifest.json fro…
Browse files Browse the repository at this point in the history
…m custom `Keyfactor` git repo, default to `kfutil` repo.

Signed-off-by: spbsoluble <[email protected]>
  • Loading branch information
spbsoluble committed Dec 9, 2024
1 parent 9e407af commit 0596632
Show file tree
Hide file tree
Showing 55 changed files with 127 additions and 70 deletions.
3 changes: 3 additions & 0 deletions cmd/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ const (
DefaultAPIPath = "KeyfactorAPI"
DefaultConfigFileName = "command_config.json"
DefaultStoreTypesFileName = "store_types.json"
DefaultGitRepo = "kfutil"
DefaultGitRef = "main"
FailedAuthMsg = "Login failed!"
SuccessfulAuthMsg = "Login successful!"
XKeyfactorRequestedWith = "APIClient"
XKeyfactorApiVersion = "1"
FlagGitRef = "git-ref"
FlagGitRepo = "repo"
FlagFromFile = "from-file"
DebugFuncEnter = "entered: %s"
DebugFuncExit = "exiting: %s"
Expand Down
83 changes: 67 additions & 16 deletions cmd/storeTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ var storesTypeCreateCmd = &cobra.Command{
cmd.SilenceUsage = true
// Specific flags
gitRef, _ := cmd.Flags().GetString(FlagGitRef)
gitRepo, _ := cmd.Flags().GetString(FlagGitRepo)
creatAll, _ := cmd.Flags().GetBool("all")
validStoreTypes := getValidStoreTypes("", gitRef)
validStoreTypes := getValidStoreTypes("", gitRef, gitRepo)
storeType, _ := cmd.Flags().GetString("name")
listTypes, _ := cmd.Flags().GetBool("list")
storeTypeConfigFile, _ := cmd.Flags().GetString("from-file")
Expand All @@ -110,7 +111,10 @@ var storesTypeCreateCmd = &cobra.Command{

// CLI Logic
if gitRef == "" {
gitRef = "main"
gitRef = DefaultGitRef
}
if gitRepo == "" {
gitRepo = DefaultGitRepo
}
storeTypeIsValid := false

Expand All @@ -119,6 +123,7 @@ var storesTypeCreateCmd = &cobra.Command{
Str("storeTypeConfigFile", storeTypeConfigFile).
Bool("creatAll", creatAll).
Str("gitRef", gitRef).
Str("gitRepo", gitRepo).
Strs("validStoreTypes", validStoreTypes).
Msg("create command flags")

Expand Down Expand Up @@ -183,7 +188,7 @@ var storesTypeCreateCmd = &cobra.Command{
} else {
typesToCreate = validStoreTypes
}
storeTypeConfig, stErr := readStoreTypesConfig("", gitRef, offline)
storeTypeConfig, stErr := readStoreTypesConfig("", gitRef, gitRepo, offline)
if stErr != nil {
log.Error().Err(stErr).Send()
return stErr
Expand Down Expand Up @@ -263,7 +268,7 @@ var storesTypeDeleteCmd = &cobra.Command{
validStoreTypesResp, vstErr := kfClient.ListCertificateStoreTypes()
if vstErr != nil {
log.Error().Err(vstErr).Msg("unable to list certificate store types")
validStoreTypes = getValidStoreTypes("", gitRef)
validStoreTypes = getValidStoreTypes("", gitRef, DefaultGitRepo)
} else {
for _, v := range *validStoreTypesResp {
validStoreTypes = append(validStoreTypes, v.ShortName)
Expand Down Expand Up @@ -360,6 +365,7 @@ var fetchStoreTypesCmd = &cobra.Command{
cmd.SilenceUsage = true
// Specific flags
gitRef, _ := cmd.Flags().GetString(FlagGitRef)
gitRepo, _ := cmd.Flags().GetString(FlagGitRepo)

// Debug + expEnabled checks
isExperimental := false
Expand All @@ -372,7 +378,7 @@ var fetchStoreTypesCmd = &cobra.Command{
if gitRef == "" {
gitRef = "main"
}
templates, err := readStoreTypesConfig("", gitRef, offline)
templates, err := readStoreTypesConfig("", gitRef, gitRepo, offline)
if err != nil {
log.Error().Err(err).Send()
return err
Expand Down Expand Up @@ -480,15 +486,26 @@ func formatStoreTypes(sTypesList *[]interface{}) (map[string]interface{}, error)
return output, nil
}

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

baseUrl := "https://raw.githubusercontent.com/Keyfactor/kfutil/%s/store_types.json"
baseUrl := "https://raw.githubusercontent.com/Keyfactor/%s/%s/%s"
if gitRef == "" {
gitRef = "main"
gitRef = DefaultGitRef
}
if repo == "" {
repo = DefaultGitRepo
}

var fileName string
if repo == "kfutil" {
fileName = "store_types.json"
} else {
fileName = "integration-manifest.json"
}
url := fmt.Sprintf(baseUrl, gitRef)

url := fmt.Sprintf(baseUrl, repo, gitRef, fileName)
log.Debug().
Str("url", url).
Msg("Getting store types from internet")
Expand All @@ -513,7 +530,23 @@ func getStoreTypesInternet(gitRef string) (map[string]interface{}, error) {
var result []interface{}
jErr := json.Unmarshal(body, &result)
if jErr != nil {
return nil, jErr
log.Warn().Err(jErr).Msg("Unable to decode JSON file, attempting to parse an integration manifest")
// Attempt to parse as an integration manifest
var manifest IntegrationManifest
log.Debug().Msg("Decoding JSON file as integration manifest")
// Reset the file pointer

mErr := json.Unmarshal(body, &manifest)
if mErr != nil {
return nil, jErr
}
log.Debug().Msg("Decoded JSON file as integration manifest")
sTypes := manifest.About.Orchestrator.StoreTypes
output := make(map[string]interface{})
for _, st := range sTypes {
output[st.ShortName] = st
}
return output, nil
}
output, sErr := formatStoreTypes(&result)
if sErr != nil {
Expand All @@ -525,18 +558,20 @@ func getStoreTypesInternet(gitRef string) (map[string]interface{}, error) {

}

func getValidStoreTypes(fp string, gitRef string) []string {
func getValidStoreTypes(fp string, gitRef string, gitRepo string) []string {
log.Debug().
Str("file", fp).
Str("gitRef", gitRef).
Str("gitRepo", gitRepo).
Bool("offline", offline).
Msg(DebugFuncEnter)

log.Debug().
Str("file", fp).
Str("gitRef", gitRef).
Str("gitRepo", gitRepo).
Msg("Reading store types config.")
validStoreTypes, rErr := readStoreTypesConfig(fp, gitRef, offline)
validStoreTypes, rErr := readStoreTypesConfig(fp, gitRef, gitRepo, offline)
if rErr != nil {
log.Error().Err(rErr).Msg("unable to read store types")
return nil
Expand All @@ -549,7 +584,7 @@ func getValidStoreTypes(fp string, gitRef string) []string {
return validStoreTypesList
}

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

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

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

func init() {
defaultGitRef := "main"
offline = true // temporarily set to true as it runs before the flag is set
debugFlag = false // temporarily set to false as it runs before the flag is set
var gitRef string
validTypesString := strings.Join(getValidStoreTypes("", defaultGitRef), ", ")
var gitRepo string
validTypesString := strings.Join(getValidStoreTypes("", DefaultGitRef, DefaultGitRepo), ", ")
offline = false //revert this so that flag is not set to true by default
RootCmd.AddCommand(storeTypesCmd)

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

fetchStoreTypesCmd.Flags().StringVarP(
&gitRepo,
FlagGitRepo,
"r",
DefaultGitRepo,
"The repository to pull store-type definitions from.",
)

// LIST command
storeTypesCmd.AddCommand(storesTypesListCmd)

Expand Down Expand Up @@ -653,6 +696,14 @@ func init() {
"main",
"The git branch or tag to reference when pulling store-types from the internet.",
)
storesTypeCreateCmd.Flags().StringVarP(
&gitRepo,
FlagGitRepo,
"r",
DefaultGitRepo,
"The repository to pull store-types definitions from.",
)

storesTypeCreateCmd.Flags().BoolVarP(&createAll, "all", "a", false, "Create all store types.")

// UPDATE command
Expand Down
3 changes: 2 additions & 1 deletion cmd/storeTypes_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ type StoreTypesGetOptions struct {
storeTypeName string
genericFormat bool
gitRef string
gitRepo string
storeTypeInterface interface{}
outputType string
outputToIntegrationManifest bool
Expand Down Expand Up @@ -243,7 +244,7 @@ func (f *StoreTypesGetOptions) Validate() error {
// Check inputs and prompt if necessary
// The f.storeTypeInterface is used to pass the store type to the API
if f.storeTypeID < 0 && f.storeTypeName == "" {
validStoreTypes := getValidStoreTypes("", f.gitRef)
validStoreTypes := getValidStoreTypes("", f.gitRef, DefaultGitRepo)
prompt := &survey.Select{
Message: "Choose a store type:",
Options: validStoreTypes,
Expand Down
4 changes: 2 additions & 2 deletions cmd/storesBulkOperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func formatProperties(json *gabs.Container, reqPropertiesForStoreType []string)

func serializeStoreFromTypeDef(storeTypeName string, input string) (string, error) {
// check if storetypename is an integer
storeTypes, _ := readStoreTypesConfig("", "", offline)
storeTypes, _ := readStoreTypesConfig("", DefaultGitRef, DefaultGitRepo, offline)
log.Debug().
Str("storeTypeName", storeTypeName).
Msg("checking if storeTypeName is an integer")
Expand Down Expand Up @@ -399,7 +399,7 @@ Store type IDs can be found by running the "store-types" command.`,
validStoreTypesResp, vstErr := kfClient.ListCertificateStoreTypes()
if vstErr != nil {
log.Error().Err(vstErr).Msg("unable to list certificate store types")
validStoreTypes = getValidStoreTypes("", "main")
validStoreTypes = getValidStoreTypes("", DefaultGitRef, DefaultGitRepo)
} else {
for _, v := range *validStoreTypesResp {
validStoreTypes = append(validStoreTypes, v.ShortName)
Expand Down
2 changes: 1 addition & 1 deletion docs/kfutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ A CLI wrapper around the Keyfactor Platform API.
* [kfutil stores](kfutil_stores.md) - Keyfactor certificate stores APIs and utilities.
* [kfutil version](kfutil_version.md) - Shows version of kfutil

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ See each sub-command's help for details on how to use the generated script.
* [kfutil completion powershell](kfutil_completion_powershell.md) - Generate the autocompletion script for powershell
* [kfutil completion zsh](kfutil_completion_zsh.md) - Generate the autocompletion script for zsh

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_completion_bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ kfutil completion bash

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

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_completion_fish.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ kfutil completion fish [flags]

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

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_completion_powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ kfutil completion powershell [flags]

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

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ kfutil completion zsh [flags]

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

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ A collections of APIs and utilities for interacting with Keyfactor certificate s
* [kfutil containers get](kfutil_containers_get.md) - Get certificate store container by ID or name.
* [kfutil containers list](kfutil_containers_list.md) - List certificate store containers.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_containers_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kfutil containers get [flags]

* [kfutil containers](kfutil_containers.md) - Keyfactor certificate store container API and utilities.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_containers_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ kfutil containers list [flags]

* [kfutil containers](kfutil_containers.md) - Keyfactor certificate store container API and utilities.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ kfutil export [flags]

* [kfutil](kfutil.md) - Keyfactor CLI utilities

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ kubectl helm uo | helm install -f - keyfactor-universal-orchestrator keyfactor/k
* [kfutil](kfutil.md) - Keyfactor CLI utilities
* [kfutil helm uo](kfutil_helm_uo.md) - Configure the Keyfactor Universal Orchestrator Helm Chart

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_helm_uo.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ kfutil helm uo [-t <token>] [-o <path>] [-f <file, url, or '-'>] [-e <extension

* [kfutil helm](kfutil_helm.md) - Helm utilities for configuring Keyfactor Helm charts

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_import.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ kfutil import [flags]

* [kfutil](kfutil.md) - Keyfactor CLI utilities

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_login.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ kfutil login [flags]

* [kfutil](kfutil.md) - Keyfactor CLI utilities

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_logout.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ kfutil logout [flags]

* [kfutil](kfutil.md) - Keyfactor CLI utilities

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_orchs.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ A collections of APIs and utilities for interacting with Keyfactor orchestrators
* [kfutil orchs logs](kfutil_orchs_logs.md) - Get orchestrator logs by machine/client name.
* [kfutil orchs reset](kfutil_orchs_reset.md) - Reset orchestrator by machine/client name.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_orchs_approve.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kfutil orchs approve [flags]

* [kfutil orchs](kfutil_orchs.md) - Keyfactor agents/orchestrators APIs and utilities.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_orchs_disapprove.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kfutil orchs disapprove [flags]

* [kfutil orchs](kfutil_orchs.md) - Keyfactor agents/orchestrators APIs and utilities.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_orchs_ext.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ ext -t <token> -e <extension>@<version>,<extension>@<version> -o ./app/extension

* [kfutil orchs](kfutil_orchs.md) - Keyfactor agents/orchestrators APIs and utilities.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_orchs_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kfutil orchs get [flags]

* [kfutil orchs](kfutil_orchs.md) - Keyfactor agents/orchestrators APIs and utilities.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_orchs_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ kfutil orchs list [flags]

* [kfutil orchs](kfutil_orchs.md) - Keyfactor agents/orchestrators APIs and utilities.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_orchs_logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kfutil orchs logs [flags]

* [kfutil orchs](kfutil_orchs.md) - Keyfactor agents/orchestrators APIs and utilities.

###### Auto generated by spf13/cobra on 20-Nov-2024
###### Auto generated by spf13/cobra on 9-Dec-2024
2 changes: 1 addition & 1 deletion docs/kfutil_orchs_reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kfutil orchs reset [flags]

* [kfutil orchs](kfutil_orchs.md) - Keyfactor agents/orchestrators APIs and utilities.

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

0 comments on commit 0596632

Please sign in to comment.