Skip to content

Commit dbba08d

Browse files
committed
add SystemResourceIT
1 parent 4156400 commit dbba08d

File tree

5 files changed

+109
-24
lines changed

5 files changed

+109
-24
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,25 @@ protected void withProject(String orgName, Consumer<String> consumer) throws Exc
245245
}
246246
}
247247

248+
protected UserInfo addUser(String username, Set<String> roles) throws ApiException {
249+
var usersApi = new UsersApi(getApiClient());
250+
var user = usersApi.createOrUpdateUser(new CreateUserRequest().username(username)
251+
.type(CreateUserRequest.TypeEnum.LOCAL));
252+
253+
if (!roles.isEmpty()) {
254+
usersApi.updateUserRoles(username, new UpdateUserRolesRequest()
255+
.roles(roles));
256+
}
257+
258+
var apiKeysApi = new ApiKeysApi(getApiClient());
259+
var apiKeyResp = apiKeysApi.createUserApiKey(new CreateApiKeyRequest()
260+
.userId(user.getId()));
261+
262+
return new UserInfo(username, user.getId(), apiKeyResp.getKey());
263+
}
264+
265+
protected record UserInfo(String username, UUID userId, String apiKey) { }
266+
248267
@FunctionalInterface
249268
public interface Consumer<T> {
250269
void accept(T t) throws Exception;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.walmartlabs.concord.it.server;
2+
3+
/*-
4+
* *****
5+
* Concord
6+
* -----
7+
* Copyright (C) 2017 - 2018 Walmart Inc.
8+
* -----
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* =====
21+
*/
22+
23+
import com.walmartlabs.concord.client2.ApiException;
24+
import com.walmartlabs.concord.client2.SystemApi;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.net.URI;
28+
import java.util.Set;
29+
30+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
import static org.junit.jupiter.api.Assertions.assertNull;
34+
import static org.junit.jupiter.api.Assertions.assertThrows;
35+
36+
class ExternalTokenProviderIT extends AbstractServerIT {
37+
38+
private static final URI URI001 = URI.create("https://github001.local/owner/repo.git");
39+
private static final URI URI002 = URI.create("https://github002.local/owner/repo.git");
40+
41+
@Test
42+
void testGetExternalToken() throws Exception {
43+
// user with externalTokenLookup role
44+
var userBName = "user_external_token_lookup_" + randomString();
45+
var externalTokenLookupUser = addUser(userBName, Set.of("externalTokenLookup"));
46+
47+
// get system-provided token with externalTokenLookup role
48+
var systemApi = new SystemApi(getApiClientForKey(externalTokenLookupUser.apiKey()));
49+
var token = assertDoesNotThrow(() -> systemApi.getExternalToken(URI001));
50+
assertEquals("mock-token", token.getToken());
51+
assertNull(token.getUsername());
52+
53+
// again, but from config that provides username
54+
token = assertDoesNotThrow(() -> systemApi.getExternalToken(URI002));
55+
assertEquals("mock-token", token.getToken());
56+
assertNotNull(token.getUsername());
57+
assertEquals("customUser", token.getUsername());
58+
}
59+
60+
@Test
61+
void testGetExternalTokenNoPermission() throws Exception {
62+
// user with no roles
63+
var userAName = "user_basic_" + randomString();
64+
var noRolesUser = addUser(userAName, Set.of());
65+
66+
// attempt to get system-provided token with insufficient privileges
67+
var systemApiNoPerm = new SystemApi(getApiClientForKey(noRolesUser.apiKey()));
68+
var ex1 = assertThrows(ApiException.class, () -> systemApiNoPerm.getExternalToken(URI001));
69+
assertEquals(403, ex1.getCode());
70+
}
71+
72+
}

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@
2121
*/
2222

2323
import com.walmartlabs.concord.client2.ApiException;
24-
import com.walmartlabs.concord.client2.ApiKeysApi;
25-
import com.walmartlabs.concord.client2.CreateApiKeyRequest;
26-
import com.walmartlabs.concord.client2.CreateUserRequest;
27-
import com.walmartlabs.concord.client2.UpdateUserRolesRequest;
2824
import com.walmartlabs.concord.client2.UserEntry;
2925
import com.walmartlabs.concord.client2.UserV2Api;
30-
import com.walmartlabs.concord.client2.UsersApi;
3126
import org.junit.jupiter.api.Test;
3227

3328
import java.util.Set;
@@ -61,28 +56,10 @@ void testGetUser() throws Exception {
6156
assertEquals(user.getId(), noRolesUser.userId());
6257
}
6358

64-
private UserEntry getUser(UserInfo userInfo, UUID userToGet) throws ApiException {
59+
protected UserEntry getUser(UserInfo userInfo, UUID userToGet) throws ApiException {
6560
var apiClient = new UserV2Api(getApiClientForKey(userInfo.apiKey()));
6661

6762
return apiClient.getUser(userToGet);
6863
}
6964

70-
private UserInfo addUser(String username, Set<String> roles) throws ApiException {
71-
var usersApi = new UsersApi(getApiClient());
72-
var user = usersApi.createOrUpdateUser(new CreateUserRequest().username(username)
73-
.type(CreateUserRequest.TypeEnum.LOCAL));
74-
75-
if (!roles.isEmpty()) {
76-
usersApi.updateUserRoles(username, new UpdateUserRolesRequest()
77-
.roles(roles));
78-
}
79-
80-
var apiKeysApi = new ApiKeysApi(getApiClient());
81-
var apiKeyResp = apiKeysApi.createUserApiKey(new CreateApiKeyRequest()
82-
.userId(user.getId()));
83-
84-
return new UserInfo(username, user.getId(), apiKeyResp.getKey());
85-
}
86-
87-
private record UserInfo(String username, UUID userId, String apiKey) { }
8865
}

it/server/src/test/resources/agent.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@ concord-agent {
2020
apiKey = "cTJxMnEycTI="
2121
processRequestDelay = "250 milliseconds"
2222
}
23+
24+
externalTokenProvider {
25+
enabled = true
26+
# Regex matching URI host + port + path for providing lookup for
27+
# external auth tokens. URI scheme is ignored. Requires externalTokenLookup
28+
# permission for the client user.
29+
# e.g. "github.com/my-org/" or "github.com/(orgA|orgB))/"
30+
urlPattern = "github(\d+).local/.*"
31+
}
32+
2333
}

it/server/src/test/resources/server.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ concord-server {
2323
secret = "12345"
2424
useSenderLdapDn = true
2525
disableReposOnDeletedRef = true
26+
27+
appInstallation {
28+
auth = [
29+
{ type = "OAUTH_TOKEN", urlPattern = "(?<baseUrl>github001.local)/", token = "mock-token" },
30+
{ type = "OAUTH_TOKEN", username = "customUser", urlPattern = "(?<baseUrl>github002.local)/", token = "mock-token" },
31+
]
32+
}
2633
}
2734

2835
git {

0 commit comments

Comments
 (0)