SkipLabs / skdb

SKDB is an embedded SQL database that stays in sync.

Home Page:https://skdb.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Server using most recently created user?

ltique opened this issue · comments

If I start the dev server and then the CLI I am logged in as root:

$ npx skdb --dev --db chat-app --remote-repl
root@ws://localhost:3586/chat-app>

If I then create a user with the CLI I get credentials I can use but (and I think this is sensible) nothing is put in ~/.skdb/credentials:

$ npx skdb --dev --create-user --db chat-app
Successfully created user:  {
  f8a0ksyieWZ5aWIU9iLrXGMqsV4: '...'
}
$ cat ~/.skdb/credentials
{}

However now when I start the CLI I am logged in as that recently created user:

$ npx skdb --dev --db chat-app --remote-repl
f8a0ksyieWZ5aWIU9iLrXGMqsV4@ws://localhost:3586/chat-app>

This surprises me because I have an empty credentials file and --help says:

$ npx skdb --help
Usage: node_modules/.bin/skdb-cli [--help]                      
...   
    [--access-key <key>]  -- Access key to use. Default: first specified in credentials file.

Indeed, I can't see any file anywhere on my file system which contains the string "f8a0ksyieWZ5aWIU9iLrXGMqsV4". Interestingly, if I create more users with --create-user I am still logged in as "f8a0ksyieWZ5aWIU9iLrXGMqsV4".

My best guess is that the server has the following logic:

  1. If no users have been created, use root as the access key.
  2. If one or more users have been created, use the first of those as the access key.

This isn't necessarily wrong or bad behaviour, but I'm also not sure if it's what's intended or not. And, perhaps, I'm completely missing out on something and there is state locally on my machine that I'm not aware of!

Pass —user to control who you’re logged in as.

In dev mode credentials are fetched from the server itself. If you don’t specify a user, it uses the first in the map. First depends on the map implementation. Based on what you describe it seems the data structure sorts keys and is returning the first user alphabetically. This will be the first you create as our uuids order by time and begin with ‘f’ so sort before ‘root’.

I wonder if we should force/suggest that people always specify --user (well, my version still seems to require --access-key #119) so that they're very clear which user they're logging in as?

I meant --access-key.

Probably we should. Probably we should re-write the CLI interface to make this and your other issues clearer. Something that takes an operation (like how git, or skdb itself work) and then tells you what the valid flags are would make sense to me.