Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup.pl - Clicking create button causes Bad request: CSRF token failure #8411

Closed
walidmujahid opened this issue Sep 14, 2024 · 11 comments
Closed
Labels
waiting-for-user Issue can't progress unless feedback from reporter is received

Comments

@walidmujahid
Copy link
Contributor

walidmujahid commented Sep 14, 2024

Version

1.11.18-dev

What browsers are you seeing the problem on?

No response

What happened?

I'm running a modified docker image on Digitalocean's App Platform based on the code at commit 0bcd809: https://gist.github.com/walidmujahid/db362783268147b3c3259be33dc76d1f

I was trying to setup a company through setup.pl, however, when I click the create button, I get a Bad request: CSRF token failure.

This issue happened to me also when I initially tested with the docker hub ledgersmb/ledgersmb:1.11.17 image without nginx.

What should have happened?

I was expecting to be able to setup the company after clicking the create button.

@ehuelsmann
Copy link
Member

As s work around, the login button also creates a new company when you try to log into a non existent database.

@walidmujahid
Copy link
Contributor Author

@ehuelsmann I think perhaps due to my using Digitalocean's managed databases, that's not much of an option (I'm not 100% sure). When I try logging in with a database I didn't create through the digitalocean dashboard, I get:

Error!
Could not connect to database: connection to server at "ledgersmb-do-user-66666666-0.k.db.ondigitalocean.com" (11.111.111.111), port 25060 failed: FATAL: database "postgres" does not exist at lib/LedgerSMB/Scripts/setup.pm line 267.

dbversion: 1.11.18-dev, company:

@walidmujahid
Copy link
Contributor Author

walidmujahid commented Sep 21, 2024

So, after familiarising myself a bit with this codebase, and figuring out what I needed to do for #8410, I've been able to play with my own workaround for my specific setup getting around the createdb: https://gist.github.com/walidmujahid/f4049bce9364b67da09a33385d81bf02

I'm still figuring things out, but I'm better able to make progress now.

@ehuelsmann Should this issue stay open?

EDIT:
I successfully created a script that prepares a company database in a "pre-created" database in Digitalocean's managed database platform.

I don't know how many people are facing this use case, but I'll keep the gist public for anyone that may stumble across this in the future. My specific use case is: Deploying ledgersmb app on the Digitalocean platform and connecting it with a managed Postgres database.

@walidmujahid
Copy link
Contributor Author

walidmujahid commented Oct 2, 2024

@ehuelsmann After a lot of reading of your codebase, and the merging of PR #8414, I have settled on a workaround that stable for my use case. By the way, this file was really useful for me in understanding the usage of ledgersmb-admin script: https://github.com/ledgersmb/LedgerSMB/blob/1.11/UI/tests/specs/openapi/database.js

Use Case Overview

Launching LedgerSMB on DigitalOcean App Platform

Steps Taken to launch LedgerSMB on DigitalOcean App Platform

Modifications to Dockerfile

https://gist.github.com/walidmujahid/db362783268147b3c3259be33dc76d1f

Modifications include:

  • Pulling from ledgersmb codebase at a given commit (to include changes and fixes not released)
  • combine nginx into single image and modify start.sh script (app should be small enough for it to not be a problem despite not being best practice to have multiple services inside a single image)
  • create transpiled version of javascript with (since it's being pulled from repo instead of distributed archives): make js

Create Container Registry on DigitalOcean

For easy deployments to App platform, the following container registry was created for our custom docker images.

Create Managed Database

  1. Go to: cloud.digitalocean.com/databases
  2. Click "Create Database" button
  3. Choose datacenter
  4. Select database engine: PostgreSQL v16
  5. CPU options. E.g: Regular
  6. Select a plan. E.g: $13.00 - 1 vCPU / 1 GB RAM / Storage minimum: 10 GB / Connection limit: 22
  7. Choose database name. E.G: ledgersmb (or any other name)

Our Launch Steps on App Platform

  1. Go to: cloud.digitalocean.com/apps
  2. Select service provider: DigitalOcean Container Registry
  3. Use appropriate custom image from registry
  4. Keep autodeploy
  5. adjust resource and app settings
  6. connect managed database
  7. add PG* environment variables for db connection: PGHOST, PGPORT, PGDATABASE, PGUSER, and PGPASSWORD.

Prepare Company Databse

Use custom script to prepare the company in the Digitalocean managed database.

  1. Create testdb (or any other name) in the Digitalocean managed database dashboard
  2. In the App Platform console, Run: curl https://gist.githubusercontent.com/walidmujahid/f4049bce9364b67da09a33385d81bf02/raw/20f2c39fc810d533c25b0cedca3d2b8351bf2c42/prepare-company-with-digitalocean-managed-database >> /srv/ledgersmb/bin/prepare-company-with-digitalocean-managed-database && chmod +x /srv/ledgersmb/bin/prepare-company-with-digitalocean-managed-database
  3. Then, with /srv/ledgersmb as the working directory: ./bin/prepare-company-with-digitalocean-managed-database --host=ledgersmb-do-user-66666666-0.k.db.ondigitalocean.com --port=25060 --company=testdb --owner=doadmin --password=DB_PASSWORD --cc=us --coa=locale/coa/us/GeneralHierarchical.xml

Create User

In the App Platform console, Run: ./ledgersmb-admin user create testdb --username=admin --password=admin --employeennumber=1 --country='United States' --first-name=Admin --last-name=Name --permission='Full Permissions'

Load COA

In the App Platform console, Run: ./bin/ledgersmb-admin setup load testdb locale/coa/us/GeneralHierarchical.xml

walidmujahid added a commit to walidmujahid/LedgerSMB that referenced this issue Oct 3, 2024
This commit adds a new 'prepare' command to the ledgersmb-admin tool, allowing users to prepare an existing database for use with LedgerSMB without recreating it. This is particularly useful in deployment scenarios where the database is pre-created, such as when using managed database services like DigitalOcean Managed Databases.

The 'prepare' command:

- Accepts a <db-uri> as a positional argument, consistent with other ledgersmb-admin commands.
- Loads the base schema, applies changes, and loads modules into the specified database.
- Utilizes environment variables (PG*) or defaults for connection parameters when they are not specified.

This addition addresses the need to prepare existing databases in situations where the 'create' command cannot be used because it attempts to create the database, which may not be possible or desired in certain environments.

This is inspired from the workaround for issue: ledgersmb#8411
walidmujahid added a commit to walidmujahid/LedgerSMB that referenced this issue Oct 3, 2024
This commit adds a new 'prepare' command to the ledgersmb-admin tool, allowing users to prepare an existing database for use with LedgerSMB without recreating it. This is particularly useful in deployment scenarios where the database is pre-created, such as when using managed database services like DigitalOcean Managed Databases.

The 'prepare' command:

- Accepts a <db-uri> as a positional argument, consistent with other ledgersmb-admin commands.
- Loads the base schema, applies changes, and loads modules into the specified database.
- Utilizes environment variables (PG*) or defaults for connection parameters when they are not specified.

This addition addresses the need to prepare existing databases in situations where the 'create' command cannot be used because it attempts to create the database, which may not be possible or desired in certain environments.

This is inspired from the workaround for issue: ledgersmb#8411
walidmujahid added a commit to walidmujahid/LedgerSMB that referenced this issue Oct 3, 2024
This commit adds a new 'prepare' command to the ledgersmb-admin tool, allowing users to prepare an existing database for use with LedgerSMB without recreating it. This is particularly useful in deployment scenarios where the database is pre-created, such as when using managed database services like DigitalOcean Managed Databases.

The 'prepare' command:

- Accepts a <db-uri> as a positional argument, consistent with other ledgersmb-admin commands.
- Loads the base schema, applies changes, and loads modules into the specified database.
- Utilizes environment variables (PG*) or defaults for connection parameters when they are not specified.

This addition addresses the need to prepare existing databases in situations where the 'create' command cannot be used because it attempts to create the database, which may not be possible or desired in certain environments.

This is inspired from the workaround for issue: ledgersmb#8411
walidmujahid added a commit to walidmujahid/LedgerSMB that referenced this issue Oct 4, 2024
This commit adds a '--prepare-only' option to the 'ledgersmb-admin create' command, allowing users to prepare an existing database without attempting to create it. This is particularly useful in deployment scenarios where the database is pre-created, such as when using managed database services like DigitalOcean Managed Databases.

The '--prepare-only' option:

- Skips the database creation step, avoiding errors when the database already exists.
- Performs the preparation steps by loading the base schema, applying changes, and loading modules into the specified database.
- Provides a consistent user experience by integrating the functionality into the existing 'create' command.

This addition addresses the need to prepare existing databases in situations where the 'create' command cannot be used because it attempts to create the database, which may not be possible or desired in certain environments.

This is inspired from the workaround for issue: ledgersmb#8411
walidmujahid added a commit to walidmujahid/LedgerSMB that referenced this issue Oct 4, 2024
This commit adds a '--prepare-only' option to the 'ledgersmb-admin create' command, allowing users to prepare an existing database without attempting to create it. This is particularly useful in deployment scenarios where the database is pre-created, such as when using managed database services like DigitalOcean Managed Databases.

The '--prepare-only' option:

- Skips the database creation step, avoiding errors when the database already exists.
- Performs the preparation steps by loading the base schema, applying changes, and loading modules into the specified database.
- Provides a consistent user experience by integrating the functionality into the existing 'create' command.

This addition addresses the need to prepare existing databases in situations where the 'create' command cannot be used because it attempts to create the database, which may not be possible or desired in certain environments.

This is inspired from the workaround for issue:  ledgersmb#8411
walidmujahid added a commit to walidmujahid/LedgerSMB that referenced this issue Oct 4, 2024
This commit adds a '--prepare-only' option to the 'ledgersmb-admin create' command, allowing users to prepare an existing database without attempting to create it. This is particularly useful in deployment scenarios where the database is pre-created, such as when using managed database services like DigitalOcean Managed Databases.

The '--prepare-only' option:

- Skips the database creation step, avoiding errors when the database already exists.
- Performs the preparation steps by loading the base schema, applying changes, and loading modules into the specified database.
- Provides a consistent user experience by integrating the functionality into the existing 'create' command.

This addition addresses the need to prepare existing databases in situations where the 'create' command cannot be used because it attempts to create the database, which may not be possible or desired in certain environments.

This is inspired from the workaround for issue:  ledgersmb#8411
@ehuelsmann
Copy link
Member

Er soll need to investigate the reason for the rejection of the CSRF token.

@walidmujahid
Copy link
Contributor Author

@ehuelsmann On our setup, I navigated to the setup.pl endpoint. If I hit the login button with a nonexistent database, I get this error:

Error!
Could not connect to database: connection to server at "ledgersmb-do-user-6666666-0.k.db.ondigitalocean.com" (111.11.111.111), port 25060 failed: FATAL: database "postgres" does not exist at lib/LedgerSMB/Scripts/setup.pm line 267.

dbversion: 1.11.18-dev, company:

If I hit create for that non-existent databse, I get this:

Error!
Could not connect to database: connection to server at "ledgersmb-do-user-6666666-0.k.db.ondigitalocean.com" (111.11.111.111), port 25060 failed: FATAL: database "postgres" does not exist at lib/LedgerSMB/Scripts/setup.pm line 1123.

dbversion: 1.11.18-dev, company:

@ehuelsmann
Copy link
Member

Does a digital ocean Pg database have an administrative database by another name, maybe? The "postgres" database is normally the database that is the administrator's "default database". The admin database to be used is configurable in the Docker YAML configuration file; see the configuration section in https://github.com/ledgersmb/LedgerSMB/blob/master/doc/conf/ledgersmb.yaml#L243-L255. See https://github.com/ledgersmb/ledgersmb-docker/blob/1.11/README.md#overriding-or-adding-configuration for more information on how to add the extra config to your container.

@ehuelsmann
Copy link
Member

I was trying to setup a company through setup.pl, however, when I click the create button, I get a Bad request: CSRF token failure.

@walidmujahid could it be that you restarted the container between the point where you loaded the page in setup.pl and the point where you clicked on "Create" or "Login" triggering the CSRF error? If so, this was a side-effect of restarting the container.

@ehuelsmann ehuelsmann added the waiting-for-user Issue can't progress unless feedback from reporter is received label Oct 14, 2024
@walidmujahid
Copy link
Contributor Author

@ehuelsmann I'm no longer seeing the CSRF token failure. I'm not really sure what resolved that. I' just seeing those other errors I mentioned. I'm going to looking into the default database question you posed on digitalocean and do some tests with the config yaml file.

I was trying to setup a company through setup.pl, however, when I click the create button, I get a Bad request: CSRF token failure.

@walidmujahid could it be that you restarted the container between the point where you loaded the page in setup.pl and the point where you clicked on "Create" or "Login" triggering the CSRF error? If so, this was a side-effect of restarting the container.

@github-actions github-actions bot removed the waiting-for-user Issue can't progress unless feedback from reporter is received label Oct 15, 2024
@ehuelsmann
Copy link
Member

Let's create a separate issue for the DigitalOcean Login/Create failures and close the problem with the CSRF token as that's disappeared now that things settled down.

@ehuelsmann ehuelsmann added the waiting-for-user Issue can't progress unless feedback from reporter is received label Oct 15, 2024
@walidmujahid
Copy link
Contributor Author

Let's create a separate issue for the DigitalOcean Login/Create failures and close the problem with the CSRF token as that's disappeared now that things settled down.

@ehuelsmann Sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting-for-user Issue can't progress unless feedback from reporter is received
Projects
None yet
Development

No branches or pull requests

2 participants