Skip to content

Commit 3c2c98a

Browse files
API keypair restructure
1 parent b8813c7 commit 3c2c98a

File tree

88 files changed

+3241
-606
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+3241
-606
lines changed

api/src/main/java/com/cloud/event/EventTypes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,9 @@ public class EventTypes {
298298
public static final String EVENT_REGISTER_CNI_CONFIG = "REGISTER.CNI.CONFIG";
299299
public static final String EVENT_DELETE_CNI_CONFIG = "DELETE.CNI.CONFIG";
300300

301-
//register for user API and secret keys
301+
//user API and secret keys
302302
public static final String EVENT_REGISTER_FOR_SECRET_API_KEY = "REGISTER.USER.KEY";
303+
public static final String EVENT_DELETE_SECRET_API_KEY = "DELETE.USER.KEY";
303304
public static final String API_KEY_ACCESS_UPDATE = "API.KEY.ACCESS.UPDATE";
304305

305306
// Template Events

api/src/main/java/com/cloud/user/AccountService.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import org.apache.cloudstack.acl.ControlledEntity;
2424
import org.apache.cloudstack.acl.RoleType;
2525
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
26+
import org.apache.cloudstack.acl.apikeypair.ApiKeyPair;
27+
import org.apache.cloudstack.acl.apikeypair.ApiKeyPairPermission;
28+
import org.apache.cloudstack.api.BaseCmd;
2629
import org.apache.cloudstack.api.command.admin.account.CreateAccountCmd;
27-
import org.apache.cloudstack.api.command.admin.user.GetUserKeysCmd;
28-
import org.apache.cloudstack.api.command.admin.user.RegisterUserKeyCmd;
29-
import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd;
3030

3131
import com.cloud.dc.DataCenter;
3232
import com.cloud.domain.Domain;
@@ -35,6 +35,14 @@
3535
import com.cloud.offering.DiskOffering;
3636
import com.cloud.offering.NetworkOffering;
3737
import com.cloud.offering.ServiceOffering;
38+
import org.apache.cloudstack.api.command.admin.user.DeleteUserKeysCmd;
39+
import org.apache.cloudstack.api.command.admin.user.GetUserKeysCmd;
40+
import org.apache.cloudstack.api.command.admin.user.ListUserKeyRulesCmd;
41+
import org.apache.cloudstack.api.command.admin.user.ListUserKeysCmd;
42+
import org.apache.cloudstack.api.command.admin.user.RegisterUserKeysCmd;
43+
import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd;
44+
import org.apache.cloudstack.api.response.ApiKeyPairResponse;
45+
import org.apache.cloudstack.api.response.ListResponse;
3846
import org.apache.cloudstack.auth.UserTwoFactorAuthenticator;
3947

4048
public interface AccountService {
@@ -95,7 +103,7 @@ User createUser(String userName, String password, String firstName, String lastN
95103

96104
void markUserRegistered(long userId);
97105

98-
public String[] createApiKeyAndSecretKey(RegisterUserKeyCmd cmd);
106+
ApiKeyPair createApiKeyAndSecretKey(RegisterUserKeysCmd cmd);
99107

100108
public String[] createApiKeyAndSecretKey(final long userId);
101109

@@ -121,6 +129,8 @@ User createUser(String userName, String password, String firstName, String lastN
121129

122130
void validateAccountHasAccessToResource(Account account, AccessType accessType, Object resource);
123131

132+
void validateCallingUserHasAccessToDesiredUser(Long userId);
133+
124134
Long finalyzeAccountId(String accountName, Long domainId, Long projectId, boolean enabledOnly);
125135

126136
/**
@@ -130,9 +140,15 @@ User createUser(String userName, String password, String firstName, String lastN
130140
*/
131141
UserAccount getUserAccountById(Long userId);
132142

133-
public Pair<Boolean, Map<String, String>> getKeys(GetUserKeysCmd cmd);
143+
Pair<Boolean, Map<String, String>> getKeys(GetUserKeysCmd cmd);
144+
145+
ListResponse<ApiKeyPairResponse> listKeys(ListUserKeysCmd cmd);
134146

135-
public Pair<Boolean, Map<String, String>> getKeys(Long userId);
147+
List<ApiKeyPairPermission> listKeyRules(ListUserKeyRulesCmd cmd);
148+
149+
void deleteApiKey(DeleteUserKeysCmd cmd);
150+
151+
void deleteApiKey(ApiKeyPair id);
136152

137153
/**
138154
* Lists user two-factor authentication provider plugins
@@ -147,4 +163,11 @@ User createUser(String userName, String password, String firstName, String lastN
147163
*/
148164
UserTwoFactorAuthenticator getUserTwoFactorAuthenticationProvider(final Long domainId);
149165

166+
ApiKeyPair getLatestUserKeyPair(Long userId);
167+
168+
ApiKeyPair getKeyPairById(Long id);
169+
170+
ApiKeyPair getKeyPairByApiKey(String apiKey);
171+
172+
String getAccessingApiKey(BaseCmd cmd);
150173
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 com.cloud.user;
18+
19+
public enum ApiKeyPairState {
20+
ENABLED, REMOVED, EXPIRED
21+
}

api/src/main/java/com/cloud/user/User.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ public enum Source {
6565

6666
public void setState(Account.State state);
6767

68-
public String getApiKey();
69-
70-
public void setApiKey(String apiKey);
71-
72-
public String getSecretKey();
73-
74-
public void setSecretKey(String secretKey);
75-
7668
public String getTimezone();
7769

7870
public void setTimezone(String timezone);

api/src/main/java/com/cloud/user/UserAccount.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ public interface UserAccount extends InternalIdentity {
3939

4040
String getState();
4141

42-
String getApiKey();
43-
44-
String getSecretKey();
45-
4642
Date getCreated();
4743

4844
Date getRemoved();

api/src/main/java/org/apache/cloudstack/acl/APIChecker.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.cloud.user.Account;
2121
import com.cloud.user.User;
2222
import com.cloud.utils.component.Adapter;
23+
import org.apache.cloudstack.acl.apikeypair.ApiKeyPairPermission;
2324

2425
import java.util.List;
2526

@@ -31,8 +32,8 @@ public interface APIChecker extends Adapter {
3132
// If true, apiChecker has checked the operation
3233
// If false, apiChecker is unable to handle the operation or not implemented
3334
// On exception, checkAccess failed don't allow
34-
boolean checkAccess(User user, String apiCommandName) throws PermissionDeniedException;
35-
boolean checkAccess(Account account, String apiCommandName) throws PermissionDeniedException;
35+
boolean checkAccess(User user, String apiCommandName, ApiKeyPairPermission... apiKeyPairPermissions) throws PermissionDeniedException;
36+
boolean checkAccess(Account account, String apiCommandName, ApiKeyPairPermission... apiKeyPairPermissions) throws PermissionDeniedException;
3637
/**
3738
* Verifies if the account has permission for the given list of APIs and returns only the allowed ones.
3839
*

api/src/main/java/org/apache/cloudstack/acl/RoleService.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,26 @@ public interface RoleService {
104104

105105
List<RolePermission> findAllPermissionsBy(Long roleId);
106106

107+
List<RolePermissionEntity> findAllRolePermissionsEntityBy(Long roleId);
108+
107109
Permission getRolePermission(String permission);
110+
111+
int removeRolesIfNeeded(List<? extends Role> roles);
112+
113+
/**
114+
* Checks if the role of the caller account has compatible permissions of the specified role permissions.
115+
* For each permission of the roleToAccess, the role of the caller needs to contain the same permission.
116+
*
117+
* @param rolePermissions the permissions of the caller role.
118+
* @param rolePermissionsToAccess the permissions for the role that the caller role wants to access.
119+
* @return True if the role can be accessed with the given permissions; false otherwise.
120+
*/
121+
boolean roleHasPermission(Map<String, Permission> rolePermissions, List<RolePermissionEntity> rolePermissionsToAccess);
122+
123+
/**
124+
* Given a list of role permissions, returns a {@link Map} containing the API name as the key and the {@link Permission} for the API as the value.
125+
*
126+
* @param rolePermissions Permissions for the role from role.
127+
*/
128+
Map<String, Permission> getRoleRulesAndPermissions(List<RolePermissionEntity> rolePermissions);
108129
}

api/src/main/java/org/apache/cloudstack/acl/Rule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@
2525

2626
public final class Rule {
2727
private final String rule;
28+
private final Pattern matchingPattern;
2829
private final static Pattern ALLOWED_PATTERN = Pattern.compile("^[a-zA-Z0-9*]+$");
2930

3031
public Rule(final String rule) {
3132
validate(rule);
3233
this.rule = rule;
34+
matchingPattern = Pattern.compile(rule.toLowerCase().replace("*", "(\\w*\\*?)+"));
3335
}
3436

3537
public boolean matches(final String commandName) {
36-
return StringUtils.isNotEmpty(commandName)
37-
&& commandName.toLowerCase().matches(rule.toLowerCase().replace("*", "\\w*"));
38+
return StringUtils.isNotEmpty(commandName) &&
39+
matchingPattern.matcher(commandName.toLowerCase()).matches();
3840
}
3941

4042
public String getRuleString() {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.apikeypair;
18+
19+
import org.apache.cloudstack.acl.ControlledEntity;
20+
import org.apache.cloudstack.api.Identity;
21+
import org.apache.cloudstack.api.InternalIdentity;
22+
23+
import java.util.Date;
24+
25+
public interface ApiKeyPair extends ControlledEntity, InternalIdentity, Identity {
26+
Long getUserId();
27+
Date getStartDate();
28+
Date getEndDate();
29+
Date getCreated();
30+
String getDescription();
31+
String getApiKey();
32+
String getSecretKey();
33+
String getName();
34+
Date getRemoved();
35+
void setRemoved(Date date);
36+
void validateDate();
37+
boolean hasEndDatePassed();
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.apikeypair;
18+
19+
import org.apache.cloudstack.acl.RolePermissionEntity;
20+
21+
public interface ApiKeyPairPermission extends RolePermissionEntity {
22+
long getApiKeyPairId();
23+
}

0 commit comments

Comments
 (0)