Browse Source

Evict cached miss when an API key is created with an explicit value

pull/16456/head
Sergey Matvienko 1 month ago
parent
commit
6d31dfd958
  1. 13
      dao/src/main/java/org/thingsboard/server/dao/pat/ApiKeyServiceImpl.java
  2. 18
      dao/src/test/java/org/thingsboard/server/dao/pat/ApiKeyCacheEvictionTest.java
  3. 20
      dao/src/test/java/org/thingsboard/server/dao/service/ApiKeyServiceTest.java

13
dao/src/main/java/org/thingsboard/server/dao/pat/ApiKeyServiceImpl.java

@ -86,10 +86,10 @@ public class ApiKeyServiceImpl extends AbstractCachedEntityService<ApiKeyCacheKe
if (isPublishEvictRequired(old, apiKey)) {
// the cache is keyed by value, so the entry to evict is the one the old value still resolves to
publishEvictEvent(new ApiKeyEvictEvent(old.getValue()));
if (!Objects.equals(old.getValue(), apiKey.getValue())) {
// a failed authentication attempt may have cached a miss for the new value
publishEvictEvent(new ApiKeyEvictEvent(apiKey.getValue()));
}
}
if (value != null && (old == null || !value.equals(old.getValue()))) {
// a failed authentication attempt may have cached a miss for the explicitly provided value (edge sync)
publishEvictEvent(new ApiKeyEvictEvent(value));
}
return savedApiKey;
} catch (Exception e) {
@ -187,8 +187,9 @@ public class ApiKeyServiceImpl extends AbstractCachedEntityService<ApiKeyCacheKe
}
/**
* Authentication resolves keys by value straight from the cache, so a changed enabled flag or value (explicit-value
* save, e.g. edge sync) requires eviction. A newly created key has nothing cached yet, hence the null check.
* Authentication resolves keys by value straight from the cache, so a changed enabled flag or value requires
* evicting the entry under the old value. Null old means create: there is no previous value to evict; a cached
* miss for an explicitly provided value is evicted separately in saveApiKey.
*/
private static boolean isPublishEvictRequired(ApiKey old, ApiKey apiKey) {
return old != null

18
dao/src/test/java/org/thingsboard/server/dao/pat/ApiKeyCacheEvictionTest.java

@ -8,9 +8,11 @@ import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import org.thingsboard.server.cache.TbTransactionalCache;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.id.ApiKeyId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.pat.ApiKey;
@ -69,6 +71,22 @@ public class ApiKeyCacheEvictionTest extends AbstractServiceTest {
Mockito.verify(apiKeyCache, Mockito.times(2)).evict(Mockito.any(ApiKeyCacheKey.class));
}
@Test
public void testCreateApiKeyWithExplicitValue_evictsValueExactlyOnce() {
// the edge uplink creates the key on the cloud with a preset id and the value generated on the edge
ApiKeyInfo apiKeyInfo = createApiKeyInfo();
ApiKeyId apiKeyId = new ApiKeyId(Uuids.timeBased());
apiKeyInfo.setId(apiKeyId);
apiKeyInfo.setCreatedTime(Uuids.unixTimestamp(apiKeyId.getId()));
String value = "edge-api-key-value-" + StringUtils.randomAlphanumeric(32);
Mockito.clearInvocations(apiKeyCache);
apiKeyService.saveApiKey(tenantId, apiKeyInfo, value, false);
Mockito.verify(apiKeyCache).evict(ApiKeyCacheKey.of(value));
Mockito.verify(apiKeyCache, Mockito.times(1)).evict(Mockito.any(ApiKeyCacheKey.class));
}
@Test
public void testDisableApiKey_evictsValueExactlyOnce() {
ApiKey savedApiKey = apiKeyService.saveApiKey(tenantId, createApiKeyInfo());

20
dao/src/test/java/org/thingsboard/server/dao/service/ApiKeyServiceTest.java

@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.thingsboard.server.dao.service;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -9,6 +10,7 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.id.ApiKeyId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.page.PageData;
@ -163,6 +165,24 @@ public class ApiKeyServiceTest extends AbstractServiceTest {
Assert.assertEquals(savedApiKey.getId(), foundByNewValue.getId());
}
@Test
public void testCreateApiKeyWithExplicitValue_cachedMissForValueEvicted() {
String value = "edge-api-key-value-" + StringUtils.randomAlphanumeric(32);
// a failed authentication attempt caches the miss before the edge uplink delivers the key
Assert.assertNull(apiKeyService.findApiKeyByValue(value));
// the edge uplink creates the key on the cloud with a preset id and the value generated on the edge
ApiKeyInfo apiKeyInfo = createApiKeyInfo(TEST_API_KEY_DESCRIPTION);
ApiKeyId apiKeyId = new ApiKeyId(Uuids.timeBased());
apiKeyInfo.setId(apiKeyId);
apiKeyInfo.setCreatedTime(Uuids.unixTimestamp(apiKeyId.getId()));
ApiKey savedApiKey = apiKeyService.saveApiKey(tenantId, apiKeyInfo, value, false);
ApiKey foundByValue = apiKeyService.findApiKeyByValue(value);
Assert.assertNotNull("API key value must resolve after the key is created with it", foundByValue);
Assert.assertEquals(savedApiKey.getId(), foundByValue.getId());
}
@Test
public void testFindApiKeysByUserId() {
int size = 3;

Loading…
Cancel
Save