Skip to content

Commit

Permalink
Merge pull request #109 from Keyfactor/manifest
Browse files Browse the repository at this point in the history
feat(store-types): Add flag to update integration manifest with retrieved store type definition
  • Loading branch information
m8rmclaren authored Nov 17, 2023
2 parents 1447000 + bec2695 commit 687e12d
Show file tree
Hide file tree
Showing 62 changed files with 1,780 additions and 427 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
uses: actions/checkout@v4
- name: Run tests
run: |
unset KFUTIL_DEBUG
export KFUTIL_DEBUG=1
go test -v ./cmd -run "^Test_StoreTypes*"
Test_StoreTypes_KFC_10_2_1:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -486,4 +486,4 @@ jobs:

# Run the tests with coverage found in the pkg directory
- name: Run tests
run: go test -v -cover ./pkg/...
run: go test -v -cover ./pkg/...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ vendor/
*.csv
/.vs/**/*
/.vscode/**/*
.DS_Store
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v1.3.0
## Features

### Store-Type Get
- Added `--output-to-integration-manifest` flag to `kfutil store-types get` to download a remote certificate store type definition into an `integration-manifest.json` file locally.
- This path now has the following usage: `get [-i <store-type-id> | -n <store-type-name>] [-b <git-ref>] [-g | --output-to-integration-manifest]`

# v1.2.0
## Features

Expand Down Expand Up @@ -39,4 +46,4 @@ Initial release of the Keyfactor Command Utility (kfutil)
Production Supported CLIs:
- `login`: [kfutil login](docs/kfutil_login.md)
- `store-types`: [kfutil store-types](docs/kfutil_store-types.md)
- `stores rot`: [kfutil rot](docs/kfutil_stores_rot.md)
- `stores rot`: [kfutil rot](docs/kfutil_stores_rot.md)
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ Keyfactor Command Utility (kfutil) is open source and there is **no SLA** for th
### Installation:

```bash
bash <(curl -s https://raw.githubusercontent.com/Keyfactor/kfutil/main/install.sh)
curl -fsSL -o get-kfutil.sh https://raw.githubusercontent.com/Keyfactor/kfutil/main/install.sh
chmod +x get-kfutil.sh
# Install kfutil to $HOME/.local/bin. Use -h for help and examples.
./get-kfutil.sh
````

### Environmental Variables
Expand Down
161 changes: 4 additions & 157 deletions cmd/storeTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/Keyfactor/keyfactor-go-client/v2/api"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -71,83 +70,6 @@ var storesTypesListCmd = &cobra.Command{
},
}

var storesTypeGetCmd = &cobra.Command{
Use: "get",
Short: "Get a specific store type by either name or ID.",
Long: `Get a specific store type by either name or ID.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
// Specific flags
genericFormat, _ := cmd.Flags().GetBool("generic")
gitRef, _ := cmd.Flags().GetString(FlagGitRef)
id, _ := cmd.Flags().GetInt("id")
name, _ := cmd.Flags().GetString("name")

// Debug + expEnabled checks
isExperimental := false
debugErr := warnExperimentalFeature(expEnabled, isExperimental)
if debugErr != nil {
return debugErr
}
informDebug(debugFlag)

// Authenticate
authConfig := createAuthConfigFromParams(kfcHostName, kfcUsername, kfcPassword, kfcDomain, kfcAPIPath)
kfClient, _ := initClient(configFile, profile, providerType, providerProfile, noPrompt, authConfig, false)

// CLI Logic
if gitRef == "" {
gitRef = "main"
}
outputType := "full"
if genericFormat {
outputType = "generic"
}
var st interface{}
// Check inputs
if id < 0 && name == "" {
validStoreTypes := getValidStoreTypes("", gitRef)
prompt := &survey.Select{
Message: "Choose a store type:",
Options: validStoreTypes,
}
var selected string
err := survey.AskOne(prompt, &selected)
if err != nil {
fmt.Println(err)
return err
}
st = selected
} else if id >= 0 && name != "" {
mexErr := fmt.Errorf("ID and Name are mutually exclusive")
log.Error().Err(mexErr).Send()
return mexErr
} else if id >= 0 {
st = id
} else if name != "" {
st = name
} else {
log.Error().Err(InvalidInputError).Send()
return InvalidInputError
}

storeTypes, err := kfClient.GetCertificateStoreType(st)
if err != nil {

log.Error().Err(err).Msg(fmt.Sprintf("unable to get certificate store type %s", st))
return err
}
output, jErr := formatStoreTypeOutput(storeTypes, outputFormat, outputType)
if jErr != nil {

log.Error().Err(jErr).Msg("unable to format certificate store type output")
return jErr
}
outputResult(output, outputFormat)
return nil
},
}

var storesTypeCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new certificate store type in Keyfactor.",
Expand Down Expand Up @@ -484,74 +406,6 @@ func createStoreFromFile(filename string, kfClient *api.Client) (*api.Certificat
return createResp, nil
}

func formatStoreTypeOutput(storeType *api.CertificateStoreType, outputFormat string, outputType string) (string, error) {
var sOut interface{}
sOut = storeType
if outputType == "generic" {
// Convert to api.GenericCertificateStoreType
var genericProperties []api.StoreTypePropertyDefinitionGeneric
for _, prop := range *storeType.Properties {
genericProp := api.StoreTypePropertyDefinitionGeneric{
Name: prop.Name,
DisplayName: prop.DisplayName,
Type: prop.Type,
DependsOn: prop.DependsOn,
DefaultValue: prop.DefaultValue,
Required: prop.Required,
}
genericProperties = append(genericProperties, genericProp)
}

var genericEntryParameters []api.EntryParameterGeneric
for _, param := range *storeType.EntryParameters {
genericParam := api.EntryParameterGeneric{
Name: param.Name,
DisplayName: param.DisplayName,
Type: param.Type,
RequiredWhen: param.RequiredWhen,
DependsOn: param.DependsOn,
DefaultValue: param.DefaultValue,
Options: param.Options,
}
genericEntryParameters = append(genericEntryParameters, genericParam)
}

genericStoreType := api.CertificateStoreTypeGeneric{
Name: storeType.Name,
ShortName: storeType.ShortName,
Capability: storeType.Capability,
SupportedOperations: storeType.SupportedOperations,
Properties: &genericProperties,
EntryParameters: &genericEntryParameters,
PasswordOptions: storeType.PasswordOptions,
//StorePathType: storeType.StorePathType,
StorePathValue: storeType.StorePathValue,
PrivateKeyAllowed: storeType.PrivateKeyAllowed,
//JobProperties: jobProperties,
ServerRequired: storeType.ServerRequired,
PowerShell: storeType.PowerShell,
BlueprintAllowed: storeType.BlueprintAllowed,
CustomAliasAllowed: storeType.CustomAliasAllowed,
}
sOut = genericStoreType
}

switch {
case outputFormat == "yaml" || outputFormat == "yml":
output, jErr := yaml.Marshal(sOut)
if jErr != nil {
return "", jErr
}
return fmt.Sprintf("%s", output), nil
default:
output, jErr := json.MarshalIndent(sOut, "", " ")
if jErr != nil {
return "", jErr
}
return fmt.Sprintf("%s", output), nil
}
}

func getStoreTypesInternet(gitRef 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")
Expand Down Expand Up @@ -641,22 +495,14 @@ func init() {
storeTypesCmd.AddCommand(storesTypesListCmd)

// GET commands
storeTypesCmd.AddCommand(storesTypeGetCmd)
var storeTypeID int
var storeTypeName string
var dryRun bool
var genericFormat bool
storesTypeGetCmd.Flags().IntVarP(&storeTypeID, "id", "i", -1, "ID of the certificate store type to get.")
storesTypeGetCmd.Flags().StringVarP(&storeTypeName, "name", "n", "", "Name of the certificate store type to get.")
storesTypeGetCmd.MarkFlagsMutuallyExclusive("id", "name")
storesTypeGetCmd.Flags().BoolVarP(&genericFormat, "generic", "g", false, "Output the store type in a generic format stripped of all fields specific to the Command instance.")
//storesTypeGetCmd.Flags().StringVarP(&outputFormat, "format", "f", "json", "Output format. Valid choices are: 'json', 'yaml'. Default is 'json'.")
storesTypeGetCmd.Flags().StringVarP(&gitRef, FlagGitRef, "b", "main", "The git branch or tag to reference when pulling store-types from the internet.")
storeTypesCmd.AddCommand(CreateCmdStoreTypesGet())

// CREATE command
var listValidStoreTypes bool
var filePath string
var createAll bool
var storeTypeName string
var storeTypeID int
storeTypesCmd.AddCommand(storesTypeCreateCmd)
storesTypeCreateCmd.Flags().StringVarP(&storeTypeName, "name", "n", "", "Short name of the certificate store type to get. Valid choices are: "+validTypesString)
storesTypeCreateCmd.Flags().BoolVarP(&listValidStoreTypes, "list", "l", false, "List valid store types.")
Expand All @@ -670,6 +516,7 @@ func init() {

// DELETE command
var deleteAll bool
var dryRun bool
storeTypesCmd.AddCommand(storesTypeDeleteCmd)
storesTypeDeleteCmd.Flags().IntVarP(&storeTypeID, "id", "i", -1, "ID of the certificate store type to delete.")
storesTypeDeleteCmd.Flags().StringVarP(&storeTypeName, "name", "n", "", "Name of the certificate store type to delete.")
Expand Down
Loading

0 comments on commit 687e12d

Please sign in to comment.