Skip to content

Commit

Permalink
Make identityRef optional
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Oct 25, 2024
1 parent 3210fbf commit e4fc548
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
18 changes: 9 additions & 9 deletions tests/integration/acp.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type AddPolicy struct {
Policy string

// The policy creator identity, i.e. actor creating the policy.
Identity identRef
Identity immutable.Option[identityRef]

// The expected policyID generated based on the Policy loaded in to the ACP system.
ExpectedPolicyID string
Expand Down Expand Up @@ -158,14 +158,14 @@ type AddDocActorRelationship struct {

// The target public identity, i.e. the identity of the actor to tie the document's relation with.
//
// This is a required field. To test the invalid usage of not having this arg, use -1 index.
TargetIdentity identRef
// This is a required field. To test the invalid usage of not having this arg, use NoIdentity() or leave default.
TargetIdentity immutable.Option[identityRef]

// The requestor identity, i.e. identity of the actor creating the relationship.
// Note: This identity must either own or have managing access defined in the policy.
//
// This is a required field. To test the invalid usage of not having this arg, use -1 index.
RequestorIdentity identRef
// This is a required field. To test the invalid usage of not having this arg, use NoIdentity() or leave default.
RequestorIdentity immutable.Option[identityRef]

// Result returns true if it was a no-op due to existing before, and false if a new relationship was made.
ExpectedExistence bool
Expand Down Expand Up @@ -238,14 +238,14 @@ type DeleteDocActorRelationship struct {

// The target public identity, i.e. the identity of the actor with whom the relationship is with.
//
// This is a required field. To test the invalid usage of not having this arg, use -1 index.
TargetIdentity identRef
// This is a required field. To test the invalid usage of not having this arg, use NoIdentity() or leave default.
TargetIdentity immutable.Option[identityRef]

// The requestor identity, i.e. identity of the actor deleting the relationship.
// Note: This identity must either own or have managing access defined in the policy.
//
// This is a required field. To test the invalid usage of not having this arg, use -1 index.
RequestorIdentity identRef
// This is a required field. To test the invalid usage of not having this arg, use NoIdentity() or leave default.
RequestorIdentity immutable.Option[identityRef]

// Result returns true if the relationship record was expected to be found and deleted,
// and returns false if no matching relationship record was found (no-op).
Expand Down
65 changes: 34 additions & 31 deletions tests/integration/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,31 @@ import (
acpIdentity "github.com/sourcenetwork/defradb/acp/identity"
)

// identRef is a type that refers to a specific identity of a certain type.
type identRef struct {
hasValue bool
isUser bool
index int
// identityRef is a type that refers to a specific identity of a certain type.
type identityRef struct {
isUser bool
index int
}

// NoIdentity returns an reference to an identity that represents no identity.
func NoIdentity() identRef {
return identRef{
hasValue: false,
}
func NoIdentity() immutable.Option[identityRef] {
return immutable.None[identityRef]()
}

// UserIdentity returns a reference to a user identity with a given index.
func UserIdentity(index int) identRef {
return identRef{
hasValue: true,
isUser: true,
index: index,
}
func UserIdentity(index int) immutable.Option[identityRef] {
return immutable.Some(identityRef{
isUser: true,
index: index,
})
}

// NodeIdentity returns a reference to a node identity with a given index.
func NodeIdentity(index int) identRef {
return identRef{
hasValue: true,
isUser: false,
index: index,
}
func NodeIdentity(index int) immutable.Option[identityRef] {
return immutable.Some(identityRef{
isUser: false,
index: index,
})
}

// identityHolder holds an identity and the generated tokens for each target node.
Expand All @@ -71,13 +66,16 @@ func newIdentityHolder(ident acpIdentity.Identity) *identityHolder {

// getIdentity returns the identity for the given reference.
// If the identity does not exist, it will be generated.
func getIdentity(s *state, ref identRef) acpIdentity.Identity {
return getIdentityHolder(s, ref).Identity
func getIdentity(s *state, ref immutable.Option[identityRef]) acpIdentity.Identity {
if !ref.HasValue() {
return acpIdentity.Identity{}
}
return getIdentityHolder(s, ref.Value()).Identity
}

// getIdentityHolder returns the identity holder for the given reference.
// If the identity does not exist, it will be generated.
func getIdentityHolder(s *state, ref identRef) *identityHolder {
func getIdentityHolder(s *state, ref identityRef) *identityHolder {
ident, ok := s.identities[ref]
if ok {
return ident
Expand All @@ -90,7 +88,7 @@ func getIdentityHolder(s *state, ref identRef) *identityHolder {
// getIdentityForRequest returns the identity for the given reference and node index.
// It prepares the identity for a request by generating a token if needed, i.e. it will
// return an identity with [Identity.BearerToken] set.
func getIdentityForRequest(s *state, ref identRef, nodeIndex int) acpIdentity.Identity {
func getIdentityForRequest(s *state, ref identityRef, nodeIndex int) acpIdentity.Identity {
identHolder := getIdentityHolder(s, ref)
ident := identHolder.Identity

Expand Down Expand Up @@ -128,17 +126,22 @@ func generateIdentity(s *state) acpIdentity.Identity {
// getContextWithIdentity returns a context with the identity for the given reference and node index.
// If the identity does not exist, it will be generated.
// The identity added to the context is prepared for a request, i.e. its [Identity.BearerToken] is set.
func getContextWithIdentity(ctx context.Context, s *state, ref identRef, nodeIndex int) context.Context {
if !ref.hasValue {
func getContextWithIdentity(
ctx context.Context,
s *state,
ref immutable.Option[identityRef],
nodeIndex int,
) context.Context {
if !ref.HasValue() {
return ctx
}
ident := getIdentityForRequest(s, ref, nodeIndex)
ident := getIdentityForRequest(s, ref.Value(), nodeIndex)
return acpIdentity.WithContext(ctx, immutable.Some(ident))
}

func getIdentityDID(s *state, ident identRef) string {
if ident.hasValue {
return getIdentity(s, ident).DID
func getIdentityDID(s *state, ref immutable.Option[identityRef]) string {
if ref.HasValue() {
return getIdentity(s, ref).DID
}
return ""
}
4 changes: 2 additions & 2 deletions tests/integration/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ type state struct {
// types. See [identRef].
// The map value is the identity holder that contains the identity itself and token
// generated for different target nodes. See [identityHolder].
identities map[identRef]*identityHolder
identities map[identityRef]*identityHolder

// The seed for the next identity generation. We want identities to be deterministic.
nextIdentityGenSeed int
Expand Down Expand Up @@ -213,7 +213,7 @@ func newState(
clientType: clientType,
txns: []datastore.Txn{},
allActionsDone: make(chan struct{}),
identities: map[identRef]*identityHolder{},
identities: map[identityRef]*identityHolder{},
subscriptionResultsChans: []chan func(){},
nodeEvents: []*eventState{},
nodeAddresses: []peer.AddrInfo{},
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ type CreateDoc struct {
//
// Use `UserIdentity` to create a user identity and `NodeIdentity` to create a node identity.
// Default value is `NoIdentity()`.
Identity identRef
Identity immutable.Option[identityRef]

// Specifies whether the document should be encrypted.
IsDocEncrypted bool
Expand Down Expand Up @@ -368,7 +368,7 @@ type DeleteDoc struct {
//
// Use `UserIdentity` to create a user identity and `NodeIdentity` to create a node identity.
// Default value is `NoIdentity()`.
Identity identRef
Identity immutable.Option[identityRef]

// The collection in which this document should be deleted.
CollectionID int
Expand Down Expand Up @@ -401,7 +401,7 @@ type UpdateDoc struct {
//
// Use `UserIdentity` to create a user identity and `NodeIdentity` to create a node identity.
// Default value is `NoIdentity()`.
Identity identRef
Identity immutable.Option[identityRef]

// The collection in which this document exists.
CollectionID int
Expand Down Expand Up @@ -444,7 +444,7 @@ type UpdateWithFilter struct {
//
// Use `UserIdentity` to create a user identity and `NodeIdentity` to create a node identity.
// Default value is `NoIdentity()`.
Identity identRef
Identity immutable.Option[identityRef]

// The collection in which this document exists.
CollectionID int
Expand Down Expand Up @@ -601,7 +601,7 @@ type Request struct {
//
// Use `UserIdentity` to create a user identity and `NodeIdentity` to create a node identity.
// Default value is `NoIdentity()`.
Identity identRef
Identity immutable.Option[identityRef]

// Used to identify the transaction for this to run against. Optional.
TransactionID immutable.Option[int]
Expand Down Expand Up @@ -804,5 +804,5 @@ type GetNodeIdentity struct {
//
// Use `UserIdentity` to create a user identity and `NodeIdentity` to create a node identity.
// Default value is `NoIdentity()`.
ExpectedIdentity identRef
ExpectedIdentity immutable.Option[identityRef]
}

0 comments on commit e4fc548

Please sign in to comment.