Skip to content

Commit fc8d997

Browse files
committed
PR(MAIN): Gate & Test - Set Active Schema Version
1 parent 24993ba commit fc8d997

5 files changed

Lines changed: 91 additions & 3 deletions

File tree

acp/types/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const (
9090
AdminAACRelationDeletePerm
9191
AdminSchemaGetPerm
9292
AdminSchemaGetByVersionPerm
93+
AdminSchemaSetActiveVersionPerm
9394
AdminSchemaAddPerm
9495
AdminSchemaPatchPerm
9596
AdminPatchCollectionPerm
@@ -115,6 +116,7 @@ var RequiredResourcePermissionsForAdmin = []string{
115116
"aac-relation-delete",
116117
"schema-get",
117118
"schema-get-by-version",
119+
"schema-set-active-version",
118120
"schema-add",
119121
"schema-patch",
120122
"patch-collection",
@@ -165,6 +167,8 @@ resources:
165167
schema-get:
166168
expr: owner + admin
167169
schema-get-by-version:
170+
expr: owner + admin
171+
schema-set-active-version:
168172
expr: owner + admin
169173
schema-add:
170174
expr: owner + admin

internal/db/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ func (db *DB) SetActiveSchemaVersion(ctx context.Context, schemaVersionID string
244244
ctx, span := tracer.Start(ctx)
245245
defer span.End()
246246

247+
err := db.checkAdminAccess(ctx, acpTypes.AdminSchemaSetActiveVersionPerm)
248+
if err != nil {
249+
return err
250+
}
251+
247252
ctx, txn, err := ensureContextTxn(ctx, db, false)
248253
if err != nil {
249254
return err
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2025 Democratized Data Foundation
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the file licenses/BSL.txt.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0, included in the file
9+
// licenses/APL.txt.
10+
11+
package test_acp_aac
12+
13+
import (
14+
"testing"
15+
16+
testUtils "github.com/sourcenetwork/defradb/tests/integration"
17+
"github.com/sourcenetwork/immutable"
18+
)
19+
20+
func TestAAC_GatesSchemaSetActiveVersion_AllowIfAuthorizedElseError(t *testing.T) {
21+
test := testUtils.TestCase{
22+
Description: "admin acp correctly gates set active schema version operation, allow if authorized, otherwise error",
23+
Actions: []any{
24+
// Setup that we can do before aac is enabled.
25+
testUtils.SchemaUpdate{
26+
Schema: `
27+
type Users {
28+
name: String
29+
}
30+
`,
31+
},
32+
testUtils.SchemaPatch{
33+
Patch: `
34+
[
35+
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "email", "Kind": 11} }
36+
]
37+
`,
38+
SetAsDefaultVersion: immutable.Some(false),
39+
},
40+
41+
// Starting with ACC, so only authorized user(s) can perform operations.
42+
testUtils.Close{},
43+
testUtils.Start{
44+
Identity: testUtils.ClientIdentity(1),
45+
EnableAAC: true,
46+
},
47+
48+
// We haven't authorized non-identities. So, this should error.
49+
testUtils.SetActiveSchemaVersion{
50+
SchemaVersionID: "bafkreidt4i22v4bzga3aezlcxsrfbvuhzcbqo5bnfe2x2dgkpz3eds2afe",
51+
ExpectedError: "not authorized to perform operation",
52+
},
53+
54+
// Wrong user/identity will also not be authorized.
55+
testUtils.SetActiveSchemaVersion{
56+
Identity: testUtils.ClientIdentity(2),
57+
SchemaVersionID: "bafkreidt4i22v4bzga3aezlcxsrfbvuhzcbqo5bnfe2x2dgkpz3eds2afe",
58+
ExpectedError: "not authorized to perform operation",
59+
},
60+
61+
// This should work as the identity is authorized.
62+
testUtils.SetActiveSchemaVersion{
63+
Identity: testUtils.ClientIdentity(1),
64+
SchemaVersionID: "bafkreidt4i22v4bzga3aezlcxsrfbvuhzcbqo5bnfe2x2dgkpz3eds2afe",
65+
},
66+
},
67+
}
68+
69+
testUtils.ExecuteTestCase(t, test)
70+
}

tests/integration/test_case.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,14 @@ type SetActiveSchemaVersion struct {
333333
// If a value is not provided the default will be set on all nodes.
334334
NodeID immutable.Option[int]
335335

336+
// The identity of this request. Optional.
337+
//
338+
// If admin acp is enabled, identity will be used to check if this operation can be performed.
339+
Identity immutable.Option[Identity]
340+
336341
SchemaVersionID string
337-
ExpectedError string
342+
343+
ExpectedError string
338344
}
339345

340346
// CreateView is an action that will create a new View.

tests/integration/utils.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,9 +1213,12 @@ func setActiveSchemaVersion(
12131213
s *state,
12141214
action SetActiveSchemaVersion,
12151215
) {
1216-
_, nodes := getNodesWithIDs(action.NodeID, s.nodes)
1217-
for _, node := range nodes {
1216+
nodeIDs, nodes := getNodesWithIDs(action.NodeID, s.nodes)
1217+
for index, node := range nodes {
1218+
nodeID := nodeIDs[index]
1219+
s.ctx = getContextWithIdentity(s.ctx, s, action.Identity, nodeID)
12181220
err := node.SetActiveSchemaVersion(s.ctx, action.SchemaVersionID)
1221+
resetStateContext(s)
12191222
expectedErrorRaised := AssertError(s.t, s.testCase.Description, err, action.ExpectedError)
12201223

12211224
assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)

0 commit comments

Comments
 (0)