@@ -21,76 +21,131 @@ func TestMasterKeysFromRecipientsEmpty(t *testing.T) {
2121 assert .Equal (recipients , make ([]* MasterKey , 0 ))
2222}
2323
24- func TestMasterKeyFromRecipientWithLeadingAndTrailingSpaces (t * testing.T ) {
24+ func TestMasterKeyFromRecipientWithLeadingAndTrailingSpacesSingle (t * testing.T ) {
2525 assert := assert .New (t )
2626
27- key , err := MasterKeyFromRecipient (" age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw " )
27+ commaSeparatedRecipients := " age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw "
28+ keys , err := MasterKeysFromRecipients (commaSeparatedRecipients )
2829
2930 assert .NoError (err )
3031
31- assert .Equal (key .Recipient , "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
32+ assert .Equal (len (keys ), 1 )
33+ assert .Equal (keys [0 ].Recipient , "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
3234}
3335
34- func TestAge (t * testing.T ) {
36+ func TestMasterKeyFromRecipientWithLeadingAndTrailingSpacesMultiple (t * testing.T ) {
3537 assert := assert .New (t )
3638
37- key , err := MasterKeyFromRecipient ("age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
39+ commaSeparatedRecipients := " age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw , age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep "
40+ keys , err := MasterKeysFromRecipients (commaSeparatedRecipients )
3841
3942 assert .NoError (err )
40- assert .Equal ("age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" , key .ToString ())
4143
42- dataKey := []byte ("abcdefghijklmnopqrstuvwxyz123456" )
44+ assert .Equal (len (keys ), 2 )
45+ assert .Equal (keys [0 ].Recipient , "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
46+ assert .Equal (keys [1 ].Recipient , "age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep" )
47+ }
48+
49+ func TestMasterKeysFromRecipientsWithSingle (t * testing.T ) {
50+ assert := assert .New (t )
51+
52+ commaSeparatedRecipients := "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw"
53+ keys , err := MasterKeysFromRecipients (commaSeparatedRecipients )
4354
44- err = key .Encrypt (dataKey )
4555 assert .NoError (err )
4656
47- _ , filename , _ , _ := runtime .Caller (0 )
48- err = os .Setenv (SopsAgeKeyFileEnv , path .Join (path .Dir (filename ), "keys.txt" ))
57+ assert .Equal (len (keys ), 1 )
58+ assert .Equal (keys [0 ].Recipient , "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
59+ }
60+
61+ func TestMasterKeysFromRecipientsWithMultiple (t * testing.T ) {
62+ assert := assert .New (t )
63+
64+ commaSeparatedRecipients := "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw,age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep"
65+ keys , err := MasterKeysFromRecipients (commaSeparatedRecipients )
66+
4967 assert .NoError (err )
5068
51- decryptedKey , err := key .Decrypt ()
69+ assert .Equal (len (keys ), 2 )
70+ assert .Equal (keys [0 ].Recipient , "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
71+ assert .Equal (keys [1 ].Recipient , "age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep" )
72+ }
73+
74+ func TestAge (t * testing.T ) {
75+ assert := assert .New (t )
76+
77+ commaSeparatedRecipients := "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw,age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep"
78+ keys , err := MasterKeysFromRecipients (commaSeparatedRecipients )
79+
5280 assert .NoError (err )
53- assert .Equal (dataKey , decryptedKey )
81+
82+ assert .Equal (len (keys ), 2 )
83+ assert .Equal (keys [0 ].Recipient , "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
84+ assert .Equal (keys [1 ].Recipient , "age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep" )
85+
86+ dataKey := []byte ("abcdefghijklmnopqrstuvwxyz123456" )
87+
88+ for _ , key := range keys {
89+ err = key .Encrypt (dataKey )
90+ assert .NoError (err )
91+
92+ _ , filename , _ , _ := runtime .Caller (0 )
93+ err = os .Setenv ("SOPS_AGE_KEY_FILE" , path .Join (path .Dir (filename ), "keys.txt" ))
94+ assert .NoError (err )
95+
96+ decryptedKey , err := key .Decrypt ()
97+ assert .NoError (err )
98+ assert .Equal (dataKey , decryptedKey )
99+ }
100+
54101}
55102
56103func TestAgeDotEnv (t * testing.T ) {
57104 assert := assert .New (t )
58105
59- key , err := MasterKeyFromRecipient ("age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
106+ commaSeparatedRecipients := "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw,age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep"
107+ keys , err := MasterKeysFromRecipients (commaSeparatedRecipients )
60108
61109 assert .NoError (err )
62- assert .Equal ("age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" , key .ToString ())
110+
111+ assert .Equal (len (keys ), 2 )
112+ assert .Equal (keys [0 ].Recipient , "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
113+ assert .Equal (keys [1 ].Recipient , "age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep" )
63114
64115 dotenv := `IMAGE_PREFIX=repo/service-
65116APPLICATION_KEY=K6pfAWuUVND9Fz5SC7jmA6pfAWuUVND9Fz5SC7jmA
66117KEY_ID=003683d721f2ae683d721f2a1
67118DOMAIN=files.127.0.0.1.nip.io`
68119 dataKey := []byte (dotenv )
69120
70- err = key .Encrypt (dataKey )
121+ err = keys [ 0 ] .Encrypt (dataKey )
71122 assert .NoError (err )
72123
73124 _ , filename , _ , _ := runtime .Caller (0 )
74125 err = os .Setenv (SopsAgeKeyFileEnv , path .Join (path .Dir (filename ), "keys.txt" ))
75126 defer os .Unsetenv (SopsAgeKeyFileEnv )
76127 assert .NoError (err )
77128
78- decryptedKey , err := key .Decrypt ()
129+ decryptedKey , err := keys [ 0 ] .Decrypt ()
79130 assert .NoError (err )
80131 assert .Equal (dataKey , decryptedKey )
81132}
82133
83134func TestAgeEnv (t * testing.T ) {
84135 assert := assert .New (t )
85136
86- key , err := MasterKeyFromRecipient ("age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
137+ commaSeparatedRecipients := "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw,age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep"
138+ keys , err := MasterKeysFromRecipients (commaSeparatedRecipients )
87139
88140 assert .NoError (err )
89- assert .Equal ("age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" , key .ToString ())
141+
142+ assert .Equal (len (keys ), 2 )
143+ assert .Equal (keys [0 ].Recipient , "age1yt3tfqlfrwdwx0z0ynwplcr6qxcxfaqycuprpmy89nr83ltx74tqdpszlw" )
144+ assert .Equal (keys [1 ].Recipient , "age1tmaae3ld5vpevmsh5yacsauzx8jetg300mpvc4ugp5zr5l6ssq9sla97ep" )
90145
91146 dataKey := []byte ("abcdefghijklmnopqrstuvwxyz123456" )
92147
93- err = key .Encrypt (dataKey )
148+ err = keys [ 0 ] .Encrypt (dataKey )
94149 assert .NoError (err )
95150
96151 _ , filename , _ , _ := runtime .Caller (0 )
@@ -100,7 +155,7 @@ func TestAgeEnv(t *testing.T) {
100155 defer os .Unsetenv (SopsAgeKeyEnv )
101156 assert .NoError (err )
102157
103- decryptedKey , err := key .Decrypt ()
158+ decryptedKey , err := keys [ 0 ] .Decrypt ()
104159 assert .NoError (err )
105160 assert .Equal (dataKey , decryptedKey )
106161}
0 commit comments