Search in sources :

Example 1 with IAMPolicyResponse

use of org.apache.cloudstack.api.response.iam.IAMPolicyResponse in project cloudstack by apache.

the class IAMApiServiceTest method createIAMPolicyTest.

@Test
public void createIAMPolicyTest() {
    IAMPolicy policy = new IAMPolicyVO("policy1", "tester policy1");
    List<IAMPolicy> policies = new ArrayList<IAMPolicy>();
    policies.add(policy);
    Pair<List<IAMPolicy>, Integer> policyList = new Pair<List<IAMPolicy>, Integer>(policies, 1);
    when(_iamSrv.createIAMPolicy("policy1", "tester policy1", null, callerDomainPath)).thenReturn(policy);
    when(_iamSrv.listIAMPolicies(null, null, callerDomainPath, 0L, 20L)).thenReturn(policyList);
    IAMPolicy createdPolicy = _aclSrv.createIAMPolicy(caller, "policy1", "tester policy1", null);
    assertNotNull("IAM policy 'policy1' failed to create ", createdPolicy);
    ListResponse<IAMPolicyResponse> policyResp = _aclSrv.listIAMPolicies(null, null, callerDomainId, 0L, 20L);
    assertTrue("No. of response items should be one", policyResp.getCount() == 1);
    IAMPolicyResponse resp = policyResp.getResponses().get(0);
    assertEquals("Error in created group name", "policy1", resp.getName());
}
Also used : IAMPolicy(org.apache.cloudstack.iam.api.IAMPolicy) IAMPolicyVO(org.apache.cloudstack.iam.server.IAMPolicyVO) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair) IAMPolicyResponse(org.apache.cloudstack.api.response.iam.IAMPolicyResponse) Test(org.junit.Test)

Example 2 with IAMPolicyResponse

use of org.apache.cloudstack.api.response.iam.IAMPolicyResponse in project cloudstack by apache.

the class IAMApiServiceImpl method listIAMPolicies.

@Override
public ListResponse<IAMPolicyResponse> listIAMPolicies(Long iamPolicyId, String iamPolicyName, Long domainId, Long startIndex, Long pageSize) {
    // acl check
    Account caller = CallContext.current().getCallingAccount();
    Domain domain = null;
    if (domainId != null) {
        domain = _domainDao.findById(domainId);
        if (domain == null) {
            throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist");
        }
        _accountMgr.checkAccess(caller, domain);
    } else {
        domain = _domainDao.findById(caller.getDomainId());
    }
    String domainPath = domain.getPath();
    // search for policies
    Pair<List<IAMPolicy>, Integer> result = _iamSrv.listIAMPolicies(iamPolicyId, iamPolicyName, domainPath, startIndex, pageSize);
    // generate policy response
    ListResponse<IAMPolicyResponse> response = new ListResponse<IAMPolicyResponse>();
    List<IAMPolicyResponse> policyResponses = new ArrayList<IAMPolicyResponse>();
    for (IAMPolicy policy : result.first()) {
        IAMPolicyResponse resp = createIAMPolicyResponse(policy);
        policyResponses.add(resp);
    }
    response.setResponses(policyResponses, result.second());
    return response;
}
Also used : Account(com.cloud.user.Account) ListResponse(org.apache.cloudstack.api.response.ListResponse) IAMPolicy(org.apache.cloudstack.iam.api.IAMPolicy) ArrayList(java.util.ArrayList) IAMPolicyResponse(org.apache.cloudstack.api.response.iam.IAMPolicyResponse) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) List(java.util.List) ArrayList(java.util.ArrayList) Domain(com.cloud.domain.Domain)

Example 3 with IAMPolicyResponse

use of org.apache.cloudstack.api.response.iam.IAMPolicyResponse in project cloudstack by apache.

the class CreateIAMPolicyCmd method execute.

