Search in sources :

Example 1 with ExternalValidator

use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.

the class ExternalValidatorSource method addValidator.

@Override
public AddValidatorResult addValidator(final BLSPublicKey publicKey, final Optional<URL> signerUrl) {
    if (!canUpdateValidators()) {
        return new AddValidatorResult(PostKeyResult.error("Cannot add validator to a read only source."), Optional.empty());
    }
    final DataDirLayout dataDirLayout = maybeDataDirLayout.orElseThrow();
    final String fileName = publicKey.toBytesCompressed().toUnprefixedHexString();
    final Path path = ValidatorClientService.getManagedRemoteKeyPath(dataDirLayout).resolve(fileName + ".json");
    try {
        ensureDirectoryExists(ValidatorClientService.getManagedRemoteKeyPath(dataDirLayout));
        if (path.toFile().exists()) {
            return new AddValidatorResult(PostKeyResult.duplicate(), Optional.empty());
        }
        Files.write(path, serialize(new ExternalValidator(publicKey, signerUrl), ValidatorTypes.EXTERNAL_VALIDATOR_STORE).getBytes(UTF_8));
        final URL url = signerUrl.orElse(config.getValidatorExternalSignerUrl());
        final ValidatorProvider provider = new ExternalValidatorProvider(spec, externalSignerHttpClientFactory, url, publicKey, config.getValidatorExternalSignerTimeout(), externalSignerTaskQueue, metricsSystem, readOnly);
        externalValidatorSourceMap.put(publicKey, url);
        return new AddValidatorResult(PostKeyResult.success(), Optional.of(provider.createSigner()));
    } catch (InvalidConfigurationException | IOException ex) {
        cleanupIncompleteSave(path);
        return new AddValidatorResult(PostKeyResult.error(ex.getMessage()), Optional.empty());
    }
}
Also used : Path(java.nio.file.Path) ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) IOException(java.io.IOException) DataDirLayout(tech.pegasys.teku.service.serviceutils.layout.DataDirLayout) URL(java.net.URL) InvalidConfigurationException(tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException)

Example 2 with ExternalValidator

use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.

the class ActiveKeyManagerTest method shouldReturnActiveRemoteValidatorsList.

@Test
void shouldReturnActiveRemoteValidatorsList() throws MalformedURLException {
    final BLSKeyPair keyPair1 = BLSTestUtil.randomKeyPair(1);
    final BLSKeyPair keyPair2 = BLSTestUtil.randomKeyPair(2);
    final BLSKeyPair keyPair3 = BLSTestUtil.randomKeyPair(3);
    final Validator validator1 = new Validator(keyPair1.getPublicKey(), NO_OP_REMOTE_SIGNER, Optional::empty, true);
    final Validator validator2 = new Validator(keyPair2.getPublicKey(), NO_OP_REMOTE_SIGNER, Optional::empty, false);
    final Validator validator3 = new Validator(keyPair3.getPublicKey(), NO_OP_SIGNER, Optional::empty, false);
    List<Validator> activeValidators = Arrays.asList(validator1, validator2, validator3);
    when(ownedValidators.getActiveValidators()).thenReturn(activeValidators);
    when(validatorLoader.getOwnedValidators()).thenReturn(ownedValidators);
    final List<ExternalValidator> result = keyManager.getActiveRemoteValidatorKeys();
    List<ExternalValidator> externalValidators = Arrays.asList(new ExternalValidator(keyPair1.getPublicKey(), Optional.of(new URL("http://example.com/")), true), new ExternalValidator(keyPair2.getPublicKey(), Optional.of(new URL("http://example.com/")), false));
    assertThat(result).isEqualTo(externalValidators);
}
Also used : Optional(java.util.Optional) ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 3 with ExternalValidator

use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.

the class PostRemoteKeys method handle.

