【问题标题】:How to create a GitHub repository secret /encrypt it using bash on Ubuntu?如何在 Ubuntu 上使用 bash 创建 GitHub 存储库机密/加密它?
【发布时间】:2021-10-28 05:46:34
【问题描述】:

我正在尝试在 Ubuntu 上使用 bash 创建/更新 GitHub 机密。

他们的 api 文档说我应该

  1. 从 repo 中获取公钥
  2. 用它加密秘密
  3. 创建/更新 GitHub 密码

但示例仅在 NodeJS 和 Python 中,我不确定如何使用 Ubuntu 上的 bash 中提到的 libsodium 来实现我需要的。

https://docs.github.com/en/rest/reference/actions#create-or-update-a-repository-secret

我能够使用keykey_id 获得公众

$ curl -s \
-H "authorization: Bearer $MY_ACCESS_TOKEN" \
https://api.github.com/repos/MYORG/MYREPO/actions/secrets/public-key
{
  "key_id": "123456789012345678",
  "key": "abcdHXZ2BrPAFPrZHy1AAct3B12k7BPgxXgdtxcABCo="
}

我能够使用 Python 3 示例生成似乎是我的秘密的有效加密值:

#!/usr/bin/python3

from base64 import b64encode
from nacl import encoding, public

def encrypt(public_key: str, secret_value: str) -> str:
  public_key = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder())
  sealed_box = public.SealedBox(public_key)

  encrypted = sealed_box.encrypt(secret_value.encode("utf-8"))
  return b64encode(encrypted).decode("utf-8")

print(encrypt("abcdHXZ2BrPAFPrZHy1AAct3B12k7BPgxXgdtxcABCo=", "ABCDEF1234"))
$ ./encrypt-secret.py 
ST5Blke5GXO2FyMLUbYAhkmzLKJ3cljd1lI97q028gcrq3XC9aTqPlNzbMQAI5iHoj/70ao0/GOrhg==

但我正在寻找一个 bash 实现。

【问题讨论】:

    标签: bash github encryption command-line-interface libsodium


    【解决方案1】:

    看起来可以使用 github cli 实用程序 (gh) 创建机密,而无需运行 python/nodejs 代码,这看起来像是我一直在寻找的解决方案。 以下是你的做法:

    export DEBIAN_FRONTEND=noninteractive
    curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo gpg --dearmor -o /usr/share/keyrings/githubcli-archive-keyring.gpg && \
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null && \
    sudo apt update && \
    sudo apt-get install -y gh
    
    export GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}" && \
    export SECRET_VALUE_FILE_PATH="your_file_with_secret_value" && \
    gh secret set KUBECONFIG < "$SECRET_VALUE_FILE_PATH"
    

    https://cli.github.com/manual/gh_secret_set

    【讨论】:

      猜你喜欢
      • 2022-12-18
      • 2023-02-24
      • 2021-04-19
      • 2022-01-15
      • 2020-12-13
      • 2023-03-09
      • 2012-05-18
      • 1970-01-01
      • 2021-08-07
      相关资源
      最近更新 更多