@@ -19,6 +19,7 @@ package controllers
1919import (
2020 "bytes"
2121 "context"
22+ "encoding/base64"
2223 "fmt"
2324 "io/ioutil"
2425 "os"
@@ -75,47 +76,103 @@ func (kd *KustomizeDecryptor) Decrypt(res *resource.Resource) (*resource.Resourc
7576 return nil , err
7677 }
7778
78- if kd .kustomization .Spec .Decryption != nil && kd .kustomization .Spec .Decryption .Provider == DecryptionProviderSOPS &&
79- bytes .Contains (out , []byte ("sops:" )) && bytes .Contains (out , []byte ("mac: ENC[" )) {
80- store := common .StoreForFormat (formats .Yaml )
79+ if kd .kustomization .Spec .Decryption != nil && kd .kustomization .Spec .Decryption .Provider == DecryptionProviderSOPS {
8180
82- tree , err := store .LoadEncryptedFile (out )
83- if err != nil {
84- return nil , fmt .Errorf ("LoadEncryptedFile: %w" , err )
85- }
81+ if bytes .Contains (out , []byte ("sops:" )) && bytes .Contains (out , []byte ("mac: ENC[" )) {
82+ store := common .StoreForFormat (formats .Yaml )
8683
87- key , err := tree .Metadata .GetDataKeyWithKeyServices (
88- []keyservice.KeyServiceClient {
89- intkeyservice .NewLocalClient (intkeyservice .NewServer (false , kd .homeDir , kd .ageIdentities )),
90- },
91- )
92- if err != nil {
93- if userErr , ok := err .(sops.UserError ); ok {
94- err = fmt .Errorf (userErr .UserError ())
84+ tree , err := store .LoadEncryptedFile (out )
85+ if err != nil {
86+ return nil , fmt .Errorf ("LoadEncryptedFile: %w" , err )
9587 }
96- return nil , fmt .Errorf ("GetDataKey: %w" , err )
97- }
9888
99- cipher := aes .NewCipher ()
100- if _ , err := tree .Decrypt (key , cipher ); err != nil {
101- return nil , fmt .Errorf ("AES decrypt: %w" , err )
102- }
89+ key , err := tree .Metadata .GetDataKeyWithKeyServices (
90+ []keyservice.KeyServiceClient {
91+ intkeyservice .NewLocalClient (intkeyservice .NewServer (false , kd .homeDir , kd .ageIdentities )),
92+ },
93+ )
94+ if err != nil {
95+ if userErr , ok := err .(sops.UserError ); ok {
96+ err = fmt .Errorf (userErr .UserError ())
97+ }
98+ return nil , fmt .Errorf ("GetDataKey: %w" , err )
99+ }
103100
104- data , err := store . EmitPlainFile ( tree . Branches )
105- if err != nil {
106- return nil , fmt .Errorf ("EmitPlainFile : %w" , err )
107- }
101+ cipher := aes . NewCipher ( )
102+ if _ , err := tree . Decrypt ( key , cipher ); err != nil {
103+ return nil , fmt .Errorf ("AES decrypt : %w" , err )
104+ }
108105
109- jsonData , err := yaml .YAMLToJSON (data )
110- if err != nil {
111- return nil , fmt .Errorf ("YAMLToJSON: %w" , err )
112- }
106+ data , err := store .EmitPlainFile (tree .Branches )
107+ if err != nil {
108+ return nil , fmt .Errorf ("EmitPlainFile: %w" , err )
109+ }
110+
111+ jsonData , err := yaml .YAMLToJSON (data )
112+ if err != nil {
113+ return nil , fmt .Errorf ("YAMLToJSON: %w" , err )
114+ }
115+
116+ err = res .UnmarshalJSON (jsonData )
117+ if err != nil {
118+ return nil , fmt .Errorf ("UnmarshalJSON: %w" , err )
119+ }
120+ return res , nil
121+
122+ } else if res .GetKind () == "Secret" {
123+
124+ dataMap := res .GetDataMap ()
125+
126+ for key , value := range dataMap {
127+
128+ data , err := base64 .StdEncoding .DecodeString (value )
129+ if err != nil {
130+ fmt .Println ("Base64 Decode: %w" , err )
131+ }
132+
133+ if bytes .Contains (data , []byte ("sops" )) && bytes .Contains (data , []byte ("ENC[" )) {
134+
135+ store := common .StoreForFormat (formats .Yaml )
136+
137+ tree , err := store .LoadEncryptedFile (data )
138+ if err != nil {
139+ return nil , fmt .Errorf ("LoadEncryptedFile: %w" , err )
140+ }
141+
142+ metadataKey , err := tree .Metadata .GetDataKeyWithKeyServices (
143+ []keyservice.KeyServiceClient {
144+ intkeyservice .NewLocalClient (intkeyservice .NewServer (false , kd .homeDir , kd .ageIdentities )),
145+ },
146+ )
147+
148+ if err != nil {
149+ if userErr , ok := err .(sops.UserError ); ok {
150+ err = fmt .Errorf (userErr .UserError ())
151+ }
152+ return nil , fmt .Errorf ("GetDataKey: %w" , err )
153+ }
154+
155+ cipher := aes .NewCipher ()
156+ if _ , err := tree .Decrypt (metadataKey , cipher ); err != nil {
157+ return nil , fmt .Errorf ("AES decrypt: %w" , err )
158+ }
159+
160+ binaryStore := common .StoreForFormat (formats .Binary )
161+
162+ out , err := binaryStore .EmitPlainFile (tree .Branches )
163+ if err != nil {
164+ return nil , fmt .Errorf ("EmitPlainFile: %w" , err )
165+ }
166+
167+ dataMap [key ] = base64 .StdEncoding .EncodeToString (out )
168+ }
169+ }
170+
171+ res .SetDataMap (dataMap )
172+
173+ return res , nil
113174
114- err = res .UnmarshalJSON (jsonData )
115- if err != nil {
116- return nil , fmt .Errorf ("UnmarshalJSON: %w" , err )
117175 }
118- return res , nil
119176 }
120177 return nil , nil
121178}
0 commit comments