[dev] first working version (#4)

works fine for one month

Co-authored-by: Dict Xiong <me@beardic.cn>
Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
Co-authored-by: xiuting.xu <xuxiuting04@126.com>
Co-authored-by: lintaothu <lintaothu@163.com>
Co-authored-by: toghrul <tabbasli@hotmail.com>
Co-authored-by: baiyu <baiyu@zgclab.edu.cn>
Reviewed-on: https://git.nasp.ob.ac.cn/NASP/registry/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
2023-05-30 13:21:29 +08:00
parent 7069fdbd72
commit 8bc58f889c
12 changed files with 137 additions and 0 deletions

21
scripts/jumpserver_cron.sh Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
set -ex
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
tmp_path="/tmp/authorized_keys"
dest_path="/home/ssh/.ssh/authorized_keys"
echo "# This file is autoly generated. Changes here will not work." > "$tmp_path"
for file in $(find "$THIS_DIR/../authorized_keys" -type f); do
(echo "# key file: ${file#*authorized_keys/}";cat "$file"; echo) >> "$tmp_path"
done
if [[ ! -d "/home/ssh/.ssh" ]]; then
mkdir -p "/home/ssh/.ssh"
chown ssh:ssh "/home/ssh/.ssh"
chmod 700 "/home/ssh/.ssh"
fi
cat "$tmp_path" > "$dest_path"
rm "$tmp_path"
chown ssh:ssh "$dest_path"
chmod 600 "$dest_path"

25
scripts/jumpserver_deploy.sh Normal file → Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
set -ex
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
adduser \
--disabled-password \
--home /home/ssh \
--gecos "jumpserver user ssh" \
ssh
usermod -p '*' ssh
insert_if_not_exist()
{
filename=$1
line=$2
if [ ! -f "$filename" ]; then
touch $filename
fi
grep -qxF -- "$line" "$filename" || echo "$line" >> "$filename"
}
insert_if_not_exist "/etc/crontabs/root" "*/5 * * * * cd \"$THIS_DIR\" && git pull && \"$THIS_DIR\"/jumpserver_cron.sh"
"$THIS_DIR"/jumpserver_cron.sh

5
scripts/nasp Normal file
View File

@@ -0,0 +1,5 @@
%nasp ALL = (root) NOPASSWD: /usr/bin/docker
%nasp ALL = (root) NOPASSWD: /usr/sbin/reboot
%nasp ALL = (root) NOPASSWD: /usr/bin/whoami
%nasp ALL = (root) NOPASSWD: /usr/bin/nvidia-smi
%nasp ALL = (root) NOPASSWD: /usr/sbin/shutdown

57
scripts/testbed_cron.sh Executable file
View File

@@ -0,0 +1,57 @@
#!/bin/bash
set -ex
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
touch_user() {
test -n "$1"
if id -u $1 1>/dev/null 2>&1; then
return
fi
if ! getent group nasp ; then
echo "Group 'nasp' does not exist\!"
exit 1
fi
adduser \
--shell /bin/bash \
--disabled-password \
--home /home/$1 \
--gecos "nasp member" \
$1
usermod -a -G nasp $1
}
update_key() {
tmp_path="/tmp/authorized_keys_$1"
dest_path="/home/$1/.ssh/authorized_keys"
dest_dir=$(dirname "$dest_path")
echo "# This file is autoly generated. Changes here will not work." > "$tmp_path"
for file in $(find "$THIS_DIR/../authorized_keys/$1" -type f); do
(echo "# key file: ${file#*authorized_keys/}";cat "$file"; echo) >> "$tmp_path"
done
if [[ ! -d "$dest_dir" ]]; then
mkdir -p "$dest_dir"
chown $1:$1 "$dest_dir"
chmod 700 "$dest_dir"
fi
cat "$tmp_path" > "$dest_path"
rm "$tmp_path"
chown $1:$1 "$dest_path"
chmod 600 "$dest_path"
}
main() {
cp "$THIS_DIR/nasp" "/etc/sudoers.d/nasp"
for file in "$THIS_DIR"/../authorized_keys/* ; do
if [[ ! -d "$file" ]]; then
continue
fi
username=$(basename $file)
touch_user $username
update_key $username
done
}
main

20
scripts/testbed_deploy.sh Normal file → Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
set -ex
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
apt update && apt install sudo
addgroup nasp
insert_if_not_exist()
{
filename=$1
line=$2
if [ ! -f "$filename" ]; then
touch $filename
fi
grep -qxF -- "$line" "$filename" || echo "$line" >> "$filename"
}
insert_if_not_exist "/etc/crontab" "*/5 * * * * root cd \"$THIS_DIR\" && git pull && \"$THIS_DIR\"/testbed_cron.sh"
"$THIS_DIR"/testbed_cron.sh