Skip to content

Commit f231cec

Browse files
author
Prachi Damle
committed
Loading api permissions from commands.properties during startup
1 parent 015d06e commit f231cec

7 files changed

Lines changed: 200 additions & 2 deletions

File tree

‎api/src/org/apache/cloudstack/acl/PermissionScope.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public enum PermissionScope {
44
RESOURCE(0),
55
ACCOUNT(1),
66
DOMAIN(2),
7-
REGION(3);
7+
REGION(3), ALL(4);
88

99
private int _scale;
1010

‎engine/schema/src/org/apache/cloudstack/acl/AclPermissionVO.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ public AclPermissionVO() {
7171

7272
}
7373

74-
74+
public AclPermissionVO(String action, String entityType, AccessType accessType, PermissionScope scope,
75+
Long scopeId, Permission permission) {
76+
this.action = action;
77+
this.entityType = entityType;
78+
this.accessType = accessType;
79+
this.scope = scope;
80+
this.scopeId = scopeId;
81+
this.permission = permission;
82+
}
7583

7684
@Override
7785
public long getId() {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.acl.dao;
18+
19+
import org.apache.cloudstack.acl.AclPermissionVO;
20+
import com.cloud.utils.db.GenericDao;
21+
22+
public interface AclPermissionDao extends GenericDao<AclPermissionVO, Long> {
23+
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.acl.dao;
18+
19+
import java.util.Map;
20+
21+
import javax.naming.ConfigurationException;
22+
23+
import org.apache.cloudstack.acl.AclPermissionVO;
24+
import com.cloud.utils.db.GenericDaoBase;
25+
26+
public class AclPermissionDaoImpl extends GenericDaoBase<AclPermissionVO, Long> implements AclPermissionDao {
27+
28+
public AclPermissionDaoImpl()
29+
{
30+
31+
}
32+
33+
@Override
34+
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
35+
super.configure(name, params);
36+
37+
return true;
38+
}
39+
40+
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.acl.dao;
18+
19+
import org.apache.cloudstack.acl.AclPolicyPermissionMapVO;
20+
21+
import com.cloud.utils.db.GenericDao;
22+
23+
public interface AclPolicyPermissionMapDao extends GenericDao<AclPolicyPermissionMapVO, Long> {
24+
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.acl.dao;
18+
19+
import java.util.Map;
20+
21+
import javax.naming.ConfigurationException;
22+
23+
import org.apache.cloudstack.acl.AclPolicyPermissionMapVO;
24+
25+
import com.cloud.utils.db.GenericDaoBase;
26+
27+
public class AclPolicyPermissionMapDaoImpl extends GenericDaoBase<AclPolicyPermissionMapVO, Long> implements
28+
AclPolicyPermissionMapDao {
29+
30+
public AclPolicyPermissionMapDaoImpl()
31+
{
32+
33+
}
34+
35+
@Override
36+
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
37+
super.configure(name, params);
38+
39+
return true;
40+
}
41+
42+
43+
}

‎server/src/com/cloud/api/ApiServer.java‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
import javax.servlet.http.HttpSession;
5353

5454
import org.apache.cloudstack.acl.APIChecker;
55+
import org.apache.cloudstack.acl.AclPermissionVO;
56+
import org.apache.cloudstack.acl.AclPolicyPermissionMapVO;
57+
import org.apache.cloudstack.acl.PermissionScope;
58+
import org.apache.cloudstack.acl.RoleType;
59+
import org.apache.cloudstack.acl.AclPermission.Permission;
60+
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
61+
import org.apache.cloudstack.acl.dao.AclPermissionDao;
62+
import org.apache.cloudstack.acl.dao.AclPolicyPermissionMapDao;
5563
import org.apache.cloudstack.api.APICommand;
5664
import org.apache.cloudstack.api.ApiErrorCode;
5765
import org.apache.cloudstack.api.BaseAsyncCmd;
@@ -171,6 +179,10 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer
171179

172180
List<PluggableService> _pluggableServices;
173181
List<APIChecker> _apiAccessCheckers;
182+
@Inject
183+
private AclPermissionDao _aclPermissionDao;
184+
@Inject
185+
private AclPolicyPermissionMapDao _aclPolicyPermissionMapDao;
174186

175187
@Inject
176188
protected ApiAsyncJobDispatcher _asyncDispatcher;
@@ -233,6 +245,51 @@ public boolean start() {
233245
_apiNameCmdClassMap.put(apiName, apiCmdList);
234246
}
235247
apiCmdList.add(cmdClass);
248+
249+
boolean isReadCommand = false;
250+
BaseCmd cmdObj;
251+
try {
252+
cmdObj = (BaseCmd) cmdClass.newInstance();
253+
if (cmdObj instanceof BaseListCmd) {
254+
isReadCommand = true;
255+
}
256+
} catch (Exception e) {
257+
}
258+
259+
for (RoleType role : at.authorized()) {
260+
AclPermissionVO apiPermission = null;
261+
switch (role) {
262+
case User:
263+
apiPermission = new AclPermissionVO(apiName, null, null, PermissionScope.ACCOUNT, null,
264+
Permission.Allow);
265+
break;
266+
267+
case Admin:
268+
apiPermission = new AclPermissionVO(apiName, null, null, PermissionScope.ALL, null,
269+
Permission.Allow);
270+
break;
271+
272+
case DomainAdmin:
273+
apiPermission = new AclPermissionVO(apiName, null, null, PermissionScope.DOMAIN, null,
274+
Permission.Allow);
275+
break;
276+
277+
case ResourceAdmin:
278+
apiPermission = new AclPermissionVO(apiName, null, null, PermissionScope.DOMAIN, null,
279+
Permission.Allow);
280+
break;
281+
}
282+
283+
if (apiPermission != null) {
284+
if (isReadCommand) {
285+
apiPermission.setAccessType(AccessType.ListEntry);
286+
}
287+
_aclPermissionDao.persist(apiPermission);
288+
AclPolicyPermissionMapVO policyPermMapEntry = new AclPolicyPermissionMapVO(role.ordinal() + 1,
289+
apiPermission.getId());
290+
_aclPolicyPermissionMapDao.persist(policyPermMapEntry);
291+
}
292+
}
236293
}
237294

238295
encodeApiResponse = Boolean.valueOf(_configDao.getValue(Config.EncodeApiResponse.key()));

0 commit comments

Comments
 (0)