|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.api.command.iam; |
| 18 | + |
| 19 | +import javax.inject.Inject; |
| 20 | + |
| 21 | +import org.apache.log4j.Logger; |
| 22 | + |
| 23 | +import org.apache.cloudstack.acl.PermissionScope; |
| 24 | +import org.apache.cloudstack.api.ACL; |
| 25 | +import org.apache.cloudstack.api.APICommand; |
| 26 | +import org.apache.cloudstack.api.ApiCommandJobType; |
| 27 | +import org.apache.cloudstack.api.ApiConstants; |
| 28 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 29 | +import org.apache.cloudstack.api.BaseAsyncCmd; |
| 30 | +import org.apache.cloudstack.api.Parameter; |
| 31 | +import org.apache.cloudstack.api.ServerApiException; |
| 32 | +import org.apache.cloudstack.api.response.iam.IAMPolicyResponse; |
| 33 | +import org.apache.cloudstack.context.CallContext; |
| 34 | +import org.apache.cloudstack.iam.IAMApiService; |
| 35 | +import org.apache.cloudstack.iam.api.IAMPolicy; |
| 36 | +import org.apache.cloudstack.iam.api.IAMPolicyPermission.Permission; |
| 37 | + |
| 38 | +import com.cloud.event.EventTypes; |
| 39 | +import com.cloud.exception.InsufficientCapacityException; |
| 40 | +import com.cloud.exception.ResourceUnavailableException; |
| 41 | +import com.cloud.user.Account; |
| 42 | +import com.cloud.utils.db.EntityManager; |
| 43 | + |
| 44 | + |
| 45 | +@APICommand(name = "addIAMPermissionToIAMPolicy", description = "Add IAM permission to an iam policy", responseObject = IAMPolicyResponse.class) |
| 46 | +public class AddIAMPermissionToIAMPolicyCmd extends BaseAsyncCmd { |
| 47 | + public static final Logger s_logger = Logger.getLogger(AddIAMPermissionToIAMPolicyCmd.class.getName()); |
| 48 | + private static final String s_name = "addiampermissiontoiampolicyresponse"; |
| 49 | + |
| 50 | + @Inject |
| 51 | + public IAMApiService _iamApiSrv; |
| 52 | + @Inject |
| 53 | + public EntityManager _entityMgr; |
| 54 | + |
| 55 | + ///////////////////////////////////////////////////// |
| 56 | + //////////////// API parameters ///////////////////// |
| 57 | + ///////////////////////////////////////////////////// |
| 58 | + |
| 59 | + |
| 60 | + @ACL |
| 61 | + @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = IAMPolicyResponse.class, |
| 62 | + required = true, description = "The ID of the iam policy") |
| 63 | + private Long id; |
| 64 | + |
| 65 | + @Parameter(name = ApiConstants.IAM_ACTION, type = CommandType.STRING, required = true, description = "action api name.") |
| 66 | + private String action; |
| 67 | + |
| 68 | + @Parameter(name = ApiConstants.ENTITY_TYPE, type = CommandType.STRING, required = false, description = "entity class simple name.") |
| 69 | + private String entityType; |
| 70 | + |
| 71 | + @Parameter(name = ApiConstants.IAM_SCOPE, type = CommandType.STRING, |
| 72 | + required = false, description = "iam permission scope") |
| 73 | + private String scope; |
| 74 | + |
| 75 | + @Parameter(name = ApiConstants.IAM_SCOPE_ID, type = CommandType.STRING, required = false, description = "The UUID of the permission scope id") |
| 76 | + private String scopeId; |
| 77 | + |
| 78 | + |
| 79 | + ///////////////////////////////////////////////////// |
| 80 | + /////////////////// Accessors /////////////////////// |
| 81 | + ///////////////////////////////////////////////////// |
| 82 | + |
| 83 | + |
| 84 | + public Long getId() { |
| 85 | + return id; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + public String getAction() { |
| 90 | + return action; |
| 91 | + } |
| 92 | + |
| 93 | + public String getEntityType() { |
| 94 | + return entityType; |
| 95 | + } |
| 96 | + |
| 97 | + public String getScope() { |
| 98 | + return scope; |
| 99 | + } |
| 100 | + |
| 101 | + public Long getScopeId() { |
| 102 | + // here we will convert the passed String UUID to Long ID since internally we store it as entity internal ID. |
| 103 | + return _iamApiSrv.getPermissionScopeId(scope, entityType, scopeId); |
| 104 | + } |
| 105 | + |
| 106 | + ///////////////////////////////////////////////////// |
| 107 | + /////////////// API Implementation/////////////////// |
| 108 | + ///////////////////////////////////////////////////// |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + @Override |
| 113 | + public String getCommandName() { |
| 114 | + return s_name; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + @Override |
| 119 | + public long getEntityOwnerId() { |
| 120 | + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void execute() throws ResourceUnavailableException, |
| 125 | + InsufficientCapacityException, ServerApiException { |
| 126 | + CallContext.current().setEventDetails("IAM policy Id: " + getId()); |
| 127 | + // Only explicit ALLOW is supported for this release, no explicit deny |
| 128 | + IAMPolicy result = _iamApiSrv.addIAMPermissionToIAMPolicy(id, entityType, PermissionScope.valueOf(scope), |
| 129 | + getScopeId(), action, Permission.Allow, false); |
| 130 | + if (result != null) { |
| 131 | + IAMPolicyResponse response = _iamApiSrv.createIAMPolicyResponse(result); |
| 132 | + response.setResponseName(getCommandName()); |
| 133 | + setResponseObject(response); |
| 134 | + } else { |
| 135 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to grant permission to iam policy " |
| 136 | + + getId()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String getEventType() { |
| 142 | + return EventTypes.EVENT_IAM_POLICY_GRANT; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String getEventDescription() { |
| 147 | + return "granting permission to iam policy"; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public ApiCommandJobType getInstanceType() { |
| 152 | + return ApiCommandJobType.IAMPolicy; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments