Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void testCreateProject() throws Exception {
*/
@Test
public void testExternalApiToken() throws Exception {

String username = "user_" + randomString();

UsersApi usersApi = new UsersApi(concord.apiClient());
Expand Down Expand Up @@ -217,6 +218,7 @@ public void testDryRunForChildProcess() throws Exception {
proc.assertLog(".*Done!.*");
}


@Test
public void testCreateApiKey() throws Exception {
String apiKeyName1 = "test1_" + randomString();
Expand Down Expand Up @@ -259,40 +261,4 @@ public void testCreateApiKey() throws Exception {
apiKeys = apiKeysApi.listUserApiKeys(adminId);
assertEquals(apiKeyCount, apiKeys.size());
}

@Test
public void testCreateOrUpdateApiKey() throws Exception {
String username = "user_" + randomString();

UsersApi usersApi = new UsersApi(concord.apiClient());
usersApi.createOrUpdateUser(new CreateUserRequest()
.username(username)
.type(CreateUserRequest.TypeEnum.LOCAL));

// --

String apiKeyValue = Base64.getEncoder().encodeToString(("foo_" + randomString()).getBytes(UTF_8));

Payload payload = new Payload()
.archive(resource("concord/createOrUpdateApiKey"))
.arg("apiKeyValue", apiKeyValue)
.arg("apiKeyUsername", username);

// ---

ConcordProcess proc = concord.processes().start(payload);
expectStatus(proc, ProcessEntry.StatusEnum.FINISHED);

// ---

proc.assertLog(".*result1=.*");
proc.assertLog(".*result2=.*");
proc.assertLog(".*result3=.*");

// ---

ApiKeysApi apiKeysApi = new ApiKeysApi(concord.apiClient().setApiKey(apiKeyValue));
List<ApiKeyEntry> apiKeys = apiKeysApi.listUserApiKeys(null);
assertEquals(1, apiKeys.size());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public TaskResult execute(ConcordTaskParams in) throws Exception {
kill((KillParams) in);
yield TaskResult.success();
}
case CREATEAPIKEY -> createApiKey((CreateOrUpdateApiKeyParams) in);
case CREATEORUPDATEAPIKEY -> createOrUpdateApiKey((CreateOrUpdateApiKeyParams) in);
case CREATEAPIKEY -> createApiKey((CreateApiKeyParams) in);
};
}

Expand Down Expand Up @@ -181,11 +180,23 @@ public void kill(KillParams in) throws Exception {
}
}

public TaskResult createApiKey(CreateOrUpdateApiKeyParams in) throws Exception {
public TaskResult createApiKey(CreateApiKeyParams in) throws Exception {
return withClient(in.baseUrl(), in.apiKey(), client -> {
log.info("Creating a new API key in {}", client.getBaseUri());
UUID userId = in.userId();
if (userId == null) {
String username = in.username();
if (username == null) {
throw new IllegalArgumentException("User ID or user name is required");
}

UUID userId = assertUserId(client, in);
UsersApi usersApi = new UsersApi(client);
UserEntry user = usersApi.findByUsername(username);
if (user == null) {
throw new IllegalArgumentException("User '" + username + "' not found.");
}
userId = user.getId();
}

String keyName = in.name();
if (keyName != null) {
Expand Down Expand Up @@ -215,51 +226,10 @@ public TaskResult createApiKey(CreateOrUpdateApiKeyParams in) throws Exception {

return TaskResult.success()
.value("id", response.getId())
.value("name", response.getName())
.value("key", response.getKey());
});
}

public TaskResult createOrUpdateApiKey(CreateOrUpdateApiKeyParams in) throws Exception {
return withClient(in.baseUrl(), in.apiKey(), client -> {
log.info("Creating or updating an API key in {}", client.getBaseUri());

UUID userId = assertUserId(client, in);

ApiKeysV2Api apiKeysApi = new ApiKeysV2Api(client);
CreateApiKeyResponse response = apiKeysApi.createOrUpdateUserApiKey(new CreateApiKeyRequest()
.name(in.name())
.userId(userId)
.userDomain(in.userDomain())
.userType(in.userType())
.key(in.key()));

return TaskResult.success()
.value("id", response.getId())
.value("name", response.getName())
.value("key", response.getKey())
.value("result", response.getResult().toString());
});
}

private UUID assertUserId(ApiClient client, CreateOrUpdateApiKeyParams in) throws ApiException {
UUID userId = in.userId();
if (userId == null) {
String username = in.username();
if (username == null) {
throw new IllegalArgumentException("User ID or user name is required");
}

UsersApi usersApi = new UsersApi(client);
UserEntry user = usersApi.findByUsername(username);
if (user == null) {
throw new IllegalArgumentException("User '" + username + "' not found.");
}
userId = user.getId();
}
return userId;
}

public Map<String, Map<String, Object>> getOutVars(String baseUrl, String apiKey, List<UUID> ids, long timeout) {
return waitForCompletion(ids, timeout, p -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static ConcordTaskParams of(Variables input, Map<String, Object> defaults
case STARTEXTERNAL -> new StartExternalParams(variables);
case FORK -> new ForkParams(variables);
case KILL -> new KillParams(variables);
case CREATEAPIKEY, CREATEORUPDATEAPIKEY -> new CreateOrUpdateApiKeyParams(variables);
case CREATEAPIKEY -> new CreateApiKeyParams(variables);
};
}

Expand Down Expand Up @@ -428,7 +428,7 @@ public boolean sync() {
}
}

public static class CreateOrUpdateApiKeyParams extends ConcordTaskParams {
public static class CreateApiKeyParams extends ConcordTaskParams {

private static final String BASE_URL_KEY = "baseUrl";
private static final String API_KEY = "apiKey";
Expand All @@ -440,7 +440,7 @@ public static class CreateOrUpdateApiKeyParams extends ConcordTaskParams {
private static final String IGNORE_EXISTING_KEY = "ignoreExisting";
private static final String KEY = "key";

CreateOrUpdateApiKeyParams(Variables variables) {
CreateApiKeyParams(Variables variables) {
super(variables);
}

Expand Down Expand Up @@ -535,8 +535,7 @@ public enum Action {
STARTEXTERNAL,
FORK,
KILL,
CREATEAPIKEY,
CREATEORUPDATEAPIKEY,
CREATEAPIKEY
}

private static class DelegateVariables implements Variables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import static com.walmartlabs.concord.server.jooq.tables.ApiKeys.API_KEYS;
Expand Down Expand Up @@ -90,14 +91,6 @@ public UUID insert(UUID userId, String key, String name, OffsetDateTime expiredA
.getKeyId());
}

public void update(UUID keyId, String key, OffsetDateTime expiredAt) {
tx(tx -> tx.update(API_KEYS)
.set(API_KEYS.API_KEY, hash(key))
.set(API_KEYS.EXPIRED_AT, expiredAt)
.where(API_KEYS.KEY_ID.eq(keyId))
.execute());
}

public void delete(UUID id) {
tx(tx -> tx.deleteFrom(API_KEYS)
.where(API_KEYS.KEY_ID.eq(id))
Expand Down
Loading
Loading