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

create sysadmin account from user-provided values #144

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ 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
CKAN___API_TOKEN__JWT__DECODE__SECRET=string:CHANGE_ME
CKAN_SYSADMIN_NAME=ckan_admin
CKAN_SYSADMIN_PASSWORD=test1234
[email protected]
CKAN_STORAGE_PATH=/var/lib/ckan
CKAN_SMTP_SERVER=smtp.corporateict.domain:25
CKAN_SMTP_STARTTLS=True
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [3. docker compose vs docker-compose](#3-docker-compose-vs-docker-compose)
* [4. Install (build and run) CKAN plus dependencies](#4-install-build-and-run-ckan-plus-dependencies)
* [Base mode](#base-mode)
* [Create a sysadmin account](#create-a-sysadmin-account)
* [Development mode](#development-mode)
* [Create an extension](#create-an-extension)
* [Running HTTPS on development mode](#running-https-on-development-mode)
Expand Down Expand Up @@ -63,7 +64,7 @@ Copy the included `.env.example` and rename it to `.env`. Modify it depending on
Please note that when accessing CKAN directly (via a browser) ie: not going through NGINX you will need to make sure you have "ckan" set up
to be an alias to localhost in the local hosts file. Either that or you will need to change the `.env` entry for `CKAN_SITE_URL`

Using the default values on the `.env.example` file will get you a working CKAN instance. There is a sysadmin user created by default with the values defined in `CKAN_SYSADMIN_NAME` and `CKAN_SYSADMIN_PASSWORD`(`ckan_admin` and `test1234` by default). This should be obviously changed before running this setup as a public CKAN instance.
Using the default values on the `.env.example` file will get you a working CKAN instance.

To build the images:

Expand All @@ -84,6 +85,13 @@ At the end of the container start sequence there should be 6 containers running
After this step, CKAN should be running at `CKAN_SITE_URL`.


#### Create a sysadmin account

Create a sysadmin account and password to log in to the site:

docker compose -f docker-compose.yml exec ckan ckan sysadmin add <ADMIN-NAME>


### Development mode

Use this mode if you are making code changes to CKAN and either creating new extensions or making code changes to existing extensions. This mode also uses the `.env` file for config options.
Expand All @@ -100,12 +108,16 @@ To start the containers:

See [CKAN images](#5-ckan-images) for more details of what happens when using development mode.

Similar to above, create a sysadmin account and password in development mode:

docker compose -f docker-compose.dev.yml exec ckan-dev ckan sysadmin add <ADMIN-NAME>


#### Create an extension

You can use the ckan [extension](https://docs.ckan.org/en/latest/extensions/tutorial.html#creating-a-new-extension) instructions to create a CKAN extension, only executing the command inside the CKAN container and setting the mounted `src/` folder as output:

docker compose -f docker-compose.dev.yml exec ckan-dev /bin/sh -c "ckan -c /srv/app/ckan.ini generate extension --output-dir /srv/app/src_extensions"
docker compose -f docker-compose.dev.yml exec ckan-dev ckan generate extension --output-dir /srv/app/src_extensions

![Screenshot 2023-02-22 at 1 45 55 pm](https://user-images.githubusercontent.com/54408245/220623568-b4e074c7-6d07-4d27-ae29-35ce70961463.png)

Expand Down Expand Up @@ -291,11 +303,11 @@ For convenience the CKAN_SITE_URL parameter should be set in the .env file. For

1. Create a new user from the Docker host, for example to create a new user called 'admin'

`docker exec -it <container-id> ckan -c ckan.ini user add admin email=admin@localhost`
`docker exec <container-id> ckan user add admin email=admin@localhost`

To delete the 'admin' user

`docker exec -it <container-id> ckan -c ckan.ini user remove admin`
`docker exec <container-id> ckan user remove admin`

2. Create a new user from within the ckan container. You will need to get a session on the running container

Expand Down
40 changes: 0 additions & 40 deletions ckan/setup/prerun.py.override
Original file line number Diff line number Diff line change
Expand Up @@ -157,44 +157,6 @@ def init_datastore_db():
connection.close()


def create_sysadmin():

name = os.environ.get("CKAN_SYSADMIN_NAME")
password = os.environ.get("CKAN_SYSADMIN_PASSWORD")
email = os.environ.get("CKAN_SYSADMIN_EMAIL")

if name and password and email:

# Check if user exists
command = ["ckan", "-c", ckan_ini, "user", "show", name]

out = subprocess.check_output(command)
if b"User:None" not in re.sub(b"\s", b"", out):
print("[prerun] Sysadmin user exists, skipping creation")
return

# Create user
command = [
"ckan",
"-c",
ckan_ini,
"user",
"add",
name,
"password=" + password,
"email=" + email,
]

subprocess.call(command)
print("[prerun] Created user {0}".format(name))

# Make it sysadmin
command = ["ckan", "-c", ckan_ini, "sysadmin", "add", name]

subprocess.call(command)
print("[prerun] Made user {0} a sysadmin".format(name))


if __name__ == "__main__":

maintenance = os.environ.get("MAINTENANCE_MODE", "").lower() == "true"
Expand All @@ -208,5 +170,3 @@ if __name__ == "__main__":
check_datastore_db_connection()
init_datastore_db()
check_solr_connection()
create_sysadmin()

Loading