Search in sources :

Example 76 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class VaultService method publishSecret.

public void publishSecret(DeploymentConfiguration deploymentConfiguration, String name, String contents) {
    String vaultAddress = deploymentConfiguration.getDeploymentEnvironment().getVault().getAddress();
    String encodedContents = Base64.getEncoder().encodeToString(contents.getBytes());
    String secretName = vaultSecretPrefix + name;
    List<String> command = new ArrayList<>();
    command.add("vault");
    command.add("write");
    command.add("--address");
    command.add(vaultAddress);
    command.add(secretName);
    command.add(encodedContents);
    JobRequest request = new JobRequest().setTokenizedCommand(command).setTimeoutMillis(TimeUnit.SECONDS.toMillis(vaultTimeoutSeconds));
    String id = jobExecutor.startJob(request);
    DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
    JobStatus status = jobExecutor.updateJob(id);
    if (!status.getResult().equals(JobStatus.Result.SUCCESS)) {
        throw new HalException(Problem.Severity.FATAL, "Failed to publish secret " + name + ": " + status.getStdOut() + status.getStdErr());
    }
}
Also used : JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArrayList(java.util.ArrayList)

Example 77 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class GoogleWriteableProfileRegistry method writeTextObject.

private void writeTextObject(String name, String contents) {
    try {
        byte[] bytes = contents.getBytes();
        StorageObject object = new StorageObject().setBucket(spinconfigBucket).setName(name);
        ByteArrayContent content = new ByteArrayContent("application/text", bytes);
        storage.objects().insert(spinconfigBucket, object, content).execute();
    } catch (IOException e) {
        log.error("Failed to write new object " + name, e);
        throw new HalException(new ProblemBuilder(Severity.FATAL, "Failed to write to " + name + ": " + e.getMessage()).build());
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent) ProblemBuilder(com.netflix.spinnaker.halyard.core.problem.v1.ProblemBuilder)

Example 78 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class SecureStorage method backupFile.

public void backupFile(String name, File file) {
    String contents;
    try {
        contents = IOUtils.toString(new FileInputStream(file));
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Can't load file for secure storage: " + e.getMessage(), e);
    }
    storeContents(name, contents);
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 79 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class GoogleKms method ensureCryptoKeyExists.

private static CryptoKey ensureCryptoKeyExists(CloudKMS cloudKms, GoogleCredential credential, String keyRingId, String cryptoKeyId) {
    CryptoKey cryptoKey;
    try {
        cryptoKey = cloudKms.projects().locations().keyRings().cryptoKeys().get(cryptoKeyId).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 404) {
            cryptoKey = null;
        } else {
            throw new HalException(Problem.Severity.FATAL, "Unexpected error retrieving crypto key: " + e.getMessage(), e);
        }
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Unexpected error retrieving crypto key: " + e.getMessage(), e);
    }
    if (cryptoKey == null) {
        String cryptoKeyName = cryptoKeyId.substring(cryptoKeyId.lastIndexOf('/') + 1);
        log.info("Creating a new crypto key " + cryptoKeyName);
        String user = "serviceAccount:" + credential.getServiceAccountId();
        cryptoKey = createCryptoKey(cloudKms, keyRingId, cryptoKeyName, user);
    }
    return cryptoKey;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException)

Example 80 with HalException

use of com.netflix.spinnaker.halyard.core.error.v1.HalException in project halyard by spinnaker.

the class GoogleKms method ensureKeyRingExists.

private static KeyRing ensureKeyRingExists(CloudKMS cloudKms, String locationId, String keyRingId) {
    KeyRing keyRing;
    try {
        keyRing = cloudKms.projects().locations().keyRings().get(keyRingId).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 404) {
            keyRing = null;
        } else {
            throw new HalException(Problem.Severity.FATAL, "Unexpected error retrieving key ring: " + e.getMessage(), e);
        }
    } catch (IOException e) {
        throw new HalException(Problem.Severity.FATAL, "Unexpected error retrieving key ring: " + e.getMessage(), e);
    }
    if (keyRing == null) {
        String keyRingName = keyRingId.substring(keyRingId.lastIndexOf('/') + 1);
        log.info("Creating a new key ring " + keyRingName);
        keyRing = createKeyRing(cloudKms, locationId, keyRingName);
    }
    return keyRing;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException)

Aggregations

HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)88 IOException (java.io.IOException)37 ConfigProblemBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder)17 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)16 ArrayList (java.util.ArrayList)15 FileInputStream (java.io.FileInputStream)14 File (java.io.File)12 HashMap (java.util.HashMap)12 JobStatus (com.netflix.spinnaker.halyard.core.job.v1.JobStatus)11 RunningServiceDetails (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails)11 Map (java.util.Map)11 JobRequest (com.netflix.spinnaker.halyard.core.job.v1.JobRequest)10 Field (java.lang.reflect.Field)9 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)8 Path (java.nio.file.Path)8 List (java.util.List)7 Compute (com.google.api.services.compute.Compute)6 Problem (com.netflix.spinnaker.halyard.core.problem.v1.Problem)6 Paths (java.nio.file.Paths)6 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)5