Skip to content

Commit

Permalink
refactor(i): DB identity context (#2528)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #2439

## Description

This PR moves the identity param from the `client.DB` and
`client.Collection` interfaces to the context.

Notable changes:
- `acp/identity.go` added `Identity` type alias to make future updates
simpler

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

`make test`

Specify the platform(s) on which this was tested:
- MacOS
  • Loading branch information
nasdf authored Apr 18, 2024
1 parent 5343bfd commit 734b326
Show file tree
Hide file tree
Showing 177 changed files with 902 additions and 1,306 deletions.
24 changes: 15 additions & 9 deletions acp/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@ Package identity provides defradb identity.

package identity

import (
"github.com/sourcenetwork/immutable"
)
import "github.com/sourcenetwork/immutable"

// Identity is the unique identifier for an actor.
type Identity string

var (
// NoIdentity is an empty identity.
NoIdentity = immutable.None[string]()
// None is an empty identity.
None = immutable.None[Identity]()
)

// NewIdentity makes a new identity if the input is not empty otherwise, returns an empty Option.
func NewIdentity(identity string) immutable.Option[string] {
// New makes a new identity if the input is not empty otherwise, returns None.
func New(identity string) immutable.Option[Identity] {
// TODO-ACP: There will be more validation once sourcehub gets some utilities.
// Then a validation function would do the validation, will likely do outside this function.
// https://github.com/sourcenetwork/defradb/issues/2358
if identity == "" {
return NoIdentity
return None
}
return immutable.Some[string](identity)
return immutable.Some(Identity(identity))
}

// String returns the string representation of the identity.
func (i Identity) String() string {
return string(i)
}
20 changes: 0 additions & 20 deletions cli/acp_policy_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,12 @@ import (
"os"

"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/acp"
)

func MakeACPPolicyAddCommand() *cobra.Command {
const identityFlagLongRequired string = "identity"
const identityFlagShortRequired string = "i"

const fileFlagLong string = "file"
const fileFlagShort string = "f"

var identityValue string
var policyFile string

var cmd = &cobra.Command{
Expand Down Expand Up @@ -77,10 +71,6 @@ Example: add from stdin:
`,
RunE: func(cmd *cobra.Command, args []string) error {
if identityValue == "" {
return acp.ErrPolicyCreatorMustNotBeEmpty
}

// TODO-ACP: Ensure here (before going through acp system) if the required identity argument
// is valid, if it is valid then keep proceeding further, otherwise return this error:
// `NewErrRequiredFlagInvalid(identityFlagLongRequired, identityFlagShortRequired)`
Expand Down Expand Up @@ -114,7 +104,6 @@ Example: add from stdin:
db := mustGetContextDB(cmd)
policyResult, err := db.AddPolicy(
cmd.Context(),
identityValue,
policy,
)

Expand All @@ -126,14 +115,5 @@ Example: add from stdin:
},
}
cmd.Flags().StringVarP(&policyFile, fileFlagLong, fileFlagShort, "", "File to load a policy from")
cmd.Flags().StringVarP(
&identityValue,
identityFlagLongRequired,
identityFlagShortRequired,
"",
"[Required] Identity of the creator",
)
_ = cmd.MarkFlagRequired(identityFlagLongRequired)

return cmd
}
5 changes: 5 additions & 0 deletions cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

func MakeClientCommand() *cobra.Command {
var txID uint64
var identity string
var cmd = &cobra.Command{
Use: "client",
Short: "Interact with a DefraDB node",
Expand All @@ -28,12 +29,16 @@ Execute queries, add schema types, obtain node info, etc.`,
if err := setContextConfig(cmd); err != nil {
return err
}
if err := setContextIdentity(cmd, identity); err != nil {
return err
}
if err := setContextTransaction(cmd, txID); err != nil {
return err
}
return setContextDB(cmd)
},
}
cmd.PersistentFlags().StringVarP(&identity, "identity", "i", "", "ACP Identity")
cmd.PersistentFlags().Uint64Var(&txID, "tx", 0, "Transaction ID")
return cmd
}
5 changes: 5 additions & 0 deletions cli/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

func MakeCollectionCommand() *cobra.Command {
var txID uint64
var identity string
var name string
var schemaRoot string
var versionID string
Expand All @@ -37,6 +38,9 @@ func MakeCollectionCommand() *cobra.Command {
if err := setContextConfig(cmd); err != nil {
return err
}
if err := setContextIdentity(cmd, identity); err != nil {
return err
}
if err := setContextTransaction(cmd, txID); err != nil {
return err
}
Expand Down Expand Up @@ -76,6 +80,7 @@ func MakeCollectionCommand() *cobra.Command {
},
}
cmd.PersistentFlags().Uint64Var(&txID, "tx", 0, "Transaction ID")
cmd.PersistentFlags().StringVarP(&identity, "identity", "i", "", "ACP Identity")
cmd.PersistentFlags().StringVar(&name, "name", "", "Collection name")
cmd.PersistentFlags().StringVar(&schemaRoot, "schema", "", "Collection schema Root")
cmd.PersistentFlags().StringVar(&versionID, "version", "", "Collection version ID")
Expand Down
20 changes: 2 additions & 18 deletions cli/collection_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@ import (

"github.com/spf13/cobra"

acpIdentity "github.com/sourcenetwork/defradb/acp/identity"
"github.com/sourcenetwork/defradb/client"
)

func MakeCollectionCreateCommand() *cobra.Command {
const identityFlagLongRequired string = "identity"
const identityFlagShortRequired string = "i"

var identityValue string
var file string

var cmd = &cobra.Command{
Use: "create [-i --identity] <document>",
Short: "Create a new document.",
Expand All @@ -49,9 +43,6 @@ Example: create from stdin:
`,
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO-ACP: `https://github.com/sourcenetwork/defradb/issues/2358` do the validation here.
identity := acpIdentity.NewIdentity(identityValue)

var docData []byte
switch {
case file != "":
Expand Down Expand Up @@ -82,23 +73,16 @@ Example: create from stdin:
if err != nil {
return err
}
return col.CreateMany(cmd.Context(), identity, docs)
return col.CreateMany(cmd.Context(), docs)
}

doc, err := client.NewDocFromJSON(docData, col.Schema())
if err != nil {
return err
}
return col.Create(cmd.Context(), identity, doc)
return col.Create(cmd.Context(), doc)
},
}
cmd.Flags().StringVarP(&file, "file", "f", "", "File containing document(s)")
cmd.Flags().StringVarP(
&identityValue,
identityFlagLongRequired,
identityFlagShortRequired,
"",
"Identity of the actor",
)
return cmd
}
21 changes: 3 additions & 18 deletions cli/collection_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ package cli
import (
"github.com/spf13/cobra"

acpIdentity "github.com/sourcenetwork/defradb/acp/identity"
"github.com/sourcenetwork/defradb/client"
)

func MakeCollectionDeleteCommand() *cobra.Command {
const identityFlagLongRequired string = "identity"
const identityFlagShortRequired string = "i"

var identityValue string
var argDocIDs []string
var filter string
var cmd = &cobra.Command{
Expand All @@ -39,9 +34,6 @@ Example: delete by filter:
defradb client collection delete --name User --filter '{ "_gte": { "points": 100 } }'
`,
RunE: func(cmd *cobra.Command, args []string) error {
// TODO-ACP: `https://github.com/sourcenetwork/defradb/issues/2358` do the validation here.
identity := acpIdentity.NewIdentity(identityValue)

col, ok := tryGetContextCollection(cmd)
if !ok {
return cmd.Usage()
Expand All @@ -53,7 +45,7 @@ Example: delete by filter:
if err != nil {
return err
}
res, err := col.DeleteWithDocID(cmd.Context(), identity, docID)
res, err := col.DeleteWithDocID(cmd.Context(), docID)
if err != nil {
return err
}
Expand All @@ -67,13 +59,13 @@ Example: delete by filter:
}
docIDs[i] = docID
}
res, err := col.DeleteWithDocIDs(cmd.Context(), identity, docIDs)
res, err := col.DeleteWithDocIDs(cmd.Context(), docIDs)
if err != nil {
return err
}
return writeJSON(cmd, res)
case filter != "":
res, err := col.DeleteWithFilter(cmd.Context(), identity, filter)
res, err := col.DeleteWithFilter(cmd.Context(), filter)
if err != nil {
return err
}
Expand All @@ -85,12 +77,5 @@ Example: delete by filter:
}
cmd.Flags().StringSliceVar(&argDocIDs, "docID", nil, "Document ID")
cmd.Flags().StringVar(&filter, "filter", "", "Document filter")
cmd.Flags().StringVarP(
&identityValue,
identityFlagLongRequired,
identityFlagShortRequired,
"",
"Identity of the actor",
)
return cmd
}
17 changes: 1 addition & 16 deletions cli/collection_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ package cli
import (
"github.com/spf13/cobra"

acpIdentity "github.com/sourcenetwork/defradb/acp/identity"
"github.com/sourcenetwork/defradb/client"
)

