Có nhiều cách để triển khai domain lên DCDN của Alibaba Cloud. Triển khai thông qua giao diện web console có thể thao tác trực quan và tiện lợi, nhưng để tự động hóa quá trình triển khai, hoặc muốn triển khai đồng loạt nhiều domain lên DCDN một cách nhanh chóng, thì có thể dùng các API do Alibaba Cloud cung cấp. Trong series này, mình thử nghiệm triển khai DCDN thông qua Python SDK, đồng thời tự động enable HTTPS traffic với domain đã triển khai qua việc tự động cấp Free SSL từ Let's encrypt (Alibaba Cloud có cung cấp SSL nhưng không có bản miễn phí).
Trước khi đến với DCDN, mình xin bắt đầu với việc đăng ký một chứng chỉ SSL miễn phí từ Let's encrypt qua ACME và tự động trỏ dns record để xác thực tên miền qua Alibaba Cloud DNS. Để chuẩn bị cho việc enable HTTPS trên DCDN.
def GenAccountKey(FileName):
account_key = rsa.generate_private_key(public_exponent=65537,key_size=2048,backend=default_backend())
with open(FileName, "wb") as f:
f.write(account_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.BestAvailableEncryption(b"passx"),))
def GetACMEAccount(emailAddress,staging):
if staging==True:
path='https://acme-staging-v02.api.letsencrypt.org/directory'
else:
path='https://acme-v02.api.letsencrypt.org/directory'
with open("Accout_Key", "rb") as keyfile:
key_data=keyfile.read()
pkey=serialization.load_pem_private_key(key_data,b"passx")
acc_key = jose.JWKRSA(key=pkey)
net = client.ClientNetwork(acc_key, user_agent="self-test")
directory = client.ClientV2.get_directory(path, net)
client_acme = client.ClientV2(directory, net=net)
try:
regr=client_acme.new_account(messages.NewRegistration.from_data(email=emailAddress, terms_of_service_agreed=True))
return client_acme
except acme_errors.ConflictError as e:
print(e)
return client_acme
def genCSR(domainName, email_address):
""" Generate a certificate signing request """
emailAddress=email_address
KEY_FILE = domainName + '.key'
CSR_FILE = domainName + '.csr'
key = rsa.generate_private_key(public_exponent=65537,key_size=2048,)
with open(KEY_FILE, "wb") as f:
f.write(key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
))
with open(KEY_FILE, "rb") as keyfile:
key_data=keyfile.read()
pkey=serialization.load_pem_private_key(key_data,None)
csr = x509.CertificateSigningRequestBuilder().subject_name(x509.Name([
# Provide various details about who we are.
x509.NameAttribute(NameOID.COUNTRY_NAME, "VN"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "HCM"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "HCM"),
x509.NameAttribute(NameOID.EMAIL_ADDRESS, emailAddress),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Client"),
x509.NameAttribute(NameOID.COMMON_NAME, domainName),
])).add_extension(x509.SubjectAlternativeName([x509.DNSName(domainName)]), critical=False,
).sign(pkey, hashes.SHA256())
with open(CSR_FILE, "wb") as f:
f.write(csr.public_bytes(serialization.Encoding.PEM))
return pkey,csr
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from acme import errors as acme_errors
from acme import messages, client, crypto_util, challenges, jose
def GenAccountKey(FileName):
account_key = rsa.generate_private_key(public_exponent=65537,key_size=2048,backend=default_backend())
with open(FileName, "wb") as f:
f.write(account_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.BestAvailableEncryption(b"passx"),))
def GetACMEAccount(emailAddress,staging):
if staging==True:
path='https://acme-staging-v02.api.letsencrypt.org/directory'
else:
path='https://acme-v02.api.letsencrypt.org/directory'
with open("Accout_Key", "rb") as keyfile:
key_data=keyfile.read()
pkey=serialization.load_pem_private_key(key_data,b"passx")
acc_key = jose.JWKRSA(key=pkey)
net = client.ClientNetwork(acc_key, user_agent="self-test")
directory = client.ClientV2.get_directory(path, net)
client_acme = client.ClientV2(directory, net=net)
try:
regr=client_acme.new_account(messages.NewRegistration.from_data(email=emailAddress, terms_of_service_agreed=True))
return client_acme
except acme_errors.ConflictError as e:
print(e)
return client_acme
def genCSR(domainName, email_address):
""" Generate a certificate signing request """
emailAddress=email_address
KEY_FILE = domainName + '.key'
CSR_FILE = domainName + '.csr'
key = rsa.generate_private_key(public_exponent=65537,key_size=2048,)
with open(KEY_FILE, "wb") as f:
f.write(key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
))
with open(KEY_FILE, "rb") as keyfile:
key_data=keyfile.read()
pkey=serialization.load_pem_private_key(key_data,None)
csr = x509.CertificateSigningRequestBuilder().subject_name(x509.Name([
# Provide various details about who we are.
x509.NameAttribute(NameOID.COUNTRY_NAME, "VN"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "HCM"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "HCM"),
x509.NameAttribute(NameOID.EMAIL_ADDRESS, emailAddress),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Client"),
x509.NameAttribute(NameOID.COMMON_NAME, domainName),
])).add_extension(x509.SubjectAlternativeName([x509.DNSName(domainName)]), critical=False,
).sign(pkey, hashes.SHA256())
with open(CSR_FILE, "wb") as f:
f.write(csr.public_bytes(serialization.Encoding.PEM))
return pkey,csr
def main():
print("===========START-MAIN==============")
genAccoutKey()
acme_client=getACMEAccount('tem@gmail.com',True)
pkey, csr= genCSR('vinahost.cloud','tempx@gmail.com')
main()
5 posts | 1 followers
FollowNguyen Phuc Khang - June 4, 2024
Nguyen Phuc Khang - June 4, 2024
Regional Content Hub - July 14, 2025
Regional Content Hub - July 28, 2025
Regional Content Hub - August 29, 2024
Regional Content Hub - August 29, 2024
5 posts | 1 followers
Follow
CDN(Alibaba Cloud CDN)
A scalable and high-performance content delivery service for accelerated distribution of content to users across the globe
Learn More
OpenAPI Explorer
OpenAPI Explorer allows you to call an API through its web interface or WebCLI, and view the entire process.
Learn More
API Gateway
API Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.
Learn More
AgentBay
Multimodal cloud-based operating environment and expert agent platform, supporting automation and remote control across browsers, desktops, mobile devices, and code.
Learn More