@@ -29,18 +29,18 @@ import (
2929)
3030
3131const (
32- keyFileName = "talm.key"
33- encryptedSecretsFile = "secrets.encrypted.yaml"
34- plainSecretsFile = "secrets.yaml"
35- ageEncryptionPrefix = "ENC[AGE,data:"
36- ageEncryptionSuffix = "]"
32+ keyFileName = "talm.key"
33+ encryptedSecretsFile = "secrets.encrypted.yaml"
34+ plainSecretsFile = "secrets.yaml"
35+ ageEncryptionPrefix = "ENC[AGE,data:"
36+ ageEncryptionSuffix = "]"
3737)
3838
3939// GenerateKey generates a new age identity and saves it to talm.key file in age keygen format
4040// Returns true if a new key was created (not loaded from existing file)
4141func GenerateKey (rootDir string ) (* age.X25519Identity , bool , error ) {
4242 keyFile := filepath .Join (rootDir , keyFileName )
43-
43+
4444 // Check if key already exists
4545 if _ , err := os .Stat (keyFile ); err == nil {
4646 // Key exists, load it
@@ -50,21 +50,21 @@ func GenerateKey(rootDir string) (*age.X25519Identity, bool, error) {
5050 }
5151 return identity , false , nil
5252 }
53-
53+
5454 // Generate new key
5555 identity , err := age .GenerateX25519Identity ()
5656 if err != nil {
5757 return nil , false , fmt .Errorf ("failed to generate age identity: %w" , err )
5858 }
5959
6060 publicKey := identity .Recipient ().String ()
61-
61+
6262 // Format key file in age keygen format
6363 now := time .Now ()
6464 keyData := fmt .Sprintf ("# created: %s\n " , now .Format (time .RFC3339 ))
6565 keyData += fmt .Sprintf ("# public key: %s\n " , publicKey )
6666 keyData += identity .String () + "\n "
67-
67+
6868 if err := os .WriteFile (keyFile , []byte (keyData ), 0o600 ); err != nil {
6969 return nil , false , fmt .Errorf ("failed to write key file: %w" , err )
7070 }
@@ -125,11 +125,11 @@ func GetPublicKeyFromFile(rootDir string) (string, error) {
125125 }
126126
127127 // Find the public key line (starts with # public key:)
128- lines := strings .Split (string (keyData ), "\n " )
129- for _ , line := range lines {
128+ lines := strings .SplitSeq (string (keyData ), "\n " )
129+ for line := range lines {
130130 line = strings .TrimSpace (line )
131- if strings .HasPrefix (line , "# public key: " ) {
132- return strings . TrimPrefix ( line , "# public key: " ) , nil
131+ if after , ok := strings .CutPrefix (line , "# public key: " ); ok {
132+ return after , nil
133133 }
134134 }
135135
@@ -171,13 +171,13 @@ func EncryptSecretsFile(rootDir string) error {
171171 }
172172
173173 // Parse YAML
174- var secrets map [string ]interface {}
174+ var secrets map [string ]any
175175 if err := yaml .Unmarshal (secretsData , & secrets ); err != nil {
176176 return fmt .Errorf ("failed to parse secrets YAML: %w" , err )
177177 }
178178
179179 // If encrypted file exists, load it and merge (preserve unchanged encrypted values)
180- var encryptedSecrets map [string ]interface {}
180+ var encryptedSecrets map [string ]any
181181 if _ , err := os .Stat (encryptedFile ); err == nil {
182182 encryptedData , err := os .ReadFile (encryptedFile )
183183 if err == nil {
@@ -187,30 +187,30 @@ func EncryptSecretsFile(rootDir string) error {
187187 if err != nil {
188188 return fmt .Errorf ("failed to merge and encrypt: %w" , err )
189189 }
190- encryptedSecrets = merged .(map [string ]interface {} )
190+ encryptedSecrets = merged .(map [string ]any )
191191 } else {
192192 // If parsing fails, encrypt everything
193193 encrypted , err := encryptYAMLValues (secrets , identity .Recipient ())
194194 if err != nil {
195195 return fmt .Errorf ("failed to encrypt secrets: %w" , err )
196196 }
197- encryptedSecrets = encrypted .(map [string ]interface {} )
197+ encryptedSecrets = encrypted .(map [string ]any )
198198 }
199199 } else {
200200 // If reading fails, encrypt everything
201201 encrypted , err := encryptYAMLValues (secrets , identity .Recipient ())
202202 if err != nil {
203203 return fmt .Errorf ("failed to encrypt secrets: %w" , err )
204204 }
205- encryptedSecrets = encrypted .(map [string ]interface {} )
205+ encryptedSecrets = encrypted .(map [string ]any )
206206 }
207207 } else {
208208 // No encrypted file exists, encrypt everything
209209 encrypted , err := encryptYAMLValues (secrets , identity .Recipient ())
210210 if err != nil {
211211 return fmt .Errorf ("failed to encrypt secrets: %w" , err )
212212 }
213- encryptedSecrets = encrypted .(map [string ]interface {} )
213+ encryptedSecrets = encrypted .(map [string ]any )
214214 }
215215
216216 // Marshal encrypted YAML
@@ -245,7 +245,7 @@ func DecryptSecretsFile(rootDir string) error {
245245 }
246246
247247 // Parse YAML
248- var encryptedSecrets map [string ]interface {}
248+ var encryptedSecrets map [string ]any
249249 if err := yaml .Unmarshal (encryptedData , & encryptedSecrets ); err != nil {
250250 return fmt .Errorf ("failed to parse encrypted YAML: %w" , err )
251251 }
@@ -271,10 +271,10 @@ func DecryptSecretsFile(rootDir string) error {
271271}
272272
273273// encryptYAMLValues recursively encrypts string values in YAML structure
274- func encryptYAMLValues (data interface {} , recipient * age.X25519Recipient ) (interface {} , error ) {
274+ func encryptYAMLValues (data any , recipient * age.X25519Recipient ) (any , error ) {
275275 switch v := data .(type ) {
276- case map [string ]interface {} :
277- result := make (map [string ]interface {} )
276+ case map [string ]any :
277+ result := make (map [string ]any )
278278 for key , value := range v {
279279 encryptedValue , err := encryptYAMLValues (value , recipient )
280280 if err != nil {
@@ -283,8 +283,8 @@ func encryptYAMLValues(data interface{}, recipient *age.X25519Recipient) (interf
283283 result [key ] = encryptedValue
284284 }
285285 return result , nil
286- case []interface {} :
287- result := make ([]interface {} , len (v ))
286+ case []any :
287+ result := make ([]any , len (v ))
288288 for i , item := range v {
289289 encryptedItem , err := encryptYAMLValues (item , recipient )
290290 if err != nil {
@@ -306,10 +306,10 @@ func encryptYAMLValues(data interface{}, recipient *age.X25519Recipient) (interf
306306}
307307
308308// decryptYAMLValues recursively decrypts string values in YAML structure
309- func decryptYAMLValues (data interface {} , identity * age.X25519Identity ) (interface {} , error ) {
309+ func decryptYAMLValues (data any , identity * age.X25519Identity ) (any , error ) {
310310 switch v := data .(type ) {
311- case map [string ]interface {} :
312- result := make (map [string ]interface {} )
311+ case map [string ]any :
312+ result := make (map [string ]any )
313313 for key , value := range v {
314314 decryptedValue , err := decryptYAMLValues (value , identity )
315315 if err != nil {
@@ -318,8 +318,8 @@ func decryptYAMLValues(data interface{}, identity *age.X25519Identity) (interfac
318318 result [key ] = decryptedValue
319319 }
320320 return result , nil
321- case []interface {} :
322- result := make ([]interface {} , len (v ))
321+ case []any :
322+ result := make ([]any , len (v ))
323323 for i , item := range v {
324324 decryptedItem , err := decryptYAMLValues (item , identity )
325325 if err != nil {
@@ -358,16 +358,16 @@ func decryptYAMLValuesString(encrypted string, identity *age.X25519Identity) (st
358358
359359// mergeAndEncryptYAMLValues merges plain and encrypted YAML, encrypting only changed values
360360// This ensures idempotency: unchanged values keep their encrypted form
361- func mergeAndEncryptYAMLValues (plain , encrypted interface {} , identity * age.X25519Identity ) (interface {} , error ) {
361+ func mergeAndEncryptYAMLValues (plain , encrypted any , identity * age.X25519Identity ) (any , error ) {
362362 switch plainVal := plain .(type ) {
363- case map [string ]interface {} :
364- encryptedMap , ok := encrypted .(map [string ]interface {} )
363+ case map [string ]any :
364+ encryptedMap , ok := encrypted .(map [string ]any )
365365 if ! ok {
366366 // Type mismatch, encrypt everything
367367 return encryptYAMLValues (plain , identity .Recipient ())
368368 }
369-
370- result := make (map [string ]interface {} )
369+
370+ result := make (map [string ]any )
371371 // Copy all keys from plain (to handle new keys)
372372 for key , plainValue := range plainVal {
373373 if encryptedValue , exists := encryptedMap [key ]; exists {
@@ -387,15 +387,15 @@ func mergeAndEncryptYAMLValues(plain, encrypted interface{}, identity *age.X2551
387387 }
388388 }
389389 return result , nil
390-
391- case []interface {} :
392- encryptedSlice , ok := encrypted .([]interface {} )
390+
391+ case []any :
392+ encryptedSlice , ok := encrypted .([]any )
393393 if ! ok || len (plainVal ) != len (encryptedSlice ) {
394394 // Type or length mismatch, encrypt everything
395395 return encryptYAMLValues (plain , identity .Recipient ())
396396 }
397-
398- result := make ([]interface {} , len (plainVal ))
397+
398+ result := make ([]any , len (plainVal ))
399399 for i , plainItem := range plainVal {
400400 merged , err := mergeAndEncryptYAMLValues (plainItem , encryptedSlice [i ], identity )
401401 if err != nil {
@@ -404,14 +404,14 @@ func mergeAndEncryptYAMLValues(plain, encrypted interface{}, identity *age.X2551
404404 result [i ] = merged
405405 }
406406 return result , nil
407-
407+
408408 case string :
409409 encryptedStr , ok := encrypted .(string )
410410 if ! ok {
411411 // Type mismatch, encrypt
412412 return encryptYAMLValues (plain , identity .Recipient ())
413413 }
414-
414+
415415 // Check if encrypted value is already encrypted
416416 if strings .HasPrefix (encryptedStr , ageEncryptionPrefix ) && strings .HasSuffix (encryptedStr , ageEncryptionSuffix ) {
417417 // Decrypt existing value to compare
@@ -423,7 +423,7 @@ func mergeAndEncryptYAMLValues(plain, encrypted interface{}, identity *age.X2551
423423 }
424424 // Encrypt the new value (if decryption fails, values differ, or both are plain)
425425 return encryptYAMLValues (plain , identity .Recipient ())
426-
426+
427427 default :
428428 // For other types, compare directly
429429 if plain == encrypted {
@@ -495,7 +495,7 @@ func RotateKeys(rootDir string) error {
495495 return fmt .Errorf ("failed to read encrypted file: %w" , err )
496496 }
497497
498- var encryptedSecrets map [string ]interface {}
498+ var encryptedSecrets map [string ]any
499499 if err := yaml .Unmarshal (encryptedData , & encryptedSecrets ); err != nil {
500500 return fmt .Errorf ("failed to parse encrypted YAML: %w" , err )
501501 }
@@ -560,13 +560,13 @@ func EncryptYAMLFile(rootDir, plainFile, encryptedFile string) error {
560560 }
561561
562562 // Parse YAML
563- var yamlData map [string ]interface {}
563+ var yamlData map [string ]any
564564 if err := yaml .Unmarshal (plainData , & yamlData ); err != nil {
565565 return fmt .Errorf ("failed to parse YAML: %w" , err )
566566 }
567567
568568 // If encrypted file exists, load it and merge (preserve unchanged encrypted values)
569- var encryptedYAML map [string ]interface {}
569+ var encryptedYAML map [string ]any
570570 if _ , err := os .Stat (encryptedFilePath ); err == nil {
571571 encryptedData , err := os .ReadFile (encryptedFilePath )
572572 if err == nil {
@@ -576,30 +576,30 @@ func EncryptYAMLFile(rootDir, plainFile, encryptedFile string) error {
576576 if err != nil {
577577 return fmt .Errorf ("failed to merge and encrypt: %w" , err )
578578 }
579- encryptedYAML = merged .(map [string ]interface {} )
579+ encryptedYAML = merged .(map [string ]any )
580580 } else {
581581 // If parsing fails, encrypt everything
582582 encrypted , err := encryptYAMLValues (yamlData , identity .Recipient ())
583583 if err != nil {
584584 return fmt .Errorf ("failed to encrypt YAML values: %w" , err )
585585 }
586- encryptedYAML = encrypted .(map [string ]interface {} )
586+ encryptedYAML = encrypted .(map [string ]any )
587587 }
588588 } else {
589589 // If reading fails, encrypt everything
590590 encrypted , err := encryptYAMLValues (yamlData , identity .Recipient ())
591591 if err != nil {
592592 return fmt .Errorf ("failed to encrypt YAML values: %w" , err )
593593 }
594- encryptedYAML = encrypted .(map [string ]interface {} )
594+ encryptedYAML = encrypted .(map [string ]any )
595595 }
596596 } else {
597597 // No encrypted file exists, encrypt everything
598598 encrypted , err := encryptYAMLValues (yamlData , identity .Recipient ())
599599 if err != nil {
600600 return fmt .Errorf ("failed to encrypt YAML values: %w" , err )
601601 }
602- encryptedYAML = encrypted .(map [string ]interface {} )
602+ encryptedYAML = encrypted .(map [string ]any )
603603 }
604604
605605 // Marshal encrypted YAML
@@ -634,7 +634,7 @@ func DecryptYAMLFile(rootDir, encryptedFile, plainFile string) error {
634634 }
635635
636636 // Parse YAML
637- var encryptedYAML map [string ]interface {}
637+ var encryptedYAML map [string ]any
638638 if err := yaml .Unmarshal (encryptedData , & encryptedYAML ); err != nil {
639639 return fmt .Errorf ("failed to parse encrypted YAML: %w" , err )
640640 }
@@ -658,4 +658,3 @@ func DecryptYAMLFile(rootDir, encryptedFile, plainFile string) error {
658658
659659 return nil
660660}
661-
0 commit comments