Skip to content

Commit 112e7da

Browse files
committed
concord-server: fix API key creation for the current user
1 parent 8887c50 commit 112e7da

File tree

2 files changed

+42
-4
lines changed
  • it/server/src/test/java/com/walmartlabs/concord/it/server
  • server/impl/src/main/java/com/walmartlabs/concord/server/security/apikey

2 files changed

+42
-4
lines changed

it/server/src/test/java/com/walmartlabs/concord/it/server/ApiKeyIT.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
import com.walmartlabs.concord.client2.*;
2424
import org.junit.jupiter.api.Test;
2525

26-
import static org.junit.jupiter.api.Assertions.assertTrue;
27-
import static org.junit.jupiter.api.Assertions.fail;
26+
import java.util.List;
27+
28+
import static org.junit.jupiter.api.Assertions.*;
2829

2930
public class ApiKeyIT extends AbstractServerIT {
3031

@@ -62,4 +63,38 @@ public void testOwner() throws Exception {
6263
cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest().username(userAName));
6364
assertTrue(cakr.getOk());
6465
}
66+
67+
@Test
68+
public void testCreatingKeyWithoutUsername() throws Exception {
69+
String userName = "userA_" + randomString();
70+
71+
UsersApi usersApi = new UsersApi(getApiClient());
72+
CreateUserResponse user = usersApi.createOrUpdateUser(new CreateUserRequest()
73+
.username(userName)
74+
.type(CreateUserRequest.TypeEnum.LOCAL));
75+
76+
// the new user has no api keys initially
77+
78+
ApiKeysApi apiKeyResource = new ApiKeysApi(getApiClient());
79+
List<ApiKeyEntry> keys = apiKeyResource.listUserApiKeys(user.getId());
80+
assertEquals(0, keys.size());
81+
82+
// admin creates a new api key for the new user
83+
84+
CreateApiKeyResponse cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest().username(userName));
85+
assertTrue(cakr.getOk());
86+
keys = apiKeyResource.listUserApiKeys(user.getId());
87+
assertEquals(1, keys.size());
88+
89+
// the new user creates another api key for themselves
90+
91+
setApiKey(cakr.getKey());
92+
cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest());
93+
assertTrue(cakr.getOk());
94+
95+
// the new user lists all their api keys (should be 2)
96+
97+
keys = apiKeyResource.listUserApiKeys(user.getId());
98+
assertEquals(2, keys.size());
99+
}
65100
}

server/impl/src/main/java/com/walmartlabs/concord/server/security/apikey/ApiKeyManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,18 @@ public ApiKeyManager(ApiKeyConfiguration cfg,
6868
this.auditLog = requireNonNull(auditLog);
6969
}
7070

71-
7271
public CreateApiKeyResponse create(CreateApiKeyRequest req) {
7372
String key = assertKeyValue(req);
7473

7574
UUID userId = assertUserId(req.getUserId());
7675
if (userId == null) {
7776
userId = assertUsername(req.getUsername(), req.getUserDomain(), req.getUserType());
7877
}
79-
78+
79+
if (userId == null) {
80+
userId = UserPrincipal.assertCurrent().getId();
81+
}
82+
8083
assertOwner(userId);
8184

8285
String name = trim(req.getName());

0 commit comments

Comments
 (0)