@Override
public void execute() {
    IAMPolicy policy = _entityMgr.findById(IAMPolicy.class, getEntityId());
    if (policy != null) {
        IAMPolicyResponse response = _iamApiSrv.createIAMPolicyResponse(policy);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create iam policy:" + name);
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) IAMPolicy(org.apache.cloudstack.iam.api.IAMPolicy) IAMPolicyResponse(org.apache.cloudstack.api.response.iam.IAMPolicyResponse)

Example 4 with IAMPolicyResponse

use of org.apache.cloudstack.api.response.iam.IAMPolicyResponse in project cloudstack by apache.

the class IAMApiServiceTest method addRemovePermissionToPolicyTest.

@Test
public void addRemovePermissionToPolicyTest() {
    IAMPolicy policy = new IAMPolicyVO("policy1", "tester policy1");
    List<IAMPolicy> policies = new ArrayList<IAMPolicy>();
    policies.add(policy);
    Long policyId = policy.getId();
    Long resId = 200L;
    Class clz = ListVMsCmd.class;
    when(_apiServer.getCmdClass("listVirtualMachines")).thenReturn(clz);
    when(_iamSrv.addIAMPermissionToIAMPolicy(policyId, VirtualMachine.class.getSimpleName(), PermissionScope.RESOURCE.toString(), resId, "listVirtualMachines", AccessType.UseEntry.toString(), Permission.Allow, false)).thenReturn(policy);
    _aclSrv.addIAMPermissionToIAMPolicy(policyId, VirtualMachine.class.getSimpleName(), PermissionScope.RESOURCE, resId, "listVirtualMachines", Permission.Allow, false, false);
    Pair<List<IAMPolicy>, Integer> policyList = new Pair<List<IAMPolicy>, Integer>(policies, 1);
    List<IAMPolicyPermission> policyPerms = new ArrayList<IAMPolicyPermission>();
    IAMPolicyPermission perm = new IAMPolicyPermissionVO(policyId, "listVirtualMachines", VirtualMachine.class.getSimpleName(), AccessType.UseEntry.toString(), PermissionScope.RESOURCE.toString(), resId, Permission.Allow, false);
    policyPerms.add(perm);
    when(_iamSrv.listIAMPolicies(null, "policy1", callerDomainPath, 0L, 20L)).thenReturn(policyList);
    when(_iamSrv.listPolicyPermissions(policyId)).thenReturn(policyPerms);
    ListResponse<IAMPolicyResponse> policyResp = _aclSrv.listIAMPolicies(null, "policy1", callerDomainId, 0L, 20L);
    assertTrue("No. of response items should be one", policyResp.getCount() == 1);
    IAMPolicyResponse resp = policyResp.getResponses().get(0);
    Set<IAMPermissionResponse> permList = resp.getPermissionList();
    assertTrue("Permission list should not be empty", permList != null && permList.size() > 0);
    IAMPermissionResponse permResp = permList.iterator().next();
    assertEquals("There should be one permission for listVirtualMachines", "listVirtualMachines", permResp.getAction());
    //remove permission from policy
    policyPerms.remove(perm);
    _aclSrv.removeIAMPermissionFromIAMPolicy(policyId, VirtualMachine.class.getSimpleName(), PermissionScope.RESOURCE, resId, "listVirtualMachines");
    policyResp = _aclSrv.listIAMPolicies(null, "policy1", callerDomainId, 0L, 20L);
    assertTrue("No. of response items should be one", policyResp.getCount() == 1);
    resp = policyResp.getResponses().get(0);
    permList = resp.getPermissionList();
    assertTrue("Permission list should be empty", permList != null && permList.size() == 0);
}
Also used : IAMPolicy(org.apache.cloudstack.iam.api.IAMPolicy) IAMPolicyVO(org.apache.cloudstack.iam.server.IAMPolicyVO) ArrayList(java.util.ArrayList) IAMPolicyPermissionVO(org.apache.cloudstack.iam.server.IAMPolicyPermissionVO) IAMPolicyResponse(org.apache.cloudstack.api.response.iam.IAMPolicyResponse) ListVMsCmd(org.apache.cloudstack.api.command.user.vm.ListVMsCmd) IAMPolicyPermission(org.apache.cloudstack.iam.api.IAMPolicyPermission) IAMPermissionResponse(org.apache.cloudstack.api.response.iam.IAMPermissionResponse) BeforeClass(org.junit.BeforeClass) List(java.util.List) ArrayList(java.util.ArrayList) VirtualMachine(com.cloud.vm.VirtualMachine) Pair(com.cloud.utils.Pair) Test(org.junit.Test)

Example 5 with IAMPolicyResponse

use of org.apache.cloudstack.api.response.iam.IAMPolicyResponse in project cloudstack by apache.

the class IAMApiServiceImpl method createIAMPolicyResponse.

@Override
public IAMPolicyResponse createIAMPolicyResponse(IAMPolicy policy) {
    IAMPolicyResponse response = new IAMPolicyResponse();
    response.setId(policy.getUuid());
    response.setName(policy.getName());
    response.setDescription(policy.getDescription());
    String domainPath = policy.getPath();
    if (domainPath != null) {
        DomainVO domain = _domainDao.findDomainByPath(domainPath);
        if (domain != null) {
            response.setDomainId(domain.getUuid());
            response.setDomainName(domain.getName());
        }
    }
    long accountId = policy.getAccountId();
    AccountVO owner = _accountDao.findById(accountId);
    if (owner != null) {
        response.setAccountName(owner.getAccountName());
    }
    // find permissions associated with this policy
    List<IAMPolicyPermission> permissions = _iamSrv.listPolicyPermissions(policy.getId());
    if (permissions != null && permissions.size() > 0) {
        for (IAMPolicyPermission permission : permissions) {
            IAMPermissionResponse perm = new IAMPermissionResponse();
            perm.setAction(permission.getAction());
            if (permission.getEntityType() != null) {
                perm.setEntityType(permission.getEntityType());
            }
            if (permission.getScope() != null) {
                perm.setScope(PermissionScope.valueOf(permission.getScope()));
            }
            perm.setScopeId(permission.getScopeId());
            perm.setPermission(permission.getPermission());
            response.addPermission(perm);
        }
    }
    response.setObjectName("aclpolicy");
    return response;
}
Also used : DomainVO(com.cloud.domain.DomainVO) IAMPolicyPermission(org.apache.cloudstack.iam.api.IAMPolicyPermission) IAMPermissionResponse(org.apache.cloudstack.api.response.iam.IAMPermissionResponse) AccountVO(com.cloud.user.AccountVO) IAMPolicyResponse(org.apache.cloudstack.api.response.iam.IAMPolicyResponse)

Aggregations

IAMPolicyResponse (org.apache.cloudstack.api.response.iam.IAMPolicyResponse)7 IAMPolicy (org.apache.cloudstack.iam.api.IAMPolicy)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ServerApiException (org.apache.cloudstack.api.ServerApiException)3 Pair (com.cloud.utils.Pair)2 IAMPermissionResponse (org.apache.cloudstack.api.response.iam.IAMPermissionResponse)2 IAMPolicyPermission (org.apache.cloudstack.iam.api.IAMPolicyPermission)2 IAMPolicyVO (org.apache.cloudstack.iam.server.IAMPolicyVO)2 Test (org.junit.Test)2 Domain (com.cloud.domain.Domain)1 DomainVO (com.cloud.domain.DomainVO)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 Account (com.cloud.user.Account)1 AccountVO (com.cloud.user.AccountVO)1 VirtualMachine (com.cloud.vm.VirtualMachine)1 ListVMsCmd (org.apache.cloudstack.api.command.user.vm.ListVMsCmd)1 ListResponse (org.apache.cloudstack.api.response.ListResponse)1 IAMPolicyPermissionVO (org.apache.cloudstack.iam.server.IAMPolicyPermissionVO)1 BeforeClass (org.junit.BeforeClass)1