密钥对管理服务中创建SSH登录密钥对相关示例
本示例展示如何通过java版本的KPS SDK方式创建SSH登录密钥对
功能介绍
密钥对管理,即密钥对管理服务(Key Pair Service,KPS),是一种安全、可靠、简单易用的SSH密钥对托管服务,帮助用户集中管理SSH密钥对,保护SSH密钥对的安全。
前置条件
1.已 注册 华为云,并完成 实名认证 。
2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。
3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥 。
4.已具备开发环境 ,支持Java JDK 1.8及其以上版本。
代码示例
以下代码展示如何使用KPS SDK创建SSH登录密钥对:
Copied!
package com.huawei.demo;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.kps.v3.KpsClient;
import com.huaweicloud.sdk.kps.v3.model.CreateKeypairAction;
import com.huaweicloud.sdk.kps.v3.model.CreateKeypairRequest;
import com.huaweicloud.sdk.kps.v3.model.CreateKeypairRequestBody;
import com.huaweicloud.sdk.kps.v3.model.CreateKeypairResponse;
import com.huaweicloud.sdk.kps.v3.model.ListKeypairsRequest;
import com.huaweicloud.sdk.kps.v3.model.ListKeypairsResponse;
public class BasicSSHKeyPairExample {
private static final String ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_AK");
private static final String SECRET_ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_SK");
private static final String PROJECT_ID = "<ProjectID>";
private static final String KPS_ENDPOINT = "<KpsEndpoint>";
private static final int HTTP_CODE_CONFLICT = 409;
public static void main(final String[] args) {
final String sshKeyAlias = args[0];
basicSSHKeyPairOperate(sshKeyAlias);
}
static void basicSSHKeyPairOperate(String sshKeyAlias) {
final BasicCredentials auth = new BasicCredentials().withAk(ACCESS_KEY).withSk(SECRET_ACCESS_KEY)
.withProjectId(PROJECT_ID);
final KpsClient kpsClient = KpsClient.newBuilder().withCredential(auth).withEndpoint(KPS_ENDPOINT).build();
final CreateKeypairRequest createKeypairRequest = new CreateKeypairRequest()
.withBody(new CreateKeypairRequestBody().withKeypair(new CreateKeypairAction().withName(sshKeyAlias)
.withScope(CreateKeypairAction.ScopeEnum.DOMAIN)));
try {
final CreateKeypairResponse createKeypairResponse = kpsClient.createKeypair(createKeypairRequest);
assert null != createKeypairResponse.getKeypair().getPublicKey();
} catch (ClientRequestException e) {
assert HTTP_CODE_CONFLICT == e.getHttpStatusCode();
}
final ListKeypairsRequest listKeypairsRequest = new ListKeypairsRequest();
final ListKeypairsResponse listKeypairsResponse = kpsClient.listKeypairs(listKeypairsRequest);
assert listKeypairsResponse.getKeypairs().stream()
.anyMatch(keyPairs -> sshKeyAlias.equals(keyPairs.getKeypair().getName()));
}
}
您可以在API Explorer中直接运行调试该接口。
修订记录
发布日期 |
文档版本 |
修订说明 |
2021-09-10 |
1.0 |
文档首次发布 |
版本说明
本示例配套的SDK版本为:3.1.19
密钥对管理服务中创建SSH登录密钥对相关示例
本示例展示如何通过java版本的KPS SDK方式创建SSH登录密钥对
功能介绍
密钥对管理,即密钥对管理服务(Key Pair Service,KPS),是一种安全、可靠、简单易用的SSH密钥对托管服务,帮助用户集中管理SSH密钥对,保护SSH密钥对的安全。
前置条件
1.已 注册 华为云,并完成 实名认证 。
2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。
3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥 。
4.已具备开发环境 ,支持Java JDK 1.8及其以上版本。
代码示例
以下代码展示如何使用KPS SDK创建SSH登录密钥对:
package com.huawei.demo; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ClientRequestException; import com.huaweicloud.sdk.kps.v3.KpsClient; import com.huaweicloud.sdk.kps.v3.model.CreateKeypairAction; import com.huaweicloud.sdk.kps.v3.model.CreateKeypairRequest; import com.huaweicloud.sdk.kps.v3.model.CreateKeypairRequestBody; import com.huaweicloud.sdk.kps.v3.model.CreateKeypairResponse; import com.huaweicloud.sdk.kps.v3.model.ListKeypairsRequest; import com.huaweicloud.sdk.kps.v3.model.ListKeypairsResponse; /** * 创建SSH登录密钥对 * 激活assert语法,请在VM_OPTIONS中添加-ea */ public class BasicSSHKeyPairExample { /** * 基础认证信息: * 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; * 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 * - ACCESS_KEY: 华为云账号Access Key * - SECRET_ACCESS_KEY: 华为云账号Secret Access Key * - PROJECT_ID: 华为云局点项目ID 详情见https://support.huaweicloud.com/productdesc-iam/iam_01_0023.html * - KPS_ENDPOINT: 华为云KPS服务访问终端地址 详情见https://support.huaweicloud.com/api-dew/dew_02_0052.html */ private static final String ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_AK"); private static final String SECRET_ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_SK"); private static final String PROJECT_ID = "<ProjectID>"; private static final String KPS_ENDPOINT = "<KpsEndpoint>"; // Http状态码: 资源冲突 private static final int HTTP_CODE_CONFLICT = 409; public static void main(final String[] args) { // 密钥对别名 final String sshKeyAlias = args[0]; basicSSHKeyPairOperate(sshKeyAlias); } static void basicSSHKeyPairOperate(String sshKeyAlias) { // 1.准备访问华为云的认证信息 final BasicCredentials auth = new BasicCredentials().withAk(ACCESS_KEY).withSk(SECRET_ACCESS_KEY) .withProjectId(PROJECT_ID); // 2.初始化SDK,传入认证信息及KPS访问终端地址 final KpsClient kpsClient = KpsClient.newBuilder().withCredential(auth).withEndpoint(KPS_ENDPOINT).build(); // 3.组装创建账号密钥对请求 final CreateKeypairRequest createKeypairRequest = new CreateKeypairRequest() .withBody(new CreateKeypairRequestBody().withKeypair(new CreateKeypairAction().withName(sshKeyAlias) .withScope(CreateKeypairAction.ScopeEnum.DOMAIN))); // 4.创建账号密钥对 // 重复创建相同别名的密钥对会返回资源冲突错误(HttpStatusCode=409) try { final CreateKeypairResponse createKeypairResponse = kpsClient.createKeypair(createKeypairRequest); assert null != createKeypairResponse.getKeypair().getPublicKey(); } catch (ClientRequestException e) { assert HTTP_CODE_CONFLICT == e.getHttpStatusCode(); } // 5.组装查询密钥对列表请求 final ListKeypairsRequest listKeypairsRequest = new ListKeypairsRequest(); // 6.获取密钥对列表 final ListKeypairsResponse listKeypairsResponse = kpsClient.listKeypairs(listKeypairsRequest); // 7.校验密钥对列表有新创建的密钥对 assert listKeypairsResponse.getKeypairs().stream() .anyMatch(keyPairs -> sshKeyAlias.equals(keyPairs.getKeypair().getName())); } }
您可以在API Explorer中直接运行调试该接口。
修订记录