-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
15 changed files
with
4,776 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env bash | ||
# shellcheck disable=SC2034 | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
# 定义全局命令变量 | ||
CMD_DATE=$(command -v gdate || command -v date) | ||
CMD_GREP=$(command -v ggrep || command -v grep) | ||
CMD_SED=$(command -v gsed || command -v sed) | ||
CMD_READLINK=$(command -v greadlink || command -v readlink) | ||
CMD_CURL=$(command -v /usr/local/opt/curl/bin/curl || command -v curl) | ||
|
||
## 定义全局执行所在目录 | ||
SCRIPT_DIR=$(dirname "$($CMD_READLINK -f "${BASH_SOURCE[0]}")") | ||
## 定义 lib 目录 | ||
SCRIPT_LIB="$(dirname "${SCRIPT_DIR}")/lib" | ||
# 定义通用数据目录 | ||
SCRIPT_DATA_DIR="$(dirname "${SCRIPT_DIR}")/data" | ||
|
||
# 主函数 | ||
main() { | ||
# 导入其他脚本 | ||
for file in "${SCRIPT_LIB}"/aliyun/*.sh; do | ||
# shellcheck source=/dev/null | ||
source "$file" | ||
done | ||
|
||
check_dependencies | ||
|
||
local profile="default" | ||
local region="" | ||
local args=() | ||
local i=0 | ||
|
||
# 解析参数 | ||
while [[ $# -gt 0 ]]; do | ||
case "$1" in | ||
-p | --profile) | ||
if [[ -z "$2" || "$2" == -* ]]; then | ||
echo "错误:--profile 选项需要指定一个配置名称" >&2 | ||
return 1 | ||
fi | ||
profile="$2" | ||
shift | ||
;; | ||
-r | --region) | ||
if [[ -z "$2" || "$2" == -* ]]; then | ||
echo "错误:--region 选项需要指定一个地域" >&2 | ||
return 1 | ||
fi | ||
region="$2" | ||
shift | ||
;; | ||
*) | ||
args[i]="$1" | ||
((i++)) | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
# 如果没有指定 region,则从配置文件中读取,如果配置文件中也没有��则使用默认值 "cn-hangzhou" | ||
region=${region:-$(read_config "$profile")} | ||
region=${region:-"cn-hangzhou"} | ||
|
||
if [ ${#args[@]} -lt 1 ]; then | ||
show_help | ||
return 1 | ||
fi | ||
|
||
local service=${args[0]} | ||
unset 'args[0]' | ||
args=("${args[@]}") # 重新索引数组 | ||
|
||
# 显示当前配置 | ||
# echo "当前配置: Profile==$profile , Region==$region" | ||
|
||
case "$service" in | ||
list-all) list_all_services ;; | ||
config) handle_config_commands "${args[@]}" || show_config_help ;; | ||
balance) handle_balance_commands "${args[@]}" || show_balance_help ;; | ||
cost) handle_cost_commands "${args[@]}" || show_cost_help ;; | ||
ecs) handle_ecs_commands "${args[@]}" || show_ecs_help ;; | ||
dns) handle_dns_commands "${args[@]}" || show_dns_help ;; | ||
domain) handle_domain_commands "${args[@]}" || show_domain_help ;; | ||
cdn) handle_cdn_commands "${args[@]}" || show_cdn_help ;; | ||
oss) handle_oss_commands "${args[@]}" || show_oss_help ;; | ||
lbs) handle_lbs_commands "${args[@]}" || show_lbs_help ;; | ||
rds) handle_rds_commands "${args[@]}" || show_rds_help ;; | ||
kvstore) handle_kvstore_commands "${args[@]}" || show_kvstore_help ;; | ||
vpc) handle_vpc_commands "${args[@]}" || show_vpc_help ;; | ||
nat) handle_nat_commands "${args[@]}" || show_nat_help ;; | ||
eip) handle_eip_commands "${args[@]}" || show_eip_help ;; | ||
cas) handle_cas_commands "${args[@]}" || show_cas_help ;; | ||
ram) handle_ram_commands "${args[@]}" || show_ram_help ;; | ||
*) echo "错误:未知的服务:$service" >&2 && show_help && exit 1 ;; | ||
esac | ||
} | ||
|
||
# 运行主函数 | ||
main "$@" |
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,188 @@ | ||
#!/usr/bin/env bash | ||
# -*- coding: utf-8 -*- | ||
|
||
# 证书服务(Certificate Authority Service)相关函数 | ||
|
||
# 使用通用数据目录 | ||
CAS_CERT_FILE="${SCRIPT_DATA_DIR}/cas/cas_certs.json" | ||
|
||
show_cas_help() { | ||
echo "证书服务 (Certificate Authority Service) 操作:" | ||
echo " list - 列出所有已上传的证书" | ||
echo " create <证书名称> <证书文件> <私钥文件> - 上传并创建新证书" | ||
echo " delete <证书ID> - 删除指定证书" | ||
echo " detail <证书ID> - 获取证书详情" | ||
echo | ||
echo "示例:" | ||
echo " $0 cas list" | ||
echo " $0 cas create my-cert /path/to/cert.pem /path/to/key.pem" | ||
echo " $0 cas delete 15246052" | ||
echo " $0 cas detail 15246052" | ||
} | ||
|
||
handle_cas_commands() { | ||
local operation=${1:-list} | ||
shift | ||
|
||
case "$operation" in | ||
list) cas_list "$@" ;; | ||
create) cas_create "$@" ;; | ||
delete) cas_delete "$@" ;; | ||
detail) cas_detail "$@" ;; | ||
*) | ||
echo "错误:未知的证书服务操作:$operation" >&2 | ||
show_cas_help | ||
exit 1 | ||
;; | ||
esac | ||
} | ||
|
||
cas_list() { | ||
local format=${1:-human} | ||
local result | ||
|
||
if [ -f "$CAS_CERT_FILE" ]; then | ||
result=$(jq -r '.[] | [.CertId, .Name, .UploadTime] | @tsv' "$CAS_CERT_FILE") | ||
else | ||
result="" | ||
fi | ||
|
||
case "$format" in | ||
json) ##此处非标准化数据不需要变更代码 | ||
if [ -n "$result" ]; then | ||
echo "$result" | jq -R -s ' | ||
split("\n") | | ||
map(select(length > 0) | split("\t")) | | ||
map({"CertId": .[0], "Name": .[1], "UploadTime": .[2]}) | ||
' | ||
else | ||
echo "[]" | ||
fi | ||
;; | ||
tsv) | ||
echo -e "CertId\tName\tUploadTime" | ||
if [ -n "$result" ]; then | ||
echo "$result" | jq -r '.[] | [.CertId, .Name, .UploadTime] | @tsv' | ||
fi | ||
;; | ||
human|*) | ||
echo "列出所有已上传的证书:" | ||
if [ -n "$result" ]; then | ||
echo "证书ID 名称 上传时间" | ||
echo "---------------- ---------------------------- -------------------------" | ||
echo "$result" | jq -r '.[] | [.CertId, .Name, .UploadTime] | @tsv' | | ||
awk 'BEGIN {FS="\t"; OFS="\t"} | ||
{printf "%-16s %-28s %s\n", $1, $2, $3}' | ||
else | ||
echo "没有找到已上传的证书记录。" | ||
fi | ||
;; | ||
esac | ||
log_result "${profile:-}" "${region:-}" "cas" "list" "$result" "$format" | ||
} | ||
|
||
cas_create() { | ||
local name=$1 | ||
local cert_file=$2 | ||
local key_file=$3 | ||
|
||
if [ -z "$name" ] || [ -z "$cert_file" ] || [ -z "$key_file" ]; then | ||
echo "错误:缺少必要参数。用法:$0 cas create <证书名称> <证书文件> <私钥文件>" >&2 | ||
return 1 | ||
fi | ||
|
||
if [ ! -f "$cert_file" ] || [ ! -f "$key_file" ]; then | ||
echo "错误:证书文件或私钥文件不存在。" >&2 | ||
return 1 | ||
fi | ||
|
||
echo "上传并创建新证书:" | ||
local result | ||
result=$(aliyun --profile "${profile:-}" cas UploadUserCertificate \ | ||
--Name "$name" \ | ||
--Cert "$(cat "$cert_file")" \ | ||
--Key "$(cat "$key_file")") | ||
|
||
if [ $? -eq 0 ]; then | ||
echo "证书创建成功:" | ||
echo "$result" | jq '.' | ||
local cert_id | ||
cert_id=$(echo "$result" | jq -r '.CertId') | ||
local upload_time | ||
upload_time=$($CMD_DATE "+%Y-%m-%d %H:%M:%S") | ||
|
||
# 确保目录存在 | ||
mkdir -p "$(dirname "$CAS_CERT_FILE")" | ||
|
||
# 将新证书信息添加到本地文件 | ||
if [ -f "$CAS_CERT_FILE" ]; then | ||
jq --arg id "$cert_id" --arg name "$name" --arg time "$upload_time" \ | ||
'. += [{"CertId": $id, "Name": $name, "UploadTime": $time}]' "$CAS_CERT_FILE" > "${CAS_CERT_FILE}.tmp" && | ||
mv "${CAS_CERT_FILE}.tmp" "$CAS_CERT_FILE" | ||
else | ||
echo '[{"CertId": "'"$cert_id"'", "Name": "'"$name"'", "UploadTime": "'"$upload_time"'"}]' > "$CAS_CERT_FILE" | ||
fi | ||
else | ||
echo "错误:证书创建失败。" | ||
echo "$result" | ||
fi | ||
log_result "${profile:-}" "${region:-}" "cas" "create" "$result" | ||
} | ||
|
||
cas_delete() { | ||
local cert_id=$1 | ||
|
||
if [ -z "$cert_id" ]; then | ||
echo "错误:缺少证书ID。用法:$0 cas delete <证书ID>" >&2 | ||
return 1 | ||
fi | ||
|
||
echo "警告:您即将删除证书 ID: $cert_id" | ||
read -r -p "请输入 'YES' 以确认删除操作: " confirm | ||
|
||
if [ "$confirm" != "YES" ]; then | ||
echo "操作已取消。" | ||
return 1 | ||
fi | ||
|
||
echo "删除证书:" | ||
local result | ||
result=$(aliyun --profile "${profile:-}" cas DeleteUserCertificate --CertId "$cert_id") | ||
|
||
if [ $? -eq 0 ]; then | ||
echo "证书删除成功。" | ||
# 从本地文件中删除证书信息 | ||
if [ -f "$CAS_CERT_FILE" ]; then | ||
jq --arg id "$cert_id" 'map(select(.CertId != $id))' "$CAS_CERT_FILE" > "${CAS_CERT_FILE}.tmp" && | ||
mv "${CAS_CERT_FILE}.tmp" "$CAS_CERT_FILE" | ||
fi | ||
log_delete_operation "${profile:-}" "${region:-}" "cas" "$cert_id" "证书" "成功" | ||
else | ||
echo "错误:证书删除失败。" | ||
echo "$result" | ||
log_delete_operation "${profile:-}" "${region:-}" "cas" "$cert_id" "证书" "失败" | ||
fi | ||
|
||
log_result "${profile:-}" "${region:-}" "cas" "delete" "$result" | ||
} | ||
|
||
cas_detail() { | ||
local cert_id=$1 | ||
|
||
if [ -z "$cert_id" ]; then | ||
echo "错误:缺少证书ID。用法:$0 cas detail <证书ID>" >&2 | ||
return 1 | ||
fi | ||
|
||
echo "获取证书详情:" | ||
local result | ||
result=$(aliyun --profile "${profile:-}" cas GetUserCertificateDetail --CertId "$cert_id") | ||
|
||
if [ $? -eq 0 ]; then | ||
echo "$result" | jq '.' | ||
else | ||
echo "错误:无法获取证书详情。" | ||
echo "$result" | ||
fi | ||
log_result "${profile:-}" "${region:-}" "cas" "detail" "$result" | ||
} |
Oops, something went wrong.