Skip to content

Instantly share code, notes, and snippets.

@lewangdev
Last active July 6, 2023 11:26
Show Gist options
  • Select an option

  • Save lewangdev/620ede2156c8c4915d9031c9f4b6a339 to your computer and use it in GitHub Desktop.

Select an option

Save lewangdev/620ede2156c8c4915d9031c9f4b6a339 to your computer and use it in GitHub Desktop.

Revisions

  1. lewangdev revised this gist Jan 16, 2023. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion qiniu_sslcert.py
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,8 @@
    #
    ## Use https://github.com/acmesh-official/acme.sh to issue a let's encrypt cert
    ## More info plz visit https://github.com/acmesh-official/acme.sh/wiki
    # docker run --rm -v /data/var/lib/acme.sh:/acme.sh neilpang/acme.sh --issue --dns dns_ali --log -k 4096 --force --dnssleep 300 \
    # docker run --rm -v /data/var/lib/acme.sh:/acme.sh neilpang/acme.sh \
    # --issue --dns dns_ali --log -k 4096 --force --dnssleep 300 \
    # -d ${DOMAIN} \
    # -d *.{DOMAIN}
    #
  2. lewangdev renamed this gist Jan 16, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sslcert.py → qiniu_sslcert.py
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@
    #
    # rm -rf ./${DOMAIN} | true
    # cp -R /data/var/lib/acme.sh/${DOMAIN} ./
    # python qiniu_ssl_tool.py
    # python qiniu_sslcert.py

    import qiniu
    from qiniu import DomainManager
  3. lewangdev created this gist Jan 16, 2023.
    71 changes: 71 additions & 0 deletions sslcert.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    #!/usr/bin/env python

    ## Dependencies:
    #
    # pip install qiniu
    #
    ## Tips for Deploy certs to Qiniu:
    #
    # export DOMAIN=lewangdev.com
    # export QINIU_DOMAIN=images.${DOMAIN},staticfiles.${DOMAIN}
    # export QINIU_ACCESS_KEY=
    # export QINIU_SECRET_KEY=
    #
    ## Use https://github.com/acmesh-official/acme.sh to issue a let's encrypt cert
    ## More info plz visit https://github.com/acmesh-official/acme.sh/wiki
    # docker run --rm -v /data/var/lib/acme.sh:/acme.sh neilpang/acme.sh --issue --dns dns_ali --log -k 4096 --force --dnssleep 300 \
    # -d ${DOMAIN} \
    # -d *.{DOMAIN}
    #
    # rm -rf ./${DOMAIN} | true
    # cp -R /data/var/lib/acme.sh/${DOMAIN} ./
    # python qiniu_ssl_tool.py

    import qiniu
    from qiniu import DomainManager
    import os
    import time
    import logging

    LOGGER = logging.getLogger(__name__)


    def config_logger():
    LOGGER.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    LOGGER.addHandler(ch)


    if __name__ == "__main__":
    config_logger()

    domain_name = os.getenv('DOMAIN', '')
    access_key = os.getenv('QINIU_ACCESS_KEY', '')
    secret_key = os.getenv('QINIU_SECRET_KEY', '')
    qiniu_domain_names_string = os.getenv('QINIU_DOMAIN', '')

    auth = qiniu.Auth(access_key=access_key, secret_key=secret_key)
    domain_manager = DomainManager(auth)

    privatekey = "{}/{}.key".format(domain_name, domain_name)
    ca = "{}/fullchain.cer".format(domain_name)

    with open(privatekey, 'r') as f:
    privatekey_str = f.read()

    with open(ca, 'r') as f:
    ca_str = f.read()

    ret, info = domain_manager.create_sslcert("{}/{}".format(domain_name, time.strftime("%Y-%m-%d", time.localtime())),
    domain_name, privatekey_str, ca_str)
    LOGGER.info("CertId: %s", ret['certID'])
    cert_id = ret['certID']

    qiniu_domain_names = qiniu_domain_names_string.split(",")
    for qiniu_domain_name in qiniu_domain_names:
    LOGGER.info("Deploy ssl cert to domain=%s", qiniu_domain_name)
    ret, info = domain_manager.put_httpsconf(qiniu_domain_name, cert_id, False)
    LOGGER.info("Ret: %s", info)