Search in sources :

Example 51 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project java-docs-samples by GoogleCloudPlatform.

the class CustomerSuppliedEncryptionKeysSamples method rotateKey.

/**
 * Given an existing, CSEK-protected object, changes the key used to store that object.
 *
 * @param storage A Storage object, ready for use
 * @param bucketName The name of the destination bucket
 * @param objectName The name of the destination object
 * @param originalBase64Key The AES256 key currently associated with this object,
 *     encoded as a base64 string.
 * @param originalBase64KeyHash The SHA-256 hash of the above key,
 *     also encoded as a base64 string.
 * @param newBase64Key An AES256 key which will replace the existing key,
 *     encoded as a base64 string.
 * @param newBase64KeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
 * @throws IOException if there was some error download from GCS.
 */
public static void rotateKey(Storage storage, String bucketName, String objectName, String originalBase64Key, String originalBase64KeyHash, String newBase64Key, String newBase64KeyHash) throws Exception {
    // Set the CSEK headers
    final HttpHeaders httpHeaders = new HttpHeaders();
    // Specify the exiting object's current CSEK.
    httpHeaders.set("x-goog-copy-source-encryption-algorithm", "AES256");
    httpHeaders.set("x-goog-copy-source-encryption-key", originalBase64Key);
    httpHeaders.set("x-goog-copy-source-encryption-key-sha256", originalBase64KeyHash);
    // Specify the new CSEK that we would like to apply.
    httpHeaders.set("x-goog-encryption-algorithm", "AES256");
    httpHeaders.set("x-goog-encryption-key", newBase64Key);
    httpHeaders.set("x-goog-encryption-key-sha256", newBase64KeyHash);
    Storage.Objects.Rewrite rewriteObject = storage.objects().rewrite(bucketName, objectName, bucketName, objectName, null);
    rewriteObject.setRequestHeaders(httpHeaders);
    try {
        RewriteResponse rewriteResponse = rewriteObject.execute();
        // rewrite until the operation completes.
        while (!rewriteResponse.getDone()) {
            System.out.println("Rewrite did not complete. Resuming...");
            rewriteObject.setRewriteToken(rewriteResponse.getRewriteToken());
            rewriteResponse = rewriteObject.execute();
        }
    } catch (GoogleJsonResponseException e) {
        System.out.println("Error rotating key: " + e.getContent());
        System.exit(1);
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) RewriteResponse(com.google.api.services.storage.model.RewriteResponse)

Example 52 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project halyard by spinnaker.

the class GoogleDistributedService method deleteVersion.

@Override
default void deleteVersion(AccountDeploymentDetails<GoogleAccount> details, ServiceSettings settings, Integer version) {
    String migName = getVersionedName(version);
    String zone = settings.getLocation();
    String project = details.getAccount().getProject();
    Compute compute = GoogleProviderUtils.getCompute(details);
    InstanceGroupManager mig;
    try {
        mig = compute.instanceGroupManagers().get(project, zone, migName).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 404) {
            return;
        } else {
            throw new HalException(FATAL, "Failed to load mig " + migName + " in " + zone, e);
        }
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to load mig " + migName + " in " + zone, e);
    }
    try {
        GoogleProviderUtils.waitOnZoneOperation(compute, project, zone, compute.instanceGroupManagers().delete(project, zone, migName).execute());
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to delete mig " + migName + " in " + zone, e);
    }
    String instanceTemplateName = mig.getInstanceTemplate();
    instanceTemplateName = instanceTemplateName.substring(instanceTemplateName.lastIndexOf('/') + 1);
    try {
        GoogleProviderUtils.waitOnGlobalOperation(compute, project, compute.instanceTemplates().delete(project, instanceTemplateName).execute());
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to delete template " + instanceTemplateName + " in " + zone, e);
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) InstanceGroupManager(com.google.api.services.compute.model.InstanceGroupManager) Compute(com.google.api.services.compute.Compute) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException)

Example 53 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project halyard by spinnaker.

the class GoogleProviderUtils method ensureSpinnakerNetworkExists.

