vuls: init at 0.27.0
[NixPkgs.git] / nixos / modules / services / web-apps / kasmweb / initialize_kasmweb.sh
blob232fbf34028b501d63a9723a20f4e6681adbf3c5
1 #! @runtimeShell@
2 export PATH=@binPath@:$PATH
4 mkdir -p @datastorePath@/log
5 chmod -R a+rw @datastorePath@
7 ln -sf @kasmweb@/bin @datastorePath@
8 rm -r @datastorePath@/conf
9 cp -r @kasmweb@/conf @datastorePath@
10 mkdir -p @datastorePath@/conf/nginx/containers.d
11 chmod -R a+rw @datastorePath@/conf
12 ln -sf @kasmweb@/www @datastorePath@
14 cat >@datastorePath@/init_seeds.sh <<EOF
15 #!/bin/bash
16 if [ ! -e /opt/kasm/current/.done_initing_data ]; then
17 while true; do
18 sleep 15;
19 /usr/bin/kasm_server.so --initialize-database --cfg \
20 /opt/kasm/current/conf/app/api.app.config.yaml \
21 --seed-file \
22 /opt/kasm/current/conf/database/seed_data/default_properties.yaml \
23 --populate-production \
24 && break
25 done && /usr/bin/kasm_server.so --cfg \
26 /opt/kasm/current/conf/app/api.app.config.yaml \
27 --populate-production \
28 --seed-file \
29 /opt/kasm/current/conf/database/seed_data/default_agents.yaml \
30 && /usr/bin/kasm_server.so --cfg \
31 /opt/kasm/current/conf/app/api.app.config.yaml \
32 --populate-production \
33 --seed-file \
34 /opt/kasm/current/conf/database/seed_data/default_images_amd64.yaml \
35 && touch /opt/kasm/current/.done_initing_data
37 while true; do sleep 10 ; done
38 else
39 echo "skipping database init"
40 while true; do sleep 10 ; done
42 EOF
44 docker network inspect kasm_default_network >/dev/null || docker network create kasm_default_network --subnet @networkSubnet@
45 if [ -e @datastorePath@/ids.env ]; then
46 source @datastorePath@/ids.env
47 else
48 API_SERVER_ID=$(cat /proc/sys/kernel/random/uuid)
49 MANAGER_ID=$(cat /proc/sys/kernel/random/uuid)
50 SHARE_ID=$(cat /proc/sys/kernel/random/uuid)
51 SERVER_ID=$(cat /proc/sys/kernel/random/uuid)
52 echo "export API_SERVER_ID=$API_SERVER_ID" > @datastorePath@/ids.env
53 echo "export MANAGER_ID=$MANAGER_ID" >> @datastorePath@/ids.env
54 echo "export SHARE_ID=$SHARE_ID" >> @datastorePath@/ids.env
55 echo "export SERVER_ID=$SERVER_ID" >> @datastorePath@/ids.env
57 mkdir -p @datastorePath@/certs
58 openssl req -x509 -nodes -days 1825 -newkey rsa:2048 -keyout @datastorePath@/certs/kasm_nginx.key -out @datastorePath@/certs/kasm_nginx.crt -subj "/C=US/ST=VA/L=None/O=None/OU=DoFu/CN=$(hostname)/emailAddress=none@none.none" 2> /dev/null
60 mkdir -p @datastorePath@/file_mappings
62 docker volume create kasmweb_db || true
63 rm @datastorePath@/.done_initing_data
66 chmod +x @datastorePath@/init_seeds.sh
67 chmod a+w @datastorePath@/init_seeds.sh
69 if [ -e @sslCertificate@ ]; then
70 cp @sslCertificate@ @datastorePath@/certs/kasm_nginx.crt
71 cp @sslCertificateKey@ @datastorePath@/certs/kasm_nginx.key
74 yq -i '.server.zone_name = "'default'"' @datastorePath@/conf/app/api.app.config.yaml
75 yq -i '(.zones.[0]) .zone_name = "'default'"' @datastorePath@/conf/database/seed_data/default_properties.yaml
77 sed -i -e "s/username.*/username: @postgresUser@/g" \
78 -e "s/password.*/password: @postgresPassword@/g" \
79 -e "s/host.*db/host: kasm_db/g" \
80 -e "s/ssl: true/ssl: false/g" \
81 -e "s/redis_password.*/redis_password: @redisPassword@/g" \
82 -e "s/server_hostname.*/server_hostname: kasm_api/g" \
83 -e "s/server_id.*/server_id: $API_SERVER_ID/g" \
84 -e "s/manager_id.*/manager_id: $MANAGER_ID/g" \
85 -e "s/share_id.*/share_id: $SHARE_ID/g" \
86 @datastorePath@/conf/app/api.app.config.yaml
88 sed -i -e "s/ token:.*/ token: \"@defaultManagerToken@\"/g" \
89 -e "s/hostnames: \['proxy.*/hostnames: \['kasm_proxy'\]/g" \
90 -e "s/server_id.*/server_id: $SERVER_ID/g" \
91 @datastorePath@/conf/app/agent.app.config.yaml
93 # Generate a salt and hash for the desired passwords. Update the yaml
94 ADMIN_SALT=$(cat /proc/sys/kernel/random/uuid)
95 ADMIN_HASH=$(printf @defaultAdminPassword@${ADMIN_SALT} | sha256sum | cut -c-64)
96 USER_SALT=$(cat /proc/sys/kernel/random/uuid)
97 USER_HASH=$(printf @defaultUserPassword@${USER_SALT} | sha256sum | cut -c-64)
99 yq -i '(.users.[] | select(.username=="admin@kasm.local") | .salt) = "'${ADMIN_SALT}'"' @datastorePath@/conf/database/seed_data/default_properties.yaml
100 yq -i '(.users.[] | select(.username=="admin@kasm.local") | .pw_hash) = "'${ADMIN_HASH}'"' @datastorePath@/conf/database/seed_data/default_properties.yaml
102 yq -i '(.users.[] | select(.username=="user@kasm.local") | .salt) = "'${USER_SALT}'"' @datastorePath@/conf/database/seed_data/default_properties.yaml
103 yq -i '(.users.[] | select(.username=="user@kasm.local") | .pw_hash) = "'${USER_HASH}'"' @datastorePath@/conf/database/seed_data/default_properties.yaml
105 yq -i '(.settings.[] | select(.name=="token") | select(.category == "manager")) .value = "'@defaultManagerToken@'"' @datastorePath@/conf/database/seed_data/default_properties.yaml
107 yq -i '(.settings.[] | select(.name=="registration_token") | select(.category == "auth")) .value = "'@defaultRegistrationToken@'"' @datastorePath@/conf/database/seed_data/default_properties.yaml
109 sed -i -e "s/upstream_auth_address:.*/upstream_auth_address: 'proxy'/g" \
110 @datastorePath@/conf/database/seed_data/default_properties.yaml
112 sed -i -e "s/GUACTOKEN/@defaultGuacToken@/g" \
113 -e "s/APIHOSTNAME/proxy/g" \
114 @datastorePath@/conf/app/kasmguac.app.config.yaml
116 sed -i "s/00000000-0000-0000-0000-000000000000/$SERVER_ID/g" \
117 @datastorePath@/conf/database/seed_data/default_agents.yaml
120 while [ ! -e @datastorePath@/.done_initing_data ]; do
121 sleep 10;
122 done
124 systemctl restart docker-kasm_proxy.service