use of com.vmware.xenon.common.LocalizableValidationException in project photon-model by vmware.
the class EncryptorServiceTest method testWrongFile.
@Test
public void testWrongFile() throws IOException {
// it doesn't exist!
File keyFile = new File("wrong");
try {
new EncryptorService(keyFile);
fail("It shouldn't get here");
} catch (LocalizableValidationException e) {
assertTrue(e.getMessage().equalsIgnoreCase("Invalid encryption key file!"));
}
}
use of com.vmware.xenon.common.LocalizableValidationException in project photon-model by vmware.
the class EncryptionUtils method getEncryptionFile.
private static File getEncryptionFile() {
String param = System.getProperty(ENCRYPTION_KEY);
if (param == null) {
return null;
}
File encryptionKeyFile = new File(param);
if (!encryptionKeyFile.exists()) {
if (Boolean.getBoolean(INIT_KEY_IF_MISSING)) {
try {
Files.write(encryptionKeyFile.toPath(), EncryptorService.generateKey());
} catch (Exception e) {
throw new LocalizableValidationException(e, "Error initializing the encryption key file '" + param + "'!", "common.encryption.file.init", param);
}
} else {
throw new LocalizableValidationException("File '" + param + "' does not exist!", "common.encryption.file.missing", param);
}
}
return encryptionKeyFile;
}
use of com.vmware.xenon.common.LocalizableValidationException in project photon-model by vmware.
the class AWSEndpointAdapterService method validateCredentialsWithRegions.
/**
* Method to validate credentials until atleast one region returns success. Validation fails if
* unable to validate in any region.
*/
private void validateCredentialsWithRegions(AuthCredentialsServiceState credentials, AtomicInteger index, Regions[] regions, DeferredResult<Void> deferredResult) {
if (index.get() >= regions.length) {
// Unable to validate in any of the Regions.
deferredResult.fail(new LocalizableValidationException(UNABLE_TO_VALIDATE_CREDENTIALS_IN_ANY_AWS_REGION, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE));
return;
}
String region = regions[index.get()].getName();
getEc2AsyncClient(credentials, region, this.clientManager.getExecutor()).thenCompose(this::validateCredentials).whenComplete((res, e) -> {
if (e == null) {
// Validation succeeded in the region
deferredResult.complete((Void) null);
return;
}
if (!(e.getCause() instanceof LocalizableValidationException)) {
deferredResult.fail(new LocalizableValidationException(e, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE));
return;
}
index.getAndIncrement();
validateCredentialsWithRegions(credentials, index, regions, deferredResult);
});
}
use of com.vmware.xenon.common.LocalizableValidationException in project photon-model by vmware.
the class AzureEndpointAdapterService method validate.
private BiConsumer<AuthCredentialsServiceState, BiConsumer<ServiceErrorResponse, Throwable>> validate(EndpointConfigRequest body) {
return (credentials, callback) -> {
try {
Boolean shouldProvision = Boolean.parseBoolean(body.endpointProperties.get(AZURE_PROVISIONING_PERMISSION));
validateEndpointUniqueness(credentials, body.checkForEndpointUniqueness, body.tenantLinks).thenCompose(aVoid -> validateCredentials(credentials)).thenCompose(subscription -> getPermissions(credentials)).thenCompose(permList -> verifyPermissions(permList, shouldProvision)).whenComplete((aVoid, e) -> {
if (e == null) {
callback.accept(null, null);
return;
}
if (e instanceof CompletionException) {
e = e.getCause();
}
final LocalizableValidationException localizableExc;
if (e instanceof LocalizableValidationException) {
localizableExc = (LocalizableValidationException) e;
} else {
// Azure doesn't send us any meaningful status code to work with
localizableExc = new LocalizableValidationException(e, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE);
}
ServiceErrorResponse rsp = Utils.toServiceErrorResponse(localizableExc);
rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
callback.accept(rsp, localizableExc);
});
} catch (Throwable e) {
logSevere(e);
ServiceErrorResponse rsp = new ServiceErrorResponse();
rsp.message = "Invalid Azure credentials";
rsp.statusCode = STATUS_CODE_UNAUTHORIZED;
callback.accept(rsp, e);
}
};
}
use of com.vmware.xenon.common.LocalizableValidationException in project photon-model by vmware.
the class CertificateUtil method validateCertificateChain.
public static void validateCertificateChain(X509Certificate[] certificateChain) throws Exception {
List<X509Certificate> certificates = Arrays.asList(certificateChain);
for (X509Certificate certificate : certificates) {
checkIfCertificateExistsMoreThanOneTimeInChain(certificates, certificate);
}
Iterator<X509Certificate> it = certificates.iterator();
X509Certificate current = it.next();
current.checkValidity();
while (it.hasNext()) {
X509Certificate next = it.next();
next.checkValidity();
try {
current.verify(next.getPublicKey());
} catch (InvalidKeyException | CertificateException | NoSuchAlgorithmException | NoSuchProviderException e) {
throw new IllegalArgumentException(e);
} catch (SignatureException e) {
throw new LocalizableValidationException("Certificate chain is not valid.", "security.certificate.invalid");
}
current = next;
}
}
Aggregations