Skip to content

Commit

Permalink
feat(store-types): Add ability to create store-types from `integratio…
Browse files Browse the repository at this point in the history
…n-manifest.json`

Signed-off-by: spbsoluble <[email protected]>
  • Loading branch information
spbsoluble committed Dec 9, 2024
1 parent 4b1ca60 commit 9e407af
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
30 changes: 30 additions & 0 deletions cmd/integration_manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/Keyfactor/keyfactor-go-client/v3/api"
)

type IntegrationManifest struct {
Schema string `json:"$schema"`
IntegrationType string `json:"integration_type"`
Name string `json:"name"`
Status string `json:"status"`
LinkGithub bool `json:"link_github"`
UpdateCatalog bool `json:"update_catalog"`
SupportLevel string `json:"support_level"`
ReleaseDir string `json:"release_dir"`
ReleaseProject string `json:"release_project"`
Description string `json:"description"`
About About `json:"about"`
}

type About struct {
Orchestrator Orchestrator `json:"orchestrator"`
}

type Orchestrator struct {
UOFramework string `json:"UOFramework"`
PAMSupport bool `json:"pam_support"`
KeyfactorPlatformVersion string `json:"keyfactor_platform_version"`
StoreTypes []api.CertificateStoreType `json:"store_types"`
}
57 changes: 45 additions & 12 deletions cmd/storeTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ var storesTypeCreateCmd = &cobra.Command{
}

if storeTypeConfigFile != "" {
createdStore, err := createStoreFromFile(storeTypeConfigFile, kfClient)
createdStoreTypes, err := createStoreTypeFromFile(storeTypeConfigFile, kfClient)
if err != nil {
fmt.Printf("Failed to create store type from file \"%s\"", err)
return err
}

fmt.Printf("Created store type called \"%s\"\n", createdStore.Name)
for _, v := range createdStoreTypes {
fmt.Printf("Created store type \"%s\"\n", v.Name)
}
return nil
}

Expand Down Expand Up @@ -409,27 +411,58 @@ var fetchStoreTypesCmd = &cobra.Command{
// return nil
// },
// }
func createStoreFromFile(filename string, kfClient *api.Client) (*api.CertificateStoreType, error) {
func createStoreTypeFromFile(filename string, kfClient *api.Client) ([]api.CertificateStoreType, error) {
// Read the file
log.Debug().Str("filename", filename).Msg("Reading store type from file")
file, err := os.Open(filename)
defer file.Close()
if err != nil {
log.Error().
Str("filename", filename).
Err(err).Msg("unable to open file")
return nil, err
}

// Compile JSON contents to a api.CertificateStoreType struct
var storeType api.CertificateStoreType
var sType api.CertificateStoreType
var sTypes []api.CertificateStoreType

log.Debug().Msg("Decoding JSON file as single store type")
decoder := json.NewDecoder(file)
err = decoder.Decode(&storeType)
if err != nil {
return nil, err
err = decoder.Decode(&sType)
if err != nil || (sType.ShortName == "" && sType.Capability == "") {
log.Warn().Err(err).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
_, err = file.Seek(0, 0)
decoder = json.NewDecoder(file)
mErr := decoder.Decode(&manifest)
if mErr != nil {
return nil, err
}
log.Debug().Msg("Decoded JSON file as integration manifest")
sTypes = manifest.About.Orchestrator.StoreTypes
} else {
log.Debug().Msg("Decoded JSON file as single store type")
sTypes = []api.CertificateStoreType{sType}
}

// Use the Keyfactor client to create the store type
createResp, err := kfClient.CreateStoreType(&storeType)
if err != nil {
return nil, err
for _, st := range sTypes {
log.Debug().Msgf("Creating certificate store type %s", st.Name)
createResp, cErr := kfClient.CreateStoreType(&st)
if cErr != nil {
log.Error().
Str("storeType", st.Name).
Err(cErr).Msg("unable to create certificate store type")
return nil, cErr
}
log.Info().Msgf("Certificate store type %s created with ID: %d", st.Name, createResp.StoreType)
}
return createResp, nil
// Use the Keyfactor client to create the store type
log.Debug().Msg("Store type created")
return sTypes, nil
}

func formatStoreTypes(sTypesList *[]interface{}) (map[string]interface{}, error) {
Expand Down

0 comments on commit 9e407af

Please sign in to comment.