func MakeCollectionGetCommand() *cobra.Command {
const identityFlagLongRequired string = "identity"
const identityFlagShortRequired string = "i"

var identityValue string
var showDeleted bool
var cmd = &cobra.Command{
Use: "get [-i --identity] [--show-deleted] <docID> ",
Expand All @@ -36,9 +31,6 @@ Example to get a private document we must use an identity:
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO-ACP: `https://github.com/sourcenetwork/defradb/issues/2358` do the validation here.
identity := acpIdentity.NewIdentity(identityValue)

col, ok := tryGetContextCollection(cmd)
if !ok {
return cmd.Usage()
Expand All @@ -48,7 +40,7 @@ Example to get a private document we must use an identity:
if err != nil {
return err
}
doc, err := col.Get(cmd.Context(), identity, docID, showDeleted)
doc, err := col.Get(cmd.Context(), docID, showDeleted)
if err != nil {
return err
}
Expand All @@ -60,12 +52,5 @@ Example to get a private document we must use an identity:
},
}
cmd.Flags().BoolVar(&showDeleted, "show-deleted", false, "Show deleted documents")
cmd.Flags().StringVarP(
&identityValue,
identityFlagLongRequired,
identityFlagShortRequired,
"",
"Identity of the actor",
)
return cmd
}
18 changes: 1 addition & 17 deletions cli/collection_list_doc_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ package cli
import (
"github.com/spf13/cobra"

acpIdentity "github.com/sourcenetwork/defradb/acp/identity"
"github.com/sourcenetwork/defradb/http"
)

