balena-io / balena-cli

The official balena CLI tool.
Apache License 2.0
455 stars 141 forks source link

Cannot login when installed on Ubuntu #318

Closed a-musing-moose closed 8 years ago

a-musing-moose commented 8 years ago

Hi All,

I am able to install resin-cli on my Ubuntu 15.10 box. But I am unable to run any commands. whatever I try I get an "Unauthorized" message.

Ubuntu: 15.10 Node JS: v4.3.1 npm: 2.14.2

mossj@pretty-on-the-inside:~$ sudo npm install -g resin-cli
[sudo] password for mossj: 
npm WARN engine valid-email@0.0.2: wanted: {"node":"^0.8.x"} (current: {"node":"4.3.1","npm":"2.14.2"})
npm WARN deprecated graceful-fs@3.0.8: graceful-fs version 3 and before will fail on newer node releases. Please update to graceful-fs@^4.0.0 as soon as possible.
npm WARN deprecated graceful-fs@1.2.3: graceful-fs version 3 and before will fail on newer node releases. Please update to graceful-fs@^4.0.0 as soon as possible.
npm WARN optional dep failed, continuing diskpart@1.0.1
npm WARN engine agentkeepalive@0.2.4: wanted: {"node":"0.10.x"} (current: {"node":"4.3.1","npm":"2.14.2"})
npm WARN engine agentkeepalive@0.2.4: wanted: {"node":"0.10.x"} (current: {"node":"4.3.1","npm":"2.14.2"})
npm WARN engine agentkeepalive@0.2.4: wanted: {"node":"0.10.x"} (current: {"node":"4.3.1","npm":"2.14.2"})
npm WARN engine agentkeepalive@0.2.4: wanted: {"node":"0.10.x"} (current: {"node":"4.3.1","npm":"2.14.2"})
/usr/bin/resin -> /usr/lib/node_modules/resin-cli/bin/resin
npm WARN unmet dependency /usr/lib/node_modules/keybase-installer/node_modules/gpg-wrapper requires iced-coffee-script@'~1.7.1' but will load
npm WARN unmet dependency /usr/lib/node_modules/keybase-installer/node_modules/iced-coffee-script,
npm WARN unmet dependency which is version 1.7.1-b
npm WARN unmet dependency /usr/lib/node_modules/phonegap/node_modules/phonegap-build requires colors@'0.6.x' but will load
npm WARN unmet dependency /usr/lib/node_modules/phonegap/node_modules/colors,
npm WARN unmet dependency which is version 0.6.0-1
npm WARN unmet dependency /usr/lib/node_modules/phonegap/node_modules/phonegap-build/node_modules/qrcode-terminal requires colors@'*' but will load
npm WARN unmet dependency /usr/lib/node_modules/phonegap/node_modules/colors,
npm WARN unmet dependency which is version 0.6.0-1
npm WARN unmet dependency /usr/lib/node_modules/phonegap/node_modules/prompt/node_modules/winston requires colors@'0.x.x' but will load
npm WARN unmet dependency /usr/lib/node_modules/phonegap/node_modules/colors,
npm WARN unmet dependency which is version 0.6.0-1
resin-cli@2.6.1 /usr/lib/node_modules/resin-cli
├── is-root@1.0.0
├── valid-email@0.0.2
├── resin-cli-errors@1.1.1
├── mixpanel@0.4.0
├── umount@1.1.2
├── coffee-script@1.10.0
├── tmp@0.0.28 (os-tmpdir@1.0.1)
├── chalk@1.1.1 (escape-string-regexp@1.0.4, ansi-styles@2.1.0, supports-color@2.0.0, strip-ansi@3.0.0, has-ansi@2.0.0)
├── bluebird@2.10.2
├── rindle@1.3.0 (string-to-stream@1.0.1)
├── prettyjson@1.1.3 (colors@1.1.2, minimist@1.2.0)
├── columnify@1.5.4 (strip-ansi@3.0.0, wcwidth@1.0.0)
├── resin-image-manager@4.0.0 (mime@1.3.4, mkdirp@0.5.1)
├── underscore.string@3.2.3
├── rimraf@2.5.2 (glob@7.0.0)
├── resin-cli-form@1.4.1 (inquirer@0.11.4)
├── president@2.0.3 (windosu@0.2.0)
├── update-notifier@0.5.0 (is-npm@1.0.0, string-length@1.0.1, semver-diff@2.1.0, repeating@1.1.3, configstore@1.4.0, latest-version@1.0.1)
├── nplugm@3.0.3 (yeoman-environment@1.5.2)
├── resin-settings-client@3.4.2 (home-or-tmp@2.0.0, js-yaml@3.5.3)
├── unzip2@0.2.5 (setimmediate@1.0.4, readable-stream@1.0.33, pullstream@0.4.1, match-stream@0.0.2, fstream@0.1.31, binary@0.3.0)
├── moment@2.11.2
├── resin-cli-visuals@1.2.8 (progress-bar-formatter@2.0.1, cli-spinner@0.2.4, inquirer-dynamic-list@1.0.0, moment-duration-format@1.3.0, inquirer@0.11.4, drivelist@2.0.7)
├── resin-pine@1.3.1 (resin-errors@2.2.0, resin-token@2.4.2, pinejs-client@1.7.1, resin-request@2.4.3)
├── resin-vcs@2.0.2 (gitwrap@1.1.0, mochainon@1.0.0)
├── lodash@3.10.1
├── capitano@1.7.1 (get-stdin@4.0.1, async@1.5.2, minimist@1.1.3, is-elevated@1.0.0, underscore.string@3.0.3, lodash@3.9.3)
├── resin-sdk@5.1.0 (resin-register-device@2.1.0, resin-errors@2.2.0, resin-request@4.0.3, lodash@3.9.3, resin-token@2.4.2, resin-device-status@1.0.0, resin-device-logs@2.0.2)
├── resin-config-json@1.0.1 (string-to-stream@1.0.1, bluebird@3.3.1, resin-image-fs@2.1.2, resin-sdk@4.1.3)
├── resin-cli-auth@1.1.1 (open@0.0.5, bluebird@3.3.1, body-parser@1.15.0, express@4.13.4, resin-token@2.4.2, resin-sdk@4.1.3)
└── resin-device-init@2.0.2 (string-to-stream@1.0.1, resin-device-config@2.6.0, resin-image-fs@2.1.2, resin-device-operations@1.3.1, resin-sdk@4.1.3)
mossj@pretty-on-the-inside:~$ resin login
ResinRequestError: Request error: Unauthorized

