go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers-sdk/v1/vault/config.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package vault 5 6 import ( 7 "encoding/json" 8 "errors" 9 "strings" 10 ) 11 12 func NewVaultType(name string) (VaultType, error) { 13 entry := strings.TrimSpace(strings.ToLower(name)) 14 var code VaultType 15 ok := false 16 for k := range vaultMarshalNameMap { 17 if vaultMarshalNameMap[k] == entry { 18 ok = true 19 code = k 20 break 21 } 22 } 23 if !ok { 24 return VaultType_None, errors.New("unknown type value: " + string(name)) 25 } 26 27 return code, nil 28 } 29 30 var vaultMarshalNameMap = map[VaultType]string{ 31 VaultType_None: "none", 32 VaultType_KeyRing: "keyring", 33 VaultType_LinuxKernelKeyring: "linux-kernel-keyring", 34 VaultType_EncryptedFile: "encrypted-file", 35 VaultType_HashiCorp: "hashicorp-vault", 36 VaultType_GCPSecretsManager: "gcp-secret-manager", 37 VaultType_AWSSecretsManager: "aws-secrets-manager", 38 VaultType_AWSParameterStore: "aws-parameter-store", 39 VaultType_GCPBerglas: "gcp-berglas", 40 VaultType_Memory: "memory", 41 } 42 43 func (t *VaultType) Value() string { 44 if t == nil { 45 return "" 46 } 47 return vaultMarshalNameMap[*t] 48 } 49 50 func TypeIds() []string { 51 var types []string 52 for _, v := range vaultMarshalNameMap { 53 types = append(types, v) 54 } 55 return types 56 } 57 58 // UnmarshalJSON parses either an int or a string representation of 59 // VaultType into the struct 60 func (t *VaultType) UnmarshalJSON(data []byte) error { 61 // check if we have a number 62 var code int32 63 err := json.Unmarshal(data, &code) 64 if err == nil { 65 *t = VaultType(code) 66 } 67 if err != nil { 68 var name string 69 err = json.Unmarshal(data, &name) 70 entry := strings.TrimSpace(strings.ToLower(name)) 71 72 var code VaultType 73 ok := false 74 for k := range vaultMarshalNameMap { 75 if vaultMarshalNameMap[k] == entry { 76 ok = true 77 code = k 78 break 79 } 80 } 81 if !ok { 82 return errors.New("unknown type value: " + string(data)) 83 } 84 *t = code 85 } 86 return nil 87 } 88 89 // MarshalJSON returns the JSON representation of VaultType 90 // NOTE: we do not use pointers here to ensure its converted properly 91 // even if the struct is used directly 92 func (t VaultType) MarshalJSON() ([]byte, error) { 93 v, ok := vaultMarshalNameMap[t] 94 if !ok { 95 return nil, errors.New("could not marshal CredentialType") 96 } 97 return json.Marshal(v) 98 }