appwrite / sdk-for-cli

[READ-ONLY] Official Appwrite CLI >_
BSD 3-Clause "New" or "Revised" License
85 stars 29 forks source link

πŸ› Bug Report: appwrite cli logout automatically #110

Closed StarZeus closed 2 months ago

StarZeus commented 8 months ago

πŸ‘Ÿ Reproduction steps

  1. Login to appwrite instance from appwrite-cli with appwrite login command and console user id and password
  2. Retrieve project details with `appwrite projects get --projectId
  3. Able to retrieve project details
  4. Retrieve project details with `appwrite projects get --projectId
  5. Getting error message saying "Error User (role: guests) missing scope (projects.read)"

Regardless of what command we execute 2nd time onwards the command fails as the user is not logged in anymore

πŸ‘ Expected behavior

appwrite cli should stay logged in until logout

πŸ‘Ž Actual Behavior

appwrite cli logout automatically after each command

🎲 Appwrite version

Version 1.4.x

πŸ’» Operating system

Linux

🧱 Your Environment

Appwrite version 1.4.7 installed on kubernetes v1.20 appwrite-cli@4.1.0 installed on mac os Monterey

πŸ‘€ Have you spent some time to check if this issue has been raised before?

🏒 Have you read the Code of Conduct?

stnguyen90 commented 8 months ago

@StarZeus, thanks for creating this issue! πŸ™πŸΌ I just tried this and I can get my project for both attempts.

Would you please check and make sure:

  1. your ~/.appwrite/prefs.json file is not malformed
  2. your ./appwrite.json file is not malformed
  3. you have a CLI session in the Appwrite console at /console/account/sessions
StarZeus commented 8 months ago

@stnguyen90 - Thank you for looking into this.

To add clarity.

  1. ~/.appwrite/prefs.json is not malformed
  2. ./appwrite.json is not malformed
  3. I can see email session created and see from /console/account/sessions page

Also, I noticed that the ~/.appwrite/pref.json updated with cookies a_session_console_legacy and domain after login but removed immediately after executing 1st command. I believe that's why the 2nd command onwards is not working.

StarZeus commented 8 months ago

The appwrite url is https and I have set selfSigned=true if that make a difference

stnguyen90 commented 8 months ago

Also, I noticed that the ~/.appwrite/pref.json updated with cookies a_session_console_legacy and domain after login but removed immediately after executing 1st command.

It's replaced with an empty string? The only time that would happen is either:

  1. the response header included set-cookie
  2. the config is reset

Maybe you have something between the CLI and Appwrite messing up the cookies?

stnguyen90 commented 8 months ago

Does this happen with other commands like appwrite projects list? Do you get logged out from the Appwrite web console?

StarZeus commented 8 months ago

It happens for all command. I'm not logged out from appwrite web console. Thats stays logged in.

After every command the a_session_console_legacy and domain are not in cookie anymore.

"cookie": "route=b1c61a93f68b5c389187caa649ee5027; Expires=Mon, 30-Oct-23 17:02:35 GMT; Max-Age=172800; Path=/(.*); Secure; HttpOnly"

stnguyen90 commented 8 months ago

Very bizarre...Would you be able to curl Appwrite to see the full request? Something like:

ENDPOINT="https://[YOUR_HOSTNAME]/v1"

# Sign in (make sure to replace [YOUR_EMAIL] and [YOUR_PASSWORD]
curl -s -k -v -b console-cookies.txt -c console-cookies.txt "$ENDPOINT/account/sessions/email" \
  -H 'content-type: application/json' \
  -H 'x-appwrite-project: console' \
  --data-raw '{"email":"[YOUR_EMAIL]","password":"[YOUR_PASSWORD]"}' \
  --compressed \
  --insecure

# List projects
curl -s -k -v -b console-cookies.txt -c console-cookies.txt "$ENDPOINT/projects" \
  -H 'content-type: application/json' \
  -H 'x-appwrite-project: console' \
  --compressed \
  --insecure
stnguyen90 commented 7 months ago

@StarZeus, have you had a chance to try the commands I suggested. Btw, I'll need to close this soon due to inactivity.

StarZeus commented 6 months ago

I'll try the above curl command and post an update

StarZeus commented 6 months ago

@stnguyen90 - I was able to execute above curl commands. I can see project details returning even when I hit the 2nd curl repeatedly.

stnguyen90 commented 6 months ago

"cookie": "route=b1c61a93f68b5c389187caa649ee5027; Expires=Mon, 30-Oct-23 17:02:35 GMT; Max-Age=172800; Path=/(.*); Secure; HttpOnly"

What is this you shared? This doesn't look like what Appwrite would typically respond. It seems there's something else that is passing back additional cookies...And since the CLI only grabs the first set-cookie, the cookie gets overwritten

StarZeus commented 6 months ago

I am running appwrite in kubernetes cluster. "route" is the Sticky/Affinity cookie set by the nginx ingress controller so that subsequent requests are sent to the same container.

Is there a reason why the cli captures only the first cookie?

StarZeus commented 5 months ago

Below change to capture all cookies from response fixes my problem. I'll be raising a PR

    let cookieString = "";
    response.headers["set-cookie"].forEach((cookie) => {
        cookieString += cookie.split(";")[0] + ";";
    });        
    globalConfig.setCookie(cookieString);
StarZeus commented 5 months ago

Raised PR https://github.com/appwrite/sdk-for-cli/pull/104

StarZeus commented 4 months ago

@stnguyen90 raised https://github.com/StarZeus/sdk-generator/pull/1

stnguyen90 commented 3 months ago

@StarZeus, thanks for creating this PR. Would you please create the PR to target our repo instead of your forked master branch?

StarZeus commented 3 months ago

@stnguyen90 - I have raised a new PR targeting appwrite repo.