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 15 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 @@ -284,8 +284,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
14 changes: 11 additions & 3 deletions api/src/main/java/com/cloud/user/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
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.api.command.admin.account.CreateAccountCmd;
import org.apache.cloudstack.api.command.admin.user.GetUserKeysCmd;
import org.apache.cloudstack.api.command.admin.user.ListUserKeysCmd;
import org.apache.cloudstack.api.command.admin.user.RegisterCmd;
import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd;

Expand All @@ -34,6 +36,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 +96,7 @@ User createUser(String userName, String password, String firstName, String lastN

void markUserRegistered(long userId);

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

public String[] createApiKeyAndSecretKey(final long userId);

Expand Down Expand Up @@ -125,9 +129,9 @@ 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);

public Map<String, String> getKeys(Long userId);
ListResponse<ApiKeyPairResponse> getKeys(ListUserKeysCmd cmd);

/**
* Lists user two-factor authentication provider plugins
Expand All @@ -142,4 +146,8 @@ User createUser(String userName, String password, String firstName, String lastN
*/
UserTwoFactorAuthenticator getUserTwoFactorAuthenticationProvider(final Long domainId);

ApiKeyPair getLatestUserKeyPair(Long userId);

ApiKeyPair getKeyPairById(Long id);

}
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
2 changes: 2 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 @@ -96,4 +96,6 @@ public interface RoleService {
List<RolePermission> findAllPermissionsBy(Long roleId);

Permission getRolePermission(String permission);

int removeRolesIfNeeded(List<? extends Role> roles);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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();
boolean validateDate(boolean throwException);
}
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,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.apikeypair;

import java.util.List;

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

ApiKeyPair findByApiKey(String apiKey);

ApiKeyPair findById(Long id);

void deleteApiKey(ApiKeyPair id);

void validateCallingUserHasAccessToDesiredUser(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,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
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 @@ -34,6 +34,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 @@ -217,6 +218,8 @@ public static enum CommandType {
public VnfTemplateManager vnfTemplateManager;
@Inject
public BucketApiService _bucketService;
@Inject
public ApiKeyPairService apiKeyPairService;


public abstract void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import java.util.Map;
import java.util.Set;

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 @@ -549,4 +553,9 @@ List<TemplateResponse> createTemplateResponses(ResponseView view, VirtualMachine
ObjectStoreResponse createObjectStoreResponse(ObjectStore os);

BucketResponse createBucketResponse(Bucket bucket);

ApiKeyPairResponse createKeyPairResponse(ApiKeyPair keyPair);

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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.command.admin.user;

import com.cloud.event.EventTypes;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.user.Account;
import org.apache.cloudstack.acl.apikeypair.ApiKeyPair;
import org.apache.cloudstack.api.ACL;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.response.ApiKeyPairResponse;
import org.apache.cloudstack.api.response.SuccessResponse;

@APICommand(name = "deleteUserKeys", description = "Deletes a keypair from a user", responseObject = SuccessResponse.class,
since = "4.20.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
public class DeleteUserKeysCmd extends BaseAsyncCmd {

@ACL
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = ApiKeyPairResponse.class, required = true, description = "ID of the keypair to be deleted.")
private Long id;

@Override
public ApiCommandResourceType getApiResourceType() {
return ApiCommandResourceType.User;
}

Check warning on line 43 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L41-L43

Added lines #L41 - L43 were not covered by tests

@Override
public long getEntityOwnerId() {
ApiKeyPair keyPair = apiKeyPairService.findById(id);

Check warning on line 47 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L46-L47

Added lines #L46 - L47 were not covered by tests
if (keyPair != null) {
return keyPair.getAccountId();

Check warning on line 49 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L49

Added line #L49 was not covered by tests
}
return Account.ACCOUNT_ID_SYSTEM;
}

Check warning on line 52 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L51-L52

Added lines #L51 - L52 were not covered by tests

private Long getId() {
return id;
}

Check warning on line 56 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L54-L56

Added lines #L54 - L56 were not covered by tests

@Override
public void execute() {
ApiKeyPair keyPair = apiKeyPairService.findById(getId());

Check warning on line 60 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L59-L60

Added lines #L59 - L60 were not covered by tests
if (keyPair == null) {
throw new InvalidParameterValueException(String.format("No keypair found with the id [%s].", getId()));

Check warning on line 62 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L62

Added line #L62 was not covered by tests
}
apiKeyPairService.validateCallingUserHasAccessToDesiredUser(keyPair.getUserId());

Check warning on line 64 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L64

Added line #L64 was not covered by tests

apiKeyPairService.deleteApiKey(keyPair);

Check warning on line 66 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L66

Added line #L66 was not covered by tests

SuccessResponse response = new SuccessResponse(getCommandName());
this.setResponseObject(response);
}

Check warning on line 70 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L68-L70

Added lines #L68 - L70 were not covered by tests

@Override
public String getEventType() {
return EventTypes.EVENT_DELETE_SECRET_API_KEY;
}

Check warning on line 75 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L73-L75

Added lines #L73 - L75 were not covered by tests

@Override
public String getEventDescription() {
return ("Deleting API keypair " + id);
}

Check warning on line 80 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L78-L80

Added lines #L78 - L80 were not covered by tests

@Override
public Long getSyncObjId() {
return getId();
}

Check warning on line 85 in api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/user/DeleteUserKeysCmd.java#L83-L85

Added lines #L83 - L85 were not covered by tests
}
Loading
Loading