Skip to content

Commit 677a289

Browse files
committed
fix tests
1 parent e1497c8 commit 677a289

File tree

4 files changed

+157
-105
lines changed

4 files changed

+157
-105
lines changed

service/integration/attribute_fqns_test.go

Lines changed: 59 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,14 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_Definition
469469
s.Contains(grantIDs, got.GetGrants()[1].GetId())
470470
pemIsPresent := false
471471

472-
for i, g := range got.GetGrants() {
472+
for _, g := range got.GetGrants() {
473473
if g.GetId() == key2.KeyAccessServerID {
474-
s.Equal(base64.StdEncoding.EncodeToString([]byte(g.GetPublicKey().GetCached().GetKeys()[i].GetPem())), key2.PublicKeyCtx)
474+
decodedPubKey, err := base64.StdEncoding.DecodeString(key2.PublicKeyCtx)
475+
s.Require().NoError(err)
476+
s.Equal(
477+
strings.TrimRight(string(decodedPubKey), "\n"),
478+
fmt.Sprintf("{\"pem\":\"%s\"}", base64.StdEncoding.EncodeToString([]byte(g.GetPublicKey().GetCached().GetKeys()[0].GetPem()))),
479+
)
475480
s.Equal(g.GetId(), key2.KeyAccessServerID)
476481
pemIsPresent = true
477482
}
@@ -541,16 +546,6 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_Values() {
541546
s.Empty(got.GetGrants())
542547
s.Empty(got.GetValues()[0].GetGrants())
543548

544-
// create a new kas registration
545-
remoteKASName := "testing-io-remote"
546-
remoteKAS, err := s.db.PolicyClient.CreateKeyAccessServer(s.ctx, &kasregistry.CreateKeyAccessServerRequest{
547-
Uri: "https://testing.io/kas",
548-
PublicKey: &policy.PublicKey{},
549-
Name: remoteKASName,
550-
})
551-
s.Require().NoError(err)
552-
s.NotNil(remoteKAS)
553-
554549
// make a grant association to the first value
555550
grant, err := s.db.PolicyClient.AssignPublicKeyToValue(s.ctx, &attributes.ValueKey{
556551
KeyId: key.ID,
@@ -559,16 +554,6 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_Values() {
559554
s.Require().NoError(err)
560555
s.NotNil(grant)
561556

562-
// create a second kas registration and grant it to the second value
563-
cachedKASName := "testion-io-local"
564-
cachedKAS, err := s.db.PolicyClient.CreateKeyAccessServer(s.ctx, &kasregistry.CreateKeyAccessServerRequest{
565-
Uri: "https://testing.io/kas2",
566-
PublicKey: &policy.PublicKey{},
567-
Name: cachedKASName,
568-
})
569-
s.Require().NoError(err)
570-
s.NotNil(cachedKAS)
571-
572557
grant2, err := s.db.PolicyClient.AssignPublicKeyToValue(s.ctx, &attributes.ValueKey{
573558
KeyId: key2.ID,
574559
ValueId: valueSecond.GetId(),
@@ -597,11 +582,9 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_Values() {
597582
firstGrant := grants[0]
598583
switch v.GetId() {
599584
case valueFirst.GetId():
600-
s.Equal(remoteKAS.GetId(), firstGrant.GetId())
601-
s.Equal(remoteKASName, firstGrant.GetName())
585+
s.Equal(key.KeyAccessServerID, firstGrant.GetId())
602586
case valueSecond.GetId():
603-
s.Equal(cachedKAS.GetId(), firstGrant.GetId())
604-
s.Equal(cachedKASName, firstGrant.GetName())
587+
s.Equal(key2.KeyAccessServerID, firstGrant.GetId())
605588
default:
606589
s.Fail("unexpected value", v)
607590
}
@@ -652,14 +635,6 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_DefAndValu
652635
s.Empty(got.GetGrants())
653636
s.Empty(got.GetValues()[0].GetGrants())
654637

655-
// create a new kas registration
656-
valKAS1, err := s.db.PolicyClient.CreateKeyAccessServer(s.ctx, &kasregistry.CreateKeyAccessServerRequest{
657-
Uri: "https://testing.org/kas",
658-
PublicKey: &policy.PublicKey{},
659-
})
660-
s.Require().NoError(err)
661-
s.NotNil(valKAS1)
662-
663638
// make a grant association to the first value
664639
grant, err := s.db.PolicyClient.AssignPublicKeyToValue(s.ctx, &attributes.ValueKey{
665640
KeyId: key.ID,
@@ -668,29 +643,13 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_DefAndValu
668643
s.Require().NoError(err)
669644
s.NotNil(grant)
670645

671-
// create a second kas registration and grant it to the second value
672-
valKAS2, err := s.db.PolicyClient.CreateKeyAccessServer(s.ctx, &kasregistry.CreateKeyAccessServerRequest{
673-
Uri: "https://testing.org/kas2",
674-
PublicKey: &policy.PublicKey{},
675-
})
676-
s.Require().NoError(err)
677-
s.NotNil(valKAS2)
678-
679646
grant2, err := s.db.PolicyClient.AssignPublicKeyToValue(s.ctx, &attributes.ValueKey{
680647
KeyId: key2.ID,
681648
ValueId: valueSecond.GetId(),
682649
})
683650
s.Require().NoError(err)
684651
s.NotNil(grant2)
685652

686-
// create a third kas registration and grant it to the attribute definition
687-
defKAS, err := s.db.PolicyClient.CreateKeyAccessServer(s.ctx, &kasregistry.CreateKeyAccessServerRequest{
688-
Uri: "https://testing.org/kas3",
689-
PublicKey: &policy.PublicKey{},
690-
})
691-
s.Require().NoError(err)
692-
s.NotNil(defKAS)
693-
694653
defGrant, err := s.db.PolicyClient.AssignPublicKeyToAttribute(s.ctx, &attributes.AttributeKey{
695654
KeyId: key3.ID,
696655
AttributeId: a.GetId(),
@@ -705,24 +664,24 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_DefAndValu
705664

706665
// ensure the attribute has exactly one definition grant
707666
s.Len(got.GetGrants(), 1)
708-
s.Equal(defKAS.GetId(), got.GetGrants()[0].GetId())
667+
s.Equal(key3.KeyAccessServerID, got.GetGrants()[0].GetId())
709668

710669
// get the attribute by the fqn of one of its values and ensure the grants are present
711670
got, err = s.db.PolicyClient.GetAttributeByFqn(s.ctx, val1Fqn)
712671
s.Require().NoError(err)
713672
s.NotNil(got)
714673
s.Len(got.GetValues(), 2)
715674
s.Len(got.GetGrants(), 1)
716-
s.Equal(defKAS.GetId(), got.GetGrants()[0].GetId())
675+
s.Equal(key.KeyAccessServerID, got.GetGrants()[0].GetId())
717676

718677
for _, v := range got.GetValues() {
719678
switch v.GetId() {
720679
case valueFirst.GetId():
721680
s.Require().Len(v.GetGrants(), 1)
722-
s.Equal(valKAS1.GetId(), v.GetGrants()[0].GetId())
681+
s.Equal(key.KeyAccessServerID, v.GetGrants()[0].GetId())
723682
case valueSecond.GetId():
724683
s.Require().Len(v.GetGrants(), 1)
725-
s.Equal(valKAS2.GetId(), v.GetGrants()[0].GetId())
684+
s.Equal(key2.KeyAccessServerID, v.GetGrants()[0].GetId())
726685
default:
727686
s.Fail("unexpected value", v)
728687
}
@@ -748,16 +707,6 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_NamespaceG
748707
s.Require().NoError(err)
749708
s.NotNil(attr)
750709

751-
// create a new kas registration
752-
nsKASName := "namespace-kas1"
753-
kas, err := s.db.PolicyClient.CreateKeyAccessServer(s.ctx, &kasregistry.CreateKeyAccessServerRequest{
754-
Uri: "https://testing_granted_namespace.com/kas",
755-
PublicKey: &policy.PublicKey{},
756-
Name: nsKASName,
757-
})
758-
s.Require().NoError(err)
759-
s.NotNil(kas)
760-
761710
// make a grant association to the namespace
762711
grant, err := s.db.PolicyClient.AssignPublicKeyToNamespace(s.ctx, &namespaces.NamespaceKey{
763712
KeyId: key.ID,
@@ -775,20 +724,25 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_NamespaceG
775724
gotNs := got.GetNamespace()
776725
grants := gotNs.GetGrants()
777726
s.Len(grants, 1)
778-
s.Equal(kas.GetId(), grants[0].GetId())
779-
s.Equal(nsKASName, grants[0].GetName())
727+
s.Equal(key.KeyAccessServerID, grants[0].GetId())
780728
}
781729

782730
// for all the big tests set up:
783731
// attribute name is "test_attr", values are "value1" and "value2"
784732
// kas uris granted to each are "https://testing_granted_<ns | attr | val1 | val1>.com/<ns>/kas",
733+
type KasAssociations struct {
734+
kasID string
735+
uri string
736+
keyID string
737+
keyUUID string
738+
}
785739
type bigSetup struct {
786740
attrFqn string
787741
nsID string
788742
attrID string
789743
val1ID string
790744
val2ID string
791-
kasAssociations map[string]string
745+
kasAssociations map[string]*KasAssociations
792746
}
793747

794748
func (s *AttributeFqnSuite) TestGetAttributeByFqn_SameResultsWhetherAttrOrValueFqnUsed() {
@@ -846,53 +800,53 @@ func (s *AttributeFqnSuite) TestGetAttributeByFqn_WithKeyAccessGrants_ProperOnAl
846800
// ensure the namespace has the grants
847801
s.Len(got.GetNamespace().GetGrants(), 1)
848802
nsGrant := got.GetNamespace().GetGrants()[0]
849-
s.Equal(setup.kasAssociations[got.GetNamespace().GetId()], nsGrant.GetId())
803+
s.Equal(setup.kasAssociations[got.GetNamespace().GetId()].kasID, nsGrant.GetId())
850804
s.Equal(fmt.Sprintf("https://testing_granted_ns.com/%s/kas", ns), nsGrant.GetUri())
851805

852806
// ensure the attribute has the grants
853807
s.Len(got.GetGrants(), 1)
854808
attrGrant := got.GetGrants()[0]
855-
s.Equal(setup.kasAssociations[got.GetId()], attrGrant.GetId())
809+
s.Equal(setup.kasAssociations[got.GetId()].kasID, attrGrant.GetId())
856810
s.Equal(fmt.Sprintf("https://testing_granted_attr.com/%s/kas", ns), attrGrant.GetUri())
857811

858812
// ensure the first value has the grants
859813
val1 := got.GetValues()[0]
860814
s.Len(val1.GetGrants(), 1)
861815
val1Grant := val1.GetGrants()[0]
862-
s.Equal(setup.kasAssociations[val1.GetId()], val1Grant.GetId())
816+
s.Equal(setup.kasAssociations[val1.GetId()].kasID, val1Grant.GetId())
863817
s.Equal(fmt.Sprintf("https://testing_granted_val.com/%s/kas", ns), val1Grant.GetUri())
864818

865819
// ensure the second value has the grants
866820
val2 := got.GetValues()[1]
867821
s.Len(val2.GetGrants(), 1)
868822
val2Grant := val2.GetGrants()[0]
869-
s.Equal(setup.kasAssociations[val2.GetId()], val2Grant.GetId())
823+
s.Equal(setup.kasAssociations[val2.GetId()].kasID, val2Grant.GetId())
870824
s.Equal(fmt.Sprintf("https://testing_granted_val2.com/%s/kas", ns), val2Grant.GetUri())
871825

872826
// remove grants from all objects
873-
// _, err = s.db.PolicyClient.RemoveKeyAccessServerFromNamespace(s.ctx, &namespaces.NamespaceKeyAccessServer{
874-
// KeyAccessServerId: nsGrant.GetId(),
875-
// NamespaceId: got.GetNamespace().GetId(),
876-
// })
877-
// s.Require().NoError(err)
878-
879-
// _, err = s.db.PolicyClient.RemoveKeyAccessServerFromAttribute(s.ctx, &attributes.AttributeKeyAccessServer{
880-
// KeyAccessServerId: attrGrant.GetId(),
881-
// AttributeId: got.GetId(),
882-
// })
883-
// s.Require().NoError(err)
884-
885-
// _, err = s.db.PolicyClient.RemoveKeyAccessServerFromValue(s.ctx, &attributes.ValueKeyAccessServer{
886-
// KeyAccessServerId: val1Grant.GetId(),
887-
// ValueId: val1.GetId(),
888-
// })
889-
// s.Require().NoError(err)
890-
891-
// _, err = s.db.PolicyClient.RemoveKeyAccessServerFromValue(s.ctx, &attributes.ValueKeyAccessServer{
892-
// KeyAccessServerId: val2Grant.GetId(),
893-
// ValueId: val2.GetId(),
894-
// })
895-
// s.Require().NoError(err)
827+
_, err = s.db.PolicyClient.RemovePublicKeyFromNamespace(s.ctx, &namespaces.NamespaceKey{
828+
KeyId: setup.kasAssociations[got.GetNamespace().GetId()].keyUUID,
829+
NamespaceId: got.GetNamespace().GetId(),
830+
})
831+
s.Require().NoError(err)
832+
833+
_, err = s.db.PolicyClient.RemovePublicKeyFromAttribute(s.ctx, &attributes.AttributeKey{
834+
KeyId: setup.kasAssociations[got.GetId()].keyUUID,
835+
AttributeId: got.GetId(),
836+
})
837+
s.Require().NoError(err)
838+
839+
_, err = s.db.PolicyClient.RemovePublicKeyFromValue(s.ctx, &attributes.ValueKey{
840+
KeyId: setup.kasAssociations[val1.GetId()].keyUUID,
841+
ValueId: val1.GetId(),
842+
})
843+
s.Require().NoError(err)
844+
845+
_, err = s.db.PolicyClient.RemovePublicKeyFromValue(s.ctx, &attributes.ValueKey{
846+
KeyId: setup.kasAssociations[val2.GetId()].keyUUID,
847+
ValueId: val2.GetId(),
848+
})
849+
s.Require().NoError(err)
896850

897851
// ensure the grants are removed from all objects
898852
got, err = s.db.PolicyClient.GetAttributeByFqn(s.ctx, setup.attrFqn)
@@ -1684,7 +1638,7 @@ func (s *AttributeFqnSuite) bigTestSetup(namespaceName string) bigSetup {
16841638
val1KasURI := fmt.Sprintf("https://testing_granted_val.com/%s/kas", namespaceName)
16851639
val2KasURI := fmt.Sprintf("https://testing_granted_val2.com/%s/kas", namespaceName)
16861640

1687-
kasAssociations := map[string]string{}
1641+
kasAssociations := map[string]*KasAssociations{}
16881642
// create new KASes
16891643
for _, toAssociate := range []struct {
16901644
id string
@@ -1720,36 +1674,41 @@ func (s *AttributeFqnSuite) bigTestSetup(namespaceName string) bigSetup {
17201674
s.Require().NoError(err)
17211675
s.NotNil(resp)
17221676

1723-
kasAssociations[toAssociate.id] = resp.GetKasKey().GetKey().GetId()
1677+
kasAssociations[toAssociate.id] = &KasAssociations{
1678+
kasID: kas.GetId(),
1679+
uri: toAssociate.uri,
1680+
keyID: resp.GetKasKey().GetKey().GetKeyId(),
1681+
keyUUID: resp.GetKasKey().GetKey().GetId(),
1682+
}
17241683
}
17251684

17261685
// make a grant association to the namespace
17271686
nsGrant, err := s.db.PolicyClient.AssignPublicKeyToNamespace(s.ctx, &namespaces.NamespaceKey{
1728-
KeyId: kasAssociations[ns.GetId()],
1687+
KeyId: kasAssociations[ns.GetId()].keyUUID,
17291688
NamespaceId: ns.GetId(),
17301689
})
17311690
s.Require().NoError(err)
17321691
s.NotNil(nsGrant)
17331692

17341693
// make a grant association to the attribute definition
17351694
attrGrant, err := s.db.PolicyClient.AssignPublicKeyToAttribute(s.ctx, &attributes.AttributeKey{
1736-
KeyId: kasAssociations[attr.GetId()],
1695+
KeyId: kasAssociations[attr.GetId()].keyUUID,
17371696
AttributeId: attr.GetId(),
17381697
})
17391698
s.Require().NoError(err)
17401699
s.NotNil(attrGrant)
17411700

17421701
// make a grant association to the first value
17431702
val1Grant, err := s.db.PolicyClient.AssignPublicKeyToValue(s.ctx, &attributes.ValueKey{
1744-
KeyId: kasAssociations[val1.GetId()],
1703+
KeyId: kasAssociations[val1.GetId()].keyUUID,
17451704
ValueId: val1.GetId(),
17461705
})
17471706
s.Require().NoError(err)
17481707
s.NotNil(val1Grant)
17491708

17501709
// make a grant association to the second value
17511710
val2Grant, err := s.db.PolicyClient.AssignPublicKeyToValue(s.ctx, &attributes.ValueKey{
1752-
KeyId: kasAssociations[val2.GetId()],
1711+
KeyId: kasAssociations[val2.GetId()].keyUUID,
17531712
ValueId: val2.GetId(),
17541713
})
17551714
s.Require().NoError(err)

service/internal/fixtures/fixtures.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,33 @@ func (f *Fixtures) provisionProviderConfigs() int64 {
668668

669669
func (f *Fixtures) provisionKasRegistryKeys() int64 {
670670
values := make([][]string, 0, len(fixtureData.KasRegistryKeys.Data))
671+
// Create a local copy of columns to avoid modifying the global fixtureData
672+
columns := make([]string, len(fixtureData.KasRegistryKeys.Metadata.Columns))
673+
copy(columns, fixtureData.KasRegistryKeys.Metadata.Columns)
674+
675+
// Check if provider_config_id needs to be added to the columns
676+
// This should only happen once, not inside the loop
677+
providerConfigIDColumnPresent := false
678+
for _, d := range fixtureData.KasRegistryKeys.Data {
679+
if d.ProviderConfigID != "" {
680+
providerConfigIDColumnPresent = true
681+
break
682+
}
683+
}
684+
if providerConfigIDColumnPresent {
685+
// Ensure "provider_config_id" is not already in the base columns from YAML
686+
exists := false
687+
for _, colName := range columns {
688+
if colName == "provider_config_id" {
689+
exists = true
690+
break
691+
}
692+
}
693+
if !exists {
694+
columns = append(columns, "provider_config_id")
695+
}
696+
}
697+
671698
for _, d := range fixtureData.KasRegistryKeys.Data {
672699
pubCtx, err := base64.StdEncoding.DecodeString(d.PublicKeyCtx)
673700
if err != nil {
@@ -679,7 +706,8 @@ func (f *Fixtures) provisionKasRegistryKeys() int64 {
679706
slog.Error("⛔️ 📦 issue with kas registry private key context - check policy_fixtures.yaml for issues")
680707
panic("issue with kas registry private key context")
681708
}
682-
values = append(values, []string{
709+
710+
currentRowValues := []string{
683711
f.db.StringWrap(d.ID),
684712
f.db.StringWrap(d.KeyAccessServerID),
685713
f.db.StringWrap(d.KeyAlgorithm),
@@ -688,11 +716,19 @@ func (f *Fixtures) provisionKasRegistryKeys() int64 {
688716
f.db.StringWrap(d.KeyStatus),
689717
f.db.StringWrap(string(privateCtx)),
690718
f.db.StringWrap(string(pubCtx)),
691-
f.db.StringWrap(d.ProviderConfigID),
692-
})
719+
}
720+
721+
if providerConfigIDColumnPresent {
722+
if d.ProviderConfigID == "" {
723+
currentRowValues = append(currentRowValues, "NULL") // Use NULL for empty UUID
724+
} else {
725+
currentRowValues = append(currentRowValues, f.db.StringWrap(d.ProviderConfigID))
726+
}
727+
}
728+
values = append(values, currentRowValues)
693729
}
694730

695-
return f.provision(fixtureData.KasRegistryKeys.Metadata.TableName, fixtureData.KasRegistryKeys.Metadata.Columns, values)
731+
return f.provision(fixtureData.KasRegistryKeys.Metadata.TableName, columns, values)
696732
}
697733

698734
func (f *Fixtures) provisionRegisteredResources() int64 {

0 commit comments

Comments
 (0)