@@ -46,7 +46,7 @@ func TestKeyHierarchy(t *testing.T) {
4646 expErr : model .ErrKeyHierarchyKeysListEmpty ,
4747 },
4848 {
49- name : "should return error if first key does not have role 'root '" ,
49+ name : "should return error if first key is 'kek '" ,
5050 input : & model.KeyHierarchy {
5151 Name : "production-hierarchy" ,
5252 Keys : []model.KeySpec {
@@ -59,6 +59,34 @@ func TestKeyHierarchy(t *testing.T) {
5959 },
6060 expErr : model .ErrKeyHierarchyFirstKeyNotRoot ,
6161 },
62+ {
63+ name : "should return error if first key is 'dek'" ,
64+ input : & model.KeyHierarchy {
65+ Name : "production-hierarchy" ,
66+ Keys : []model.KeySpec {
67+ {
68+ Kind : "K0" ,
69+ Role : model .KeyRoleDek ,
70+ Algorithm : model .KeyAlgorithmAES256 ,
71+ },
72+ },
73+ },
74+ expErr : model .ErrKeyHierarchyFirstKeyNotRoot ,
75+ },
76+ {
77+ name : "should return error if first key is 'tek'" ,
78+ input : & model.KeyHierarchy {
79+ Name : "production-hierarchy" ,
80+ Keys : []model.KeySpec {
81+ {
82+ Kind : "K0" ,
83+ Role : model .KeyRoleTek ,
84+ Algorithm : model .KeyAlgorithmAES256 ,
85+ },
86+ },
87+ },
88+ expErr : model .ErrKeyHierarchyFirstKeyNotRoot ,
89+ },
6290 {
6391 name : "should return error if there are duplicate key kinds" ,
6492 input : & model.KeyHierarchy {
@@ -177,10 +205,10 @@ func TestKeyHierarchy(t *testing.T) {
177205 },
178206 },
179207 },
180- expErr : model .ErrKeyHierarchyIntermediateKeyNotKek ,
208+ expErr : model .ErrKeyHierarchyInvalidIntermediateKey ,
181209 },
182210 {
183- name : "should return error if there is a non-kek key in the middle of the hierarchy" ,
211+ name : "should return error if there is a non-kek(dek) key in the middle of the hierarchy" ,
184212 input : & model.KeyHierarchy {
185213 Name : "production-hierarchy" ,
186214 Keys : []model.KeySpec {
@@ -206,7 +234,46 @@ func TestKeyHierarchy(t *testing.T) {
206234 },
207235 },
208236 },
209- expErr : model .ErrKeyHierarchyIntermediateKeyNotKek ,
237+ expErr : model .ErrKeyHierarchyInvalidIntermediateKey ,
238+ },
239+
240+ {
241+ name : "should return error if the hierarchy has root and kek keys" ,
242+ input : & model.KeyHierarchy {
243+ Name : "production-hierarchy" ,
244+ Keys : []model.KeySpec {
245+ {
246+ Kind : "K0" ,
247+ Role : model .KeyRoleRoot ,
248+ Algorithm : model .KeyAlgorithmAES256 ,
249+ },
250+ {
251+ Kind : "K1" ,
252+ Role : model .KeyRoleKek ,
253+ Algorithm : model .KeyAlgorithmAES256 ,
254+ },
255+ },
256+ },
257+ expErr : model .ErrKeyHierarchyLastKeyNotDek ,
258+ },
259+ {
260+ name : "should return error if the hierarchy has root and tek keys" ,
261+ input : & model.KeyHierarchy {
262+ Name : "production-hierarchy" ,
263+ Keys : []model.KeySpec {
264+ {
265+ Kind : "K0" ,
266+ Role : model .KeyRoleRoot ,
267+ Algorithm : model .KeyAlgorithmAES256 ,
268+ },
269+ {
270+ Kind : "K1" ,
271+ Role : model .KeyRoleTek ,
272+ Algorithm : model .KeyAlgorithmAES256 ,
273+ },
274+ },
275+ },
276+ expErr : model .ErrKeyHierarchyLastKeyNotDek ,
210277 },
211278 {
212279 name : "should return nil if the hierarchy has root and dek keys" ,
@@ -251,6 +318,40 @@ func TestKeyHierarchy(t *testing.T) {
251318 },
252319 expErr : nil ,
253320 },
321+ {
322+ name : "should return nil if the hierarchy has root, kek, tek and dek keys" ,
323+ input : & model.KeyHierarchy {
324+ Name : "production-hierarchy" ,
325+ Keys : []model.KeySpec {
326+ {
327+ Kind : "K0" ,
328+ Role : model .KeyRoleRoot ,
329+ Algorithm : model .KeyAlgorithmAES256 ,
330+ },
331+ {
332+ Kind : "K1" ,
333+ Role : model .KeyRoleKek ,
334+ Algorithm : model .KeyAlgorithmAES256 ,
335+ },
336+ {
337+ Kind : "K2" ,
338+ Role : model .KeyRoleTek ,
339+ Algorithm : model .KeyAlgorithmAES256 ,
340+ },
341+ {
342+ Kind : "K3" ,
343+ Role : model .KeyRoleKek ,
344+ Algorithm : model .KeyAlgorithmAES256 ,
345+ },
346+ {
347+ Kind : "K4" ,
348+ Role : model .KeyRoleDek ,
349+ Algorithm : model .KeyAlgorithmAES256 ,
350+ },
351+ },
352+ },
353+ expErr : nil ,
354+ },
254355 {
255356 name : "should return nil if the hierarchy has only a root key" ,
256357 input : & model.KeyHierarchy {
@@ -426,6 +527,33 @@ func TestKeyUsage(t *testing.T) {
426527 expIsWrap : true ,
427528 expIsUnwrap : true ,
428529 },
530+ {
531+ name : "should return wrap and unwrap if the key kind belongs to a 'tek' key in a multi-key hierarchy" ,
532+ input : & model.KeyHierarchy {
533+ Name : "production-hierarchy" ,
534+ Keys : []model.KeySpec {
535+ {
536+ Kind : "K0" ,
537+ Role : model .KeyRoleRoot ,
538+ Algorithm : model .KeyAlgorithmAES256 ,
539+ },
540+ {
541+ Kind : "K1" ,
542+ Role : model .KeyRoleTek ,
543+ Algorithm : model .KeyAlgorithmAES256 ,
544+ },
545+ {
546+ Kind : "K2" ,
547+ Role : model .KeyRoleDek ,
548+ Algorithm : model .KeyAlgorithmAES256 ,
549+ },
550+ },
551+ },
552+ keyToCheck : "K1" ,
553+ expIsFound : true ,
554+ expIsWrap : true ,
555+ expIsUnwrap : true ,
556+ },
429557 {
430558 name : "should return encrypt and decrypt if the key kind belongs to a 'dek' key in a multi-key hierarchy" ,
431559 input : & model.KeyHierarchy {
@@ -649,6 +777,14 @@ func TestKeySpec(t *testing.T) {
649777 Algorithm : model .KeyAlgorithmAES256 ,
650778 },
651779 },
780+ {
781+ name : "should return nil if role is 'tek'" ,
782+ input : model.KeySpec {
783+ Kind : "K0" ,
784+ Role : model .KeyRoleTek ,
785+ Algorithm : model .KeyAlgorithmAES256 ,
786+ },
787+ },
652788 }
653789
654790 for _ , tt := range tts {
0 commit comments