首页 > 解决方案 > 生成与 GCP 兼容的 Cloudflare SSL 证书

问题描述

我正在尝试为 Google Cloud 的 App Engine 配置 SSL。您可以在 GCP 中上传您自己的自定义 SSL 证书 + 私钥(见截图)。

我正在将 Cloudflare 用于 DNS,并希望在 Cloudflare 中使用“完整(严格)”SSL 策略。这意味着我必须在 GCP 中添加由 Cloudflare 创建的证书和密钥(在同一屏幕截图中)。

我已设法将私钥转换为 GCP 将接受的有效 (PEM) 格式。唯一不起作用的是证书部分。

在GCP中同时输入证书和密钥,点击上传,返回如下错误:

The certificate data is invalid. Please ensure that the private key and public certificate match.

经过一番谷歌搜索,我在 GCP 的文档中找到了以下资源,解释说我需要在证书字段中上传完整的证书链。因此,我尝试的下一件事是将来自 cloudflare 的证书与 cloudflare 本身的根证书连接在一起如 GCP 文档中所述所以我运行了以下命令来创建这个链:

cat domain.crt cloudflare-root-ca.crt > concat.crt

...并在证书字段中将其上传到 GCP 中。

这也不起作用,即使私钥和证书链的校验和如GCP docs 所解释的那样匹配,通过运行:

openssl x509 -noout -modulus -in concat.crt | openssl md5
openssl rsa -noout -modulus -in myserver.key.pem | openssl md5

...并比较 md5 输出。

所以现在我完全没有Idea。GCP 的错误消息仅限于上述一条,还有一条说您的 PK 格式无效。

通过 CLI (gcloud) 尝试时出现同样的问题

通过 cli 尝试此过程时gcloud,我们会遇到相同的错误。尝试以下命令:

gcloud app ssl-certificates create --display-name example.com --certificate ./cloudflare-concat.crt  --private-key cloudflare-pk.key

...产生以下错误:

ERROR: (gcloud.app.ssl-certificates.create) 
INVALID_ARGUMENT: Invalid certificate. 
`CertificateRawData` must contain a PEM encoded x.509 public key certificate, with header and footer included, 
and an unencrypted PEM encoded RSA private key, with header and footer included and with size at most 2048 bits. 
The requested private key and public certificate must match.

非常感谢在 GCP 中上传有效证书(来自 Cloudflare)和私钥的任何帮助。

更新 1

我发现这个(缓存的)页面描述了 Cloudflare 的所有根证书和中间证书。我已经尝试了其中的几个来连接我的域证书,但到目前为止还没有运气。也不清楚使用哪一个...

更新 2

我开始认为这永远行不通。因为我使用的是 Cloudflare 的“原始证书”,所以我相信这是 Cloudflare 本身的自签名证书,这意味着 App Engine 永远不会认为这是有效的。

我认为是这种情况的原因是因为我尝试使用cfsslcloudflare 中的工具自动“创建捆绑证书”。我从跑步中得到的回应

cfssl bundle -cert domain.crt

返回以下结果:

[INFO] bundling certificate for {Country:[] Organization:[CloudFlare, Inc.] OrganizationalUnit:[CloudFlare Origin CA] Locality:[] Province:[] StreetAddress:[] PostalCode:[] SerialNumber: CommonName:CloudFlare Origin Certificate Names:[{Type:2.5.4.10 Value:CloudFlare, Inc.} {Type:2.5.4.11 Value:CloudFlare Origin CA} {Type:2.5.4.3 Value:CloudFlare Origin Certificate}] ExtraNames:[]}
{"code":1220,"message":"x509: certificate signed by unknown authority"}

额外信息

标签: google-app-enginesslgoogle-cloud-platformopensslcloudflare

解决方案


我发现了最近的一篇博文,描述了如何解决这个问题。按照描述的步骤操作后,证书在 Google Cloud 中被接受,并且一切都适用于 cloudflare 中的“完整(严格)ssl”选项!

简而言之:它涉及到一些调整密钥,通过手动添加“RSA”,如博客文章中所述。

请参阅此链接: https ://blog.woohoosvcs.com/2019/11/running-google-app-engine-behind-cloudflare/

编辑:这可能与使用 @Andrei 描述的 shell 命令相同的最终结果


推荐阅读