static String ensureSpinnakerNetworkExists(AccountDeploymentDetails<GoogleAccount> details) {
    String networkName = getNetworkName();
    String project = details.getAccount().getProject();
    Compute compute = getCompute(details);
    boolean exists = true;
    try {
        compute.networks().get(project, networkName).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 404) {
            exists = false;
        } else {
            throw new HalException(FATAL, "Google error encountered retrieving network: " + e.getMessage(), e);
        }
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to check if spinnaker network exists: " + e.getMessage(), e);
    }
    if (!exists) {
        String networkUrl;
        Network network = new Network().setAutoCreateSubnetworks(true).setName(networkName).setDescription("Spinnaker network auto-created by Halyard");
        try {
            DaemonTaskHandler.message("Creating a spinnaker network...");
            Operation operation = compute.networks().insert(project, network).execute();
            networkUrl = operation.getTargetLink();
            GoogleProviderUtils.waitOnGlobalOperation(compute, project, operation);
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to create Spinnaker network: " + e.getMessage(), e);
        }
        Firewall.Allowed allowSsh = new Firewall.Allowed().setPorts(Collections.singletonList("22")).setIPProtocol("tcp");
        Firewall firewallSsh = new Firewall().setNetwork(networkUrl).setAllowed(Collections.singletonList(allowSsh)).setName(networkName + "-allow-ssh").setSourceRanges(Collections.singletonList("0.0.0.0/0"));
        Firewall.Allowed allowInternalTcp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("tcp");
        Firewall.Allowed allowInternalUdp = new Firewall.Allowed().setPorts(Collections.singletonList("1-65535")).setIPProtocol("udp");
        Firewall.Allowed allowInternalIcmp = new Firewall.Allowed().setIPProtocol("icmp");
        List<Firewall.Allowed> allowInteral = new ArrayList<>();
        allowInteral.add(allowInternalTcp);
        allowInteral.add(allowInternalUdp);
        allowInteral.add(allowInternalIcmp);
        Firewall firewallInternal = new Firewall().setNetwork(networkUrl).setAllowed(allowInteral).setName(networkName + "-allow-internal").setSourceRanges(Collections.singletonList("10.0.0.0/8"));
        try {
            DaemonTaskHandler.message("Adding firewall rules...");
            compute.firewalls().insert(project, firewallSsh).execute();
            compute.firewalls().insert(project, firewallInternal).execute();
        } catch (IOException e) {
            throw new HalException(FATAL, "Failed to create Firewall rule network: " + e.getMessage(), e);
        }
    }
    return String.format("projects/%s/global/networks/%s", project, networkName);
}
Also used : HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute)

Example 54 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project halyard by spinnaker.

the class AppengineAccountValidator method validate.

