Search in sources :

Example 11 with DecryptResponse

use of com.aliyuncs.kms.model.v20160120.DecryptResponse in project gapic-generator-java by googleapis.

the class SyncDecryptCryptokeynameBytestring method syncDecryptCryptokeynameBytestring.

public static void syncDecryptCryptokeynameBytestring() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        CryptoKeyName name = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]");
        ByteString ciphertext = ByteString.EMPTY;
        DecryptResponse response = keyManagementServiceClient.decrypt(name, ciphertext);
    }
}
Also used : DecryptResponse(com.google.cloud.kms.v1.DecryptResponse) CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) ByteString(com.google.protobuf.ByteString) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 12 with DecryptResponse

use of com.aliyuncs.kms.model.v20160120.DecryptResponse in project gapic-generator-java by googleapis.

the class SyncDecryptStringBytestring method syncDecryptStringBytestring.

public static void syncDecryptStringBytestring() throws Exception {
    // It may require modifications to work in your environment.
    try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
        String name = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]").toString();
        ByteString ciphertext = ByteString.EMPTY;
        DecryptResponse response = keyManagementServiceClient.decrypt(name, ciphertext);
    }
}
Also used : DecryptResponse(com.google.cloud.kms.v1.DecryptResponse) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 13 with DecryptResponse

use of com.aliyuncs.kms.model.v20160120.DecryptResponse in project gcp-ingestion by mozilla.

the class KeyStore method loadAllKeys.

private void loadAllKeys() throws IOException {
    final Map<String, PrivateKey> tempKeys = new HashMap<>();
    Schema schema;
    try {
        byte[] data = Resources.toByteArray(Resources.getResource("keystore-metadata.schema.json"));
        schema = JSONSchemaStore.readSchema(data);
    } catch (IOException e) {
        throw new IOException("Error reading keystore metadata schema file", e);
    }
    // required to validate Jackson objects
    JsonValidator validator = new JsonValidator();
    ArrayNode metadata;
    try (InputStream inputStream = BeamFileInputStream.open(this.metadataLocation)) {
        byte[] data = IOUtils.toByteArray(inputStream);
        metadata = Json.readArrayNode(data);
        validator.validate(schema, metadata);
    } catch (IOException e) {
        throw new IOException("Error reading keystore metadata schema.", e);
    }
    for (JsonNode element : metadata) {
        String privateKeyId = element.get("private_key_id").textValue();
        String privateKeyUri = element.get("private_key_uri").textValue();
        String kmsResourceId = element.get("kms_resource_id").textValue();
        try (InputStream inputStream = BeamFileInputStream.open(privateKeyUri)) {
            byte[] keyData = IOUtils.toByteArray(inputStream);
            PublicJsonWebKey key;
            if (kmsEnabled) {
                try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
                    DecryptResponse response = client.decrypt(kmsResourceId, ByteString.copyFrom(keyData));
                    key = PublicJsonWebKey.Factory.newPublicJwk(response.getPlaintext().toStringUtf8());
                }
            } else {
                key = PublicJsonWebKey.Factory.newPublicJwk(new String(keyData, StandardCharsets.UTF_8));
            }
            tempKeys.put(privateKeyId, key.getPrivateKey());
        } catch (IOException e) {
            throw new IOException("Error reading key specified by metadata.", e);
        } catch (JoseException e) {
            throw new RuntimeException(e);
        }
    }
    keys = tempKeys;
}
Also used : PrivateKey(java.security.PrivateKey) HashMap(java.util.HashMap) InputStream(java.io.InputStream) JoseException(org.jose4j.lang.JoseException) Schema(org.everit.json.schema.Schema) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) DecryptResponse(com.google.cloud.kms.v1.DecryptResponse) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

DecryptResponse (com.google.cloud.kms.v1.DecryptResponse)11 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)7 DecryptRequest (com.google.cloud.kms.v1.DecryptRequest)6 ByteString (com.google.protobuf.ByteString)6 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)5 ClientException (com.aliyun.oss.ClientException)2 DecryptResponse (com.aliyuncs.kms.model.v20160120.DecryptResponse)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Test (org.junit.jupiter.api.Test)2 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)1 DecryptRequest (com.aliyuncs.kms.model.v20160120.DecryptRequest)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 EncryptRequest (com.google.cloud.kms.v1.EncryptRequest)1 EncryptResponse (com.google.cloud.kms.v1.EncryptResponse)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 PrivateKey (java.security.PrivateKey)1 LinkedHashMap (java.util.LinkedHashMap)1