If you need help, don't hesitate in contacting us at:

  GitHub: https://github.com/resin-io/resin-cli/issues/new
  Gitter: https://gitter.im/resin-io/chat

mossj@pretty-on-the-inside:~$ sudo resin login
ResinRequestError: Request error: Unauthorized

If you need help, don't hesitate in contacting us at:

  GitHub: https://github.com/resin-io/resin-cli/issues/new
  Gitter: https://gitter.im/resin-io/chat

mossj@pretty-on-the-inside:~$ 
a-musing-moose commented 8 years ago

Note I get the same error when trying to run:

resin help and resin quickstart

jviotti commented 8 years ago

Hi @a-musing-moose, thanks for reaching out!

That's indeed very weird! Can you try re-running any of the commands that trigger the error with DEBUG=true?

DEBUG=true resin login

Exposing that environment variable will allow you to see the stack trace of the error, hopefully giving more information about why this is happening.

a-musing-moose commented 8 years ago

Thanks @jviotti

mossj@pretty-on-the-inside:~$ DEBUG=true resin login
ResinRequestError: Request error: Unauthorized
    at /usr/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/build/request.js:123:13
    at tryCatcher (/usr/lib/node_modules/resin-cli/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/usr/lib/node_modules/resin-cli/node_modules/bluebird/js/main/promise.js:505:31)
    at Promise._settlePromiseAt (/usr/lib/node_modules/resin-cli/node_modules/bluebird/js/main/promise.js:581:18)
    at Promise._settlePromises (/usr/lib/node_modules/resin-cli/node_modules/bluebird/js/main/promise.js:697:14)
    at Async._drainQueue (/usr/lib/node_modules/resin-cli/node_modules/bluebird/js/main/async.js:123:16)
    at Async._drainQueues (/usr/lib/node_modules/resin-cli/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/usr/lib/node_modules/resin-cli/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

If you need help, don't hesitate in contacting us at:

  GitHub: https://github.com/resin-io/resin-cli/issues/new
  Gitter: https://gitter.im/resin-io/chat
jviotti commented 8 years ago

@a-musing-moose Thanks for the quick response.

Can you try editing this file: /usr/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/build/request.js?

According to the stack trace the error happens in line 123, which corresponds to the following snippet:

if (utils.isErrorCode(response.statusCode)) {
  responseError = utils.getErrorMessageFromResponse(response);
  throw new errors.ResinRequestError(responseError, response.statusCode);
}

Can you try logging the options variable? So the snippet looks like this after the edition:

if (utils.isErrorCode(response.statusCode)) {
  console.log(options);
  responseError = utils.getErrorMessageFromResponse(response);
  throw new errors.ResinRequestError(responseError, response.statusCode);
}

And run any of the commands again?

Logging that object will allow us to see what is the HTTP causing the error. BTW, I'm working in a pull request to show this information when DEBUG=true automatically to better debug these kinds of issues in the future.

jviotti commented 8 years ago

I've submitted a PR to resin-request for showing debug information: https://github.com/resin-io/resin-request/pull/52

jviotti commented 8 years ago

@a-musing-moose I published a new version of resin-request with support for DEBUG=true to NPM. In case you didn't perform the manual edition yet, you can getaway with it by updating the Resin CLI.

Since the new resin-request version is a patch increment, the Resin CLI will get the update automatically. Sadly, npm update -g resin-cli doesn't seem to be enough, so I recommend re-installing from scratch:

$ sudo npm uninstall -g resin-cli
$ sudo npm install -g resin-cli

After that, you should be able to pass DEBUG=true to see detailed information about failed HTTP requests.

a-musing-moose commented 8 years ago

Right - I think I know what is going on now. When I ran the modified version I could see that the options contained a JWT. Which I thought odd as I haven't been able to log in.

So I went digging. Turns out that the token was stored in ~/.resin/token. Where it came from is anybodies guess.

I deleted the token and was able to get things up and running. So thank you for your help.

It would also be nice if certain commands (like help and login) didn't bomb out if an invalid token is found?

a-musing-moose commented 8 years ago

And just a little heads up. /lib/actions/wizard.coffee contains a hard coded path to the .resin folder that probably won't work for some of us ;-)

I'm not all that familiar with coffee script or the correct way to determine this path.

curcuz commented 8 years ago

can confirm @a-musing-moose fix - i was able to get myself out of the same issue by deleting ~/.resin/token

jviotti commented 8 years ago

@a-musing-moose @curcuz Thanks for the investigation, but I'm still struggling to reproduce this here.

@curcuz Are you running GNU/Linux as well? What NodeJS version are you running?

I also can't think of a reason why ~/.resin/token gets created automatically. The CLI "touches" the directory if it doesn't exist at startup, but doesn't put anything inside.

jviotti commented 8 years ago

@a-musing-moose Thanks for the heads up for the hardcoded path. Looks like something that was used for debugging at some point and got accidentally committed!

danielmahon commented 8 years ago

@jviotti on osx Yosemite

(deleting ~/.resin/token fixed it)

Projects > DEBUG=true resin login
{ url: 'https://api.resin.io/whoami',
  refreshToken: false,
  timeout: 30000,
  method: 'GET',
  json: true,
  strictSSL: true,
  gzip: true,
  headers: { Authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTA0MywidXNlcm5hbWUiOiJnX2RhbmllbF9tYWhvbiIsImVtYWlsIjoiZGFuaWVsQG1haG9uc3R1ZGlvcy5jb20iLCJzb2NpYWxfc2VydmljZV9hY2NvdW50IjpbeyJjcmVhdGVkX2F0IjoiMjAxNS0wNy0xNFQxOToxMzo1NC4zNTFaIiwiaWQiOjIxOSwidXNlciI6eyJfX2RlZmVycmVkIjp7InVyaSI6Ii9ld2EvdXNlcigxMDQzKSJ9LCJfX2lkIjoxMDQzfSwicHJvdmlkZXIiOiJnb29nbGUiLCJyZW1vdGVfaWQiOiIxMDMzNjcyMDcwODQ0MjM0NjM4MzciLCJkaXNwbGF5X25hbWUiOiJEYW5pZWwgTWFob24iLCJfX21ldGFkYXRhIjp7InVyaSI6Ii9ld2Evc29jaWFsX3NlcnZpY2VfYWNjb3VudCgyMTkpIiwidHlwZSI6IiJ9fV0sImhhc19kaXNhYmxlZF9uZXdzbGV0dGVyIjpmYWxzZSwiaGFzUGFzc3dvcmRTZXQiOnRydWUsIm5lZWRzUGFzc3dvcmRSZXNldCI6ZmFsc2UsInB1YmxpY19rZXkiOnRydWUsImZlYXR1cmVzIjpbXSwiaW50ZXJjb21Vc2VyTmFtZSI6ImdfZGFuaWVsX21haG9uIiwiaW50ZXJjb21Vc2VySGFzaCI6IjhmNWY4YWY1NWQ2MTY3N2I3YTgwNDY4MDAwNTA0OTE5NjBjZjEwMDI0OWNlNmU1ZDIwYTdjM2EyNGQwNjBlN2MiLCJwZXJtaXNzaW9ucyI6W10sImZpcnN0X25hbWUiOiJEYW5pZWwiLCJsYXN0X25hbWUiOiJNYWhvbiIsImNvbXBhbnkiOiJNYWhvbiBTdHVkaW9zIiwiaWF0IjoxNDUzNzYwMTc0LCJleHAiOjE0NTQzNjQ5NzR9.yRsMlcGa3jygeABHod6RQ1W_YBcCxti8cbEe2Pxjv4M' },
  statusCode: 401 }
ResinRequestError: Request error: Unauthorized
    at /Users/daniel/.nvm/versions/node/v5.7.0/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/build/request.js:126:13
    at tryCatcher (/Users/daniel/.nvm/versions/node/v5.7.0/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (/Users/daniel/.nvm/versions/node/v5.7.0/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/node_modules/bluebird/js/main/promise.js:505:31)
    at Promise._settlePromiseAt (/Users/daniel/.nvm/versions/node/v5.7.0/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/node_modules/bluebird/js/main/promise.js:581:18)
    at Promise._settlePromises (/Users/daniel/.nvm/versions/node/v5.7.0/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/node_modules/bluebird/js/main/promise.js:697:14)
    at Async._drainQueue (/Users/daniel/.nvm/versions/node/v5.7.0/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/node_modules/bluebird/js/main/async.js:123:16)
    at Async._drainQueues (/Users/daniel/.nvm/versions/node/v5.7.0/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/daniel/.nvm/versions/node/v5.7.0/lib/node_modules/resin-cli/node_modules/resin-sdk/node_modules/resin-request/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

If you need help, don't hesitate in contacting us at:

  GitHub: https://github.com/resin-io/resin-cli/issues/new
  Gitter: https://gitter.im/resin-io/chat
jviotti commented 8 years ago

@danielmahon Thanks for the information.

I've investigated this issue, and it happens when the Resin SDK tries to refresh a token that is already expired in order to make the current request.

The fix touches several modules, but will come very soon.

jviotti commented 8 years ago

The fix for this issue landed in resin-request, which is a dependency of resin-sdk, which is installed by the CLI. Given its only a patch version increment, re-installing the CLI should get you the fix thanks to semver:

$ npm uninstall -g resin-cli
$ npm install -g resin-cli
jviotti commented 8 years ago

BTW, there is another PR coming (https://github.com/resin-io/resin-cli-errors/pull/9) to interpret the new ResinExpiredToken SDK error to provide a more friendly message.