Skip to content

Commit

Permalink
backport of commit f09b76f (#25864)
Browse files Browse the repository at this point in the history
Co-authored-by: Bianca Moreira <48203644+biazmoreira@users.noreply.github.com>
  • Loading branch information
1 parent bd5b66f commit 5be121f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions vault/identity_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hashicorp/vault/helper/versions"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/locksutil"
"github.com/hashicorp/vault/sdk/logical"
"github.com/patrickmn/go-cache"
)
Expand Down Expand Up @@ -66,6 +67,7 @@ func NewIdentityStore(ctx context.Context, core *Core, config *logical.BackendCo
entityCreator: core,
mountLister: core,
mfaBackend: core.loginMFABackend,
aliasLocks: locksutil.CreateLocks(),
}

// Create a memdb instance, which by default, operates on lower cased
Expand Down
5 changes: 5 additions & 0 deletions vault/identity_store_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/vault/helper/storagepacker"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/locksutil"
"github.com/hashicorp/vault/sdk/logical"
)

Expand Down Expand Up @@ -104,6 +105,10 @@ type IdentityStore struct {
entityCreator EntityCreator
mountLister MountLister
mfaBackend *LoginMFABackend

// aliasLocks is used to protect modifications to alias entries based on the uniqueness factor
// which is name + accessor
aliasLocks []*locksutil.LockEntry
}

type groupDiff struct {
Expand Down
11 changes: 6 additions & 5 deletions vault/request_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -1659,14 +1659,15 @@ func (c *Core) handleLoginRequest(ctx context.Context, req *logical.Request) (re
var err error
// Fetch the entity for the alias, or create an entity if one
// doesn't exist.

entity, entityCreated, err := c.identityStore.CreateOrFetchEntity(ctx, auth.Alias)
if err != nil {
switch auth.Alias.Local {
case true:
// Only create a new entity if the error was a readonly error and the creation flag is true
// i.e the entity was in the middle of being created
if entityCreated && errors.Is(err, logical.ErrReadOnly) {
entity, err = possiblyForwardEntityCreation(ctx, c, err, auth, nil)
entity, err = registerLocalAlias(ctx, c, auth.Alias)
if err != nil {
if strings.Contains(err.Error(), errCreateEntityUnimplemented) {
resp.AddWarning("primary cluster doesn't yet issue entities for local auth mounts; falling back to not issuing entities for local auth mounts")
Expand All @@ -1676,14 +1677,14 @@ func (c *Core) handleLoginRequest(ctx context.Context, req *logical.Request) (re
}
}
}
err = updateLocalAlias(ctx, c, auth, entity)
default:
entity, entityCreated, err = possiblyForwardAliasCreation(ctx, c, err, auth, entity)
if err != nil {
return nil, nil, err
}
}
}
if err != nil {
return nil, nil, err
}

if entity == nil {
return nil, nil, fmt.Errorf("failed to create an entity for the authenticated alias")
}
Expand Down
8 changes: 2 additions & 6 deletions vault/request_handling_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ func possiblyForwardAliasCreation(ctx context.Context, c *Core, inErr error, aut

var errCreateEntityUnimplemented = "create entity unimplemented in the server"

func possiblyForwardEntityCreation(ctx context.Context, c *Core, inErr error, auth *logical.Auth, entity *identity.Entity) (*identity.Entity, error) {
return entity, inErr
}

func updateLocalAlias(ctx context.Context, c *Core, auth *logical.Auth, entity *identity.Entity) error {
return nil
func registerLocalAlias(_ context.Context, _ *Core, _ *logical.Alias) (*identity.Entity, error) {
return nil, logical.ErrReadOnly
}

func possiblyForwardSaveCachedAuthResponse(ctx context.Context, c *Core, respAuth *MFACachedAuthResponse) error {
Expand Down

0 comments on commit 5be121f

Please sign in to comment.