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

Docker Compose option to generate random passwords #76

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

kowh-ai
Copy link
Contributor

@kowh-ai kowh-ai commented Aug 16, 2023

Fixes: #65

In the past passwords have been included in the .env file and therefore included as Docker Compose variables when building and running Docker images and containers

This PR includes a new option to generate randomised, secure passwords for the following users:

  • The Postgres system superuser POSTGRES_USER
  • The database user that owns the CKAN database CKAN_DB_USER
  • The database user that has read-access to the Datastore database DATASTORE_READONLY_USER
  • The CKAN application System Administrator user CKAN_SYSADMIN_NAME

These passwords are kept in a new file: .pw and added to env_file: in the Docker Compose yml file

The README has been updated with a new section on the steps involved for using this new option

I could include all the steps in the generate_passwords.sh script rather than have people explicitly overriding and saving files

Update: The random password generator will now be the default option. There will also be an option to opt-out so passwords will be referenced from the .env file. This option is how passwords were implemented previously

Also, other variables (secrets) will be included as well. These are as follows:

  • The beaker session secret CKAN___BEAKER__SESSION__SECRET
  • The api token encode secret CKAN___API_TOKEN__JWT__ENCODE__SECRET
  • The api token decode secret CKAN___API_TOKEN__JWT__DECODE__SECRET

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this committed by accident? At a glance, this looks like the generated passwords Compose file was used to overwrite the regular Compose file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so...here are the last commits to docker-compose.yml:

Screenshot 2023-08-29 at 4 06 18 pm

Copy link

@themowski themowski Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant, was it added into this PR (committed to this branch) by accident?

Based on the other feedback, it seems like the auto-generated passwords will be the default behavior in the future, so presumably this comment is moot anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK, if you look at the README in the PR you will see the instructions:

Screenshot 2023-08-29 at 5 14 44 pm

At the moment you need to explicitly override the docker-compose.yml file when wanting to use more secure passwords, however after the Dev meeting this afternoon it was decided that secure passwords will be the default when this is merged


with open(fn, 'w') as f:
f.truncate(0)
for pwvar in pwvars:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to use https://docs.python.org/3/library/secrets.html#secrets.token_urlsafe secrets.token_urlsafe, as /dev/urandom is somewhat platform dependent.

f.write(f"CKAN_DATASTORE_WRITE_URL=postgresql://{CKAN_DB_USER}:{CKAN_DB_PASSWORD}@{POSTGRES_HOST}/{DATASTORE_DB}\n")
f.write(f"CKAN_DATASTORE_READ_URL=postgresql://{DATASTORE_READONLY_USER}:{DATASTORE_READONLY_PASSWORD}@{POSTGRES_HOST}/{DATASTORE_DB}\n")

print("[setup_passwords] password file: '.pw' created successfully")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably print out the ckan sysadmin password on the console, and note where it's actually saved.

- redisnet
env_file:
- .env
- .pw

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't safe.

This is passing in the database superuser password into the CKAN instance, where it should only have access to the specific urls that are needed to connect to the database.

@EricSoroos
Copy link

Also mentioned in the call -- Out of the box, this should use the random PWs. If someone wants to use old static passwords on new installs, they need to work at it.

CKAN_PORT_HOST=5000
CKAN___BEAKER__SESSION__SECRET=CHANGE_ME
# See https://docs.ckan.org/en/latest/maintaining/configuration.html#api-token-settings
CKAN___API_TOKEN__JWT__ENCODE__SECRET=string:CHANGE_ME

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably set these as well.

@kowh-ai kowh-ai mentioned this pull request Sep 3, 2023
@kowh-ai
Copy link
Contributor Author

kowh-ai commented Sep 13, 2023

Thanks @EricSoroos for helping me here. I've pretty much got it all working with the new changes however I'm thinking it might be best to have one generated password file (locked down) to pass through to the ckan container and one to pass through to the db container...

@kowh-ai kowh-ai requested a review from EricSoroos September 19, 2023 08:47
@pwalsh
Copy link
Member

pwalsh commented Jan 30, 2024

@kowh-ai I'm not sure if this is the best place to note this, so do tell me if there is another better, location:

If we are doing this work on random passwords, to bypass having passwords in .env files, perhaps it is worth exploring the docker solution for secrets, which works with docker compose:

https://docs.docker.com/compose/use-secrets/

@kowh-ai
Copy link
Contributor Author

kowh-ai commented Jan 31, 2024

@pwalsh - I had gone through secrets in Docker documentation plus examples people had written about and thought you could only use secrets when you had set up Docker to run in swarm mode. I'm very interested in pursuing this

@pwalsh
Copy link
Member

pwalsh commented Jan 31, 2024

@kowh-ai ok. I was sure it worked with compose, but must be my misreading. I was researching this for some of my own projects but have not implemented it yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use a password generator for secure passwords
4 participants