millejoh / emacs-ipython-notebook

Jupyter notebook client in Emacs
http://millejoh.github.io/emacs-ipython-notebook/
GNU General Public License v3.0
1.47k stars 122 forks source link

ein 20220419.735: ein:notebooklist-new-notebook-error: error (error http 403) #837

Open Alexander-Barth opened 2 years ago

Alexander-Barth commented 2 years ago

Problem description

ein works very well on my machine with a local jupyter notebook session. However, when I am try to connect to a Jupyterhub instance using ein, I get a 403 error. I created a token via https://example.net/hub/token (without expiration). I verified that I can connect to the Jupyterhub instance without re-entering my password in a browser (in private mode without cookies) by following the link https://example.net/user/abarth/lab?token=32_digit_token (where example.net and 32_digit_token are my actual hostname and token)

Steps to reproduce the problem

There are the steps leading to the error:

I get the error message ein:notebooklist-new-notebook-error: error (error http 403)

I do indeed see a 403 "forbidden" error in the nginx server logs of the Jupyterhub instance.

217.136.175.23 - - [02/May/2022:10:03:59 +0200] "POST /user/abarth/api/contents HTTP/1.1" 403 65 "-" "Mozilla/5.0"

At least, I can the filenames of the notebooks already present on the server (but I cannot execute them without having again 403 errors).

Maybe I missed some important steps, our jupyterhub server is not configured correctly or I did not use the right kind of token.

If somebody can point me in the right direction, this would be highly appreciated.

System info:

("EIN system info"
 :emacs-version
 "GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.20)
 of 2021-03-11"
 :window-system x
 :emacs-variant nil
 :build "--prefix /home/abarth/opt/emacs-27.1 --with-modules"
 :os
 (:uname
  "Linux gher17 5.4.0-107-generic #121-Ubuntu SMP Thu Mar 24 16:04:27 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
"
  :lsb-release
  "LSB Version: core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:    20.04
Codename:   focal
")
 :jupyter
 "jupyter core     : 4.6.3
jupyter-notebook : 6.0.3
qtconsole        : 4.7.3
ipython          : 8.0.0
ipykernel        : 5.2.1
jupyter client   : 6.1.3
jupyter lab      : 2.2.0
nbconvert        : 5.6.1
ipywidgets       : 7.5.1
nbformat         : 5.0.6
traitlets        : 5.1.1
"
 :image-types
 (svg png gif tiff jpeg xpm xbm pbm postscript)
 :image-types-available
 (svg png gif tiff jpeg xpm xbm pbm postscript)
 :request-backend curl
 :ein
 (:version "20220419.735"
       :source-dir "/home/abarth/.emacs.d/elpa/ein-20220419.735/")
 :lib
 ((:name "websocket"
     :path "~/.emacs.d/elpa/websocket-20210110.17/websocket.elc"
     :featurep t
     :version-var websocket-version
     :version "1.12")
  (:name "anaphora"
     :path "~/.emacs.d/elpa/anaphora-20180618.2200/anaphora.elc"
     :featurep t
     :version-var nil
     :version nil)
  (:name "request"
     :path "~/.emacs.d/elpa/request-20211107.1907/request.elc"
     :featurep t
     :version-var request-version
     :version "0.3.3")
  (:name "deferred"
     :path "~/.emacs.d/elpa/deferred-20170901.1330/deferred.elc"
     :featurep t
     :version-var deferred:version
     :version "0.5.0")
  (:name "polymode"
     :path "~/.emacs.d/elpa/polymode-20220322.824/polymode.elc"
     :featurep t
     :version-var nil
     :version nil)
  (:name "dash"
     :path "~/.emacs.d/elpa/dash-20220417.2250/dash.elc"
     :featurep t
     :version-var nil
     :version nil)
  (:name "with-editor"
     :path "~/.emacs.d/elpa/with-editor-20220422.1628/with-editor.elc"
     :featurep nil
     :version-var nil
     :version nil)))

Logs:

ein:log-all:

10:16:01:506: [info] Login to https://example.net/user/abarth/?token=token=32_digit_token complete. @#<buffer *scratch*>
10:16:01:913: [verbose] Opening notebooklist at https://example.net/user/abarth @#<buffer *scratch*>
10:16:12:710: [info] ein:notebooklist-new-notebook-error: error (error http 403) @#<buffer *ein:notebooklist https://example.net/user/abarth*>
10:16:14:350: [error] ein:notebooklist-new-notebook-error: error (error http 403) @#<buffer *ein:notebooklist https://example.net/user/abarth*>
10:20:17:117: [warn] ein:dev-packages: Don’t call me! @#<buffer  *temp*>
dickmao commented 2 years ago

We've tried to keep up with jupyterhub's poorly documented and ever changing arcana over the years, with obviously no success. Sorry.

Alexander-Barth commented 2 years ago

For what it is worth I can create a notebook via curl by

curl -X POST -H "Content-Type: application/json"  -H "Authorization: token $token"  -d '{"type": "notebook"}' "$URL/user/abarth/api/contents"

But this is essentially what this functions is doing: https://github.com/millejoh/emacs-ipython-notebook/blob/388c8f753cfb99b4f82acbdff26bbe27189d2299/lisp/ein-notebooklist.el#L314

Maybe the error is triggered before.

lqhuang commented 2 years ago

So how could we help to solve this? I guess it's probably caused by passing an incorrect token value to the Header, but I'm not very familiar with LISP.

Thanks for your efforts. Agree that documents of JupyterHub isn't well organized, LOL.

gzagatti commented 1 year ago

I had the same problem with getting error HTTP 403.

I somehow managed to hack a solution, but I don't seem able to fix the code:

  1. Hack the request once by hard coding the token in here:
  (ein:query-singleton-ajax
   (ein:url url-or-port "api/sessions")
   :type "GET"
   :params '(("token" . "<TOKEN>")) ; insert this line
   :parser #'ein:json-read
   :complete (apply-partially #'ein:content-query-sessions--complete url-or-port callback)
   :success (apply-partially #'ein:content-query-sessions--success url-or-port callback)
   :error (apply-partially #'ein:content-query-sessions--error url-or-port callback errback iteration)))
  1. login to the server as normal; request.el will create a cookie in emacs.d/curl-cookie-jar with the correct token.

  2. revert the code and you will be able to login as normal

After going through the code, I suspect that the token is not inserted into ein:query-authorization-tokens, so when ein:query-prepare-header is called the token is not included in the request.

This sort of problem only affected me when logging into Jupyterlab, previously when working with Jupyterhub that only had notebook support I did not face this issue.