2017-05-15 87 views
1

我在Azure上部署了一个docker镜像,该镜像正在发布一些REST API,我可以使用它将某些信息存储在数据库中。
问题是:是否可以强制HTTPS而不是HTTP并使用自签名证书?Azure上的HTTPS RESTAPI

所以如果我的REST 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的证书注册一起工作,但我想在做任何事情之前做一些测试。

用几句话我只想调用一个REST API,只有当我正在执行HTTPS请求的机器被“接纳”才能执行请求。
我有点困惑。我的意思是,我想知道如何保护我在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 -v -s -k --key client.key --cert client.crt https://172.18.0.9 

(在dir`其中客户端。键和* crt存储)我有以下输出

* 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 

有什么建议吗?可能吗?

感谢

回答

2

根据您使用的具体配置细节会有所不同的Web服务器上,但你想要做什么,简而言之就是重定向所有的HTTP请求到https。请参阅下面的链接。

在客户端为此工作,您将需要遵循重定向。通常,浏览器被配置为自动执行该操作。使用卷曲这是由-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" }' 

在服务器端,这些指令可能有所帮助。

NGINX https://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom

阿帕奇 https://wiki.apache.org/httpd/RedirectSSL

IIS https://www.iis.net/configreference/system.webserver/httpredirect

+0

请注意,重定向没有真正多大意义的一个API。根据客户端库,他们可能会或可能不会遵循它们。老实说,我只是想返回一个答复,这是不支持,甚至完全禁用HTTP。由于使用API​​,您不会遇到某人在浏览器中输入您的域名并在默认情况下转为HTTP的问题。通过API,开发人员可以将其设置为立即使用HTTPS。如果他们尝试HTTP,给他们一个错误会告诉他们他们需要使用HTTPS URL进行访问。 – juunas

+0

重定向是正确的,因为API客户端应用程序的开发人员应该了解HTTP状态代码301的含义。您可以添加一个正如您所建议的响应一样的主体,但这可能不容易仅使用Web服务器配置来完成。 –