I think I figured it out
- Deploy Keys: uses ssh/git commands, requires installing git on the container
To use Deploy Keys you get a username/token as follows
runcmd:
- git clone https://TOKEN-USER:TOKEN-PASSWD@gitlab...../REPO.git /tmp/
- cp /tmp/REPO/a /path/to/adir/a
- cp /tmp/REPO/b /path/to/bdir/b
- Project Access Tokens: Uses HTTPS, requires installing curl (or the equivalent) on the container
To use a Project Access Token you use the API via HTTP
runcmd:
- curl https://path_to_git_api_for_file_a --output /path/to/adir/a
I didn’t want to pull over an entire git repo and didn’t want to install git and all the dependencies on the container, so I opted for the API key and curl.
I didn’t want to leave private keys on the container or in the yaml file so I tested both --environment-file=conf_file.env
and --config environment.MYVARIABLE
for files that are free of keys. Both worked (see below)
Some notes from testing:
runcmd: source FILENAME
and
runcmd: . FILENAME
does not work in launching an incus cloud -enabled container
Both “runcmd source
” and “runcmd .
” generated the error
cloud-init: /var/lib/cloud/instance/scripts/runcmd: 4: source: not found
To get around this I pulled environment variables from /proc/1/environ
runcmd:
- eval $(cat /proc/1/environ | tr '\0' '\n')
- shutdown -r now
Those variables persist with reboots so I ran incus config unset CONTAINER environment.secret_value
and ran a shutdown -r now
at the end of the runcmd commands.
Here’s the full script and redacted config.yaml. I used this to add security settings like notification on the activation of any interactive shell (e.g. login) and recording commands in real-time for auditing.
incus launch images:debian/13/cloud web01 --config environment.secret=mysecret \
--environment-file=gitlab.env --profile BridgeStuff --profile DriveStuff
--profile cloud_disable_ipv6 < configA.yaml \
&& incus config unset web01 environment.GITLAB_PROJECT_TOKEN \
&& incus config unset web01 environment.mysecret
with an abbreviated configA.yaml
as
config:
user.network-config: |
version: 2
ethernets:
eth0:
dhcp4: false
addresses:
- A.B.C.D/E
routes:
- to: 0.0.0.0/0
via: A.B.C.1
nameservers:
addresses:
- F.G.H.I
- F.G.H.J
cloud-init.user-data: |
#cloud-config
users:
- name: NotDebian
groups: sudo
shell: /bin/bash
ssh_authorized_keys:
- ssh-rsa ssh-rsa REDACTED
packages:
- vim
- curl
- msmtp
runcmd:
- eval $(cat /proc/1/environ | tr '\0' '\n')
- apt-get update
- apt-get upgrade -y
- mkdir /etc/profile.d/bashrc.d
- |
curl --silent --header "PRIVATE-TOKEN: $GITLAB_PROJECT_TOKEN" \
--url "https://REDACTED/api/v4/projects/$GITLAB_PROJECT_ID/repository/files/FILEPATH_REDACTED/raw?ref=main" \
--output /etc/profile.d/bashrc.d/Z99_record_commands.sh
- chmod a+x /etc/profile.d/bashrc.d/Z99_record_commands.sh
- |
curl --silent --header "PRIVATE-TOKEN: $GITLAB_PROJECT_TOKEN" \
--url "https://REDACTED/api/v4/projects/$GITLAB_PROJECT_ID/repository/files/FILEPATH_REDACTED/raw?ref=main" \
--output /etc/profile.d/bashrc.d/Z98_notify_on_login.sh
- chmod a+x /etc/profile.d/bashrc.d/Z98_notify_on_login.sh
- |
curl --silent --header "PRIVATE-TOKEN: $GITLAB_PROJECT_TOKEN" \
--url "https://REDACTED/api/v4/projects/$GITLAB_PROJECT_ID/repository/files/FILEPATH_REDACTED/raw?ref=main" \
--output - >> /etc/bash.bashrc
#reboot now and if "incus config unset ..." has been run, the private vars are gone.
- shutdown -r now