func MakeCollectionListDocIDsCommand() *cobra.Command {
const identityFlagLongRequired string = "identity"
const identityFlagShortRequired string = "i"

var identityValue string

var cmd = &cobra.Command{
Use: "docIDs [-i --identity]",
Short: "List all document IDs (docIDs).",
Expand All @@ -35,15 +29,12 @@ Example: list all docID(s), with an identity:
defradb client collection docIDs -i cosmos1f2djr7dl9vhrk3twt3xwqp09nhtzec9mdkf70j --name User
`,
RunE: func(cmd *cobra.Command, args []string) error {
// TODO-ACP: `https://github.com/sourcenetwork/defradb/issues/2358` do the validation here.
identity := acpIdentity.NewIdentity(identityValue)

col, ok := tryGetContextCollection(cmd)
if !ok {
return cmd.Usage()
}

docCh, err := col.GetAllDocIDs(cmd.Context(), identity)
docCh, err := col.GetAllDocIDs(cmd.Context())
if err != nil {
return err
}
Expand All @@ -61,12 +52,5 @@ Example: list all docID(s), with an identity:
return nil
},
}
cmd.Flags().StringVarP(
&identityValue,
identityFlagLongRequired,
identityFlagShortRequired,
"",
"Identity of the actor",
)
return cmd
}
Loading

0 comments on commit 734b326

Please sign in to comment.