|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +// ako/mxcli#554 — an access rule on an entity carrying AutoOwner or AutoChangedBy |
| 4 | +// was written with a MemberAccess for the implicit System.owner / System.changedBy |
| 5 | +// association, and mxbuild then reported CE0066 "Entity access is out of date" |
| 6 | +// against the whole module — which `update security` could not repair, because it |
| 7 | +// re-added the very entry that caused it and reported success in the same breath. |
| 8 | +// |
| 9 | +// The audit DATE members were already known to work the other way round (an entry |
| 10 | +// is CE0066, no entry checks clean — issuetracker #20). These two were assumed to |
| 11 | +// be the opposite case because they are associations rather than attributes, and |
| 12 | +// Mendix really does add them implicitly. Measured on mxbuild 11.14.0, one entity, |
| 13 | +// one rule, one variable at a time: |
| 14 | +// |
| 15 | +// AutoOwner + MemberAccess System.owner CE0066 |
| 16 | +// AutoOwner + no entry 0 errors |
| 17 | +// AutoChangedBy + MemberAccess System.changedBy CE0066 |
| 18 | +// AutoChangedBy + no entry 0 errors |
| 19 | +package modelsdkbackend |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/mendixlabs/mxcli/mdl/backend" |
| 25 | + "github.com/mendixlabs/mxcli/mdl/types" |
| 26 | + "github.com/mendixlabs/mxcli/model" |
| 27 | + "github.com/mendixlabs/mxcli/sdk/domainmodel" |
| 28 | +) |
| 29 | + |
| 30 | +func TestAccessRule_NoMemberAccessForAuditAssociations(t *testing.T) { |
| 31 | + for _, tc := range []struct { |
| 32 | + name string |
| 33 | + owner bool |
| 34 | + changed bool |
| 35 | + stale string // an entry already in the stored rule, to be repaired away |
| 36 | + mustGone []string |
| 37 | + }{ |
| 38 | + {name: "AutoOwner", owner: true, mustGone: []string{"System.owner"}}, |
| 39 | + {name: "AutoChangedBy", changed: true, mustGone: []string{"System.changedBy"}}, |
| 40 | + {name: "both", owner: true, changed: true, mustGone: []string{"System.owner", "System.changedBy"}}, |
| 41 | + // The repair path: a project damaged by an older mxcli must come back |
| 42 | + // clean, since `update security` is the documented remedy for CE0066. |
| 43 | + // The flag is OFF here, so the entry is stale twice over. |
| 44 | + {name: "stale entry with no flag is repaired away", stale: "System.owner", mustGone: []string{"System.owner"}}, |
| 45 | + } { |
| 46 | + t.Run(tc.name, func(t *testing.T) { |
| 47 | + proj := copyFixture(t) |
| 48 | + b := New() |
| 49 | + if err := b.Connect(proj); err != nil { |
| 50 | + t.Fatalf("connect: %v", err) |
| 51 | + } |
| 52 | + t.Cleanup(func() { _ = b.Disconnect() }) |
| 53 | + |
| 54 | + mod, err := b.GetModuleByName("MyFirstModule") |
| 55 | + if err != nil || mod == nil { |
| 56 | + t.Fatalf("GetModuleByName: %v", err) |
| 57 | + } |
| 58 | + dm, err := b.GetDomainModel(mod.ID) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("GetDomainModel: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + ent := &domainmodel.Entity{ |
| 64 | + Name: "ZzFab", |
| 65 | + Persistable: true, |
| 66 | + HasOwner: tc.owner, |
| 67 | + HasChangedBy: tc.changed, |
| 68 | + Attributes: []*domainmodel.Attribute{ |
| 69 | + {Name: "FabName", Type: &domainmodel.StringAttributeType{Length: 100}}, |
| 70 | + }, |
| 71 | + } |
| 72 | + if err := b.CreateEntity(dm.ID, ent); err != nil { |
| 73 | + t.Fatalf("CreateEntity: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + members := []types.EntityMemberAccess{{AttributeRef: "MyFirstModule.ZzFab.FabName", AccessRights: "ReadWrite"}} |
| 77 | + if tc.stale != "" { |
| 78 | + members = append(members, types.EntityMemberAccess{AssociationRef: tc.stale, AccessRights: "ReadWrite"}) |
| 79 | + } |
| 80 | + if err := b.AddEntityAccessRule(backend.EntityAccessRuleParams{ |
| 81 | + UnitID: dm.ID, |
| 82 | + EntityName: "ZzFab", |
| 83 | + RoleNames: []string{"MyFirstModule.User"}, |
| 84 | + AllowCreate: true, |
| 85 | + AllowDelete: true, |
| 86 | + DefaultMemberAccess: "ReadWrite", |
| 87 | + MemberAccesses: members, |
| 88 | + }); err != nil { |
| 89 | + t.Fatalf("AddEntityAccessRule: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + // The reconcile `update security` runs, and which every write path runs |
| 93 | + // after a program. It must not re-add what the grant left out. |
| 94 | + if _, err := b.ReconcileMemberAccesses(dm.ID, "MyFirstModule"); err != nil { |
| 95 | + t.Fatalf("ReconcileMemberAccesses: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + got := memberRefsOf(t, b, mod.ID, "ZzFab") |
| 99 | + for _, ref := range tc.mustGone { |
| 100 | + for _, g := range got { |
| 101 | + if g == ref { |
| 102 | + t.Errorf("access rule carries a MemberAccess for %q — mxbuild reports the module as "+ |
| 103 | + "CE0066 \"Entity access is out of date\" (members: %v)", ref, got) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + // Control: the reconcile still covers a real member. Without this the |
| 108 | + // test passes against a build that writes no members at all. |
| 109 | + var sawAttr bool |
| 110 | + for _, g := range got { |
| 111 | + if g == "MyFirstModule.ZzFab.FabName" { |
| 112 | + sawAttr = true |
| 113 | + } |
| 114 | + } |
| 115 | + if !sawAttr { |
| 116 | + t.Errorf("the entity's own attribute lost its MemberAccess (members: %v)", got) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// memberRefsOf returns the member references of the entity's first access rule, |
| 123 | +// read back from storage rather than from the value that was written. |
| 124 | +func memberRefsOf(t *testing.T, b *Backend, moduleID model.ID, entityName string) []string { |
| 125 | + t.Helper() |
| 126 | + dm, err := b.GetDomainModel(moduleID) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("GetDomainModel: %v", err) |
| 129 | + } |
| 130 | + for _, e := range dm.Entities { |
| 131 | + if e.Name != entityName { |
| 132 | + continue |
| 133 | + } |
| 134 | + var out []string |
| 135 | + for _, r := range e.AccessRules { |
| 136 | + for _, m := range r.MemberAccesses { |
| 137 | + if m.AttributeName != "" { |
| 138 | + out = append(out, m.AttributeName) |
| 139 | + } |
| 140 | + if m.AssociationName != "" { |
| 141 | + out = append(out, m.AssociationName) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + return out |
| 146 | + } |
| 147 | + t.Fatalf("entity %s not found after write", entityName) |
| 148 | + return nil |
| 149 | +} |
0 commit comments