-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fd6417
commit ea7a390
Showing
14 changed files
with
267 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,4 @@ PRESET_WORKING_DIR | |
PRESET_SCRIPT | ||
PRESET_ENCRYPTED_URI | ||
PRESET_ENCRYPTED_PASSWORD | ||
PUSH_TOKEN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const sodium = require('libsodium-wrappers') | ||
|
||
const args = process.argv.slice(2); | ||
|
||
if (args.length != 2) { | ||
console.error("args not match", args.length) | ||
return | ||
} | ||
|
||
const key = args[0] | ||
const secret = args[1] | ||
|
||
//Check if libsodium is ready and then proceed. | ||
sodium.ready.then(() => { | ||
// Convert the secret and key to a Uint8Array. | ||
let binkey = sodium.from_base64(key, sodium.base64_variants.ORIGINAL) | ||
let binsec = sodium.from_string(secret) | ||
|
||
// Encrypt the secret using libsodium | ||
let encBytes = sodium.crypto_box_seal(binsec, binkey) | ||
|
||
// Convert the encrypted Uint8Array to Base64 | ||
let output = sodium.to_base64(encBytes, sodium.base64_variants.ORIGINAL) | ||
|
||
// Print the output | ||
console.log(output) | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "playground", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "encrypt-secret.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"libsodium-wrappers": "^0.7.15" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
FLAVOR=$1 | ||
|
||
if [ -z "$FLAVOR" ]; then | ||
echo "FLAVOR must be set." | ||
exit 1 | ||
fi | ||
|
||
# Variables | ||
OWNER="storytellerF" # 替换为仓库的拥有者 | ||
REPO="A" # 替换为你的仓库名 | ||
ENVIRONMENT="$FLAVOR" # 替换为目标环境名 | ||
ENV_FILE="$FLAVOR.env" # .env 文件路径 | ||
PUSH_TOKEN_KEY="PUSH_TOKEN" | ||
PUSH_TOKEN=$(grep "^${PUSH_TOKEN_KEY}=" "$ENV_FILE" | cut -d '=' -f2-) # 替换为你的 GitHub 访问令牌 | ||
|
||
if [ -z "$PUSH_TOKEN" ]; then | ||
echo "PUSH_TOKEN must be set." | ||
exit 1 | ||
fi | ||
|
||
check_and_set_variable() { | ||
# 接收一个参数作为环境变量的键 | ||
local env_key="$1" | ||
|
||
# 定义敏感关键词 | ||
local keywords=("TOKEN" "PASS" "PASSWORD" "KEY") | ||
|
||
# 检查环境变量键是否包含任何敏感关键字 | ||
for keyword in "${keywords[@]}"; do | ||
if [[ $env_key == *"$keyword"* ]]; then | ||
echo "环境变量键 '$env_key' 包含敏感关键字 '$keyword'。" | ||
return 1 # 返回 1 | ||
fi | ||
done | ||
|
||
# 如果没有敏感关键字,返回 0 | ||
echo "环境变量键 '$env_key' 不包含敏感关键字。" | ||
return 0 # 返回 0 | ||
} | ||
|
||
# Function to check if the environment exists | ||
check_environment_exists() { | ||
local env_name="$1" | ||
response=$(curl -fL \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "Authorization: Bearer $PUSH_TOKEN" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
"https://api.github.com/repos/$OWNER/$REPO/environments") | ||
echo "$response" | jq -e ".environments[] | select(.name == \"$env_name\")" > /dev/null | ||
} | ||
|
||
# Check if the environment exists | ||
if check_environment_exists "$ENVIRONMENT"; then | ||
echo "Environment $ENVIRONMENT exists. Proceeding to add secret..." | ||
else | ||
echo "Environment $ENVIRONMENT does not exist. Skipping secrets." | ||
exit 1 | ||
fi | ||
|
||
# Step 1: Get the public key for the environment | ||
PUBLIC_KEY_INFO=$(curl -fsL \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "Authorization: Bearer $PUSH_TOKEN" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
"https://api.github.com/repos/$OWNER/$REPO/environments/$ENVIRONMENT/secrets/public-key") | ||
|
||
PUBLIC_KEY=$(echo "$PUBLIC_KEY_INFO" | jq -r .key) | ||
KEY_ID=$(echo "$PUBLIC_KEY_INFO" | jq -r .key_id) | ||
|
||
echo "$PUBLIC_KEY $KEY_ID" | ||
|
||
# Read the .env file | ||
while IFS='=' read -r secret_name secret_value; do | ||
# Skip empty lines and comments | ||
[[ -z "$secret_name" || "$secret_name" == \#* ]] && continue | ||
if [ -z "$secret_value" ]; then | ||
echo "$secret_name is empty, skip." | ||
continue | ||
fi | ||
|
||
check_and_set_variable $secret_name | ||
result=$? | ||
|
||
# 检查返回结果 | ||
if [ $result -eq 0 ]; then | ||
exists_name=$(curl -sL \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "Authorization: Bearer $PUSH_TOKEN" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
"https://api.github.com/repos/$OWNER/$REPO/environments/$ENVIRONMENT/variables/$secret_name" | jq -r '.name') | ||
if [ "$exists_name" = "$secret_name" ]; then | ||
|
||
curl -L \ | ||
-X PATCH \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "Authorization: Bearer $PUSH_TOKEN" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
-d "{\"name\":\"$secret_name\",\"value\":\"$secret_value\"}" \ | ||
"https://api.github.com/repos/$OWNER/$REPO/environments/$ENVIRONMENT/variables/$secret_name" | ||
else | ||
curl -L \ | ||
-X POST \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "Authorization: Bearer $PUSH_TOKEN" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
-d "{\"name\":\"$secret_name\",\"value\":\"$secret_value\"}" \ | ||
"https://api.github.com/repos/$OWNER/$REPO/environments/$ENVIRONMENT/variables" | ||
fi | ||
else | ||
ENCRYPTED_SECRET=$(node scripts/tool_scripts/encrypt-secret.js $PUBLIC_KEY "$secret_value") | ||
|
||
curl -L -X PUT \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "Authorization: Bearer $PUSH_TOKEN" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
-d "{\"encrypted_value\":\"$ENCRYPTED_SECRET\",\"key_id\":\"$KEY_ID\"}" \ | ||
"https://api.github.com/repos/$OWNER/$REPO/environments/$ENVIRONMENT/secrets/$secret_name" | ||
|
||
echo "Secret $secret_name has been added to environment $ENVIRONMENT." | ||
fi | ||
|
||
sleep 2 | ||
|
||
done < "$ENV_FILE" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
if [ -z "$FLAVOR" ]; then | ||
echo "FLAVOR must set." | ||
exit 1 | ||
fi | ||
export FLAVOR=generated-$FLAVOR | ||
OUTPUT_FILE="$FLAVOR.env" | ||
|
||
# 定义 env-filter 文件名 | ||
env_filter_file="env-filter" | ||
|
||
# 创建或清空 .env 文件 | ||
> $OUTPUT_FILE | ||
|
||
# 遍历 env-filter 文件中的每一行 | ||
while IFS= read -r key; do | ||
# 去除可能的 \r 符号(处理 Windows 换行符) | ||
key=$(echo "$key" | tr -d '\r') | ||
upper_key=$(echo "$key" | tr '[:lower:]' '[:upper:]') | ||
|
||
# 获取对应的环境变量值 | ||
value="${!key}" | ||
# 对反斜杠进行转义 | ||
value=$(echo "$value" | sed 's/\\/\\\\/g') | ||
|
||
# 如果值中包含空格,则用引号包裹 | ||
if echo "$value" | grep -q ' '; then | ||
echo "$upper_key=\"$value\"" >> $OUTPUT_FILE | ||
else | ||
echo "$upper_key=$value" >> $OUTPUT_FILE | ||
fi | ||
done < "$env_filter_file" | ||
|
||
echo "$FLAVOR.env 文件已生成。" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,3 +51,5 @@ PRESET_WORKING_DIR= | |
PRESET_SCRIPT= | ||
PRESET_ENCRYPTED_URI= | ||
PRESET_ENCRYPTED_PASSWORD= | ||
|
||
PUSH_TOKEN= |