@Override
public void validate(ConfigProblemSetBuilder p, AppengineAccount account) {
    String jsonKey = null;
    String jsonPath = account.getJsonPath();
    String project = account.getProject();
    String knownHostsPath = account.getSshKnownHostsFilePath();
    AppengineNamedAccountCredentials credentials = null;
    boolean hasPassword = account.getGitHttpsPassword() != null;
    boolean hasUsername = account.getGitHttpsUsername() != null && !account.getGitHttpsUsername().isEmpty();
    if (hasPassword != hasUsername) {
        if (!hasUsername) {
            p.addProblem(Severity.ERROR, "Git HTTPS password supplied without git HTTPS username.");
        } else {
            p.addProblem(Severity.ERROR, "Git HTTPS username supplied without git HTTPS password.");
        }
    }
    boolean hasSshPrivateKeyPassphrase = account.getSshPrivateKeyPassphrase() != null;
    boolean hasSshPrivateKeyFilePath = account.getSshPrivateKeyFilePath() != null && !account.getSshPrivateKeyFilePath().isEmpty();
    if (hasSshPrivateKeyPassphrase != hasSshPrivateKeyFilePath) {
        if (!hasSshPrivateKeyFilePath) {
            p.addProblem(Severity.ERROR, "SSH private key passphrase supplied without SSH private key filepath.");
        } else {
            p.addProblem(Severity.ERROR, "SSH private key filepath supplied without SSH private key passphrase.");
        }
    } else if (hasSshPrivateKeyPassphrase && hasSshPrivateKeyFilePath) {
        String sshPrivateKey = ValidatingFileReader.contents(p, account.getSshPrivateKeyFilePath());
        if (sshPrivateKey == null) {
            return;
        } else if (sshPrivateKey.isEmpty()) {
            p.addProblem(Severity.WARNING, "The supplied SSH private key file is empty.");
        } else {
            try {
                // Assumes that the public key is sitting next to the private key with the extension ".pub".
                KeyPair keyPair = KeyPair.load(new JSch(), account.getSshPrivateKeyFilePath());
                boolean decrypted = keyPair.decrypt(account.getSshPrivateKeyPassphrase());
                if (!decrypted) {
                    p.addProblem(Severity.ERROR, "Could not unlock SSH public/private keypair with supplied passphrase.");
                }
            } catch (JSchException e) {
                p.addProblem(Severity.ERROR, "Could not unlock SSH public/private keypair: " + e.getMessage() + ".");
            }
        }
    }
    if (knownHostsPath != null && !knownHostsPath.isEmpty()) {
        String knownHosts = ValidatingFileReader.contents(p, knownHostsPath);
        if (knownHosts == null) {
            return;
        }
        if (knownHosts.isEmpty()) {
            p.addProblem(Severity.WARNING, "The supplied known_hosts file is empty.");
        }
    }
    if (jsonPath != null && !jsonPath.isEmpty()) {
        jsonKey = ValidatingFileReader.contents(p, account.getJsonPath());
        if (jsonKey == null) {
            return;
        }
        if (jsonKey.isEmpty()) {
            p.addProblem(Severity.WARNING, "The supplied credentials file is empty.");
        }
    }
    if (jsonPath != null && !jsonPath.isEmpty() && account.isSshTrustUnknownHosts()) {
        p.addProblem(Severity.WARNING, "You have supplied a known_hosts file path and set the `--ssh-trust-unknown-hosts` flag to true." + " Spinnaker will ignore your `--ssh-trust-unknown-hosts` flag.").setRemediation("Run `--ssh-trust-unknown-hosts false`.");
    }
    if (account.getProject() == null || account.getProject().isEmpty()) {
        p.addProblem(Severity.ERROR, "No appengine project supplied.");
        return;
    }
    try {
        credentials = new AppengineNamedAccountCredentials.Builder().jsonKey(jsonKey).project(project).region("halyard").applicationName("halyard " + halyardVersion).build();
    } catch (Exception e) {
        p.addProblem(Severity.ERROR, "Error instantiating appengine credentials: " + e.getMessage() + ".");
        return;
    }
    try {
        credentials.getAppengine().apps().get(project).execute();
    } catch (GoogleJsonResponseException e) {
        if (e.getStatusCode() == 404) {
            p.addProblem(Severity.ERROR, "No appengine application found for project " + project + ".").setRemediation("Run `gcloud app create --region <region>` to create an appengine application.");
        } else {
            p.addProblem(Severity.ERROR, "Failed to connect to appengine Admin API: " + e.getMessage() + ".");
        }
    } catch (Exception e) {
        p.addProblem(Severity.ERROR, "Failed to connect to appengine Admin API: " + e.getMessage() + ".");
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) KeyPair(com.jcraft.jsch.KeyPair) ConfigProblemSetBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder) AppengineNamedAccountCredentials(com.netflix.spinnaker.clouddriver.appengine.security.AppengineNamedAccountCredentials) JSch(com.jcraft.jsch.JSch) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) JSchException(com.jcraft.jsch.JSchException)

Example 55 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException 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)

Aggregations

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)94 IOException (java.io.IOException)48 YouTube (com.google.api.services.youtube.YouTube)26 Credential (com.google.api.client.auth.oauth2.Credential)25 ArrayList (java.util.ArrayList)13 Operation (com.google.api.services.compute.model.Operation)12 Test (org.junit.Test)12 Compute (com.google.api.services.compute.Compute)11 Storage (com.google.api.services.storage.Storage)10 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)8 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)7 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)5 InputStreamContent (com.google.api.client.http.InputStreamContent)5 BackOff (com.google.api.client.util.BackOff)5 HashMap (java.util.HashMap)5 Objects (com.google.api.services.storage.model.Objects)4 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)4 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)4 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)3 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)3