我打算使用最低权限的用户来完成给定的任务。问题是它涉及一种责任链,并sudo在我的脚本中翻译为链,将整体任务分散到许多地方。
sudoer 文件
"www-data" ALL = (webadmin) NOPASSWD: /home/webadmin/scripts/git-deploy.sh
"webadmin" ALL = (root) NOPASSWD: /root/scripts/copy-deploy ""
细节
为了使上一段更具体:一些请求被发送到 Apache。worker 以www-data身份运行
通过 mod_php,脚本/home/webadmin/scripts/git-deploy.sh使用
<?php
exec('/'.escapeshellarg($gitRepo))
$gitRepo 由以下脚本提供:
$bitBucketUrl=json_decode($request->getContent(),true);
if(isset($bitBucketUrl['url'])){
$bitBucketUrl=$bitBucketUrl['url'];
if(!preg_match('/\.git$/',$bitBucketUrl)){
$bitBucketUrl.='.git';
}
}else{
$result='bad url';
}
if(preg_match('/^git@bitbucket.org:TEAM\/[a-zA-Z0-9\.-]+\.git$/',$bitBucketUrl)){
exec(escapeshellcmd('sudo -u webadmin /home/webadmin/scripts/git-deploy.sh '.escapeshellarg($bitBucketUrl)),$results);
$result=$results;
}else{
$result=$bitBucketUrl;
}
当然,脚本/home/webadmin/scripts/git-deploy.sh检查$1
#!/bin/bash
valid=^git@bitbucket.org:TEAM/[a-zA-Z0-9-]+\.git$
if [[ $1 =~ $valid ]]; then
name=${1#git@bitbucket.org:TEAM/}
name=${name%.git}
fullName=/home/webadmin/websites/$name
if [[ -e $fullName ]] ; then
echo "$fullName exists, will do a git pull instead"
echo "cd $fullName && git pull"
else
echo "/usr/bin/git clone $1"
fi
if [[ -e $fullName/deploy/apache-conf/ ]]; then
#sudo -u root /root/scripts/copy-deploy
fi
fi
注意:我知道它只是回显,这是带有虚拟用户的测试版本。
每个脚本仅u+rwx由其所有者 (700) 提供。
问题
- 将脚本散布在各个地方是否违反了安全性?(很难理解发生了什么)
- 你还看到什么不对劲的地方吗?