全部产品
容器服务

调用方式

更新时间:2017-06-07 13:26:11   分享:   

应用管理 REST API 需要指向集群的接入点地址, 并通过自签名证书的 HTTPS 请求和集群进行交互。

获取集群 Endpoint 和证书

控制台方式

  1. 登录 容器服务管理控制台

  2. 单击左侧导航栏中的 集群

  3. 选择需要查看的集群并单击 管理

    集群

  4. 您可以查看集群的 endpoint 并单击 下载证书 下载集群证书。

    证书

通过 API 访问,您需要将截图里的 tcp 需要改为 https。

编程方式获取

您需要先通过集群管理的 API 获取:

  1. 获取集群的 master_url 字段值。更多详细信息,参见 查看集群信息

  2. 获取集群的证书。更多详细信息,参见 查看集群证书

API 返回结果:

  1. {
  2. "ca": "string", ##认证机构证书,ca.pem
  3. "cert": "string", ##用户公钥证书,cert.pem
  4. "key": "string" ##用户私钥证书,key.pem
  5. }

推荐将返回结果的三个 string 的内容保存为一个目录下的三个文件 ca.pemcert.pemkey.pem。大部分的工具或编程框架都是以文件的方式加载 https 证书.

调用应用管理的 API

假设您的集群名称为 ClusterName,并且已经将上面三个证书存储到 ~/.docker/aliyun/ClusterName 目录下。上面获得的 master_url 地址为 https://123.123.123.123:1234

应用 API 列表

详见 应用API列表

下面以查看应用列表接口为例 (context path/projects/)。

curl 方式

  1. # 提示: 请注意你的 curl 版本,您可能需要升级你的 curl.
  2. curl --insecure --cert ~/.docker/aliyun/ClusterName/cert.pem --key ~/.docker/aliyun/ClusterName/key.pem https://123.123.123.123:1234/projects/

PHP 方式

  1. <?php
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, "https://123.123.123.123:1234/projects/");
  4. curl_setopt($ch, CURLOPT_SSLKEY, "~/.docker/aliyun/ClusterName/key.pem");
  5. curl_setopt($ch, CURLOPT_CAINFO, "~/.docker/aliyun/ClusterName/ca.pem");
  6. curl_setopt($ch, CURLOPT_SSLCERT, "~/.docker/aliyun/ClusterName/cert.pem");
  7. $result=curl_exec($ch);
  8. echo $result;
  9. curl_close($ch);
  10. ?>

Python 方式

  1. import requests
  2. res = requests.get('https://123.123.123.123:1234/projects/', verify='~/.docker/aliyun/ClusterName/ca.pem', cert=('~/.docker/aliyun/ClusterName/cert.pem', '~/.docker/aliyun/ClusterName/key.pem'))
  3. print res.content

JAVA 方式

添加 Maven 依赖:

  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. <version>4.5.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.bouncycastle</groupId>
  8. <artifactId>bcpkix-jdk15on</artifactId>
  9. <version>1.52</version>
  10. </dependency>

代码示例:

  1. import java.nio.file.Path;
  2. import java.nio.charset.Charset;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.security.KeyFactory;
  6. import java.security.KeyStore;
  7. import java.security.PrivateKey;
  8. import java.security.cert.Certificate;
  9. import java.security.cert.CertificateFactory;
  10. import java.security.spec.PKCS8EncodedKeySpec;
  11. import javax.net.ssl.SSLContext;
  12. import org.bouncycastle.openssl.PEMKeyPair;
  13. import org.bouncycastle.openssl.PEMParser;
  14. import org.apache.http.client.methods.CloseableHttpResponse;
  15. import org.apache.http.client.methods.HttpGet;
  16. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  17. import org.apache.http.impl.client.CloseableHttpClient;
  18. import org.apache.http.impl.client.HttpClients;
  19. import org.apache.http.ssl.SSLContexts;
  20. import org.apache.http.util.EntityUtils;
  21. public class Test {
  22. public static void main(String[] argc) throws Exception {
  23. final char[] KEY_STORE_PASSWORD = "".toCharArray();
  24. //获取证书地址
  25. Path caCertPath = Paths.get("~/.docker/aliyun/ClusterName/ca.pem");
  26. Path clientCertPath = Paths.get("~/.docker/aliyun/ClusterName/cert.pem");
  27. Path clientKeyPath = Paths.get("~/.docker/aliyun/ClusterName/key.pem");
  28. final CertificateFactory cf = CertificateFactory.getInstance("X.509");
  29. final Certificate caCert = cf.generateCertificate(Files.newInputStream(caCertPath));
  30. final Certificate clientCert = cf.generateCertificate(
  31. Files.newInputStream(clientCertPath));
  32. final PEMKeyPair clientKeyPair = (PEMKeyPair) new PEMParser(
  33. Files.newBufferedReader(clientKeyPath,
  34. Charset.defaultCharset()))
  35. .readObject();
  36. final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(
  37. clientKeyPair.getPrivateKeyInfo().getEncoded());
  38. final KeyFactory kf = KeyFactory.getInstance("RSA");
  39. final PrivateKey clientKey = kf.generatePrivate(spec);
  40. //设置信任的证书
  41. final KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
  42. trustStore.load(null, null);
  43. trustStore.setEntry("ca", new KeyStore.TrustedCertificateEntry(caCert), null);
  44. //设置私钥
  45. final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  46. keyStore.load(null, null);
  47. keyStore.setCertificateEntry("client", clientCert);
  48. keyStore.setKeyEntry("key", clientKey, KEY_STORE_PASSWORD, new Certificate[]{clientCert});
  49. SSLContext sslContext = SSLContexts.custom()
  50. .loadTrustMaterial(trustStore, null)
  51. .loadKeyMaterial(keyStore, KEY_STORE_PASSWORD)
  52. .build();
  53. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
  54. sslContext,
  55. SSLConnectionSocketFactory.getDefaultHostnameVerifier());
  56. //httpclient连接
  57. CloseableHttpClient httpclient = HttpClients.custom()
  58. .setSSLSocketFactory(sslsf)
  59. .build();
  60. try {
  61. HttpGet httpget = new HttpGet("https://123.123.123.123:1234/projects/");
  62. CloseableHttpResponse response = httpclient.execute(httpget);
  63. try {
  64. System.out.println("----------------------------------------");
  65. String bodyAsString = EntityUtils.toString(response.getEntity());
  66. System.out.println(bodyAsString);
  67. } finally {
  68. response.close();
  69. }
  70. } finally {
  71. httpclient.close();
  72. }
  73. }
  74. }
本文导读目录
本文导读目录
以上内容是否对您有帮助?