Skip to content

Instantly share code, notes, and snippets.

@icyleaf
Created January 18, 2025 14:31
Show Gist options
  • Save icyleaf/05878edf31384966dee85b7a1dc00a09 to your computer and use it in GitHub Desktop.
Save icyleaf/05878edf31384966dee85b7a1dc00a09 to your computer and use it in GitHub Desktop.
Import GUNPG(PGP) key from bitwarden
set -x
function bw_download_and_import_gpg_key() {
# 登录并解锁 Bitwarden,如果没有已存在的会话
if [[ -z "$BW_SESSION" ]]; then
echo "Logging in to Bitwarden..."
export BW_SESSION=$(bw unlock --raw) || { echo "Failed to unlock Bitwarden."; return 1; }
fi
local name=$1
# 搜索精准名称为 "gpg" 的条目
echo "Searching for the item with name '$name'..."
ITEM_JSON=$(bw list items --search "$name" --session "$BW_SESSION")
ITEM_ID=$(echo "$ITEM_JSON" | jq -r ".[] | select(.name == \"$name\") | .id")
# 确认找到唯一的条目 ID
if [[ -z "$ITEM_ID" ]]; then
echo "Error: No item with name '$name' found."
return 1
fi
local filename=$2
echo "Downloading attachment '$filename'..."
bw get attachment "$filename" --itemid "$ITEM_ID" --output "/tmp/$filename" --session "$BW_SESSION" || {
echo "Failed to download '$filename'."
return 1
}
# 使用 gpg 导入下载的附件
echo "Importing '$filename' with gpg..."
gpg --import "/tmp/$filename" && echo "Import successful!" || {
echo "Failed to import '$filename' with gpg."
return 1
}
}
# 使用方法:在命令行中直接运行函数
bw_download_and_import_gpg_key "GPG | flux sops secret" "flux-sops-secret.key"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment