Skip to content

Commit f2bbf62

Browse files
author
kishan
committed
Added getUser API to get user details using API key. Services like S3 can user this API to authenticate. API is admin only.
1 parent 2b2e491 commit f2bbf62

6 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.api.commands;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import com.cloud.api.ApiConstants;
22+
import com.cloud.api.BaseCmd;
23+
import com.cloud.api.Implementation;
24+
import com.cloud.api.Parameter;
25+
import com.cloud.api.response.UserResponse;
26+
import com.cloud.exception.InvalidParameterValueException;
27+
import com.cloud.user.UserAccount;
28+
29+
@Implementation(description="Find user account by API key", responseObject=UserResponse.class)
30+
public class GetUserCmd extends BaseCmd {
31+
public static final Logger s_logger = Logger.getLogger(GetUserCmd.class.getName());
32+
33+
private static final String s_name = "getuserresponse";
34+
35+
/////////////////////////////////////////////////////
36+
//////////////// API parameters /////////////////////
37+
/////////////////////////////////////////////////////
38+
39+
@Parameter(name=ApiConstants.API_KEY, type=CommandType.STRING, required=true, description="API key of the user")
40+
private String apiKey;
41+
42+
/////////////////////////////////////////////////////
43+
/////////////////// Accessors ///////////////////////
44+
/////////////////////////////////////////////////////
45+
46+
public String getApiKey() {
47+
return apiKey;
48+
}
49+
50+
/////////////////////////////////////////////////////
51+
/////////////// API Implementation///////////////////
52+
/////////////////////////////////////////////////////
53+
54+
@Override
55+
public String getCommandName() {
56+
return s_name;
57+
}
58+
59+
@Override
60+
public long getEntityOwnerId() {
61+
return 0;
62+
}
63+
64+
@Override
65+
public void execute(){
66+
UserAccount result = _accountService.getUserByApiKey(getApiKey());
67+
if(result != null){
68+
UserResponse response = _responseGenerator.createUserResponse(result);
69+
response.setResponseName(getCommandName());
70+
response.setResponseName(getCommandName());
71+
this.setResponseObject(response);
72+
} else {
73+
throw new InvalidParameterValueException("User with specified API key does not exist");
74+
}
75+
}
76+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ UserAccount createUserAccount(String userName, String password, String firstName
196196
List<? extends UserAccount> searchForUsers(ListUsersCmd cmd)
197197
throws PermissionDeniedException;
198198

199+
UserAccount getUserByApiKey(String apiKey);
200+
199201
void checkAccess(Account account, Domain domain) throws PermissionDeniedException;
200202

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

‎client/tomcatconf/commands.properties.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ listUsers=com.cloud.api.commands.ListUsersCmd;7
1919
####lockUser=com.cloud.api.commands.LockUserCmd;7
2020
disableUser=com.cloud.api.commands.DisableUserCmd;7
2121
enableUser=com.cloud.api.commands.EnableUserCmd;7
22+
getUser=com.cloud.api.commands.GetUserCmd;1
2223

2324
#### Domain commands
2425
createDomain=com.cloud.api.commands.CreateDomainCmd;1

‎server/src/com/cloud/user/AccountManagerImpl.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,4 +2225,9 @@ public void buildACLSearchParameters(Account caller, Long id, String accountName
22252225

22262226
}
22272227
}
2228+
2229+
@Override
2230+
public UserAccount getUserByApiKey(String apiKey) {
2231+
return _userAccountDao.getUserByApiKey(apiKey);
2232+
}
22282233
}

‎server/src/com/cloud/user/dao/UserAccountDao.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
public interface UserAccountDao extends GenericDao<UserAccountVO, Long> {
2424
UserAccount getUserAccount(String username, Long domainId);
2525
boolean validateUsernameInDomain(String username, Long domainId);
26+
UserAccount getUserByApiKey(String apiKey);
2627
}

‎server/src/com/cloud/user/dao/UserAccountDaoImpl.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,20 @@
2121
import com.cloud.user.UserAccount;
2222
import com.cloud.user.UserAccountVO;
2323
import com.cloud.utils.db.GenericDaoBase;
24+
import com.cloud.utils.db.SearchBuilder;
2425
import com.cloud.utils.db.SearchCriteria;
2526

2627
@Local(value={UserAccountDao.class})
2728
public class UserAccountDaoImpl extends GenericDaoBase<UserAccountVO, Long> implements UserAccountDao {
29+
30+
protected final SearchBuilder<UserAccountVO> userAccountSearch;
31+
32+
protected UserAccountDaoImpl() {
33+
userAccountSearch = createSearchBuilder();
34+
userAccountSearch.and("apiKey", userAccountSearch.entity().getApiKey(), SearchCriteria.Op.EQ);
35+
userAccountSearch.done();
36+
}
37+
2838
@Override
2939
public UserAccount getUserAccount(String username, Long domainId) {
3040
if ((username == null) || (domainId == null)) {
@@ -45,4 +55,11 @@ public boolean validateUsernameInDomain(String username, Long domainId) {
4555
}
4656
return false;
4757
}
58+
59+
@Override
60+
public UserAccount getUserByApiKey(String apiKey) {
61+
SearchCriteria<UserAccountVO> sc = userAccountSearch.create();
62+
sc.setParameters("apiKey",apiKey);
63+
return findOneBy(sc);
64+
}
4865
}

0 commit comments

Comments
 (0)