@Override
public void handle(final RestApiRequest request) throws JsonProcessingException {
    final PostRemoteKeysRequest body = request.getRequestBody();
    if (body.getExternalValidators().isEmpty()) {
        request.respondOk(Collections.emptyList());
        return;
    }
    List<ExternalValidator> validators = body.getExternalValidators();
    request.respondOk(keyManager.importExternalValidators(validators));
}
Also used : ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) PostRemoteKeysRequest(tech.pegasys.teku.validator.client.restapi.apis.schema.PostRemoteKeysRequest)

Example 4 with ExternalValidator

use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.

the class ActiveKeyManager method importExternalValidators.

@Override
public List<PostKeyResult> importExternalValidators(final List<ExternalValidator> validators) {
    final List<PostKeyResult> importResults = new ArrayList<>();
    boolean reloadRequired = false;
    for (ExternalValidator v : validators) {
        try {
            importResults.add(validatorLoader.loadExternalMutableValidator(v.getPublicKey(), v.getUrl()));
            if (importResults.get(importResults.size() - 1).getImportStatus() == ImportStatus.IMPORTED) {
                reloadRequired = true;
            }
        } catch (Exception e) {
            importResults.add(PostKeyResult.error(e.getMessage()));
        }
    }
    if (reloadRequired) {
        validatorTimingChannel.onValidatorsAdded();
    }
    return importResults;
}
Also used : ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) PostKeyResult(tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeyResult) ArrayList(java.util.ArrayList) KeyStoreValidationException(tech.pegasys.signers.bls.keystore.KeyStoreValidationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with ExternalValidator

use of tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator in project teku by ConsenSys.

the class ValidatorTypesTest method postRemoteKeysRequest_shouldFormatPostRemoteKeysRequestOptionalUrl.

@SuppressWarnings("unchecked")
@Test
void postRemoteKeysRequest_shouldFormatPostRemoteKeysRequestOptionalUrl() throws Exception {
    final BLSPublicKey publicKey1 = dataStructureUtil.randomPublicKey();
    final BLSPublicKey publicKey2 = dataStructureUtil.randomPublicKey();
    final List<ExternalValidator> externalValidators = List.of(new ExternalValidator(publicKey1, Optional.empty()), new ExternalValidator(publicKey2, Optional.of(new URL("http://host.com"))));
    final PostRemoteKeysRequest request = new PostRemoteKeysRequest(externalValidators);
    final Map<String, Object> result = JsonTestUtil.parse(JsonUtil.serialize(request, ValidatorTypes.POST_REMOTE_KEYS_REQUEST));
    assertThat(result).containsOnlyKeys("remote_keys").isInstanceOf(HashMap.class);
    final List<Map<String, Object>> remoteKeys = (List<Map<String, Object>>) result.get("remote_keys");
    assertThat(remoteKeys).containsExactly(Map.of("pubkey", publicKey1.toString()), Map.of("pubkey", publicKey2.toString(), "url", new URL("http://host.com").toString()));
}
Also used : ExternalValidator(tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator) PostRemoteKeysRequest(tech.pegasys.teku.validator.client.restapi.apis.schema.PostRemoteKeysRequest) List(java.util.List) BLSPublicKey(tech.pegasys.teku.bls.BLSPublicKey) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Aggregations

ExternalValidator (tech.pegasys.teku.validator.client.restapi.apis.schema.ExternalValidator)12 Test (org.junit.jupiter.api.Test)7 URL (java.net.URL)6 PostRemoteKeysRequest (tech.pegasys.teku.validator.client.restapi.apis.schema.PostRemoteKeysRequest)4 PostKeyResult (tech.pegasys.teku.validator.client.restapi.apis.schema.PostKeyResult)3 IOException (java.io.IOException)2 List (java.util.List)2 Optional (java.util.Optional)2 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)2 BLSPublicKey (tech.pegasys.teku.bls.BLSPublicKey)2 InvalidConfigurationException (tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException)2 RestApiRequest (tech.pegasys.teku.infrastructure.restapi.endpoints.RestApiRequest)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1