@@ -426,6 +426,21 @@ func createStoreFromFile(filename string, kfClient *api.Client) (*api.Certificat
426426 return createResp , nil
427427}
428428
429+ func formatStoreTypes (sTypesList * []interface {}) (map [string ]interface {}, error ) {
430+
431+ if sTypesList == nil || len (* sTypesList ) == 0 {
432+ return nil , fmt .Errorf ("empty store types list" )
433+ }
434+
435+ output := make (map [string ]interface {})
436+ for _ , v := range * sTypesList {
437+ v2 := v .(map [string ]interface {})
438+ output [v2 ["ShortName" ].(string )] = v2
439+ }
440+
441+ return output , nil
442+ }
443+
429444func getStoreTypesInternet (gitRef string ) (map [string ]interface {}, error ) {
430445 //resp, err := http.Get("https://raw.githubusercontent.com/keyfactor/kfutil/main/store_types.json")
431446 //resp, err := http.Get("https://raw.githubusercontent.com/keyfactor/kfctl/master/storetypes/storetypes.json")
@@ -450,17 +465,18 @@ func getStoreTypesInternet(gitRef string) (map[string]interface{}, error) {
450465 }
451466 // read as list of interfaces
452467 var result []interface {}
453- json .Unmarshal (body , & result )
454-
455- // convert to map
456- var result2 map [string ]interface {}
457- result2 = make (map [string ]interface {})
458- for _ , v := range result {
459- v2 := v .(map [string ]interface {})
460- result2 [v2 ["ShortName" ].(string )] = v2
468+ jErr := json .Unmarshal (body , & result )
469+ if jErr != nil {
470+ return nil , jErr
471+ }
472+ output , sErr := formatStoreTypes (& result )
473+ if sErr != nil {
474+ return nil , err
475+ } else if output == nil {
476+ return nil , fmt .Errorf ("unable to fetch store types from %s" , url )
461477 }
478+ return output , nil
462479
463- return result2 , nil
464480}
465481
466482func getValidStoreTypes (fp string , gitRef string ) []string {
@@ -486,32 +502,21 @@ func getValidStoreTypes(fp string, gitRef string) []string {
486502 return validStoreTypesList
487503}
488504
489- func readStoreTypesConfig (fp string , gitRef string ) (map [string ]interface {}, error ) {
490- log .Debug ().
491- Str ("file" , fp ).
492- Str ("gitRef" , gitRef ).
493- Msg (fmt .Sprintf (DebugFuncEnter , "readStoreTypesConfig" ))
505+ func readStoreTypesConfig (fp , gitRef string ) (map [string ]interface {}, error ) {
506+ log .Debug ().Str ("file" , fp ).Str ("gitRef" , gitRef ).Msg ("Entering readStoreTypesConfig" )
494507
495- log .Debug ().
496- Str ("file" , fp ).
497- Str ("gitRef" , gitRef ).
498- Msg (fmt .Sprintf (DebugFuncCall , "getStoreTypesInternet" ))
499508 sTypes , stErr := getStoreTypesInternet (gitRef )
500- if stErr != nil {
501- log .Warn ().
502- Err (stErr ).
503- Msg ("unable to read store types from internet, attempting to reference embedded definitions" )
504- if err := json .Unmarshal (EmbeddedStoreTypesJSON , & sTypes ); err != nil {
505- log .Error ().Err (err ).Msg ("unable to unmarshal embedded store type definitions" )
509+ if stErr != nil || sTypes == nil || len (sTypes ) == 0 {
510+ log .Warn ().Err (stErr ).Msg ("Unable to read store types from internet, using embedded definitions" )
511+ var emStoreTypes []interface {}
512+ if err := json .Unmarshal (EmbeddedStoreTypesJSON , & emStoreTypes ); err != nil {
513+ log .Error ().Err (err ).Msg ("Unable to unmarshal embedded store type definitions" )
506514 return nil , err
507515 }
508- } else if sTypes == nil || len (sTypes ) == 0 {
509- log .Warn ().Err (fmt .Errorf ("empty store valid store types list" )).Msg (
510- "0 store types found from internet, attempting to reference embedded definitions" ,
511- )
512- if err := json .Unmarshal (EmbeddedStoreTypesJSON , & sTypes ); err != nil {
513- log .Error ().Err (err ).Msg ("unable to unmarshal embedded store type definitions" )
514- return nil , err
516+ sTypes , stErr = formatStoreTypes (& emStoreTypes )
517+ if stErr != nil {
518+ log .Error ().Err (stErr ).Msg ("Unable to format store types" )
519+ return nil , stErr
515520 }
516521 }
517522
@@ -522,20 +527,16 @@ func readStoreTypesConfig(fp string, gitRef string) (map[string]interface{}, err
522527 fp = DefaultStoreTypesFileName
523528 }
524529 content , err = os .ReadFile (fp )
525- if err != nil {
526- return nil , err
527- }
528530 } else {
529531 content , err = json .Marshal (sTypes )
530- if err != nil {
531- return nil , err
532- }
532+ }
533+ if err != nil {
534+ return nil , err
533535 }
534536
535537 var d map [string ]interface {}
536- err = json .Unmarshal (content , & d )
537- if err != nil {
538- log .Error ().Err (err ).Msg ("unable to unmarshal store types" )
538+ if err = json .Unmarshal (content , & d ); err != nil {
539+ log .Error ().Err (err ).Msg ("Unable to unmarshal store types" )
539540 return nil , err
540541 }
541542 return d , nil
0 commit comments