@@ -400,14 +400,12 @@ func (s *KasRegistryKeySuite) Test_ListKeys_KasID_Limit_Success() {
400400}
401401
402402func  (s  * KasRegistryKeySuite ) Test_RotateKey_Multiple_Attributes_Values_Namespaces_Success () {
403- 	attrValueIDs  :=  make ([]string , 0 )
404- 	attrDefIDs  :=  make ([]string , 0 )
405403	namespaceIDs  :=  make ([]string , 0 )
406404	keyIDs  :=  make ([]string , 0 )
407405	kasIDs  :=  make ([]string , 0 )
408406
409407	defer  func () {
410- 		s .cleanupAttrs ( attrValueIDs ,  namespaceIDs ,  attrDefIDs )
408+ 		s .cleanupNamespacesAndAttrsByIDs ( namespaceIDs )
411409		s .cleanupKeys (keyIDs , kasIDs )
412410	}()
413411
@@ -426,8 +424,6 @@ func (s *KasRegistryKeySuite) Test_RotateKey_Multiple_Attributes_Values_Namespac
426424	namespaceMap  :=  s .setupNamespaceForRotate (1 , 1 , keyMap [rotateKey ].GetKey (), keyMap [nonRotateKey ].GetKey ())
427425	namespaceIDs  =  append (namespaceIDs , namespaceMap [rotateKey ][0 ].GetId (), namespaceMap [nonRotateKey ][0 ].GetId ())
428426	attributeMap  :=  s .setupAttributesForRotate (1 , 1 , 1 , 1 , namespaceMap , keyMap [rotateKey ].GetKey (), keyMap [nonRotateKey ].GetKey ())
429- 	attrDefIDs  =  append (attrDefIDs , attributeMap [rotateKey ][0 ].GetId (), attributeMap [nonRotateKey ][0 ].GetId ())
430- 	attrValueIDs  =  append (attrValueIDs , attributeMap [rotateKey ][0 ].GetValues ()[0 ].GetId (), attributeMap [nonRotateKey ][0 ].GetValues ()[0 ].GetId ())
431427
432428	newKey  :=  kasregistry.RotateKeyRequest_NewKey {
433429		KeyId :        "new_key_id" ,
@@ -525,14 +521,12 @@ func (s *KasRegistryKeySuite) Test_RotateKey_Multiple_Attributes_Values_Namespac
525521}
526522
527523func  (s  * KasRegistryKeySuite ) Test_RotateKey_Two_Attribute_Two_Namespace_0_AttributeValue_Success () {
528- 	attrValueIDs  :=  make ([]string , 0 )
529- 	attrDefIDs  :=  make ([]string , 0 )
530524	namespaceIDs  :=  make ([]string , 0 )
531525	keyIDs  :=  make ([]string , 0 )
532526	kasIDs  :=  make ([]string , 0 )
533527
534528	defer  func () {
535- 		s .cleanupAttrs ( attrValueIDs ,  namespaceIDs ,  attrDefIDs )
529+ 		s .cleanupNamespacesAndAttrsByIDs ( namespaceIDs )
536530		s .cleanupKeys (keyIDs , kasIDs )
537531	}()
538532
@@ -551,7 +545,6 @@ func (s *KasRegistryKeySuite) Test_RotateKey_Two_Attribute_Two_Namespace_0_Attri
551545	namespaceMap  :=  s .setupNamespaceForRotate (2 , 2 , keyMap [rotateKey ].GetKey (), keyMap [nonRotateKey ].GetKey ())
552546	namespaceIDs  =  append (namespaceIDs , namespaceMap [rotateKey ][0 ].GetId (), namespaceMap [rotateKey ][1 ].GetId (), namespaceMap [nonRotateKey ][0 ].GetId (), namespaceMap [nonRotateKey ][1 ].GetId ())
553547	attributeMap  :=  s .setupAttributesForRotate (2 , 2 , 0 , 0 , namespaceMap , keyMap [rotateKey ].GetKey (), keyMap [nonRotateKey ].GetKey ())
554- 	attrDefIDs  =  append (attrDefIDs , attributeMap [rotateKey ][0 ].GetId (), attributeMap [nonRotateKey ][0 ].GetId (), attributeMap [rotateKey ][1 ].GetId (), attributeMap [nonRotateKey ][1 ].GetId ())
555548
556549	newKey  :=  kasregistry.RotateKeyRequest_NewKey {
557550		KeyId :        "new_key_id" ,
@@ -1143,7 +1136,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_ByID_OneAttrValue_Success() {
11431136			keyIDs  =  append (keyIDs , key .GetKey ().GetId ())
11441137		}
11451138		s .cleanupKeys (keyIDs , kasIDs )
1146- 		s .deleteAttributes (namespaces ,  attributeDefs ,  attrValues )
1139+ 		s .cleanupNamespacesAndAttrs (namespaces )
11471140	}()
11481141	kasKey  :=  s .createKeyAndKas ()
11491142	kasKeys  =  append (kasKeys , kasKey )
@@ -1209,7 +1202,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_By_Key_Success() {
12091202			keyIDs  =  append (keyIDs , key .GetKey ().GetId ())
12101203		}
12111204		s .cleanupKeys (keyIDs , kasIDs )
1212- 		s .deleteAttributes (namespaces ,  attributeDefs ,  attrValues )
1205+ 		s .cleanupNamespacesAndAttrs (namespaces )
12131206	}()
12141207	kasKey  :=  s .createKeyAndKas ()
12151208	kasKeys  =  append (kasKeys , kasKey )
@@ -1308,7 +1301,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_SameKeyId_DifferentKas_Succes
13081301			keyIDs  =  append (keyIDs , key .GetKey ().GetId ())
13091302		}
13101303		s .cleanupKeys (keyIDs , kasIDs )
1311- 		s .deleteAttributes (namespaces ,  attributeDefs ,  attrValues )
1304+ 		s .cleanupNamespacesAndAttrs (namespaces )
13121305	}()
13131306
13141307	kasKey  :=  s .createKeyAndKas ()
@@ -1411,7 +1404,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_Multiple_Keys_Pagination_Succ
14111404			keyIDs  =  append (keyIDs , key .GetKey ().GetId ())
14121405		}
14131406		s .cleanupKeys (keyIDs , kasIDs )
1414- 		s .deleteAttributes (namespaces ,  attributeDefs ,  attrValues )
1407+ 		s .cleanupNamespacesAndAttrs (namespaces )
14151408	}()
14161409	for  i  :=  range  2  {
14171410		kasKey  :=  s .createKeyAndKas ()
@@ -1482,7 +1475,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_Multiple_Mixed_Mappings() {
14821475			keyIDs  =  append (keyIDs , key .GetKey ().GetId ())
14831476		}
14841477		s .cleanupKeys (keyIDs , kasIDs )
1485- 		s .deleteAttributes (namespaces ,  attributeDefs ,  attrValues )
1478+ 		s .cleanupNamespacesAndAttrs (namespaces )
14861479	}()
14871480
14881481	for  range  3  {
@@ -1746,23 +1739,9 @@ func (s *KasRegistryKeySuite) setupAttributesForRotate(numAttrsToRotate, numAttr
17461739	}
17471740}
17481741
1749- func  (s  * KasRegistryKeySuite ) cleanupAttrs (attrValueIDs  []string , namespaceIDs  []string , attributeIDs  []string ) {
1750- 	for  _ , id  :=  range  attrValueIDs  {
1751- 		_ , err  :=  s .db .PolicyClient .DeleteAttributeValue (s .ctx , id )
1752- 		s .Require ().NoError (err )
1753- 	}
1754- 	for  _ , id  :=  range  namespaceIDs  {
1755- 		_ , err  :=  s .db .PolicyClient .DeleteNamespace (s .ctx , id )
1756- 		s .Require ().NoError (err )
1757- 	}
1758- 	for  _ , id  :=  range  attributeIDs  {
1759- 		_ , err  :=  s .db .PolicyClient .DeleteAttribute (s .ctx , id )
1760- 		s .Require ().NoError (err )
1761- 	}
1762- }
1763- 
17641742func  (s  * KasRegistryKeySuite ) cleanupKeys (keyIDs  []string , keyAccessServerIDs  []string ) {
1765- 	err  :=  s .db .PolicyClient .DeleteAllBaseKeys (s .ctx )
1743+ 	// use Pgx.Exec because DELETE is only for testing and should not be part of PolicyDBClient 
1744+ 	_ , err  :=  s .db .PolicyClient .Pgx .Exec (s .ctx , "DELETE FROM base_keys" )
17661745	s .Require ().NoError (err )
17671746
17681747	for  _ , id  :=  range  keyIDs  {
@@ -1846,17 +1825,22 @@ func validatePrivatePublicCtx(s *suite.Suite, expectedPrivCtx, expectedPubCtx []
18461825	})
18471826}
18481827
1849- func  (s  * KasRegistryKeySuite ) deleteAttributes (namespaces  []* policy.Namespace , attributeDefs  []* policy.Attribute , attrValues  []* policy.Value ) {
1850- 	for  _ , value  :=  range  attrValues  {
1851- 		_ , err  :=  s .db .PolicyClient .DeleteAttributeValue (s .ctx , value .GetId ())
1852- 		s .Require ().NoError (err )
1853- 	}
1854- 	for  _ , def  :=  range  attributeDefs  {
1855- 		_ , err  :=  s .db .PolicyClient .DeleteAttribute (s .ctx , def .GetId ())
1828+ // cascade delete will remove namespaces and all associated attributes and values 
1829+ func  (s  * KasRegistryKeySuite ) cleanupNamespacesAndAttrsByIDs (namespaceIDs  []string ) {
1830+ 	namespaces  :=  make ([]* policy.Namespace , len (namespaceIDs ))
1831+ 	for  i , id  :=  range  namespaceIDs  {
1832+ 		ns , err  :=  s .db .PolicyClient .GetNamespace (s .ctx , id )
18561833		s .Require ().NoError (err )
1834+ 		s .NotNil (ns )
1835+ 		namespaces [i ] =  ns 
18571836	}
1837+ 	s .cleanupNamespacesAndAttrs (namespaces )
1838+ }
1839+ 
1840+ // cascade delete will remove namespaces and all associated attributes and values 
1841+ func  (s  * KasRegistryKeySuite ) cleanupNamespacesAndAttrs (namespaces  []* policy.Namespace ) {
18581842	for  _ , ns  :=  range  namespaces  {
1859- 		_ , err  :=  s .db .PolicyClient .DeleteNamespace (s .ctx , ns . GetId ())
1843+ 		_ , err  :=  s .db .PolicyClient .UnsafeDeleteNamespace (s .ctx , ns ,  ns . GetFqn ())
18601844		s .Require ().NoError (err )
18611845	}
18621846}
0 commit comments