我正在 Azure 上部署一个 docker 镜像,该镜像正在发布一些 REST API,我可以使用它在数据库中存储一些信息。
问题是:是否可以强制使用 HTTPS 而不是 HTTP 并使用自签名证书?
所以如果我的其余 api 是这样的:
curl -i -H "Content-type: application/json" http://AZURE_IP_ADDRESS:5100/module/api/v1.0/person -X POST -d '{ "name" : "test", "surname": "test_surname" }'
我希望只有证书机器才能执行前面的命令。
据我了解,Azure 可与 Truster CA 注册的证书配合使用,但我想在完成所有操作之前先进行一些测试。
简而言之,仅当我执行 HTTPS 请求的计算机“允许”执行该请求时,我才想调用 REST API。
我有一点困惑。我的意思是,我希望知道如何保护在 Azure 上运行的 docker 提供的 REST API。但我想更通用一些。不只使用 Azure 功能,并在我使用其他功能时做好准备。 azure 。
按照以下步骤操作:
# Create the CA Key and Certificate for signing Client Certs
openssl genrsa -des3 -out ca.key 4096
openssl req -new -x509 -days 365 -key ca.key -out ca.crt
# Create the Server Key, CSR, and Certificate
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
# We're self signing our own server cert here. This is a no-no in production.
openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt
# Create the Client Key and CSR
openssl genrsa -des3 -out client.key 1024
openssl req -new -key client.key -out client.csr
# Sign the client certificate with our CA cert. Unlike signing our own server cert, this is what we want to do.
openssl x509 -req -days 365 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt
openssl rsa -in server.key -out server_without_key.key
并配置nginx.conf如下:
server {
listen 443 ssl;
ssl_certificate /etc/nginx/certs/server.crt;
ssl_certificate_key /etc/nginx/certs/server_without_key.key;
ssl_client_certificate /etc/nginx/certs/ca.crt; # the cert used to sign the client certificates
ssl_verify_client on; # force SSL verification (can also be set to 'optional')
ssl on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
keepalive_timeout 70;
}
一旦我 curl :
curl -v -s -k --key client.key --cert client.crt https://172.18.0.9
(在存储 client.key 和 *crt 的 dir` 中)我有以下输出
* Rebuilt URL to: https://172.18.0.9/
* Trying 172.18.0.9...
* Connected to 172.18.0.9 (172.18.0.9) port 443 (#0)
* found 173 certificates in /etc/ssl/certs/ca-certificates.crt
* found 698 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* error reading X.509 key or certificate file: Error while reading file.
* Closing connection 0
有什么建议吗?是否可以?
谢谢
最佳答案
根据您使用的 Web 服务器,您的具体配置详细信息会有所不同,但简而言之,您想要做的是将所有 http 请求重定向到 https。请参阅下面的一些链接。
在客户端,要实现此功能,您需要遵循重定向。通常,浏览器被配置为自动执行此操作。使用curl,这是通过-L 标志来完成的。
curl -L -i -H "Content-type: application/json" http://AZURE_IP_ADDRESS:5100/module/api/v1.0/person -X POST -d '{ "name" : "test", "surname": "test_surname" }'
在服务器端,这些说明可能会有所帮助。
Apache https://wiki.apache.org/httpd/RedirectSSL
IIS https://www.iis.net/configreference/system.webserver/httpredirect
关于rest - Azure REST API 上的 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983862/