SITUATION:
I am trying to download and decrypt some data from my google cloud bucket.
For encryption and decryption, I use:
https://cloud.google.com/kms/docs/quickstart#decrypt_data
Sadly, I get an error : "Invalid value at 'ciphertext' (TYPE_BYTES)".
I know the cyphertext is correct, I believe this may be an issue with the type of data expected by the Google KMS API, i.e.: when retrieving the encrypted data, my code somehow changed it's type before sending the POST request to the Google KMS API.
What did I do wrong and how do I fix it ?
CODE:
gcs.bucket(bucketName)
.file('mysecret.txt.encrypted.txt')
.download({ destination: 'mysecret.txt.encrypted.txt' })
.then(() => {
fs.readFile('mysecret.txt.encrypted.txt', (err, data) => {
if (err) throw err;
console.log("DATA: "+data);
var formData = {
ciphertext: data,
};
request.post({
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ...'
},
url: 'https://cloudkms.googleapis.com/v1/projects/kms-raimarketplace/locations/global/keyRings/.../cryptoKeys/...:decrypt',
form: formData
},
function (err, httpResponse, body) {
if (err) {
console.log("ERROR: "+err);
}
else {
console.log("BODY: "+body);
}
console.log(err, body);
});
});
}).catch(e => {
console.error('getEnv.js: There was an error: ${JSON.stringify(e, undefined, 2)}');
});
OUTPUT:
BODY: {
"error": {
"code": 400,
"message": "Invalid value at 'ciphertext' (TYPE_BYTES), ",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "ciphertext",
"description": "Invalid value at 'ciphertext' (TYPE_BYTES), "
}
]
}
]
}
}