kuzzleio / kuzzle

Open-source Back-end, self-hostable & ready to use - Real-time, storage, advanced search - Web, Apps, Mobile, IoT -
https://kuzzle.io
Apache License 2.0
1.44k stars 124 forks source link

createOrReplaceRole and createOrReplaceProfile throwing TypeError #1165

Closed lethak closed 6 years ago

lethak commented 6 years ago

Expected Behavior

createOrReplaceRole and createOrReplaceProfile should be working without throwing a TypeError

Current Behavior

A TypeError is thrown upon plugin initialization when kuzzle is starting

when using createOrReplaceRole:

ERROR  TypeError: Cannot read property 'createOrReplace' of null
    at RoleRepository.persistToDatabase (/var/app/lib/api/core/models/repositories/repository.js:231:
31)
    at role.validateDefinition.then (/var/app/lib/api/core/models/repositories/roleRepository.js:176:
21)
    at tryCatcher (/var/app/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/var/app/node_modules/bluebird/js/release/promise.js:512:31
)
    at Promise._settlePromise (/var/app/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/var/app/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/var/app/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/var/app/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/var/app/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/var/app/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

when using createOrReplaceProfile:

ERROR  TypeError: Cannot read property 'get' of null
    at RoleRepository.loadOneFromDatabase (/var/app/lib/api/core/models/repositories/repository.js:83
:31)
    at RoleRepository.loadRoles (/var/app/lib/api/core/models/repositories/roleRepository.js:66:31)
    at fromDTO.then.profile (/var/app/lib/api/core/models/repositories/profileRepository.js:268:46)
    at tryCatcher (/var/app/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/var/app/node_modules/bluebird/js/release/promise.js:512:31
)
    at Promise._settlePromise (/var/app/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromiseCtx (/var/app/node_modules/bluebird/js/release/promise.js:606:10)
    at Async._drainQueue (/var/app/node_modules/bluebird/js/release/async.js:138:12)
    at Async._drainQueues (/var/app/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/var/app/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

Steps to Reproduce

I am calling rebuildRoles() on plugin init()

const roles = require('./roles.json')

const rebuildRoles = () => {
    return plugin.context.accessors.execute(new plugin.context.constructors.Request({
        'controller': 'security',
        'action': 'createOrReplaceRole',
        'refresh': 'wait_for',
        '_id': roles[0]._id,
        'body': roles[0].content,
    }))
    .catch((err) => {
        console.warn('ERROR ', err)
    })
}

roles.json

[
  {
    "_id": "admin",
    "content": {
      "controllers": {
        "*": {
          "actions": {
            "*": true
          }
        }
      }
    }
  }
]

Context (Environment)

Docker image: kuzzleio/kuzzle:1.3.1

Aschen commented 6 years ago

Hi @lethak !

Thanks for reporting this.
This was caused by the Kuzzle initialization sequence, the repositories were initialized after the plugin so when you try to use them you got an error.

lethak commented 6 years ago

Hello,

Thanks for the answer, in the meantime, wrapping rebuildRoles() in a setTimeout did the trick