From 50a2f6602a5fee9e0f1b6ceb59916d0dc84bbbbe Mon Sep 17 00:00:00 2001 From: Szymon Korda Date: Thu, 28 Nov 2024 13:15:15 +0100 Subject: [PATCH] fix(console): searching for custom key by and environment_id (cherry picked from commit 90b54960fef0aa5f0aefb5bee6e3ea0438d09ecf) # Conflicts: # gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/key/ApiKeyMongoRepository.java # gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ApiKeyRepositoryTest.java # gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apiKeys.json # gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApiKeyServiceImpl.java --- .../management/api/ApiKeyRepository.java | 2 + .../jdbc/management/JdbcApiKeyRepository.java | 29 ++++ .../management/MongoApiKeyRepository.java | 5 + .../internal/key/ApiKeyMongoRepository.java | 8 + .../noop/management/NoOpApiKeyRepository.java | 5 + .../management/NoOpApiKeyRepositoryTest.java | 8 + .../management/ApiKeyRepositoryTest.java | 52 ++++++- .../resources/data/apikey-tests/apiKeys.json | 142 ++++++++++++++++++ .../resources/data/apikey-tests/apis.json | 20 +++ .../data/apikey-tests/applications.json | 27 ++++ .../data/apikey-tests/subscriptions.json | 27 ++++ .../rest/api/service/ApiKeyService.java | 2 + .../api/service/impl/ApiKeyServiceImpl.java | 23 ++- .../api/service/impl/ApiKeyServiceTest.java | 17 ++- 14 files changed, 354 insertions(+), 13 deletions(-) diff --git a/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/ApiKeyRepository.java b/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/ApiKeyRepository.java index ce85c3ca4eb..ad77c747a9f 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/ApiKeyRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/ApiKeyRepository.java @@ -44,6 +44,8 @@ public interface ApiKeyRepository extends FindAllRepository { */ List findByKey(String key) throws TechnicalException; + List findByKeyAndEnvironmentId(String key, String environmentId) throws TechnicalException; + /** * Give the API Key from the given key and api * diff --git a/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcApiKeyRepository.java b/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcApiKeyRepository.java index 23024f4e047..73754404c7b 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcApiKeyRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcApiKeyRepository.java @@ -338,6 +338,35 @@ public List findByApplication(String applicationId) throws TechnicalExce } } + @Override + public List 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 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 findByKeyAndApi(String key, String api) throws TechnicalException { LOGGER.debug("JdbcApiKeyRepository.findByKeyAndApi(****, {})", api); diff --git a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoApiKeyRepository.java b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoApiKeyRepository.java index db96cc97a90..0b29a57a93d 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoApiKeyRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoApiKeyRepository.java @@ -108,6 +108,11 @@ public List findByKey(String key) { return internalApiKeyRepo.findByKey(key).stream().map(this::toApiKey).collect(toList()); } + @Override + public List findByKeyAndEnvironmentId(String key, String environmentId) { + return internalApiKeyRepo.findByKeyAndEnvironmentId(key, environmentId).stream().map(this::toApiKey).toList(); + } + @Override public Optional findByKeyAndApi(String key, String api) { return internalApiKeyRepo.findByKeyAndApi(key, api).stream().findFirst().map(this::toApiKey); diff --git a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/key/ApiKeyMongoRepository.java b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/key/ApiKeyMongoRepository.java index baa441700a8..5aa425e6ed2 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/key/ApiKeyMongoRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/key/ApiKeyMongoRepository.java @@ -38,4 +38,12 @@ public interface ApiKeyMongoRepository extends MongoRepository findByApplication(String applicationId); List findByKey(String key); +<<<<<<< HEAD +======= + + @Query(value = "{ environmentId: ?0 }", fields = "{ _id : 1 }", delete = true) + List deleteByEnvironmentId(String environmentId); + + List findByKeyAndEnvironmentId(String apiKey, String environmentId); +>>>>>>> 90b54960fe (fix(console): searching for custom key by and environment_id) } diff --git a/gravitee-apim-repository/gravitee-apim-repository-noop/src/main/java/io/gravitee/repository/noop/management/NoOpApiKeyRepository.java b/gravitee-apim-repository/gravitee-apim-repository-noop/src/main/java/io/gravitee/repository/noop/management/NoOpApiKeyRepository.java index e6e3557732d..6a0c9e46fc1 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-noop/src/main/java/io/gravitee/repository/noop/management/NoOpApiKeyRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-noop/src/main/java/io/gravitee/repository/noop/management/NoOpApiKeyRepository.java @@ -40,6 +40,11 @@ public List findByKey(String key) throws TechnicalException { return List.of(); } + @Override + public List findByKeyAndEnvironmentId(String key, String environmentId) throws TechnicalException { + return List.of(); + } + @Override public Optional findByKeyAndApi(String key, String api) throws TechnicalException { return Optional.empty(); diff --git a/gravitee-apim-repository/gravitee-apim-repository-noop/src/test/java/io/gravitee/repository/noop/management/NoOpApiKeyRepositoryTest.java b/gravitee-apim-repository/gravitee-apim-repository-noop/src/test/java/io/gravitee/repository/noop/management/NoOpApiKeyRepositoryTest.java index 57b55aa2539..aa2d7fd551d 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-noop/src/test/java/io/gravitee/repository/noop/management/NoOpApiKeyRepositoryTest.java +++ b/gravitee-apim-repository/gravitee-apim-repository-noop/src/test/java/io/gravitee/repository/noop/management/NoOpApiKeyRepositoryTest.java @@ -53,6 +53,14 @@ public void findByKey() throws TechnicalException { assertEquals(0, apiKeys.size()); } + @Test + public void findByKeyAndEnvironmentId() throws TechnicalException { + List apiKeys = cut.findByKeyAndEnvironmentId("test_key", "environment_id"); + + assertNotNull(apiKeys); + assertEquals(0, apiKeys.size()); + } + @Test public void findByKeyAndApi() throws TechnicalException { Optional apiKey = cut.findByKeyAndApi("test_id", "test_key"); diff --git a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ApiKeyRepositoryTest.java b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ApiKeyRepositoryTest.java index d2896844189..d73aaa0c702 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ApiKeyRepositoryTest.java +++ b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/java/io/gravitee/repository/management/ApiKeyRepositoryTest.java @@ -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 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 expectedKeys = List.of("findByCriteria2", "findByCriteria1"); + List expectedKeys = List.of("findByCriteria2", "findByCriteria1", "12345678", "12345678"); assertTrue(expectedKeys.containsAll(apiKeys.stream().map(ApiKey::getKey).collect(toList()))); } @@ -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 expectedKeys = List.of("findByCriteria2", "the-key-of-api-key-7", "the-key-of-api-key-8", "findByCriteria1"); + List 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()))); } @@ -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 apiKeys = apiKeyRepository.findAll(); +<<<<<<< HEAD assertEquals(9, apiKeys.size()); assertTrue( apiKeys @@ -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 @@ -424,4 +435,37 @@ public void return_empty_if_apikey_does_not_exist() throws TechnicalException { Optional updatedApiKey = apiKeyRepository.addSubscription("unknown_apikey_id", "newSubscription"); assertTrue(updatedApiKey.isEmpty()); } +<<<<<<< HEAD +======= + + @Test + public void should_find_by_key_and_environment_id() throws TechnicalException { + List 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) } diff --git a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apiKeys.json b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apiKeys.json index 3481ee3f3f5..4f20f38a696 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apiKeys.json +++ b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apiKeys.json @@ -1,4 +1,5 @@ [ +<<<<<<< HEAD { "id": "id-of-apikey-0", "key": "d449098d-8c31-4275-ad59-8dd707865999", @@ -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) ] diff --git a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apis.json b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apis.json index 203b62fcdb1..185ec9e05ef 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apis.json +++ b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/apis.json @@ -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" } ] diff --git a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/applications.json b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/applications.json index 83c7559869a..246e1f1e01f 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/applications.json +++ b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/applications.json @@ -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" + } } ] \ No newline at end of file diff --git a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/subscriptions.json b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/subscriptions.json index b85a13b8fe6..e681dd98ad8 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/subscriptions.json +++ b/gravitee-apim-repository/gravitee-apim-repository-test/src/test/resources/data/apikey-tests/subscriptions.json @@ -94,5 +94,32 @@ "startingAt": 1645192110345, "createdAt": 1645192110345, "updatedAt": 1645192110345 + }, + + { + "id": "sub7", + "api": "api7", + "environmentId" : "env7", + "plan": "plan7", + "application": "app7", + "status": "ACCEPTED", + "consumerStatus": "STARTED", + "processedAt": 1645192111324, + "startingAt": 1645192111324, + "createdAt": 1645192111324, + "updatedAt": 1645192111324 + }, + { + "id": "sub8", + "api": "api8", + "environmentId" : "env8", + "plan": "plan8", + "application": "app8", + "status": "ACCEPTED", + "consumerStatus": "STARTED", + "processedAt": 1645192111324, + "startingAt": 1645192111324, + "createdAt": 1645192111324, + "updatedAt": 1645192111324 } ] diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApiKeyService.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApiKeyService.java index ddea8d1e864..48751cd8f19 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApiKeyService.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApiKeyService.java @@ -43,6 +43,8 @@ ApiKeyEntity generate( ApiKeyEntity reactivate(ExecutionContext executionContext, ApiKeyEntity apiKeyEntity); + List findByKeyAndEnvironmentId(ExecutionContext executionContext, String apiKey); + List findBySubscription(ExecutionContext executionContext, String subscription); List findByKey(ExecutionContext executionContext, String apiKey); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApiKeyServiceImpl.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApiKeyServiceImpl.java index 7cfe5e7c926..3ec2c351bbf 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApiKeyServiceImpl.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApiKeyServiceImpl.java @@ -307,6 +307,10 @@ private ApiKey generateForSubscription(ExecutionContext executionContext, Subscr ApiKey apiKey = generateForApplication(subscription.getApplication(), customApiKey); apiKey.setSubscriptions(List.of(subscription.getId())); +<<<<<<< HEAD +======= + apiKey.setEnvironmentId(executionContext.getEnvironmentId()); +>>>>>>> 90b54960fe (fix(console): searching for custom key by and environment_id) // By default, the API Key will expire when subscription is closed apiKey.setExpireAt(subscription.getEndingAt()); @@ -410,6 +414,21 @@ public List findByKey(ExecutionContext executionContext, String ap } } + @Override + public List findByKeyAndEnvironmentId(ExecutionContext executionContext, String apiKey) { + try { + LOGGER.debug("Find API Keys by key and environment id"); + return apiKeyRepository + .findByKeyAndEnvironmentId(apiKey, executionContext.getEnvironmentId()) + .stream() + .map(apiKey1 -> convert(executionContext, apiKey1)) + .collect(toList()); + } catch (TechnicalException e) { + LOGGER.error("An error occurs while finding API Keys", e); + throw new TechnicalManagementException("An error occurs while finding API Keys", e); + } + } + @Override public List findBySubscription(ExecutionContext executionContext, String subscription) { try { @@ -520,7 +539,9 @@ public boolean canCreate(ExecutionContext executionContext, String apiKeyValue, public boolean canCreate(ExecutionContext executionContext, String apiKey, String apiId, String applicationId) { LOGGER.debug("Check if an API Key can be created for api {} and application {}", apiId, applicationId); - return findByKey(executionContext, apiKey).stream().noneMatch(existingKey -> isConflictingKey(existingKey, apiId, applicationId)); + return findByKeyAndEnvironmentId(executionContext, apiKey) + .stream() + .noneMatch(existingKey -> isConflictingKey(existingKey, apiId, applicationId)); } private boolean isConflictingKey(ApiKeyEntity existingKey, String apiId, String applicationId) { diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApiKeyServiceTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApiKeyServiceTest.java index 5c737bdf571..84b9bcb8413 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApiKeyServiceTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApiKeyServiceTest.java @@ -93,6 +93,7 @@ public class ApiKeyServiceTest { private static final String API_KEY = "ef02ecd0-71bb-11e5-9d70-feff819cdc9f"; private static final String SUBSCRIPTION_ID = "subscription-1"; private static final String CUSTOM_API_KEY = "an-api-key"; + private static final String ENVIRONMENT_ID = "DEFAULT"; @InjectMocks private ApiKeyService apiKeyService = new ApiKeyServiceImpl(); @@ -258,7 +259,7 @@ public void shouldGenerateWithCustomApiKey() throws TechnicalException { @Test(expected = TechnicalManagementException.class) public void shouldNotGenerateBecauseTechnicalException() throws TechnicalException { - when(apiKeyRepository.findByKey(any())).thenThrow(TechnicalManagementException.class); + when(apiKeyRepository.findByKeyAndEnvironmentId(any(), any())).thenThrow(TechnicalManagementException.class); apiKeyService.generate(GraviteeContext.getExecutionContext(), application, subscription, "a-custom-key"); } @@ -285,7 +286,7 @@ public void shouldNotGenerateBecauseApiKeyAlreadyExistsForAnotherApp() throws Te application.setId(APPLICATION_ID); when(subscriptionService.findByIdIn(List.of(conflictingSubscription.getId()))).thenReturn(Set.of(conflictingSubscription)); - when(apiKeyRepository.findByKey("alreadyExistingApiKey")).thenReturn(List.of(conflictingKey)); + when(apiKeyRepository.findByKeyAndEnvironmentId("alreadyExistingApiKey", ENVIRONMENT_ID)).thenReturn(List.of(conflictingKey)); when(applicationService.findById(eq(GraviteeContext.getExecutionContext()), anyString())).thenReturn(application); apiKeyService.generate(GraviteeContext.getExecutionContext(), application, subscription, "alreadyExistingApiKey"); @@ -625,7 +626,7 @@ public void shouldNotRenewBecauseApiKeyAlreadyExistsForAnotherApp() throws Techn plan.setSecurity(PlanSecurityType.API_KEY); when(applicationService.findById(any(), eq(conflictingApplicationId))).thenReturn(conflictingApplication); - when(apiKeyRepository.findByKey("alreadyExistingApiKey")).thenReturn(List.of(conflictingKey)); + when(apiKeyRepository.findByKeyAndEnvironmentId("alreadyExistingApiKey", ENVIRONMENT_ID)).thenReturn(List.of(conflictingKey)); when(planSearchService.findById(GraviteeContext.getExecutionContext(), PLAN_ID)).thenReturn(plan); apiKeyService.renew(GraviteeContext.getExecutionContext(), subscription, "alreadyExistingApiKey"); @@ -819,7 +820,7 @@ public void canCreate_should_return_true_cause_key_doesnt_exists_yet() throws Ex subscriptionQuery.setApi(apiId); subscriptionQuery.setApplication(applicationId); - when(apiKeyRepository.findByKey(apiKeyToCreate)).thenReturn(Collections.emptyList()); + when(apiKeyRepository.findByKeyAndEnvironmentId(apiKeyToCreate, ENVIRONMENT_ID)).thenReturn(Collections.emptyList()); boolean canCreate = apiKeyService.canCreate(GraviteeContext.getExecutionContext(), apiKeyToCreate, apiId, applicationId); @@ -847,7 +848,7 @@ public void canCreate_should_return_true_cause_key_already_exists_for_same_appli when(applicationService.findById(eq(GraviteeContext.getExecutionContext()), eq(applicationId))).thenReturn(application); when(subscriptionService.findByIdIn(List.of("subscription-1"))).thenReturn(Set.of(subscriptionEntity)); - when(apiKeyRepository.findByKey(apiKeyToCreate)).thenReturn(List.of(existingApiKey)); + when(apiKeyRepository.findByKeyAndEnvironmentId(apiKeyToCreate, ENVIRONMENT_ID)).thenReturn(List.of(existingApiKey)); boolean canCreate = apiKeyService.canCreate(GraviteeContext.getExecutionContext(), apiKeyToCreate, apiId, applicationId); @@ -876,7 +877,7 @@ public void canCreate_should_return_false_cause_key_already_exists_for_same_appl existingApiKey.setApplication(applicationId); existingApiKey.setKey(apiKeyToCreate); - when(apiKeyRepository.findByKey(apiKeyToCreate)).thenReturn(List.of(existingApiKey)); + when(apiKeyRepository.findByKeyAndEnvironmentId(apiKeyToCreate, ENVIRONMENT_ID)).thenReturn(List.of(existingApiKey)); when(applicationService.findById(eq(GraviteeContext.getExecutionContext()), eq(applicationId))).thenReturn(application); when(subscriptionService.findByIdIn(argThat(subscriptionIds::containsAll))).thenReturn(subscriptions); @@ -910,7 +911,7 @@ public void canCreate_should_return_false_cause_key_already_exists_for_another_a when(applicationService.findById(eq(GraviteeContext.getExecutionContext()), eq(conflictingApplicationId))) .thenReturn(conflictingApplication); when(subscriptionService.findByIdIn(argThat(subscriptionIds::containsAll))).thenReturn(Set.of(subscriptionEntity)); - when(apiKeyRepository.findByKey(apiKeyToCreate)).thenReturn(List.of(existingApiKey)); + when(apiKeyRepository.findByKeyAndEnvironmentId(apiKeyToCreate, ENVIRONMENT_ID)).thenReturn(List.of(existingApiKey)); boolean canCreate = apiKeyService.canCreate(GraviteeContext.getExecutionContext(), apiKeyToCreate, apiId, applicationId); @@ -922,7 +923,7 @@ public void canCreate_should_throw_TechnicalManagementException_cause_key_search String apiKeyToCreate = "apikey-i-want-to-create"; String apiId = "my-api-id"; String applicationId = "my-application-id"; - when(apiKeyRepository.findByKey(apiKeyToCreate)).thenThrow(TechnicalManagementException.class); + when(apiKeyRepository.findByKeyAndEnvironmentId(apiKeyToCreate, ENVIRONMENT_ID)).thenThrow(TechnicalManagementException.class); apiKeyService.canCreate(GraviteeContext.getExecutionContext(), apiKeyToCreate, apiId, applicationId); }