|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.cloudstack.api.command.user.kms; |
| 21 | + |
| 22 | +import com.cloud.exception.ResourceAllocationException; |
| 23 | +import com.cloud.user.Account; |
| 24 | +import org.apache.cloudstack.acl.RoleType; |
| 25 | +import org.apache.cloudstack.api.APICommand; |
| 26 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 27 | +import org.apache.cloudstack.api.ApiConstants; |
| 28 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 29 | +import org.apache.cloudstack.api.BaseCmd; |
| 30 | +import org.apache.cloudstack.api.Parameter; |
| 31 | +import org.apache.cloudstack.api.ServerApiException; |
| 32 | +import org.apache.cloudstack.api.command.user.UserCmd; |
| 33 | +import org.apache.cloudstack.api.response.DomainResponse; |
| 34 | +import org.apache.cloudstack.api.response.KMSKeyResponse; |
| 35 | +import org.apache.cloudstack.api.response.ZoneResponse; |
| 36 | +import org.apache.cloudstack.context.CallContext; |
| 37 | +import org.apache.cloudstack.framework.kms.KMSException; |
| 38 | +import org.apache.cloudstack.kms.KMSManager; |
| 39 | + |
| 40 | +import javax.inject.Inject; |
| 41 | + |
| 42 | +@APICommand(name = "createKMSKey", |
| 43 | + description = "Creates a new KMS key (Key Encryption Key) for encryption", |
| 44 | + responseObject = KMSKeyResponse.class, |
| 45 | + since = "4.23.0", |
| 46 | + authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User}, |
| 47 | + requestHasSensitiveInfo = false, |
| 48 | + responseHasSensitiveInfo = false) |
| 49 | +public class CreateKMSKeyCmd extends BaseCmd implements UserCmd { |
| 50 | + private static final String s_name = "createkmskeyresponse"; |
| 51 | + |
| 52 | + @Inject |
| 53 | + private KMSManager kmsManager; |
| 54 | + |
| 55 | + ///////////////////////////////////////////////////// |
| 56 | + //////////////// API parameters ///////////////////// |
| 57 | + ///////////////////////////////////////////////////// |
| 58 | + |
| 59 | + @Parameter(name = ApiConstants.NAME, |
| 60 | + required = true, |
| 61 | + type = CommandType.STRING, |
| 62 | + description = "Name of the KMS key") |
| 63 | + private String name; |
| 64 | + |
| 65 | + @Parameter(name = ApiConstants.DESCRIPTION, |
| 66 | + type = CommandType.STRING, |
| 67 | + description = "Description of the KMS key") |
| 68 | + private String description; |
| 69 | + |
| 70 | + @Parameter(name = ApiConstants.PURPOSE, |
| 71 | + required = true, |
| 72 | + type = CommandType.STRING, |
| 73 | + description = "Purpose of the key: VOLUME_ENCRYPTION, TLS_CERT, CONFIG_SECRET") |
| 74 | + private String purpose; |
| 75 | + |
| 76 | + @Parameter(name = ApiConstants.ZONE_ID, |
| 77 | + required = true, |
| 78 | + type = CommandType.UUID, |
| 79 | + entityType = ZoneResponse.class, |
| 80 | + description = "Zone ID where the key will be valid") |
| 81 | + private Long zoneId; |
| 82 | + |
| 83 | + @Parameter(name = ApiConstants.ACCOUNT, |
| 84 | + type = CommandType.STRING, |
| 85 | + description = "Account name (for creating keys for child accounts - requires domain admin or admin)") |
| 86 | + private String accountName; |
| 87 | + |
| 88 | + @Parameter(name = ApiConstants.DOMAIN_ID, |
| 89 | + type = CommandType.UUID, |
| 90 | + entityType = DomainResponse.class, |
| 91 | + description = "Domain ID (for creating keys for child accounts - requires domain admin or admin)") |
| 92 | + private Long domainId; |
| 93 | + |
| 94 | + @Parameter(name = ApiConstants.KEY_BITS, |
| 95 | + type = CommandType.INTEGER, |
| 96 | + description = "Key size in bits: 128, 192, or 256 (default: 256)") |
| 97 | + private Integer keyBits; |
| 98 | + |
| 99 | + ///////////////////////////////////////////////////// |
| 100 | + /////////////////// Accessors /////////////////////// |
| 101 | + ///////////////////////////////////////////////////// |
| 102 | + |
| 103 | + public String getName() { |
| 104 | + return name; |
| 105 | + } |
| 106 | + |
| 107 | + public String getDescription() { |
| 108 | + return description; |
| 109 | + } |
| 110 | + |
| 111 | + public String getPurpose() { |
| 112 | + return purpose; |
| 113 | + } |
| 114 | + |
| 115 | + public Long getZoneId() { |
| 116 | + return zoneId; |
| 117 | + } |
| 118 | + |
| 119 | + public String getAccountName() { |
| 120 | + return accountName; |
| 121 | + } |
| 122 | + |
| 123 | + public Long getDomainId() { |
| 124 | + return domainId; |
| 125 | + } |
| 126 | + |
| 127 | + public Integer getKeyBits() { |
| 128 | + return keyBits != null ? keyBits : 256; // Default to 256 bits |
| 129 | + } |
| 130 | + |
| 131 | + ///////////////////////////////////////////////////// |
| 132 | + /////////////// API Implementation/////////////////// |
| 133 | + ///////////////////////////////////////////////////// |
| 134 | + |
| 135 | + @Override |
| 136 | + public void execute() throws ResourceAllocationException { |
| 137 | + try { |
| 138 | + KMSKeyResponse response = kmsManager.createKMSKey(this); |
| 139 | + response.setResponseName(getCommandName()); |
| 140 | + setResponseObject(response); |
| 141 | + } catch (KMSException e) { |
| 142 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, |
| 143 | + "Failed to create KMS key: " + e.getMessage()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String getCommandName() { |
| 149 | + return s_name; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public long getEntityOwnerId() { |
| 154 | + Account caller = CallContext.current().getCallingAccount(); |
| 155 | + if (accountName != null || domainId != null) { |
| 156 | + return _accountService.finalyzeAccountId(accountName, domainId, null, true); |
| 157 | + } |
| 158 | + return caller.getId(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public ApiCommandResourceType getApiResourceType() { |
| 163 | + return ApiCommandResourceType.KmsKey; |
| 164 | + } |
| 165 | +} |
| 166 | + |
0 commit comments