|
| 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_setup_then_start |
| 12 | + |
| 13 | +import ( |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/sourcenetwork/immutable" |
| 17 | + |
| 18 | + testUtils "github.com/sourcenetwork/defradb/tests/integration" |
| 19 | +) |
| 20 | + |
| 21 | +func TestNAC_GatesVerifySignaturePreSetup_AllowIfAuthorizedElseError(t *testing.T) { |
| 22 | + test := testUtils.TestCase{ |
| 23 | + Description: "admin acp correctly gates verify signature operation (setup before aac), allow if authorized, otherwise error", |
| 24 | + SupportedDatabaseTypes: immutable.Some( |
| 25 | + []testUtils.DatabaseType{ |
| 26 | + // This test only supports file type databases since the setup steps will be done before |
| 27 | + // the node is re-started with aac enabled (if it's in-memory it will loose setup state). |
| 28 | + testUtils.BadgerFileType, |
| 29 | + }, |
| 30 | + ), |
| 31 | + SupportedClientTypes: immutable.Some( |
| 32 | + []testUtils.ClientType{ |
| 33 | + // Default signer can be only tested with HTTP and CLI clients, because with Go client |
| 34 | + // when providing an identity, it includes the private key. |
| 35 | + testUtils.HTTPClientType, |
| 36 | + testUtils.CLIClientType, |
| 37 | + }, |
| 38 | + ), |
| 39 | + EnableSigning: true, |
| 40 | + Actions: []any{ |
| 41 | + // Note: Since this is not an in-memory test, we can do the setup steps before aac is enabled. |
| 42 | + testUtils.SchemaUpdate{ |
| 43 | + Schema: ` |
| 44 | + type Users { |
| 45 | + name: String |
| 46 | + age: Int |
| 47 | + }`, |
| 48 | + }, |
| 49 | + testUtils.CreateDoc{ |
| 50 | + DocMap: map[string]any{ |
| 51 | + "name": "John", |
| 52 | + "age": 21, |
| 53 | + }, |
| 54 | + }, |
| 55 | + |
| 56 | + // Starting with NAC, so only authorized user(s) can perform operations from here on out. |
| 57 | + testUtils.Close{}, |
| 58 | + testUtils.Start{ |
| 59 | + Identity: testUtils.ClientIdentity(1), |
| 60 | + EnableNAC: true, |
| 61 | + }, |
| 62 | + |
| 63 | + // We haven't authorized non-identities. So, this should error. |
| 64 | + testUtils.VerifyBlockSignature{ |
| 65 | + Identity: testUtils.NoIdentity(), |
| 66 | + SignerIdentity: testUtils.NodeIdentity(0).Value(), |
| 67 | + Cid: "bafyreicwhd5s762awsrx6eowwqkkfpq7r5nnjosiru7blgaxo32wx6enp4", |
| 68 | + ExpectedError: "not authorized to perform operation", |
| 69 | + }, |
| 70 | + |
| 71 | + // Wrong user/identity will also not be authorized. |
| 72 | + testUtils.VerifyBlockSignature{ |
| 73 | + Identity: testUtils.ClientIdentity(2), |
| 74 | + SignerIdentity: testUtils.NodeIdentity(0).Value(), |
| 75 | + Cid: "bafyreicwhd5s762awsrx6eowwqkkfpq7r5nnjosiru7blgaxo32wx6enp4", |
| 76 | + ExpectedError: "not authorized to perform operation", |
| 77 | + }, |
| 78 | + |
| 79 | + // This should work as the identity is authorized. |
| 80 | + testUtils.VerifyBlockSignature{ |
| 81 | + Identity: testUtils.ClientIdentity(1), |
| 82 | + SignerIdentity: testUtils.NodeIdentity(0).Value(), |
| 83 | + Cid: "bafyreicwhd5s762awsrx6eowwqkkfpq7r5nnjosiru7blgaxo32wx6enp4", |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + testUtils.ExecuteTestCase(t, test) |
| 89 | +} |
| 90 | + |
| 91 | +func TestNAC_GatesVerifySignatureGo_AllowIfAuthorizedElseError(t *testing.T) { |
| 92 | + test := testUtils.TestCase{ |
| 93 | + Description: "admin acp correctly gates verify signature operation (go client & setup before aac), allow if authorized, otherwise error", |
| 94 | + EnableSigning: true, |
| 95 | + SupportedDatabaseTypes: immutable.Some( |
| 96 | + []testUtils.DatabaseType{ |
| 97 | + // This test only supports file type databases since the setup steps will be done before |
| 98 | + // the node is re-started with aac enabled (if it's in-memory it will loose setup state). |
| 99 | + testUtils.BadgerFileType, |
| 100 | + }, |
| 101 | + ), |
| 102 | + SupportedClientTypes: immutable.Some( |
| 103 | + []testUtils.ClientType{ |
| 104 | + // Creating of signed documents over HTTP is not supported yet, because signing |
| 105 | + // requires a private key which we do not pass over HTTP. |
| 106 | + testUtils.GoClientType, |
| 107 | + }, |
| 108 | + ), |
| 109 | + Actions: []any{ |
| 110 | + // Starting with NAC, so only authorized user(s) can perform operations from here on out. |
| 111 | + testUtils.Close{}, |
| 112 | + testUtils.Start{ |
| 113 | + Identity: testUtils.ClientIdentity(1), |
| 114 | + EnableNAC: true, |
| 115 | + }, |
| 116 | + // Note: Doing setup steps after starting with aac enabled, otherwise the in-memory tests |
| 117 | + // will loose setup state when the restart happens (i.e. the restart that started aac). |
| 118 | + testUtils.SchemaUpdate{ |
| 119 | + Identity: testUtils.ClientIdentity(1), |
| 120 | + Schema: ` |
| 121 | + type Users { |
| 122 | + name: String |
| 123 | + age: Int |
| 124 | + }`, |
| 125 | + }, |
| 126 | + testUtils.CreateDoc{ |
| 127 | + Identity: testUtils.ClientIdentity(1), |
| 128 | + DocMap: map[string]any{ |
| 129 | + "name": "John", |
| 130 | + "age": 21, |
| 131 | + }, |
| 132 | + }, |
| 133 | + |
| 134 | + // We haven't authorized non-identities. So, this should error. |
| 135 | + testUtils.VerifyBlockSignature{ |
| 136 | + Identity: testUtils.NoIdentity(), |
| 137 | + SignerIdentity: testUtils.ClientIdentity(1).Value(), |
| 138 | + Cid: "bafyreicwhd5s762awsrx6eowwqkkfpq7r5nnjosiru7blgaxo32wx6enp4", |
| 139 | + ExpectedError: "not authorized to perform operation", |
| 140 | + }, |
| 141 | + |
| 142 | + // Wrong user/identity will also not be authorized. |
| 143 | + testUtils.VerifyBlockSignature{ |
| 144 | + Identity: testUtils.ClientIdentity(2), |
| 145 | + SignerIdentity: testUtils.ClientIdentity(1).Value(), |
| 146 | + Cid: "bafyreicwhd5s762awsrx6eowwqkkfpq7r5nnjosiru7blgaxo32wx6enp4", |
| 147 | + ExpectedError: "not authorized to perform operation", |
| 148 | + }, |
| 149 | + |
| 150 | + // This should work as the identity is authorized. |
| 151 | + testUtils.VerifyBlockSignature{ |
| 152 | + Identity: testUtils.ClientIdentity(1), |
| 153 | + SignerIdentity: testUtils.ClientIdentity(1).Value(), |
| 154 | + Cid: "bafyreicwhd5s762awsrx6eowwqkkfpq7r5nnjosiru7blgaxo32wx6enp4", |
| 155 | + ExpectedError: "could not find", |
| 156 | + }, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + testUtils.ExecuteTestCase(t, test) |
| 161 | +} |
0 commit comments