class EncryptionKeyCanaryMapper {

  public void mapUuidsToKeys(final EncryptionKeySet keySet) throws Exception {
    final List<EncryptionKeyCanary> encryptionKeyCanaries = encryptionKeyCanaryDataService.findAll();
    for (final EncryptionKeyProvider provider : encryptionKeysConfiguration.getProviders()) {
      final EncryptionProvider encryptionService = providerFactory.getEncryptionService(provider);
      for (final EncryptionKeyMetadata keyMetadata : provider.getKeys()) {
        final KeyProxy keyProxy = encryptionService.createKeyProxy(keyMetadata);
        EncryptionKeyCanary matchingCanary = null;

        for (final EncryptionKeyCanary canary : encryptionKeyCanaries) {
          if (keyProxy.matchesCanary(canary)) {
            matchingCanary = canary;
            break;
          }
        }

        final EncryptionKey encryptionKey = new EncryptionKey(encryptionService, null, keyProxy.getKey(), keyMetadata.getEncryptionKeyName());

        if (matchingCanary == null) {
          if (keyMetadata.isActive()) {
            matchingCanary = createCanary(keyProxy, encryptionService, encryptionKey);
          } else {
            continue;
          }
        }
        if (keyMetadata.isActive()) {
          keySet.setActive(matchingCanary.getUuid());
        }

        try {
          encryptionKey.setUuid(matchingCanary.getUuid());
          keySet.add(encryptionKey);
        } catch (final Exception e) {
          throw new RuntimeException("Failed to connect to encryption provider", e);
        }
      }
    }
    if (keySet.getActive() == null) {
      throw new RuntimeException("No active key was found");
    }
  }

  @Autowired public  EncryptionKeyCanaryMapper(
    final EncryptionKeyCanaryDataService encryptionKeyCanaryDataService,
    final EncryptionKeysConfiguration encryptionKeysConfiguration,
    final TimedRetry timedRetry,
    final EncryptionProviderFactory providerFactory
  );

}