Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.2.x] fix(console): searching for custom key by and environment_id #10046

Open
wants to merge 1 commit into
base: 4.2.x
Choose a base branch
from
Open
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 @@ -44,6 +44,8 @@ public interface ApiKeyRepository extends FindAllRepository<ApiKey> {
*/
List<ApiKey> findByKey(String key) throws TechnicalException;

List<ApiKey> findByKeyAndEnvironmentId(String key, String environmentId) throws TechnicalException;

/**
* Give the API Key from the given key and api
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,35 @@ public List<ApiKey> findByApplication(String applicationId) throws TechnicalExce
}
}

@Override
public List<ApiKey> findByKeyAndEnvironmentId(String key, String environmentId) throws TechnicalException {
LOGGER.debug("JdbcApiKeyRepository.findByKeyAndEnvironmentId(*****, {})", environmentId);
try {
String query =
getOrm().getSelectAllSql() +
" k" +
" left join " +
keySubscriptions +
" ks on ks.key_id = k.id " +
"where k." +
escapeReservedWord("key") +
" = ?" +
" and k." +
escapeReservedWord("environment_id") +
" = ?" +
" order by k.updated_at desc ";

CollatingRowMapper<ApiKey> rowMapper = new CollatingRowMapper<>(getOrm().getRowMapper(), CHILD_ADDER, "id");

jdbcTemplate.query(query, rowMapper, key, environmentId);

return rowMapper.getRows();
} catch (final Exception ex) {
LOGGER.error("Failed to find API Keys by key and environment:", ex);
throw new TechnicalException("Failed to find API Keys by key and environment", ex);
}
}

@Override
public Optional<ApiKey> findByKeyAndApi(String key, String api) throws TechnicalException {
LOGGER.debug("JdbcApiKeyRepository.findByKeyAndApi(****, {})", api);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public List<ApiKey> findByKey(String key) {
return internalApiKeyRepo.findByKey(key).stream().map(this::toApiKey).collect(toList());
}

@Override
public List<ApiKey> findByKeyAndEnvironmentId(String key, String environmentId) {
return internalApiKeyRepo.findByKeyAndEnvironmentId(key, environmentId).stream().map(this::toApiKey).toList();
}

@Override
public Optional<ApiKey> findByKeyAndApi(String key, String api) {
return internalApiKeyRepo.findByKeyAndApi(key, api).stream().findFirst().map(this::toApiKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public interface ApiKeyMongoRepository extends MongoRepository<ApiKeyMongo, Stri
List<ApiKeyMongo> findByApplication(String applicationId);

List<ApiKeyMongo> findByKey(String key);
<<<<<<< HEAD
=======

@Query(value = "{ environmentId: ?0 }", fields = "{ _id : 1 }", delete = true)
List<ApiKeyMongo> deleteByEnvironmentId(String environmentId);

List<ApiKeyMongo> findByKeyAndEnvironmentId(String apiKey, String environmentId);
>>>>>>> 90b54960fe (fix(console): searching for custom key by and environment_id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public List<ApiKey> findByKey(String key) throws TechnicalException {
return List.of();
}

@Override
public List<ApiKey> findByKeyAndEnvironmentId(String key, String environmentId) throws TechnicalException {
return List.of();
}

@Override
public Optional<ApiKey> findByKeyAndApi(String key, String api) throws TechnicalException {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public void findByKey() throws TechnicalException {
assertEquals(0, apiKeys.size());
}

@Test
public void findByKeyAndEnvironmentId() throws TechnicalException {
List<ApiKey> apiKeys = cut.findByKeyAndEnvironmentId("test_key", "environment_id");

assertNotNull(apiKeys);
assertEquals(0, apiKeys.size());
}

@Test
public void findByKeyAndApi() throws TechnicalException {
Optional<ApiKey> apiKey = cut.findByKeyAndApi("test_id", "test_key");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ public void findByCriteria_should_find_by_criteria_with_expire_at_after_dates_in
public void findByCriteria_should_find_by_criteria_with_expire_at_before_dates() throws Exception {
List<ApiKey> apiKeys = apiKeyRepository.findByCriteria(ApiKeyCriteria.builder().expireBefore(30019401755L).build());

assertEquals("found 2 API Keys", 2, apiKeys.size());
assertEquals("found 4 API Keys", 4, apiKeys.size());

List<String> expectedKeys = List.of("findByCriteria2", "findByCriteria1");
List<String> expectedKeys = List.of("findByCriteria2", "findByCriteria1", "12345678", "12345678");
assertTrue(expectedKeys.containsAll(apiKeys.stream().map(ApiKey::getKey).collect(toList())));
}

Expand All @@ -304,9 +304,16 @@ public void findByCriteria_should_find_by_criteria_with_expire_at_before_dates_i
ApiKeyCriteria.builder().expireBefore(30019401755L).includeWithoutExpiration(true).build()
);

assertEquals("found 4 API Keys", 4, apiKeys.size());
assertEquals("found 6 API Keys", 6, apiKeys.size());

List<String> expectedKeys = List.of("findByCriteria2", "the-key-of-api-key-7", "the-key-of-api-key-8", "findByCriteria1");
List<String> expectedKeys = List.of(
"findByCriteria2",
"the-key-of-api-key-7",
"the-key-of-api-key-8",
"findByCriteria1",
"12345678",
"12345678"
);
assertTrue(expectedKeys.containsAll(apiKeys.stream().map(ApiKey::getKey).collect(toList())));
}

Expand Down Expand Up @@ -365,6 +372,7 @@ public void findById_should_return_key_with_its_subscription() throws TechnicalE
public void findAll_should_find_all_api_keys_even_with_no_subscription() throws TechnicalException {
Set<ApiKey> apiKeys = apiKeyRepository.findAll();

<<<<<<< HEAD
assertEquals(9, apiKeys.size());
assertTrue(
apiKeys
Expand All @@ -373,6 +381,9 @@ public void findAll_should_find_all_api_keys_even_with_no_subscription() throws
apiKey.getId().equals("id-of-apikey-8") && apiKey.getSubscriptions() != null && apiKey.getSubscriptions().isEmpty()
)
);
=======
assertThat(apiKeys).hasSize(13).extracting(ApiKey::getId).contains("id-of-apikey-8");
>>>>>>> 90b54960fe (fix(console): searching for custom key by and environment_id)
}

@Test
Expand Down Expand Up @@ -424,4 +435,37 @@ public void return_empty_if_apikey_does_not_exist() throws TechnicalException {
Optional<ApiKey> updatedApiKey = apiKeyRepository.addSubscription("unknown_apikey_id", "newSubscription");
assertTrue(updatedApiKey.isEmpty());
}
<<<<<<< HEAD
=======

@Test
public void should_find_by_key_and_environment_id() throws TechnicalException {
List<ApiKey> apiKeys = apiKeyRepository.findByKeyAndEnvironmentId("12345678", "env7");

assertEquals(1, apiKeys.size());
assertEquals("env7", apiKeys.get(0).getEnvironmentId());
assertEquals("12345678", apiKeys.get(0).getKey());
}

@Test
public void should_delete_by_environment_id() throws TechnicalException {
final var beforeDeletion = apiKeyRepository
.findAll()
.stream()
.filter(apiKey -> "DEFAULT".equals(apiKey.getEnvironmentId()))
.map(ApiKey::getId)
.toList();

var deleted = apiKeyRepository.deleteByEnvironmentId("DEFAULT");
final var nbAfterDeletion = apiKeyRepository
.findAll()
.stream()
.filter(apiKey -> "DEFAULT".equals(apiKey.getEnvironmentId()))
.count();

assertEquals(beforeDeletion.size(), deleted.size());
assertEquals(beforeDeletion, deleted);
assertEquals(0, nbAfterDeletion);
}
>>>>>>> 90b54960fe (fix(console): searching for custom key by and environment_id)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
<<<<<<< HEAD
{
"id": "id-of-apikey-0",
"key": "d449098d-8c31-4275-ad59-8dd707865999",
Expand Down Expand Up @@ -83,5 +84,146 @@
"subscriptions": [],
"application": "app4"
}
=======
{
"id": "id-of-apikey-0",
"key": "d449098d-8c31-4275-ad59-8dd707865999",
"revoked": true,
"expireAt": 29919401755,
"subscriptions": ["subscription2"],
"environmentId" : "DEFAULT",
"createdAt": 1439022010883
},
{
"id": "id-of-apikey-1",
"key": "d449098d-8c31-4275-ad59-8dd707865a34",
"revoked": true,
"expireAt": 1439022010883,
"subscription": "bc-subscription1",
"api": "bc-api1",
"subscriptions": ["subscription1"],
"environmentId" : "DEFAULT",
"createdAt": 1439022010883,
"paused": true,
"daysToExpirationOnLastNotification": 30
},
{
"id": "id-of-apikey-2",
"key": "d449098d-8c31-4275-ad59-8dd707865a34",
"revoked": false,
"expireAt": 1439022010883,
"subscriptions": ["subscription2", "subscriptionX"],
"environmentId" : "DEFAULT",
"createdAt": 1439022010883,
"updatedAt": 1439022010000
},
{
"id": "id-of-apikey-3",
"key": "d449098d-8c31-4275-ad59-8dd707865a35",
"revoked": false,
"expireAt": 1439022010883,
"subscriptions": ["subscription1"],
"environmentId" : "env5",
"createdAt": 1439022010883,
"updatedAt": 1439022000000
},
{
"id": "id-of-apikey-4",
"key": "findByCriteria1",
"subscriptions": ["sub3"],
"environmentId" : "env4",
"application": "app1",
"expireAt": 29919401755,
"createdAt": 1486771200000,
"updatedAt": 1486771200000,
"revoked": false
},
{
"id": "id-of-apikey-5",
"key": "findByCriteria1Revoked",
"subscriptions": ["sub3"],
"environmentId" : "env5",
"application": "app1",
"expireAt": 29919401755,
"createdAt": 1486771200000,
"updatedAt": 1486771400000,
"revoked": true
},
{
"id": "id-of-apikey-6",
"key": "findByCriteria2",
"subscriptions": ["sub3"],
"environmentId" : "env6",
"application": "app2",
"expireAt": 29919401755,
"createdAt": 1486771200000,
"updatedAt": 1486771600000,
"revoked": false
},
{
"id": "id-of-apikey-7",
"key": "the-key-of-api-key-7",
"subscriptions": ["sub4", "sub5", "sub6"],
"environmentId" : "env7",
"createdAt": 1486771200000,
"updatedAt": 1486771600000,
"application": "app4"
},
{
"id": "id-of-apikey-8",
"key": "the-key-of-api-key-8",
"createdAt": 1486771200000,
"updatedAt": 1486771600000,
"subscriptions": [],
"environmentId" : "env8",
"application": "app4"
},
{
"id": "id-of-federated-9",
"key": "the-key-of-federated-9",
"createdAt": 1486771200000,
"updatedAt": 1486771600000,
"subscriptions": ["sub133"],
"environmentId" : "env9",
"application": "app4",
"federated": true
},
{
"id": "id-of-federated-10",
"key": "the-key-of-federated-10",
"subscriptions": ["sub3"],
"application": "app2",
"environmentId" : "env10",
"expireAt": 29919401755,
"createdAt": 1486771200000,
"updatedAt": 1486771600000,
"revoked": false,
"federated": true
},
{
"id": "key7",
"key": "12345678",
"subscriptions": ["sub7"],
"application": "app7",
"environmentId" : "env7",
"expireAt": 29919401755,
"createdAt": 1486771200000,
"updatedAt": 1486771600000,
"revoked": false,
"federated": false
},
{
"id": "key8",
"key": "12345678",
"subscriptions": ["sub8"],
"application": "app8",
"environmentId" : "env8",
"expireAt": 29919401755,
"createdAt": 1486771200000,
"updatedAt": 1486771600000,
"revoked": false,
"federated": false
}
>>>>>>> 90b54960fe (fix(console): searching for custom key by and environment_id)
]

Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,25 @@
"createdAt": 1439022010883,
"updatedAt": 1439022010883,
"lifecycleState": "STOPPED"
},
{
"id": "api7",
"version": "1",
"environmentId": "DEFAULT",
"definition" : "{\"id\" : \"product\",\"name\" : \"Product\",\"version\" : \"1\",\"proxy\" : { \"context_path\" : \"/product\", \"endpoint\" : \"http://toto.com\", \"endpoints\" : [ { \"target\" : \"http://toto.com\", \"weight\" : 1 } ], \"strip_context_path\" : false, \"http\" : { \"configuration\" : { \"connectTimeout\" : 5000, \"idleTimeout\" : 60000, \"keepAliveTimeout\" : 30000, \"keepAlive\" : true, \"dumpRequest\" : false } }},\"paths\" : { \"/\" : [ { \"methods\" : [ ], \"api-key\" : {} } ]},\"tags\" : [ ]\n}",
"visibility" : "PUBLIC",
"createdAt": 1439022010883,
"updatedAt": 1439022010883,
"lifecycleState": "STOPPED"
},
{
"id": "api8",
"version": "1",
"environmentId": "DEFAULT",
"definition" : "{\"id\" : \"product\",\"name\" : \"Product\",\"version\" : \"1\",\"proxy\" : { \"context_path\" : \"/product\", \"endpoint\" : \"http://toto.com\", \"endpoints\" : [ { \"target\" : \"http://toto.com\", \"weight\" : 1 } ], \"strip_context_path\" : false, \"http\" : { \"configuration\" : { \"connectTimeout\" : 5000, \"idleTimeout\" : 60000, \"keepAliveTimeout\" : 30000, \"keepAlive\" : true, \"dumpRequest\" : false } }},\"paths\" : { \"/\" : [ { \"methods\" : [ ], \"api-key\" : {} } ]},\"tags\" : [ ]\n}",
"visibility" : "PUBLIC",
"createdAt": 1439022010883,
"updatedAt": 1439022010883,
"lifecycleState": "STOPPED"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,32 @@
"metadata": {
"type": "Web"
}
},
{
"id": "app7",
"name": "app7",
"environmentId": "env7",
"description": "app7",
"status": "ACTIVE",
"type": "SIMPLE",
"createdAt": "1439022010883",
"updatedAt": "1439022010883",
"metadata": {
"type": "Web"
}
},
{
"id": "app8",
"name": "app8",
"environmentId": "env8",
"description": "app8",
"status": "ACTIVE",
"type": "SIMPLE",
"createdAt": "1439022010883",
"updatedAt": "1439022010883",
"metadata": {
"client_id": "my-client-id",
"type": "Web"
}
}
]
Loading