Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Access Control Policy #2338

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b0f8886
PR(-): Fix test framework by assert -> require
shahzadlone Feb 23, 2024
8a85297
PR(-): Rename testutils -> testUtils
shahzadlone Feb 23, 2024
a07eb08
PR(-): Fix odd line split in `collection_index`
shahzadlone Jan 26, 2024
82523b6
PR(-): Fix execute explain fetcher control flow
shahzadlone Feb 1, 2024
ecc7700
PR(-): Resolve "collection" linter error
shahzadlone Feb 29, 2024
f7a428a
PR(-): Update old docs, so new acp docs dont mix
shahzadlone Feb 27, 2024
7a6a4fa
PR(-): Remove Duplicate AddView Post Route
shahzadlone Mar 11, 2024
e72bbea
PR(-): Remove duplicate check done in applyDelete
shahzadlone Mar 13, 2024
a72ba0c
PR(-): Pass Unused Context to col.CreateMany
shahzadlone Mar 20, 2024
ee2825b
PR(-): Return the correct error in channel
shahzadlone Mar 20, 2024
bbadf0c
PR(-): Make a `mustGetContextDB` from cmd
shahzadlone Mar 21, 2024
cb8b0e5
PR(ACP-DOCS): Add Valid YAML & JSON DPI Examples
shahzadlone Feb 15, 2024
e1ad1f0
PR(ACP-DOCS): Add Permissioned Schema Examples
shahzadlone Feb 29, 2024
9ca1f63
PR(ACP-DOCS): Link the ACP.md in the README.md
shahzadlone Mar 21, 2024
f064b10
PR(ACP): Add ACP Interface Implementation
shahzadlone Feb 23, 2024
6cc17d8
PR(ACP-DEPS): Pull ACP Dependencies & Mod Tidy
shahzadlone Mar 14, 2024
2aca839
PR(ACP-TEST): Setup Test Framework To Add Policy
shahzadlone Mar 19, 2024
c098d7d
PR(ACP-TEST): Test 'Adding Of Policy' With ACP
shahzadlone Feb 8, 2024
26128da
PR(ACP): Add Policy On Collection Description
shahzadlone Mar 17, 2024
e0d1a1e
PR(ACP): Store Policy Key and Register ACP Object
shahzadlone Mar 18, 2024
186a193
PR(ACP): Remove The Policy Key
shahzadlone Mar 18, 2024
831b042
PR(ACP): Add/Implement Permissioned Fetcher
shahzadlone Feb 1, 2024
fa67a00
PR(ACP-MOCKS): Update Fetcher Mocks & Index Test
shahzadlone Feb 29, 2024
9cbb7ea
PR(ACP-TEST): Test Permission Schema Reject/Accept
shahzadlone Feb 13, 2024
650f6b7
PR(ACP): Add ACP Check On Write/Mutation Requests
shahzadlone Feb 27, 2024
b8defa2
PR(ACP): Prepare Client With ACPModule & AddPolicy
shahzadlone Feb 28, 2024
b1b27c3
PR(ACP): Add CLI Client For Adding Policy Command
shahzadlone Feb 27, 2024
a8c9191
PR(ACP): Add HTTP Client For Adding Policy Command
shahzadlone Mar 11, 2024
66a9c18
PR(IDENTITY): Add Simple Identity (No Auth)
shahzadlone Mar 28, 2024
8b04aaa
PR(IDENTITY): Pass Identity Through Auth Header
shahzadlone Mar 18, 2024
6257040
PR(IDENTITY): Consume identity for GetAllDocIDs
shahzadlone Mar 20, 2024
e544a92
PR(IDENTITY-TEST): Add ACP Tests With Identity
shahzadlone Feb 13, 2024
6e74cc3
PR(ACP): Unify NotFound & UnauthorizedAccess Error
shahzadlone Mar 14, 2024
1bc69f4
PR(ACP): Remove `ACPModule` from `client.Store`
shahzadlone Mar 14, 2024
aed7f4d
PR(ACP): Rename Registering Document Functions
shahzadlone Mar 22, 2024
7ad24a5
PR(ACP): Remove Option From Util Register Function
shahzadlone Apr 2, 2024
8ebb98f
PR(ACP): Move `AddPolicy` from Store to DB
shahzadlone Mar 19, 2024
a202f65
PR(ACP): Move ACP Availablity Check Up A Level
shahzadlone Mar 19, 2024
97b5162
PR(ACP-TEST): Implement The Add Policy Wrapper
shahzadlone Mar 22, 2024
ff29565
PR(ACP): Disable P2P Replicating & Subscribing
shahzadlone Apr 1, 2024
1a4b70e
PR(ACP): Disable Creating Index
shahzadlone Apr 1, 2024
4af1edc
PR(ACP): Disable Mutation Of Policy Through Patch
shahzadlone Apr 2, 2024
c4d2a71
PR(ACP): Remove Module Terminology From ACP
shahzadlone Apr 3, 2024
e50c927
PR(IDENTITY-MOCKS): Update Mocks For Identity
shahzadlone Mar 11, 2024
2a5e7f5
PR(ACP-DOCS): Generate CLI Docs With Identity
shahzadlone Feb 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ defradb start --tls --pubkeypath ~/path-to-pubkey.key --privkeypath ~/path-to-pr

