应用管理 REST API 需要指向集群的接入点地址, 并通过自签名证书的 HTTPS 请求和集群进行交互。
获取集群 Endpoint 和证书
控制台方式
{
"ca": "string", ##认证机构证书,ca.pem
"cert": "string", ##用户公钥证书,cert.pem
"key": "string" ##用户私钥证书,key.pem
}
# 提示: 请注意你的 curl 版本,您可能需要升级你的 curl.
curl --insecure --cert ~/.docker/aliyun/ClusterName/cert.pem --key ~/.docker/aliyun/ClusterName/key.pem https://123.123.123.123:1234/projects/
PHP 方式
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://123.123.123.123:1234/projects/");
curl_setopt($ch, CURLOPT_SSLKEY, "~/.docker/aliyun/ClusterName/key.pem");
curl_setopt($ch, CURLOPT_CAINFO, "~/.docker/aliyun/ClusterName/ca.pem");
curl_setopt($ch, CURLOPT_SSLCERT, "~/.docker/aliyun/ClusterName/cert.pem");
$result=curl_exec($ch);
echo $result;
curl_close($ch);
?>
import requests
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'))
print res.content
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.52</version>
</dependency>
import java.nio.file.Path;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.net.ssl.SSLContext;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.apache.http.client.methods.CloseableH ttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHt tpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
public class Test {
public static void main(String[] argc) throws Exception {
final char[] KEY_STORE_PASSWORD = "".toCharArray();
//获取证书地址
Path caCertPath = Paths.get("~/.docker/aliyun/ClusterName/ca.pem");
Path clientCertPath = Paths.get("~/.docker/aliyun/ClusterName/cert.pem");
Path clientKeyPath = Paths.get("~/.docker/aliyun/ClusterName/key.pem");
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
final Certificate caCert = cf.generateCertificate(Files.newInputStream(caCertPath));
final Certificate clientCert = cf.generateCertificate(
Files.newInputStream(clientCertPath));
final PEMKeyPair clientKeyPair = (PEMKeyPair) new PEMParser(
Files.newBufferedReader(clientKeyPath,
Charset.defaultCharset()))
.readObject();
final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(
clientKeyPair.getPrivateKeyInfo().getEncoded());
final KeyFactory kf = KeyFactory.getInstance("RSA");
final PrivateKey clientKey = kf.generatePrivate(spec);
//设置信任的证书
final KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
trustStore.setEntry("ca", new KeyStore.TrustedCertificateEntry(caCert), null);
//设置私钥
final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("client", clientCert);
keyStore.setKeyEntry("key", clientKey, KEY_STORE_PASSWORD, new Certificate[]{clientCert});
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(trustStore, null)
.loadKeyMaterial(keyStore, KEY_STORE_PASSWORD)
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
//httpclient连接
CloseableH ttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
try {
HttpGet httpget = new HttpGet("https://123.123.123.123:1234/projects/");
CloseableHt tpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
String bodyAsString = EntityUtils.toString(response.getEntity());
System.out.println(bodyAsString);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。