Search in sources :

Example 11 with AccessKey

use of com.ctrip.framework.apollo.biz.entity.AccessKey in project apollo by ctripcorp.

the class AccessKeyServiceWithCache method deleteAccessKeyCache.

private void deleteAccessKeyCache() {
    List<Long> ids = Lists.newArrayList(accessKeyIdCache.keySet());
    if (CollectionUtils.isEmpty(ids)) {
        return;
    }
    List<List<Long>> partitionIds = Lists.partition(ids, 500);
    for (List<Long> toRebuildIds : partitionIds) {
        Iterable<AccessKey> accessKeys = accessKeyRepository.findAllById(toRebuildIds);
        Set<Long> foundIds = Sets.newHashSet();
        for (AccessKey accessKey : accessKeys) {
            foundIds.add(accessKey.getId());
        }
        // handle deleted
        SetView<Long> deletedIds = Sets.difference(Sets.newHashSet(toRebuildIds), foundIds);
        handleDeletedAccessKeys(deletedIds);
    }
}
Also used : List(java.util.List) AccessKey(com.ctrip.framework.apollo.biz.entity.AccessKey)

Example 12 with AccessKey

use of com.ctrip.framework.apollo.biz.entity.AccessKey in project apollo by ctripcorp.

the class AccessKeyServiceWithCacheTest method assembleAccessKey.

public AccessKey assembleAccessKey(Long id, String appId, String secret, boolean enabled, boolean deleted, long dataChangeLastModifiedTime) {
    AccessKey accessKey = new AccessKey();
    accessKey.setId(id);
    accessKey.setAppId(appId);
    accessKey.setSecret(secret);
    accessKey.setEnabled(enabled);
    accessKey.setDeleted(deleted);
    accessKey.setDataChangeLastModifiedTime(new Date(dataChangeLastModifiedTime));
    return accessKey;
}
Also used : AccessKey(com.ctrip.framework.apollo.biz.entity.AccessKey) Date(java.util.Date)

Example 13 with AccessKey

use of com.ctrip.framework.apollo.biz.entity.AccessKey in project apollo by ctripcorp.

the class AccessKeyServiceWithCacheTest method testGetAvailableSecrets.

@Test
public void testGetAvailableSecrets() throws Exception {
    String appId = "someAppId";
    AccessKey firstAccessKey = assembleAccessKey(1L, appId, "secret-1", false, false, 1577808000000L);
    AccessKey secondAccessKey = assembleAccessKey(2L, appId, "secret-2", false, false, 1577808001000L);
    AccessKey thirdAccessKey = assembleAccessKey(3L, appId, "secret-3", true, false, 1577808005000L);
    // Initialize
    accessKeyServiceWithCache.afterPropertiesSet();
    assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).isEmpty();
    // Add access key, disable by default
    when(accessKeyRepository.findFirst500ByDataChangeLastModifiedTimeGreaterThanOrderByDataChangeLastModifiedTimeAsc(new Date(0L))).thenReturn(Lists.newArrayList(firstAccessKey, secondAccessKey));
    when(accessKeyRepository.findAllById(anyList())).thenReturn(Lists.newArrayList(firstAccessKey, secondAccessKey));
    await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).isEmpty());
    // Update access key, enable both of them
    firstAccessKey = assembleAccessKey(1L, appId, "secret-1", true, false, 1577808002000L);
    secondAccessKey = assembleAccessKey(2L, appId, "secret-2", true, false, 1577808003000L);
    when(accessKeyRepository.findFirst500ByDataChangeLastModifiedTimeGreaterThanOrderByDataChangeLastModifiedTimeAsc(new Date(1577808001000L))).thenReturn(Lists.newArrayList(firstAccessKey, secondAccessKey));
    when(accessKeyRepository.findAllById(anyList())).thenReturn(Lists.newArrayList(firstAccessKey, secondAccessKey));
    await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).containsExactly("secret-1", "secret-2"));
    // should also work with appid in different case
    assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId.toUpperCase())).containsExactly("secret-1", "secret-2");
    assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId.toLowerCase())).containsExactly("secret-1", "secret-2");
    // Update access key, disable the first one
    firstAccessKey = assembleAccessKey(1L, appId, "secret-1", false, false, 1577808004000L);
    when(accessKeyRepository.findFirst500ByDataChangeLastModifiedTimeGreaterThanOrderByDataChangeLastModifiedTimeAsc(new Date(1577808003000L))).thenReturn(Lists.newArrayList(firstAccessKey));
    when(accessKeyRepository.findAllById(anyList())).thenReturn(Lists.newArrayList(firstAccessKey, secondAccessKey));
    await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).containsExactly("secret-2"));
    // Delete access key, delete the second one
    when(accessKeyRepository.findAllById(anyList())).thenReturn(Lists.newArrayList(firstAccessKey));
    await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).isEmpty());
    // Add new access key in runtime, enable by default
    when(accessKeyRepository.findFirst500ByDataChangeLastModifiedTimeGreaterThanOrderByDataChangeLastModifiedTimeAsc(new Date(1577808004000L))).thenReturn(Lists.newArrayList(thirdAccessKey));
    when(accessKeyRepository.findAllById(anyList())).thenReturn(Lists.newArrayList(firstAccessKey, thirdAccessKey));
    await().untilAsserted(() -> assertThat(accessKeyServiceWithCache.getAvailableSecrets(appId)).containsExactly("secret-3"));
    reachabilityFence(accessKeyServiceWithCache);
}
Also used : AccessKey(com.ctrip.framework.apollo.biz.entity.AccessKey) Date(java.util.Date) Test(org.junit.Test)

Example 14 with AccessKey

use of com.ctrip.framework.apollo.biz.entity.AccessKey in project apollo by ctripcorp.

the class AccessKeyController method create.

@PostMapping(value = "/apps/{appId}/accesskeys")
public AccessKeyDTO create(@PathVariable String appId, @RequestBody AccessKeyDTO dto) {
    AccessKey entity = BeanUtils.transform(AccessKey.class, dto);
    entity = accessKeyService.create(appId, entity);
    return BeanUtils.transform(AccessKeyDTO.class, entity);
}
Also used : AccessKey(com.ctrip.framework.apollo.biz.entity.AccessKey) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 15 with AccessKey

use of com.ctrip.framework.apollo.biz.entity.AccessKey in project apollo by ctripcorp.

the class AccessKeyController method disable.

@PutMapping(value = "/apps/{appId}/accesskeys/{id}/disable")
public void disable(@PathVariable String appId, @PathVariable long id, String operator) {
    AccessKey entity = new AccessKey();
    entity.setId(id);
    entity.setEnabled(false);
    entity.setDataChangeLastModifiedBy(operator);
    accessKeyService.update(appId, entity);
}
Also used : AccessKey(com.ctrip.framework.apollo.biz.entity.AccessKey) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Aggregations

AccessKey (com.ctrip.framework.apollo.biz.entity.AccessKey)17 Test (org.junit.Test)5 AbstractIntegrationTest (com.ctrip.framework.apollo.biz.AbstractIntegrationTest)4 Date (java.util.Date)4 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)3 Transactional (org.springframework.transaction.annotation.Transactional)3 PutMapping (org.springframework.web.bind.annotation.PutMapping)2 Instant (java.time.Instant)1 List (java.util.List)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 Sql (org.springframework.test.context.jdbc.Sql)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1