NewCacheFromSecret loads the cache from secret
(ctx context.Context, client kubectl.Client, secretName string)
| 38 | |
| 39 | // NewCacheFromSecret loads the cache from secret |
| 40 | func NewCacheFromSecret(ctx context.Context, client kubectl.Client, secretName string) (*RemoteCache, error) { |
| 41 | secret, err := client.KubeClient().CoreV1().Secrets(client.Namespace()).Get(ctx, secretName, metav1.GetOptions{}) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | // get config name |
| 47 | configName := "" |
| 48 | if secret.Labels != nil { |
| 49 | configName = secret.Labels["name"] |
| 50 | } |
| 51 | |
| 52 | // return secret |
| 53 | if secret.Data == nil || len(secret.Data["cache"]) == 0 { |
| 54 | s := NewCache(configName, secretName) |
| 55 | s.secretNamespace = client.Namespace() |
| 56 | return s, nil |
| 57 | } |
| 58 | |
| 59 | remoteCache := &RemoteCache{} |
| 60 | remoteCache.raw = secret.Data["cache"] |
| 61 | err = yamlutil.Unmarshal(secret.Data["cache"], remoteCache) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | if remoteCache.Data == nil { |
| 67 | remoteCache.Data = make(map[string]string) |
| 68 | } |
| 69 | if remoteCache.Vars == nil { |
| 70 | remoteCache.Vars = make(map[string]string) |
| 71 | } |
| 72 | |
| 73 | // Decrypt vars if necessary |
| 74 | if remoteCache.VarsEncrypted { |
| 75 | for k, v := range remoteCache.Vars { |
| 76 | if len(v) == 0 { |
| 77 | continue |
| 78 | } |
| 79 | |
| 80 | decoded, err := base64.StdEncoding.DecodeString(v) |
| 81 | if err != nil { |
| 82 | // seems like not encrypted |
| 83 | continue |
| 84 | } |
| 85 | |
| 86 | decrypted, err := encryption.DecryptAES([]byte(localcache.EncryptionKey), decoded) |
| 87 | if err != nil { |
| 88 | // we cannot decrypt the variable, so we will ask the user again |
| 89 | delete(remoteCache.Vars, k) |
| 90 | continue |
| 91 | } |
| 92 | |
| 93 | remoteCache.Vars[k] = string(decrypted) |
| 94 | } |
| 95 | |
| 96 | remoteCache.VarsEncrypted = false |
| 97 | } |
no test coverage detected