Skip to content
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

Api keypair restructure #9504

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1257678
initial commit of api keypair restrucure
Jul 30, 2024
2e5de1e
making some fixes
Jul 30, 2024
7bfdfb4
small fixes and deleting apikeys on erasure of user and account
Jul 31, 2024
ac14de2
fixing NPE on user edition authored-by: hsato03
Jul 31, 2024
16fe2f0
small tweaks
Jul 31, 2024
378fc4f
fix tests
Jul 31, 2024
b5a8972
Merge branch 'main' into api-keypair-restructure
KlausDornsbach Aug 8, 2024
a7057c5
fixing mistake on test
Aug 12, 2024
989b4c3
fixing compilation failure
Aug 14, 2024
b0ba748
fix checkstyles
Aug 19, 2024
83bdb68
fix version
Aug 20, 2024
53ba8e9
Merge branch 'main' into api-keypair-restructure
KlausDornsbach Aug 20, 2024
33ae518
fix lint, ty Dahn
Aug 20, 2024
2b910d0
fix another compilation mistake, it compiles locally
Aug 20, 2024
a2e9aba
fix building with no redist packages, should pass tests now
Aug 21, 2024
b40e777
refactoring RegisterCmd to RegisterUserKeysCmd and userid to id in li…
Aug 28, 2024
5016f3c
Merge branch 'main' into api-keypair-restructure
KlausDornsbach Aug 28, 2024
95e79fc
fixing one last reference to old RegisterCmd class
Aug 29, 2024
72a62ed
fixing null pointer when calling getUserKeys and the user did not hav…
Sep 6, 2024
62259f7
Merge branch 'main' into api-keypair-restructure
KlausDornsbach Sep 6, 2024
ef69e83
Merge branch 'main' into api-keypair-restructure
KlausDornsbach Sep 11, 2024
4591e23
fixing marvin test
Sep 20, 2024
03632d5
fix checkstyles
Sep 25, 2024
4817b13
fixing asterisk, refactoring apikey permissions check
Oct 16, 2024
5cb0eb9
Merge branch 'main' into api-keypair-restructure
KlausDornsbach Oct 16, 2024
61e0940
fix db migration logic, list permissions with keys
Oct 17, 2024
b950353
fixing tests
Oct 22, 2024
f5efd4e
fix noredist
Oct 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/src/main/java/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,9 @@ public class EventTypes {
//registering userdata events
public static final String EVENT_REGISTER_USER_DATA = "REGISTER.USER.DATA";

//register for user API and secret keys
// User API and secret keys
public static final String EVENT_REGISTER_FOR_SECRET_API_KEY = "REGISTER.USER.KEY";
public static final String EVENT_DELETE_SECRET_API_KEY = "DELETE.USER.KEY";

// Template Events
public static final String EVENT_TEMPLATE_CREATE = "TEMPLATE.CREATE";
Expand Down
30 changes: 26 additions & 4 deletions api/src/main/java/com/cloud/user/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.acl.apikeypair.ApiKeyPair;
import org.apache.cloudstack.acl.apikeypair.ApiKeyPairPermission;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.command.admin.account.CreateAccountCmd;
import org.apache.cloudstack.api.command.admin.user.DeleteUserKeysCmd;
import org.apache.cloudstack.api.command.admin.user.GetUserKeysCmd;
import org.apache.cloudstack.api.command.admin.user.RegisterCmd;
import org.apache.cloudstack.api.command.admin.user.ListUserKeyRulesCmd;
import org.apache.cloudstack.api.command.admin.user.ListUserKeysCmd;
import org.apache.cloudstack.api.command.admin.user.RegisterUserKeysCmd;
import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd;

import com.cloud.dc.DataCenter;
Expand All @@ -34,6 +40,8 @@
import com.cloud.offering.DiskOffering;
import com.cloud.offering.NetworkOffering;
import com.cloud.offering.ServiceOffering;
import org.apache.cloudstack.api.response.ApiKeyPairResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.auth.UserTwoFactorAuthenticator;

public interface AccountService {
Expand Down Expand Up @@ -92,7 +100,7 @@ User createUser(String userName, String password, String firstName, String lastN

void markUserRegistered(long userId);

public String[] createApiKeyAndSecretKey(RegisterCmd cmd);
public ApiKeyPair createApiKeyAndSecretKey(RegisterUserKeysCmd cmd);

public String[] createApiKeyAndSecretKey(final long userId);

Expand All @@ -116,6 +124,8 @@ User createUser(String userName, String password, String firstName, String lastN

void checkAccess(Account account, AccessType accessType, boolean sameOwner, String apiName, ControlledEntity... entities) throws PermissionDeniedException;

void validateCallingUserHasAccessToDesiredUser(Long userId);

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

Long finalyzeAccountId(String accountName, Long domainId, Long projectId, boolean enabledOnly);
Expand All @@ -127,10 +137,15 @@ User createUser(String userName, String password, String firstName, String lastN
*/
UserAccount getUserAccountById(Long userId);

public Map<String, String> getKeys(GetUserKeysCmd cmd);
Map<String, String> getKeys(GetUserKeysCmd cmd);

ListResponse<ApiKeyPairResponse> getKeys(ListUserKeysCmd cmd);

List<ApiKeyPairPermission> listKeyRules(ListUserKeyRulesCmd cmd);

public Map<String, String> getKeys(Long userId);
void deleteApiKey(DeleteUserKeysCmd cmd);

void deleteApiKey(ApiKeyPair id);
/**
* Lists user two-factor authentication provider plugins
* @return list of providers
Expand All @@ -144,4 +159,11 @@ User createUser(String userName, String password, String firstName, String lastN
*/
UserTwoFactorAuthenticator getUserTwoFactorAuthenticationProvider(final Long domainId);

ApiKeyPair getLatestUserKeyPair(Long userId);

ApiKeyPair getKeyPairById(Long id);

ApiKeyPair getKeyPairByApiKey(String apiKey);

String getAccessingApiKey (BaseCmd cmd);
}
21 changes: 21 additions & 0 deletions api/src/main/java/com/cloud/user/ApiKeyPairState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 com.cloud.user;

public enum ApiKeyPairState {
ENABLED, REMOVED, EXPIRED

Check warning on line 20 in api/src/main/java/com/cloud/user/ApiKeyPairState.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/com/cloud/user/ApiKeyPairState.java#L19-L20

Added lines #L19 - L20 were not covered by tests
}
8 changes: 0 additions & 8 deletions api/src/main/java/com/cloud/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ public enum Source {

public void setState(Account.State state);

public String getApiKey();

public void setApiKey(String apiKey);

public String getSecretKey();

public void setSecretKey(String secretKey);

public String getTimezone();

public void setTimezone(String timezone);
Expand Down
4 changes: 0 additions & 4 deletions api/src/main/java/com/cloud/user/UserAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public interface UserAccount extends InternalIdentity {

String getState();

String getApiKey();

String getSecretKey();

Date getCreated();

Date getRemoved();
Expand Down
6 changes: 4 additions & 2 deletions api/src/main/java/org/apache/cloudstack/acl/APIChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.cloud.user.Account;
import com.cloud.user.User;
import com.cloud.utils.component.Adapter;
import org.apache.cloudstack.acl.apikeypair.ApiKeyPairPermission;

import java.util.List;

Expand All @@ -31,8 +32,9 @@ public interface APIChecker extends Adapter {
// If true, apiChecker has checked the operation
// If false, apiChecker is unable to handle the operation or not implemented
// On exception, checkAccess failed don't allow
boolean checkAccess(User user, String apiCommandName) throws PermissionDeniedException;
boolean checkAccess(Account account, String apiCommandName) throws PermissionDeniedException;
boolean checkAccess(User user, String apiCommandName, ApiKeyPairPermission... apiKeyPairPermissions) throws PermissionDeniedException;
boolean checkAccess(Account account, String apiCommandName, ApiKeyPairPermission... apiKeyPairPermissions) throws PermissionDeniedException;

/**
* Verifies if the account has permission for the given list of APIs and returns only the allowed ones.
*
Expand Down
21 changes: 21 additions & 0 deletions api/src/main/java/org/apache/cloudstack/acl/RoleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,25 @@ public interface RoleService {
List<RolePermission> findAllPermissionsBy(Long roleId);

Permission getRolePermission(String permission);

List<RolePermissionEntity> findAllRolePermissionsEntityBy(Long roleId);

int removeRolesIfNeeded(List<? extends Role> roles);

/**
* Checks if the role of the caller account has compatible permissions of the specified role permissions.
* For each permission of the roleToAccess, the role of the caller needs to contain the same permission.
*
* @param rolePermissions the permissions of the caller role.
* @param rolePermissionsToAccess the permissions for the role that the caller role wants to access.
* @return True if the role can be accessed with the given permissions; false otherwise.
*/
boolean roleHasPermission(Map<String, Permission> rolePermissions, List<RolePermissionEntity> rolePermissionsToAccess);

/**
* 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.
*
* @param rolePermissions Permissions for the role from role.
*/
Map<String, Permission> getRoleRulesAndPermissions(List<RolePermissionEntity> rolePermissions);
}
6 changes: 4 additions & 2 deletions api/src/main/java/org/apache/cloudstack/acl/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@

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

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

public boolean matches(final String commandName) {
return StringUtils.isNotEmpty(commandName)
&& commandName.toLowerCase().matches(rule.toLowerCase().replace("*", "\\w*"));
return StringUtils.isNotEmpty(commandName) &&
matchingPattern.matcher(commandName.toLowerCase()).matches();
}

public String getRuleString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.apikeypair;

import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.api.Identity;
import org.apache.cloudstack.api.InternalIdentity;

import java.util.Date;

public interface ApiKeyPair extends ControlledEntity, InternalIdentity, Identity {
Long getUserId();
Date getStartDate();
Date getEndDate();
Date getCreated();
String getDescription();
String getApiKey();
String getSecretKey();
String getName();
Date getRemoved();
void setRemoved(Date date);
void validateDate();
boolean hasEndDatePassed();
}
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.acl.apikeypair;

import org.apache.cloudstack.acl.RolePermissionEntity;

public interface ApiKeyPairPermission extends RolePermissionEntity {
long getApiKeyPairId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.apikeypair;

import java.util.List;

public interface ApiKeyPairService {
List<ApiKeyPairPermission> findAllPermissionsByKeyPairId(Long apiKeyPairId, Long roleId);

ApiKeyPair findByApiKey(String apiKey);

ApiKeyPair findById(Long id);

void validateCallingUserHasAccessToDesiredUser(Long userId);
}
6 changes: 6 additions & 0 deletions api/src/main/java/org/apache/cloudstack/api/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class ApiConstants {
public static final String ACCOUNT = "account";
public static final String ACCOUNTS = "accounts";
public static final String ACCOUNT_NAME = "accountname";
public static final String ACCOUNT_TYPE = "accounttype";
public static final String ACCOUNT_ID = "accountid";
public static final String ACCOUNT_IDS = "accountids";
Expand All @@ -35,6 +36,8 @@ public class ApiConstants {
public static final String ALLOW_USER_FORCE_STOP_VM = "allowuserforcestopvm";
public static final String ANNOTATION = "annotation";
public static final String API_KEY = "apikey";
public static final String API_KEY_FILTER = "apikeyfilter";
public static final String HEADER_API_KEY = "apiKey";
public static final String ARCHIVED = "archived";
public static final String ARCH = "arch";
public static final String AS_NUMBER = "asnumber";
Expand Down Expand Up @@ -303,6 +306,7 @@ public class ApiConstants {
public static final String KEEPALIVE_ENABLED = "keepaliveenabled";
public static final String KERNEL_VERSION = "kernelversion";
public static final String KEY = "key";
public static final String KEYPAIR_ID = "keypairid";
public static final String LABEL = "label";
public static final String LASTNAME = "lastname";
public static final String LAST_BOOT = "lastboottime";
Expand Down Expand Up @@ -445,6 +449,7 @@ public class ApiConstants {
public static final String SHOW_RESOURCE_ICON = "showicon";
public static final String SHOW_INACTIVE = "showinactive";
public static final String SHOW_UNIQUE = "showunique";
public static final String SHOW_PERMISSIONS = "showpermissions";
public static final String SIGNATURE = "signature";
public static final String SIGNATURE_VERSION = "signatureversion";
public static final String SINCE = "since";
Expand Down Expand Up @@ -639,6 +644,7 @@ public class ApiConstants {
public static final String ROLE_TYPE = "roletype";
public static final String ROLE_NAME = "rolename";
public static final String PERMISSION = "permission";
public static final String PERMISSIONS = "permissions";
public static final String RULE = "rule";
public static final String RULES = "rules";
public static final String RULE_ID = "ruleid";
Expand Down
3 changes: 3 additions & 0 deletions api/src/main/java/org/apache/cloudstack/api/BaseCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.cloudstack.acl.ProjectRoleService;
import org.apache.cloudstack.acl.RoleService;
import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.acl.apikeypair.ApiKeyPairService;
import org.apache.cloudstack.affinity.AffinityGroupService;
import org.apache.cloudstack.alert.AlertService;
import org.apache.cloudstack.annotation.AnnotationService;
Expand Down Expand Up @@ -220,6 +221,8 @@ public static enum CommandType {
@Inject
public BucketApiService _bucketService;
@Inject
public ApiKeyPairService apiKeyPairService;
@Inject
public BGPService bgpService;

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
import com.cloud.bgp.ASNumber;
import com.cloud.bgp.ASNumberRange;

import org.apache.cloudstack.acl.apikeypair.ApiKeyPair;
import org.apache.cloudstack.acl.apikeypair.ApiKeyPairPermission;
import org.apache.cloudstack.api.response.ApiKeyPairResponse;
import org.apache.cloudstack.api.response.BaseRolePermissionResponse;
import org.apache.cloudstack.storage.object.Bucket;
import org.apache.cloudstack.affinity.AffinityGroup;
import org.apache.cloudstack.affinity.AffinityGroupResponse;
Expand Down Expand Up @@ -561,6 +565,10 @@ List<TemplateResponse> createTemplateResponses(ResponseView view, VirtualMachine

BucketResponse createBucketResponse(Bucket bucket);

ApiKeyPairResponse createKeyPairResponse(ApiKeyPair keyPair);

ListResponse<BaseRolePermissionResponse> createKeypairPermissionsResponse(List<ApiKeyPairPermission> permissions);

ASNRangeResponse createASNumberRangeResponse(ASNumberRange asnRange);

ASNumberResponse createASNumberResponse(ASNumber asn);
Expand Down
Loading
Loading