Skip to content

Commit 690073c

Browse files
committed
Add ApiKeyAccess status in User InfoCard for Users if Api key is generated
1 parent 7858d0f commit 690073c

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.Map;
2121

22+
import com.cloud.utils.Pair;
2223
import org.apache.cloudstack.acl.ControlledEntity;
2324
import org.apache.cloudstack.acl.RoleType;
2425
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -125,9 +126,9 @@ User createUser(String userName, String password, String firstName, String lastN
125126
*/
126127
UserAccount getUserAccountById(Long userId);
127128

128-
public Map<String, String> getKeys(GetUserKeysCmd cmd);
129+
public Pair<Boolean, Map<String, String>> getKeys(GetUserKeysCmd cmd);
129130

130-
public Map<String, String> getKeys(Long userId);
131+
public Pair<Boolean, Map<String, String>> getKeys(Long userId);
131132

132133
/**
133134
* Lists user two-factor authentication provider plugins

api/src/main/java/org/apache/cloudstack/api/command/admin/user/GetUserKeysCmd.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.cloud.user.Account;
2222
import com.cloud.user.User;
23+
import com.cloud.utils.Pair;
2324
import org.apache.cloudstack.acl.RoleType;
2425
import org.apache.cloudstack.api.APICommand;
2526
import org.apache.cloudstack.api.ApiConstants;
@@ -54,11 +55,13 @@ public Long getID(){
5455
else return Account.ACCOUNT_ID_SYSTEM;
5556
}
5657
public void execute(){
57-
Map<String, String> keys = _accountService.getKeys(this);
58+
Pair<Boolean, Map<String, String>> keys = _accountService.getKeys(this);
59+
5860
RegisterResponse response = new RegisterResponse();
5961
if(keys != null){
60-
response.setApiKey(keys.get("apikey"));
61-
response.setSecretKey(keys.get("secretkey"));
62+
response.setApiKeyAccess(keys.first());
63+
response.setApiKey(keys.second().get("apikey"));
64+
response.setSecretKey(keys.second().get("secretkey"));
6265
}
6366

6467
response.setObjectName("userkeys");

api/src/main/java/org/apache/cloudstack/api/response/RegisterResponse.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@
1818

1919
import com.google.gson.annotations.SerializedName;
2020

21+
import org.apache.cloudstack.api.ApiConstants;
2122
import org.apache.cloudstack.api.BaseResponse;
2223

2324
import com.cloud.serializer.Param;
2425

2526
public class RegisterResponse extends BaseResponse {
26-
@SerializedName("apikey")
27+
@SerializedName(ApiConstants.API_KEY)
2728
@Param(description = "the api key of the registered user", isSensitive = true)
2829
private String apiKey;
2930

30-
@SerializedName("secretkey")
31+
@SerializedName(ApiConstants.SECRET_KEY)
3132
@Param(description = "the secret key of the registered user", isSensitive = true)
3233
private String secretKey;
3334

35+
@SerializedName(ApiConstants.API_KEY_ACCESS)
36+
@Param(description = "whether api key access is allowed or not", isSensitive = true)
37+
private Boolean apiKeyAccess;
38+
3439
public String getApiKey() {
3540
return apiKey;
3641
}
@@ -46,4 +51,8 @@ public String getSecretKey() {
4651
public void setSecretKey(String secretKey) {
4752
this.secretKey = secretKey;
4853
}
54+
55+
public void setApiKeyAccess(Boolean apiKeyAccess) {
56+
this.apiKeyAccess = apiKeyAccess;
57+
}
4958
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,18 +2814,18 @@ public Pair<User, Account> findUserByApiKey(String apiKey) {
28142814
}
28152815

28162816
@Override
2817-
public Map<String, String> getKeys(GetUserKeysCmd cmd) {
2817+
public Pair<Boolean, Map<String, String>> getKeys(GetUserKeysCmd cmd) {
28182818
final long userId = cmd.getID();
28192819
return getKeys(userId);
28202820
}
28212821

28222822
@Override
2823-
public Map<String, String> getKeys(Long userId) {
2823+
public Pair<Boolean, Map<String, String>> getKeys(Long userId) {
28242824
User user = getActiveUser(userId);
28252825
if (user == null) {
28262826
throw new InvalidParameterValueException("Unable to find user by id");
28272827
}
2828-
final ControlledEntity account = getAccount(getUserAccountById(userId).getAccountId()); //Extracting the Account from the userID of the requested user.
2828+
final Account account = getAccount(getUserAccountById(userId).getAccountId()); //Extracting the Account from the userID of the requested user.
28292829
User caller = CallContext.current().getCallingUser();
28302830
preventRootDomainAdminAccessToRootAdminKeys(caller, account);
28312831
checkAccess(caller, account);
@@ -2834,7 +2834,15 @@ public Map<String, String> getKeys(Long userId) {
28342834
keys.put("apikey", user.getApiKey());
28352835
keys.put("secretkey", user.getSecretKey());
28362836

2837-
return keys;
2837+
Boolean apiKeyAccess = user.getApiKeyAccess();
2838+
if (apiKeyAccess == null) {
2839+
apiKeyAccess = account.getApiKeyAccess();
2840+
if (apiKeyAccess == null) {
2841+
apiKeyAccess = AccountManagerImpl.apiKeyAccess.valueIn(account.getDomainId());
2842+
}
2843+
}
2844+
2845+
return new Pair<Boolean, Map<String, String>>(apiKeyAccess, keys);
28382846
}
28392847

28402848
protected void preventRootDomainAdminAccessToRootAdminKeys(User caller, ControlledEntity account) {

server/src/test/java/com/cloud/user/MockAccountManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,12 @@ public Long finalyzeAccountId(String accountName, Long domainId, Long projectId,
445445
}
446446

447447
@Override
448-
public Map<String, String> getKeys(GetUserKeysCmd cmd) {
448+
public Pair<Boolean, Map<String, String>> getKeys(GetUserKeysCmd cmd) {
449449
return null;
450450
}
451451

452452
@Override
453-
public Map<String, String> getKeys(Long userId) {
453+
public Pair<Boolean, Map<String, String>> getKeys(Long userId) {
454454
return null;
455455
}
456456

ui/src/components/view/InfoCard.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,12 @@
735735

736736
<div class="account-center-tags" v-if="showKeys">
737737
<a-divider/>
738+
<div class="resource-detail-item" v-if="resource.userapikeyaccess && !isAdmin()">
739+
<div class="resource-detail-item__label">{{ $t('label.apikeyaccess') }}</div>
740+
<div class="resource-detail-item__details">
741+
<status class="status" :text="resource.userapikeyaccess" displayText/>
742+
</div>
743+
</div>
738744
<div class="user-keys">
739745
<key-outlined />
740746
<strong>
@@ -1083,6 +1089,7 @@ export default {
10831089
api('getUserKeys', { id: this.resource.id }).then(json => {
10841090
this.showKeys = true
10851091
this.newResource.secretkey = json.getuserkeysresponse.userkeys.secretkey
1092+
this.newResource.userapikeyaccess = json.getuserkeysresponse.userkeys.apikeyaccess ? 'Enabled' : 'Disabled'
10861093
this.$emit('change-resource', this.newResource)
10871094
})
10881095
},
@@ -1113,6 +1120,9 @@ export default {
11131120
(this.resource.domainid === this.$store.getters.userInfo.domainid && this.resource.account === this.$store.getters.userInfo.account) ||
11141121
(this.resource.project && this.resource.projectid === this.$store.getters.project.id)
11151122
},
1123+
isAdmin () {
1124+
return ['Admin'].includes(this.$store.getters.userInfo.roletype)
1125+
},
11161126
showInput () {
11171127
this.inputVisible = true
11181128
this.$nextTick(function () {

0 commit comments

Comments
 (0)