Repository navigation
CLOUDSTACK-8562: Dynamic Role-Based API Checker for CloudStack #1489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.cloudstack.acl; | ||
|
|
||
| import org.apache.cloudstack.api.Identity; | ||
| import org.apache.cloudstack.api.InternalIdentity; | ||
|
|
||
| public interface Role extends InternalIdentity, Identity { | ||
| String getName(); | ||
| RoleType getRoleType(); | ||
| String getDescription(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.cloudstack.acl; | ||
|
|
||
| import org.apache.cloudstack.api.Identity; | ||
| import org.apache.cloudstack.api.InternalIdentity; | ||
|
|
||
| public interface RolePermission extends InternalIdentity, Identity { | ||
| enum Permission {ALLOW, DENY} | ||
|
|
||
| long getRoleId(); | ||
| Rule getRule(); | ||
| Permission getPermission(); | ||
| String getDescription(); | ||
| long getSortOrder(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.cloudstack.acl; | ||
|
|
||
| import org.apache.cloudstack.framework.config.ConfigKey; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface RoleService { | ||
|
|
||
| ConfigKey<Boolean> EnableDynamicApiChecker = new ConfigKey<>("Advanced", Boolean.class, "dynamic.apichecker.enabled", "false", | ||
| "If set to true, this enables the dynamic role-based api access checker and disables the default static role-based api access checker.", | ||
| true); | ||
|
|
||
| boolean isEnabled(); | ||
| Role findRole(final Long id); | ||
| Role createRole(final String name, final RoleType roleType, final String description); | ||
| boolean updateRole(final Role role, final String name, final RoleType roleType, final String description); | ||
| boolean deleteRole(final Role role); | ||
|
|
||
| RolePermission findRolePermission(final Long id); | ||
| RolePermission findRolePermissionByUuid(final String uuid); | ||
|
|
||
| RolePermission createRolePermission(final Role role, final Rule rule, final RolePermission.Permission permission, final String description); | ||
| /** | ||
| * updateRolePermission updates the order/position of an role permission | ||
| * @param role The role whose permissions needs to be re-ordered | ||
| * @param newOrder The new list of ordered role permissions | ||
| */ | ||
| boolean updateRolePermission(final Role role, final List<RolePermission> newOrder); | ||
| boolean deleteRolePermission(final RolePermission rolePermission); | ||
|
|
||
| List<Role> listRoles(); | ||
| List<Role> findRolesByName(final String name); | ||
| List<Role> findRolesByType(final RoleType roleType); | ||
| List<RolePermission> findAllPermissionsBy(final Long roleId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,18 +16,90 @@ | |
| // under the License. | ||
| package org.apache.cloudstack.acl; | ||
|
|
||
| import com.cloud.user.Account; | ||
| import com.google.common.base.Enums; | ||
| import com.google.common.base.Strings; | ||
|
|
||
| // Enum for default roles in CloudStack | ||
| public enum RoleType { | ||
| Admin(1), ResourceAdmin(2), DomainAdmin(4), User(8), Unknown(0); | ||
| Admin(1L, Account.ACCOUNT_TYPE_ADMIN, 1), | ||
| ResourceAdmin(2L, Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN, 2), | ||
| DomainAdmin(3L, Account.ACCOUNT_TYPE_DOMAIN_ADMIN, 4), | ||
| User(4L, Account.ACCOUNT_TYPE_NORMAL, 8), | ||
| Unknown(-1L, (short) -1, 0); | ||
|
|
||
| private long id; | ||
| private short accountType; | ||
| private int mask; | ||
|
|
||
| private RoleType(int mask) { | ||
| RoleType(final long id, final short accountType, final int mask) { | ||
| this.id = id; | ||
| this.accountType = accountType; | ||
| this.mask = mask; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public short getAccountType() { | ||
| return accountType; | ||
| } | ||
|
|
||
| public int getMask() { | ||
| return mask; | ||
| } | ||
| } | ||
|
|
||
| public static RoleType fromString(final String name) { | ||
| if (!Strings.isNullOrEmpty(name) | ||
| && Enums.getIfPresent(RoleType.class, name).isPresent()) { | ||
| return RoleType.valueOf(name); | ||
| } | ||
| throw new IllegalStateException("Illegal RoleType name provided"); | ||
| } | ||
|
|
||
| public static RoleType fromMask(int mask) { | ||
| for (RoleType roleType : RoleType.values()) { | ||
| if (roleType.getMask() == mask) { | ||
| return roleType; | ||
| } | ||
| } | ||
| return Unknown; | ||
| } | ||
|
|
||
| public static RoleType getByAccountType(final short accountType) { | ||
| RoleType roleType = RoleType.Unknown; | ||
| switch (accountType) { | ||
| case Account.ACCOUNT_TYPE_ADMIN: | ||
| roleType = RoleType.Admin; | ||
| break; | ||
| case Account.ACCOUNT_TYPE_DOMAIN_ADMIN: | ||
| roleType = RoleType.DomainAdmin; | ||
| break; | ||
| case Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN: | ||
| roleType = RoleType.ResourceAdmin; | ||
| break; | ||
| case Account.ACCOUNT_TYPE_NORMAL: | ||
| roleType = RoleType.User; | ||
| break; | ||
| } | ||
| return roleType; | ||
| } | ||
|
|
||
| public static Long getRoleByAccountType(final Long roleId, final Short accountType) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you passing in a roleId, if you are getting a role id by account type?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for doing backward compatiblity in create APIs when roleId passed is null, but an account type is passed. This does the translation using above method. |
||
| if (roleId == null && accountType != null) { | ||
| RoleType defaultRoleType = RoleType.getByAccountType(accountType); | ||
| if (defaultRoleType != null && defaultRoleType != RoleType.Unknown) { | ||
| return defaultRoleType.getId(); | ||
| } | ||
| } | ||
| return roleId; | ||
| } | ||
|
|
||
| public static Short getAccountTypeByRole(final Role role, final Short accountType) { | ||
| if (role != null && role.getId() > 0L) { | ||
| return role.getRoleType().getAccountType(); | ||
| } | ||
| return accountType; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.cloudstack.acl; | ||
|
|
||
| import com.cloud.exception.InvalidParameterValueException; | ||
| import com.google.common.base.Strings; | ||
|
|
||
| import java.util.regex.Pattern; | ||
|
|
||
| public final class Rule { | ||
| private final String rule; | ||
| private final static Pattern ALLOWED_PATTERN = Pattern.compile("^[a-zA-Z0-9*]+$"); | ||
|
|
||
| public Rule(final String rule) { | ||
| validate(rule); | ||
| this.rule = rule; | ||
| } | ||
|
|
||
| public boolean matches(final String commandName) { | ||
| return !Strings.isNullOrEmpty(commandName) | ||
| && commandName.toLowerCase().matches(rule.toLowerCase().replace("*", "\\w*")); | ||
| } | ||
|
|
||
| public String getRuleString() { | ||
| return rule; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return rule; | ||
| } | ||
|
|
||
| private static boolean validate(final String rule) { | ||
| if (Strings.isNullOrEmpty(rule) || !ALLOWED_PATTERN.matcher(rule).matches()) { | ||
| throw new InvalidParameterValueException("Only API names and wildcards are allowed, invalid rule provided: " + rule); | ||
| } | ||
| return true; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jburwell I've refactored and move all rule related methods in this class and added more unit tests. The rule itself is immutable only compo-sable by the constructor. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.cloudstack.api; | ||
|
|
||
| public enum ApiArgValidator { | ||
| NotNullOrEmpty, // does Strings.isNullOrEmpty check | ||
| PositiveNumber, // does != null and > 0 check | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this role type? Is there any documentation on it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All four default cloudstack roles -- Admin, DomainAdmin, ResourceAdmin and User. It's just an enum class, that maps these role types to account types. This method previously existed in account mgr impl, I moved it here.
They've always existed. I could not find a previous FS on this, this is the oldest document on wiki referring to RoleTypes https://cwiki.apache.org/confluence/display/CLOUDSTACK/Annotations+use+in+the+API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant the ResourceAdmin specifically. What is this role? Is there any documentation on it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pdube I think this was used to support cpbm-cloudstack integration, I don't know any documentation; please google? For backward compatibility reasons, all four roles types supported by static-role-based-checked (as in commands.properties) need to be supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I had seen it in the code and looked for more information about it, but didn't find anything conclusive