@@ -249,7 +249,27 @@ func (i *IdentityStore) entityByAliasFactors(mountAccessor, aliasName string, cl
249
249
return nil , fmt .Errorf ("missing alias name" )
250
250
}
251
251
252
- alias , err := i .MemDBAliasByFactors (mountAccessor , aliasName , false , false )
252
+ txn := i .db .Txn (false )
253
+
254
+ return i .entityByAliasFactorsInTxn (txn , mountAccessor , aliasName , clone )
255
+ }
256
+
257
+ // entityByAlaisFactorsInTxn fetches the entity based on factors of alias, i.e
258
+ // mount accessor and the alias name.
259
+ func (i * IdentityStore ) entityByAliasFactorsInTxn (txn * memdb.Txn , mountAccessor , aliasName string , clone bool ) (* identity.Entity , error ) {
260
+ if txn == nil {
261
+ return nil , fmt .Errorf ("nil txn" )
262
+ }
263
+
264
+ if mountAccessor == "" {
265
+ return nil , fmt .Errorf ("missing mount accessor" )
266
+ }
267
+
268
+ if aliasName == "" {
269
+ return nil , fmt .Errorf ("missing alias name" )
270
+ }
271
+
272
+ alias , err := i .MemDBAliasByFactorsInTxn (txn , mountAccessor , aliasName , false , false )
253
273
if err != nil {
254
274
return nil , err
255
275
}
@@ -258,12 +278,12 @@ func (i *IdentityStore) entityByAliasFactors(mountAccessor, aliasName string, cl
258
278
return nil , nil
259
279
}
260
280
261
- return i .MemDBEntityByAliasID ( alias .ID , clone )
281
+ return i .MemDBEntityByAliasIDInTxn ( txn , alias .ID , clone )
262
282
}
263
283
264
- // CreateEntity creates a new entity. This is used by core to
284
+ // CreateOrFetchEntity creates a new entity. This is used by core to
265
285
// associate each login attempt by an alias to a unified entity in Vault.
266
- func (i * IdentityStore ) CreateEntity (alias * logical.Alias ) (* identity.Entity , error ) {
286
+ func (i * IdentityStore ) CreateOrFetchEntity (alias * logical.Alias ) (* identity.Entity , error ) {
267
287
var entity * identity.Entity
268
288
var err error
269
289
@@ -290,9 +310,24 @@ func (i *IdentityStore) CreateEntity(alias *logical.Alias) (*identity.Entity, er
290
310
return nil , err
291
311
}
292
312
if entity != nil {
293
- return nil , fmt . Errorf ( "alias already belongs to a different entity" )
313
+ return entity , nil
294
314
}
295
315
316
+ // Create a MemDB transaction to update both alias and entity
317
+ txn := i .db .Txn (true )
318
+ defer txn .Abort ()
319
+
320
+ // Check if an entity was created before acquiring the lock
321
+ entity , err = i .entityByAliasFactorsInTxn (txn , alias .MountAccessor , alias .Name , false )
322
+ if err != nil {
323
+ return nil , err
324
+ }
325
+ if entity != nil {
326
+ return entity , nil
327
+ }
328
+
329
+ i .logger .Debug ("identity: creating a new entity" , "alias" , alias )
330
+
296
331
entity = & identity.Entity {}
297
332
298
333
err = i .sanitizeEntity (entity )
@@ -320,10 +355,12 @@ func (i *IdentityStore) CreateEntity(alias *logical.Alias) (*identity.Entity, er
320
355
}
321
356
322
357
// Update MemDB and persist entity object
323
- err = i .upsertEntity ( entity , nil , true )
358
+ err = i .upsertEntityInTxn ( txn , entity , nil , true , false )
324
359
if err != nil {
325
360
return nil , err
326
361
}
327
362
363
+ txn .Commit ()
364
+
328
365
return entity , nil
329
366
}
0 commit comments