{"id":2099,"date":"2025-12-03T22:35:55","date_gmt":"2025-12-03T14:35:55","guid":{"rendered":"https:\/\/huzi-baozi.com\/?p=2099"},"modified":"2025-12-03T22:35:55","modified_gmt":"2025-12-03T14:35:55","slug":"%e9%87%91%e5%b1%b1%e4%ba%91slb%e5%9f%9f%e5%90%8d%ef%bc%88cloudflare%e6%89%98%e7%ae%a1%ef%bc%89%e8%af%81%e4%b9%a6%e8%87%aa%e5%8a%a8%e6%9b%b4%e6%96%b0%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/huzi-baozi.com\/?p=2099","title":{"rendered":"\u91d1\u5c71\u4e91SLB\u57df\u540d\uff08cloudflare\u6258\u7ba1\uff09\u8bc1\u4e66\u81ea\u52a8\u66f4\u65b0\u8bb0\u5f55"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u6574\u4f53\u6b65\u9aa4<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5229\u7528certbot\u521b\u5efa\/\u7eed\u671f\u8bc1\u4e66<\/li>\n\n\n\n<li>\u5229\u7528\u91d1\u5c71\u4e91API\u63a5\u53e3\u66f4\u65b0\u8bc1\u4e66<\/li>\n<\/ol>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">\u73af\u5883\u51c6\u5907<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Python\u22653.7<\/li>\n\n\n\n<li>snap\u5b89\u88c5\u7ba1\u7406certbot\u4ee5\u53ca\u63d2\u4ef6<\/li>\n<\/ol>\n\n\n\n<p>\u5b89\u88c5certbot\u4ee5\u53ca\u63d2\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo snap install --classic certbot\nsudo snap install certbot-dns-cloudflare\n\n\u5b89\u88c5ksyun\u7684SDK\n<code>pip install --upgrade kingsoftcloud-sdk-python<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u521b\u5efa\u8bc1\u4e66<\/h2>\n\n\n\n<p><code>sudo certbot certonly --key-type rsa --rsa-key-size 2048 --dns-cloudflare --dns-cloudflare-credentials \/etc\/letsencrypt\/cloudflare.int --dns-cloudflare-propagation-seconds 60 -d huzi-baozi.com<\/code><\/p>\n\n\n\n<p>\u4e3b\u8981\u662f<code>--dns-cloudflare<\/code> \u63d2\u4ef6\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3acloudflare\u4ee3\u7406\u300180\u7aef\u53e3\u88ab\u5360\u3001SLB\u6ca1\u5f0080\uff0c\u6240\u4ee5\u7528DNS\u9a8c\u8bc1\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bc1\u4e66\u7eed\u671f<\/h2>\n\n\n\n<p><code>sudo certbot renew<\/code><\/p>\n\n\n\n<p>\u6709\u4e09\u4e2a\u94a9\u5b50\u53c2\u6570<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--pre-hook<\/code>\uff1a\u66f4\u65b0\u8bc1\u4e66\u4e4b\u524d\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--post-hook<\/code>\uff1a\u66f4\u65b0\u8bc1\u4e66\u4e4b\u540e\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--deploy-hook<\/code>\uff1a\u6210\u529f\u66f4\u65b0\u8bc1\u4e66\u4e4b\u540e\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<p>\u914d\u7f6e\u6587\u4ef6\u4f4d\u4e8e<\/p>\n\n\n\n<p><code>\/etc\/letsencrypt\/renewal\/huzi-baozi.com.conf<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u91d1\u5c71\u4e91\u66f4\u65b0\u8bc1\u4e66<\/h2>\n\n\n\n<p><a href=\"https:\/\/apiexplorer.ksyun.com\/#\/api\/96\/ModifyCertificate\/2016-03-04\/1013\" data-type=\"link\" data-id=\"https:\/\/apiexplorer.ksyun.com\/#\/api\/96\/ModifyCertificate\/2016-03-04\/1013\">API\u94fe\u63a5<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7279\u522b\u6ce8\u610f\uff0c\u6765\u6765\u56de\u56de\u8c03\u7528\u90fd\u8fc7\u4e0d\u4e86\uff0c\u56e0\u4e3a\u8981\u628acert.pem\u548cprivkey.pem\u7684\u6362\u884c\u90fd\u6539\u4e3a\\n<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\n# -*- coding: utf-8 -*-\n\nimport os\nimport sys\nimport json\nfrom ksyun.common import credential\nfrom ksyun.common.profile.client_profile import ClientProfile\nfrom ksyun.common.profile.http_profile import HttpProfile\nfrom ksyun.common.exception.ksyun_sdk_exception import KsyunSDKException\n# \u5bfc\u5165 KCM \u5ba2\u6237\u7aef\u548c\u6a21\u578b\nfrom ksyun.client.kcm.v20160304 import client as kcm_client, models as kcm_models\n\n# ================= \u914d\u7f6e\u533a\u57df =================\n\n# 1. \u91d1\u5c71\u4e91 API \u51ed\u8bc1 (\u63a8\u8350\u4f7f\u7528\u73af\u5883\u53d8\u91cf)\n# \u5982\u679c\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\uff0c\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u8bf7\u66ff\u6362\u4e3a\u60a8\u7684\u5b9e\u9645\u5bc6\u94a5\nKSYUN_ACCESS_KEY = os.environ.get(\"KSYUN_SECRET_ID\", \"\u9ed8\u8ba4\")\nKSYUN_SECRET_KEY = os.environ.get(\"KSYUN_SECRET_KEY\", \"\u9ed8\u8ba4\")\nREGION = 'cn-beijing-6'  # KCM \u670d\u52a1\u6ca1\u6709\u5730\u57df\u9650\u5236\uff0c\u4f46 SDK \u9700\u8981\u6307\u5b9a\u4e00\u4e2a\n\n# 2. \u76ee\u6807\u8bc1\u4e66 ID (\u8bf7\u586b\u5165\u60a8 KCM \u4e2d\u9700\u8981\u66f4\u65b0\u7684\u8bc1\u4e66 ID)\n# \u811a\u672c\u5c06\u76f4\u63a5\u8986\u76d6\u8fd9\u4e2a ID \u4e0b\u7684\u8bc1\u4e66\u5185\u5bb9\nCERTIFICATE_ID = '\u4f60\u7684\u8bc1\u4e66ID' \n\n# 3. Certbot \u8bc1\u4e66\u8def\u5f84\n# \u8bf7\u4fee\u6539 DOMAIN_NAME \u4e3a\u60a8\u7684\u57df\u540d\u76ee\u5f55\u540d\nDOMAIN_NAME = '\u4f60\u7684\u57df\u540d'\nCERT_PATH = f'\/etc\/letsencrypt\/live\/{DOMAIN_NAME}\/cert.pem'\nKEY_PATH = f'\/etc\/letsencrypt\/live\/{DOMAIN_NAME}\/privkey.pem'\n\n# ===========================================\n\ndef read_file_content(file_path):\n    \"\"\"\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\"\"\"\n    if not os.path.exists(file_path):\n        print(f\"Error: \u6587\u4ef6\u4e0d\u5b58\u5728: {file_path}\")\n        sys.exit(1)\n    try:\n        with open(file_path, 'r') as f:\n            return f.read()\n    except Exception as e:\n        print(f\"Error: \u65e0\u6cd5\u8bfb\u53d6\u6587\u4ef6 {file_path}. \u9519\u8bef: {e}\")\n        sys.exit(1)\n\ndef get_kcm_client():\n    \"\"\"\u521d\u59cb\u5316 KCM \u5ba2\u6237\u7aef\"\"\"\n    if KSYUN_ACCESS_KEY == \"KSYUN_SECRET_ID_HERE\" or KSYUN_SECRET_KEY == \"KSYUN_SECRET_KEY_HERE\":\n        print(\"Error: \u8bf7\u5728\u811a\u672c\u4e2d\u6216\u73af\u5883\u53d8\u91cf\u4e2d\u914d\u7f6e\u6b63\u786e\u7684 KSYUN_SECRET_ID \u548c KSYUN_SECRET_KEY\u3002\")\n        sys.exit(1)\n        \n    cred = credential.Credential(KSYUN_ACCESS_KEY, KSYUN_SECRET_KEY)\n    \n    httpProfile = HttpProfile()\n    httpProfile.endpoint = \"kcm.api.ksyun.com\"  # KCM API \u7aef\u70b9\n    httpProfile.reqMethod = \"POST\"\n    httpProfile.reqTimeout = 60\n    httpProfile.scheme = \"http\" # \u4fdd\u6301\u4e0e\u5b98\u65b9\u6848\u4f8b\u4e00\u81f4\n    \n    clientProfile = ClientProfile()\n    clientProfile.httpProfile = httpProfile\n    \n    # KCMClient \u4f7f\u7528 kcm_client.KcmClient\n    return kcm_client.KcmClient(cred, REGION, profile=clientProfile)\n\ndef modify_certificate_content(client, cert_id, cert_content, key_content):\n    \"\"\"\u8c03\u7528 ModifyCertificate \u63a5\u53e3\u76f4\u63a5\u66f4\u65b0 KCM \u8bc1\u4e66\u5185\u5bb9\"\"\"\n    print(f\"\u6b63\u5728\u66f4\u65b0 KCM \u8bc1\u4e66 ID: {cert_id} ...\")\n    \n    # \u6784\u9020\u8bf7\u6c42\u53c2\u6570\u5b57\u5178\n    params = {\n        \"CertificateId\": cert_id,\n        \"PublicKey\": cert_content.replace('\\n', '\\\\n'),\n        \"PrivateKey\": key_content.replace('\\n', '\\\\n')\n    }\n    \n    try:\n        # \u4f7f\u7528 call \u65b9\u6cd5\u6267\u884c API \u8c03\u7528\n        resp = client.call(\"ModifyCertificate\", params)\n        resp_json = json.loads(resp)\n        \n        # KCM ModifyCertificate \u63a5\u53e3\u8fd4\u56de\u4e3a\u7a7a\uff0c\u6210\u529f\u5373\u8868\u793a\u66f4\u65b0\u5b8c\u6210\n        print(f\"\u6210\u529f: KCM \u8bc1\u4e66 {cert_id} \u5185\u5bb9\u5df2\u66f4\u65b0\u3002\u6240\u6709\u5173\u8054\u7684\u8d1f\u8f7d\u5747\u8861\u6216\u670d\u52a1\u5c06\u81ea\u52a8\u4f7f\u7528\u65b0\u8bc1\u4e66\u3002\")\n        \n    except KsyunSDKException as err:\n        print(f\"\u66f4\u65b0 KCM \u8bc1\u4e66\u5931\u8d25: {err}\")\n        # \u5982\u679c\u62a5\u9519\uff0c\u68c0\u67e5 KCM \u63a5\u53e3\u6587\u6863\u786e\u8ba4\u53c2\u6570\u540d\u662f\u5426\u4e3a PublicKey \u6216 CertificateContent\n        sys.exit(1)\n\ndef main():\n    # 1. \u8bfb\u53d6\u672c\u5730\u6700\u65b0\u8bc1\u4e66\n    print(f\"\u8bfb\u53d6\u672c\u5730\u8bc1\u4e66: {DOMAIN_NAME}\")\n    cert_content = read_file_content(CERT_PATH)\n    key_content = read_file_content(KEY_PATH)\n#print(cert_content )\n\n    # 2. \u521d\u59cb\u5316\u5ba2\u6237\u7aef\n    client = get_kcm_client()\n\n    # 3. \u6267\u884c\u66f4\u65b0\n    modify_certificate_content(client, CERTIFICATE_ID, cert_content, key_content)\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u65f6\u4efb\u52a1<\/h2>\n\n\n\n<p>\u53ef\u4ee5\u5199\u4e00\u4e2acron\u4efb\u52a1\uff0c<code>certbot renew --deploy-hook<\/code>\u6765\u66f4\u65b0\u91d1\u5c71\u4e91\u8bc1\u4e66\uff0c\u7136\u540e\u53d1\u9001\u6d88\u606f\u4e4b\u7c7b\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6574\u4f53\u6b65\u9aa4<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2099","post","type-post","status-publish","format-standard","hentry","category-snippet"],"_links":{"self":[{"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/posts\/2099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2099"}],"version-history":[{"count":3,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/posts\/2099\/revisions"}],"predecessor-version":[{"id":2102,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=\/wp\/v2\/posts\/2099\/revisions\/2102"}],"wp:attachment":[{"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/huzi-baozi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}