Skip to content

Commit 20a5ab2

Browse files
committed
feat(store-types): Embed latest store_types.json
Signed-off-by: spbsoluble <[email protected]>
1 parent 0be5cbc commit 20a5ab2

File tree

2 files changed

+142
-23
lines changed

2 files changed

+142
-23
lines changed

cmd/root.go

Lines changed: 127 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@
1515
package cmd
1616

1717
import (
18+
_ "embed"
1819
"fmt"
20+
"io"
21+
stdlog "log"
22+
"os"
23+
1924
"github.com/Keyfactor/keyfactor-go-client-sdk/api/keyfactor"
2025
"github.com/Keyfactor/keyfactor-go-client/v2/api"
2126
"github.com/rs/zerolog/log"
2227
"github.com/spf13/cobra"
2328
"github.com/spf13/cobra/doc"
2429
"golang.org/x/crypto/bcrypt"
25-
"io"
26-
stdlog "log"
27-
"os"
2830
)
2931

32+
//go:embed store_types.json
33+
var EmbeddedStoreTypesJSON []byte
34+
3035
var (
3136
configFile string
3237
profile string
@@ -63,7 +68,15 @@ func hashSecretValue(secretValue string) string {
6368
return string(hashedPassword)
6469
}
6570

66-
func initClient(flagConfigFile string, flagProfile string, flagAuthProviderType string, flagAuthProviderProfile string, noPrompt bool, authConfig *api.AuthConfig, saveConfig bool) (*api.Client, error) {
71+
func initClient(
72+
flagConfigFile string,
73+
flagProfile string,
74+
flagAuthProviderType string,
75+
flagAuthProviderProfile string,
76+
noPrompt bool,
77+
authConfig *api.AuthConfig,
78+
saveConfig bool,
79+
) (*api.Client, error) {
6780
log.Debug().Msg("Enter initClient()")
6881
var clientAuth api.AuthConfig
6982
var commandConfig ConfigurationFile
@@ -163,7 +176,17 @@ func initClient(flagConfigFile string, flagProfile string, flagAuthProviderType
163176
if !noPrompt {
164177
// Auth user interactively
165178
authConfigEntry := commandConfig.Servers[flagProfile]
166-
commandConfig, _ = authInteractive(authConfigEntry.Hostname, authConfigEntry.Username, authConfigEntry.Password, authConfigEntry.Domain, authConfigEntry.APIPath, flagProfile, false, false, flagConfigFile)
179+
commandConfig, _ = authInteractive(
180+
authConfigEntry.Hostname,
181+
authConfigEntry.Username,
182+
authConfigEntry.Password,
183+
authConfigEntry.Domain,
184+
authConfigEntry.APIPath,
185+
flagProfile,
186+
false,
187+
false,
188+
flagConfigFile,
189+
)
167190
} else {
168191
//log.Fatalf("[ERROR] auth config profile: %s", flagProfile)
169192
log.Error().Str("flagProfile", flagProfile).Msg("invalid auth config profile")
@@ -198,7 +221,13 @@ func initClient(flagConfigFile string, flagProfile string, flagAuthProviderType
198221
return c, nil
199222
}
200223

201-
func initGenClient(flagConfig string, flagProfile string, noPrompt bool, authConfig *api.AuthConfig, saveConfig bool) (*keyfactor.APIClient, error) {
224+
func initGenClient(
225+
flagConfig string,
226+
flagProfile string,
227+
noPrompt bool,
228+
authConfig *api.AuthConfig,
229+
saveConfig bool,
230+
) (*keyfactor.APIClient, error) {
202231
var commandConfig ConfigurationFile
203232

204233
if providerType != "" {
@@ -246,7 +275,17 @@ func initGenClient(flagConfig string, flagProfile string, noPrompt bool, authCon
246275
if !noPrompt {
247276
// Auth user interactively
248277
authConfigEntry := commandConfig.Servers[flagProfile]
249-
commandConfig, _ = authInteractive(authConfigEntry.Hostname, authConfigEntry.Username, authConfigEntry.Password, authConfigEntry.Domain, authConfigEntry.APIPath, flagProfile, false, false, flagConfig)
278+
commandConfig, _ = authInteractive(
279+
authConfigEntry.Hostname,
280+
authConfigEntry.Username,
281+
authConfigEntry.Password,
282+
authConfigEntry.Domain,
283+
authConfigEntry.APIPath,
284+
flagProfile,
285+
false,
286+
false,
287+
flagConfig,
288+
)
250289
} else {
251290
//log.Fatalf("[ERROR] auth config profile: %s", flagProfile)
252291
log.Error().Str("flagProfile", flagProfile).Msg("invalid auth config profile")
@@ -306,24 +345,92 @@ func init() {
306345

307346
defaultConfigPath := fmt.Sprintf("$HOME/.keyfactor/%s", DefaultConfigFileName)
308347

309-
RootCmd.PersistentFlags().StringVarP(&configFile, "config", "", "", fmt.Sprintf("Full path to config file in JSON format. (default is %s)", defaultConfigPath))
310-
RootCmd.PersistentFlags().BoolVar(&noPrompt, "no-prompt", false, "Do not prompt for any user input and assume defaults or environmental variables are set.")
311-
RootCmd.PersistentFlags().BoolVar(&expEnabled, "exp", false, "Enable expEnabled features. (USE AT YOUR OWN RISK, these features are not supported and may change or be removed at any time.)")
348+
RootCmd.PersistentFlags().StringVarP(
349+
&configFile,
350+
"config",
351+
"",
352+
"",
353+
fmt.Sprintf("Full path to config file in JSON format. (default is %s)", defaultConfigPath),
354+
)
355+
RootCmd.PersistentFlags().BoolVar(
356+
&noPrompt,
357+
"no-prompt",
358+
false,
359+
"Do not prompt for any user input and assume defaults or environmental variables are set.",
360+
)
361+
RootCmd.PersistentFlags().BoolVar(
362+
&expEnabled,
363+
"exp",
364+
false,
365+
"Enable expEnabled features. (USE AT YOUR OWN RISK, these features are not supported and may change or be removed at any time.)",
366+
)
312367
RootCmd.PersistentFlags().BoolVar(&debugFlag, "debug", false, "Enable debugFlag logging.")
313-
RootCmd.PersistentFlags().BoolVar(&logInsecure, "log-insecure", false, "Log insecure API requests. (USE AT YOUR OWN RISK, this WILL log sensitive information to the console.)")
314-
RootCmd.PersistentFlags().StringVarP(&profile, "profile", "", "", "Use a specific profile from your config file. If not specified the config named 'default' will be used if it exists.")
315-
RootCmd.PersistentFlags().StringVar(&outputFormat, "format", "text", "How to format the CLI output. Currently only `text` is supported.")
368+
RootCmd.PersistentFlags().BoolVar(
369+
&logInsecure,
370+
"log-insecure",
371+
false,
372+
"Log insecure API requests. (USE AT YOUR OWN RISK, this WILL log sensitive information to the console.)",
373+
)
374+
RootCmd.PersistentFlags().StringVarP(
375+
&profile,
376+
"profile",
377+
"",
378+
"",
379+
"Use a specific profile from your config file. If not specified the config named 'default' will be used if it exists.",
380+
)
381+
RootCmd.PersistentFlags().StringVar(
382+
&outputFormat,
383+
"format",
384+
"text",
385+
"How to format the CLI output. Currently only `text` is supported.",
386+
)
316387

317388
RootCmd.PersistentFlags().StringVar(&providerType, "auth-provider-type", "", "Provider type choices: (azid)")
318389
// Validating the provider-type flag against the predefined choices
319390
RootCmd.PersistentFlags().SetAnnotation("auth-provider-type", cobra.BashCompCustom, ProviderTypeChoices)
320-
RootCmd.PersistentFlags().StringVarP(&providerProfile, "auth-provider-profile", "", "default", "The profile to use defined in the securely stored config. If not specified the config named 'default' will be used if it exists.")
321-
322-
RootCmd.PersistentFlags().StringVarP(&kfcUsername, "username", "", "", "Username to use for authenticating to Keyfactor Command.")
323-
RootCmd.PersistentFlags().StringVarP(&kfcHostName, "hostname", "", "", "Hostname to use for authenticating to Keyfactor Command.")
324-
RootCmd.PersistentFlags().StringVarP(&kfcPassword, "password", "", "", "Password to use for authenticating to Keyfactor Command. WARNING: Remember to delete your console history if providing kfcPassword here in plain text.")
325-
RootCmd.PersistentFlags().StringVarP(&kfcDomain, "domain", "", "", "Domain to use for authenticating to Keyfactor Command.")
326-
RootCmd.PersistentFlags().StringVarP(&kfcAPIPath, "api-path", "", "KeyfactorAPI", "API Path to use for authenticating to Keyfactor Command. (default is KeyfactorAPI)")
391+
RootCmd.PersistentFlags().StringVarP(
392+
&providerProfile,
393+
"auth-provider-profile",
394+
"",
395+
"default",
396+
"The profile to use defined in the securely stored config. If not specified the config named 'default' will be used if it exists.",
397+
)
398+
399+
RootCmd.PersistentFlags().StringVarP(
400+
&kfcUsername,
401+
"username",
402+
"",
403+
"",
404+
"Username to use for authenticating to Keyfactor Command.",
405+
)
406+
RootCmd.PersistentFlags().StringVarP(
407+
&kfcHostName,
408+
"hostname",
409+
"",
410+
"",
411+
"Hostname to use for authenticating to Keyfactor Command.",
412+
)
413+
RootCmd.PersistentFlags().StringVarP(
414+
&kfcPassword,
415+
"password",
416+
"",
417+
"",
418+
"Password to use for authenticating to Keyfactor Command. WARNING: Remember to delete your console history if providing kfcPassword here in plain text.",
419+
)
420+
RootCmd.PersistentFlags().StringVarP(
421+
&kfcDomain,
422+
"domain",
423+
"",
424+
"",
425+
"Domain to use for authenticating to Keyfactor Command.",
426+
)
427+
RootCmd.PersistentFlags().StringVarP(
428+
&kfcAPIPath,
429+
"api-path",
430+
"",
431+
"KeyfactorAPI",
432+
"API Path to use for authenticating to Keyfactor Command. (default is KeyfactorAPI)",
433+
)
327434

328435
// Cobra also supports local flags, which will only run
329436
// when this action is called directly.

cmd/storeTypes.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,18 @@ func getStoreTypesInternet(gitRef string) (map[string]interface{}, error) {
456456
}
457457

458458
func getValidStoreTypes(fp string, gitRef string) []string {
459+
log.Debug().
460+
Str("file", fp).
461+
Str("gitRef", gitRef).
462+
Msg(DebugFuncEnter)
463+
464+
log.Debug().
465+
Str("file", fp).
466+
Str("gitRef", gitRef).
467+
Msg("Reading store types config.")
459468
validStoreTypes, rErr := readStoreTypesConfig(fp, gitRef)
460469
if rErr != nil {
461-
log.Printf("Error: %s", rErr)
462-
fmt.Printf("Error: %s\n", rErr)
470+
log.Error().Err(rErr).Msg("unable to read store types")
463471
return nil
464472
}
465473
validStoreTypesList := make([]string, 0, len(validStoreTypes))
@@ -473,7 +481,11 @@ func getValidStoreTypes(fp string, gitRef string) []string {
473481
func readStoreTypesConfig(fp string, gitRef string) (map[string]interface{}, error) {
474482
sTypes, stErr := getStoreTypesInternet(gitRef)
475483
if stErr != nil {
476-
log.Error().Err(stErr).Msg("unable to read store types from internet")
484+
log.Error().Err(stErr).Msg("unable to read store types from internet, attempting to reference embedded definitions")
485+
if err := json.Unmarshal(EmbeddedStoreTypesJSON, &sTypes); err != nil {
486+
log.Error().Err(err).Msg("unable to unmarshal embedded store type definitions")
487+
return nil, err
488+
}
477489
}
478490

479491
var content []byte

0 commit comments

Comments
 (0)