英文:
Securely pass credentials to command-line utility
问题
Sure, here's the translated code portion without any additional content:
curl -s 'https://api.cloudflare.com/client/v4/zones/{zone_identifier}/logpush/jobs' \
-H "X-Auth-Email: carl@example.com" \
-H "X-Auth-Key: abc123"
Please note that I will only provide translations for the code and not answer the question you want to translate.
英文:
curl -s 'https://api.cloudflare.com/client/v4/zones/{zone_identifier}/logpush/jobs' \
-H "X-Auth-Email: carl@example.com" \
-H "X-Auth-Key: abc123"
If I run this, the secret string abc123
leaks up in three places:
- Shell history
- Terminal output
ps
output
I can replace the hardcoded password with something like $(pass show cloudflare-api-key)
, in which case the password only leaks in ps
output. This is still bad, as an unprivileged daemon on my machine could steal my credentials.
Is there a handy way to pass credentials to a command-line utility in an ad-hoc fashion such that the credential doesn't leak to ps
either?
答案1
得分: 2
Curl可以从标准输入读取标志,所以假设你的密码不包含换行符:
pass show cloudflare-api-key \
| sed -ne 's/"/\\"/g' -e '1s/.*/-H "X-Auth-Key: &"/p' \
| curl -K- <其他选项>
在pass
中的文件可能包含密码下面的其他字段,所以我确保sed只显示第一行。这个sed表达式非常简单:首先我们转义密码内的所有双引号,然后将密码放入curl -H
选项的参数中。这可以很容易地扩展到从pass
中提取其他字段,比如电子邮件:
$ printf %s\\n '12"3' 'email: foo@bar.com' \
| sed -ne 's/"/\\"/g' -e '1s/.*/-H "X-Auth-Key: &"/p' -e 's/^email: \(.*\)/-H "X-Auth-Email: "/p'
-H "X-Auth-Key: 12\"3"
-H "X-Auth-Email: foo@bar.com"
英文:
Curl can read flags from stdin, so assuming your password doesn't contain newlines:
pass show cloudflare-api-key \
| sed -ne 's/"/\\"/g' -e '1s/.*/-H "X-Auth-Key: &"/p' \
| curl -K- <other options>
Files in pass
may include other fields below the password, so I made sure that sed only shows the first line. The sed expression is pretty simple: first we escape all of the double quotes inside the password, then we put the password inside curl -H
option's argument. This can be easily extended to pull other fields, like email, from pass
as well:
$ printf %s\\n '12"3' 'email: foo@bar.com' \
| sed -ne 's/"/\\"/g' -e '1s/.*/-H "X-Auth-Key: &"/p' -e 's/^email: \(.*\)/-H "X-Auth-Email: "/p'
-H "X-Auth-Key: 12\"3"
-H "X-Auth-Email: foo@bar.com"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论