@@ -400,14 +400,12 @@ func (s *KasRegistryKeySuite) Test_ListKeys_KasID_Limit_Success() {
400
400
}
401
401
402
402
func (s * KasRegistryKeySuite ) Test_RotateKey_Multiple_Attributes_Values_Namespaces_Success () {
403
- attrValueIDs := make ([]string , 0 )
404
- attrDefIDs := make ([]string , 0 )
405
403
namespaceIDs := make ([]string , 0 )
406
404
keyIDs := make ([]string , 0 )
407
405
kasIDs := make ([]string , 0 )
408
406
409
407
defer func () {
410
- s .cleanupAttrs ( attrValueIDs , namespaceIDs , attrDefIDs )
408
+ s .cleanupNamespacesAndAttrsByIDs ( namespaceIDs )
411
409
s .cleanupKeys (keyIDs , kasIDs )
412
410
}()
413
411
@@ -426,8 +424,6 @@ func (s *KasRegistryKeySuite) Test_RotateKey_Multiple_Attributes_Values_Namespac
426
424
namespaceMap := s .setupNamespaceForRotate (1 , 1 , keyMap [rotateKey ].GetKey (), keyMap [nonRotateKey ].GetKey ())
427
425
namespaceIDs = append (namespaceIDs , namespaceMap [rotateKey ][0 ].GetId (), namespaceMap [nonRotateKey ][0 ].GetId ())
428
426
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 ())
431
427
432
428
newKey := kasregistry.RotateKeyRequest_NewKey {
433
429
KeyId : "new_key_id" ,
@@ -525,14 +521,12 @@ func (s *KasRegistryKeySuite) Test_RotateKey_Multiple_Attributes_Values_Namespac
525
521
}
526
522
527
523
func (s * KasRegistryKeySuite ) Test_RotateKey_Two_Attribute_Two_Namespace_0_AttributeValue_Success () {
528
- attrValueIDs := make ([]string , 0 )
529
- attrDefIDs := make ([]string , 0 )
530
524
namespaceIDs := make ([]string , 0 )
531
525
keyIDs := make ([]string , 0 )
532
526
kasIDs := make ([]string , 0 )
533
527
534
528
defer func () {
535
- s .cleanupAttrs ( attrValueIDs , namespaceIDs , attrDefIDs )
529
+ s .cleanupNamespacesAndAttrsByIDs ( namespaceIDs )
536
530
s .cleanupKeys (keyIDs , kasIDs )
537
531
}()
538
532
@@ -551,7 +545,6 @@ func (s *KasRegistryKeySuite) Test_RotateKey_Two_Attribute_Two_Namespace_0_Attri
551
545
namespaceMap := s .setupNamespaceForRotate (2 , 2 , keyMap [rotateKey ].GetKey (), keyMap [nonRotateKey ].GetKey ())
552
546
namespaceIDs = append (namespaceIDs , namespaceMap [rotateKey ][0 ].GetId (), namespaceMap [rotateKey ][1 ].GetId (), namespaceMap [nonRotateKey ][0 ].GetId (), namespaceMap [nonRotateKey ][1 ].GetId ())
553
547
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 ())
555
548
556
549
newKey := kasregistry.RotateKeyRequest_NewKey {
557
550
KeyId : "new_key_id" ,
@@ -1143,7 +1136,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_ByID_OneAttrValue_Success() {
1143
1136
keyIDs = append (keyIDs , key .GetKey ().GetId ())
1144
1137
}
1145
1138
s .cleanupKeys (keyIDs , kasIDs )
1146
- s .deleteAttributes (namespaces , attributeDefs , attrValues )
1139
+ s .cleanupNamespacesAndAttrs (namespaces )
1147
1140
}()
1148
1141
kasKey := s .createKeyAndKas ()
1149
1142
kasKeys = append (kasKeys , kasKey )
@@ -1209,7 +1202,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_By_Key_Success() {
1209
1202
keyIDs = append (keyIDs , key .GetKey ().GetId ())
1210
1203
}
1211
1204
s .cleanupKeys (keyIDs , kasIDs )
1212
- s .deleteAttributes (namespaces , attributeDefs , attrValues )
1205
+ s .cleanupNamespacesAndAttrs (namespaces )
1213
1206
}()
1214
1207
kasKey := s .createKeyAndKas ()
1215
1208
kasKeys = append (kasKeys , kasKey )
@@ -1308,7 +1301,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_SameKeyId_DifferentKas_Succes
1308
1301
keyIDs = append (keyIDs , key .GetKey ().GetId ())
1309
1302
}
1310
1303
s .cleanupKeys (keyIDs , kasIDs )
1311
- s .deleteAttributes (namespaces , attributeDefs , attrValues )
1304
+ s .cleanupNamespacesAndAttrs (namespaces )
1312
1305
}()
1313
1306
1314
1307
kasKey := s .createKeyAndKas ()
@@ -1411,7 +1404,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_Multiple_Keys_Pagination_Succ
1411
1404
keyIDs = append (keyIDs , key .GetKey ().GetId ())
1412
1405
}
1413
1406
s .cleanupKeys (keyIDs , kasIDs )
1414
- s .deleteAttributes (namespaces , attributeDefs , attrValues )
1407
+ s .cleanupNamespacesAndAttrs (namespaces )
1415
1408
}()
1416
1409
for i := range 2 {
1417
1410
kasKey := s .createKeyAndKas ()
@@ -1482,7 +1475,7 @@ func (s *KasRegistryKeySuite) Test_ListKeyMappings_Multiple_Mixed_Mappings() {
1482
1475
keyIDs = append (keyIDs , key .GetKey ().GetId ())
1483
1476
}
1484
1477
s .cleanupKeys (keyIDs , kasIDs )
1485
- s .deleteAttributes (namespaces , attributeDefs , attrValues )
1478
+ s .cleanupNamespacesAndAttrs (namespaces )
1486
1479
}()
1487
1480
1488
1481
for range 3 {
@@ -1746,23 +1739,9 @@ func (s *KasRegistryKeySuite) setupAttributesForRotate(numAttrsToRotate, numAttr
1746
1739
}
1747
1740
}
1748
1741
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
-
1764
1742
func (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" )
1766
1745
s .Require ().NoError (err )
1767
1746
1768
1747
for _ , id := range keyIDs {
@@ -1846,17 +1825,22 @@ func validatePrivatePublicCtx(s *suite.Suite, expectedPrivCtx, expectedPubCtx []
1846
1825
})
1847
1826
}
1848
1827
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 )
1856
1833
s .Require ().NoError (err )
1834
+ s .NotNil (ns )
1835
+ namespaces [i ] = ns
1857
1836
}
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 ) {
1858
1842
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 ())
1860
1844
s .Require ().NoError (err )
1861
1845
}
1862
1846
}
0 commit comments