```

## Access Control System
Read more about the access control [here](./acp/README.md).

## Supporting CORS

When accessing DefraDB through a frontend interface, you may be confronted with a CORS error. That is because, by default, DefraDB will not have any allowed origins set. To specify which origins should be allowed to access your DefraDB endpoint, you can specify them when starting the database:
Expand Down
442 changes: 442 additions & 0 deletions acp/README.md

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions acp/acp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package acp

import (
"context"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/corelog"
)

var (
log = corelog.NewLogger("acp")

// NoACP is an empty ACP, this is used to disable access control.
NoACP = immutable.None[ACP]()
)

// ACP is the interface to all types of access control that might exist.
type ACP interface {
// Init initializes the acp, with an absolute path. The provided path indicates where the
// persistent data will be stored for acp.
//
// If the path is empty then acp will run in memory.
Init(ctx context.Context, path string)

// Start starts the acp, using the initialized path. Will recover acp state
// from a previous run if under the same path.
//
// If the path is empty then acp will run in memory.
Start(ctx context.Context) error

// Close closes the resources in use by acp.
Close() error

// AddPolicy attempts to add the given policy. Detects the format of the policy automatically
// by assuming YAML format if JSON validation fails. Upon success a policyID is returned,
// otherwise returns error.
//
// A policy can not be added without a creator identity (sourcehub address).
AddPolicy(ctx context.Context, creatorID string, policy string) (string, error)

// ValidateResourceExistsOnValidDPI performs DPI validation of the resource (matching resource name)
// that is on the policy (matching policyID), returns an error upon validation failure.
//
// Learn more about the DefraDB Policy Interface [DPI](/acp/README.md)
ValidateResourceExistsOnValidDPI(
ctx context.Context,
policyID string,
resourceName string,
) error

// RegisterDocObject registers the document (object) to have access control.
// No error is returned upon successful registering of a document.
//
// Note(s):
// - This function does not check the collection to see if the document actually exists.
// - Some documents might be created without an identity signature so they would have public access.
// - actorID here is the identity of the actor registering the document object.
RegisterDocObject(
ctx context.Context,
actorID string,
policyID string,
resourceName string,
docID string,
) error

// IsDocRegistered returns true if the document was found to be registered, otherwise returns false.
// If check failed then an error and false will be returned.
IsDocRegistered(
ctx context.Context,
policyID string,
resourceName string,
docID string,
) (bool, error)

// CheckDocAccess returns true if the check was successfull and the request has access to the document. If
// the check was successful but the request does not have access to the document, then returns false.
// Otherwise if check failed then an error is returned (and the boolean result should not be used).
//
// Note(s):
// - permission here is a valid DPI permission we are checking for ("read" or "write").
CheckDocAccess(
ctx context.Context,
permission DPIPermission,
actorID string,
policyID string,
resourceName string,
docID string,
) (bool, error)
}
Loading
Loading