Skip to content

Commit abe7540

Browse files
authored
concord-server: fix API key creation for the current user (#1222)
1 parent c7ed2a0 commit abe7540

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
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());

server/impl/src/main/java/com/walmartlabs/concord/server/security/github/GithubRealm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
7878
@WithTimer
7979
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
8080
UserPrincipal p = principals.oneByType(UserPrincipal.class);
81-
if (!REALM_NAME.equals(p.getRealm())) {
81+
if (p == null || !REALM_NAME.equals(p.getRealm())) {
8282
return null;
8383
}
8484

server/impl/src/main/java/com/walmartlabs/concord/server/security/internal/InternalRealm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
4242
@Override
4343
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
4444
UserPrincipal p = principals.oneByType(UserPrincipal.class);
45-
if (!REALM_NAME.equals(p.getRealm())) {
45+
if (p == null || !REALM_NAME.equals(p.getRealm())) {
4646
return null;
4747
}
4848

server/plugins/oidc/src/main/java/com/walmartlabs/concord/server/plugins/oidc/OidcRealm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
123123
@Override
124124
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
125125
UserPrincipal p = principals.oneByType(UserPrincipal.class);
126-
if (!REALM_NAME.equals(p.getRealm())) {
126+
if (p == null || !REALM_NAME.equals(p.getRealm())) {
127127
return null;
128128
}
129129

server/plugins/pfed-sso/src/main/java/com/walmartlabs/concord/server/plugins/pfedsso/SsoRealm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
9999
@Override
100100
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
101101
UserPrincipal p = principals.oneByType(UserPrincipal.class);
102-
if (!REALM_NAME.equals(p.getRealm())) {
102+
if (p == null || !REALM_NAME.equals(p.getRealm())) {
103103
return null;
104104
}
105105

0 commit comments

Comments
 (0)