linuxgoogle-cloud-platformsftp

Creating first admin in SFTPGo using VM instance startup script?


According to readme.md in https://github.com/drakkan/sftpgo, 1 way of creating first admin is by enabling create_default_admin in your configuration file and setting the environment variables SFTPGO_DEFAULT_ADMIN_USERNAME and SFTPGO_DEFAULT_ADMIN_PASSWORD.

I followed this and despite setting those successfully, I am unable to create first admin.

The reason I want to do this is to automate creating SFTPGo instance on GCP and then initialize it with some predefined Admin Username and Password.

This is the startup script I use:

metadata_startup_script = <<-SCRIPT
#!/bin/bash

# Install SFTPGo
sudo apt-get update
sudo add-apt-repository -y ppa:sftpgo/sftpgo
sudo apt update
sudo apt-get install -y sftpgo

# Set ENV Variables
echo 'export SFTPGO_DEFAULT_ADMIN_USERNAME="user"' | sudo tee -a /etc/environment
echo 'export SFTPGO_DEFAULT_ADMIN_PASSWORD="password"' | sudo tee -a /etc/environment

# Apply changes to the current shell
source /etc/environment

# Change create_default_admin to true
sudo -s && jq '.data_provider.create_default_admin = true' /etc/sftpgo/sftpgo.json > temp.json && mv temp.json /etc/sftpgo/sftpgo.json

# Restart the shell to apply changes
exec bash

SCRIPT

While everything in my script works, when I access "https://IP:8080/web/admin" first time after installing SFTPGo, it still asks me to create first admin.

I restarted the service like this:

sudo systemctl restart sftpgo

Service failed to restart. I checked the logs like this:

journalctl -u sftpgo

I see this error:

{"level":"error","time":"2023-10-17T00:50:46.006","sender":"service","message":"error initializing data provider: to create the default admin you need to set the env vars \"SFTPGO_DEFAULT_ADMIN_USERNAME\" and \"SFTPGO_DEFAULT_ADMIN_PASSWORD\"""}

Can anyone let me know if I am missing anything here please ? I am indeed setting these ENV vars. I logged into the instance and checked that required ENV vars are globally declared.

*Apologies if this question is very specific to a service to be on Stack Overflow


Solution

  • Was able to achieve this using following approach:

    # Set environment variables in .bashrc
    echo 'export SFTPGO_DEFAULT_ADMIN_USERNAME="${var.sftp_admin_username}"' | sudo tee -a /etc/environment
    echo 'export SFTPGO_DEFAULT_ADMIN_PASSWORD="${var.sftp_admin_password}"' | sudo tee -a /etc/environment
    
    # Apply changes to the current shell
    source /etc/environment
    
    sudo -s <<EOF
        jq '.data_provider.create_default_admin = true' /etc/sftpgo/sftpgo.json > 
        temp.json
        mv temp.json /etc/sftpgo/sftpgo.json
    EOF
    
    sudo systemctl stop sftpgo && sudo sftpgo serve