eip-cloud-services 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [1.0.21] - 2023-06-21
6
+
7
+ ### Fixed
8
+ - Bug where ACL's were not copied across when moving S3 objects.
9
+
10
+ ## [1.0.2] - 2023-06-21
11
+
12
+ ### Added
13
+ - Initial release of the module.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eip-cloud-services",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Houses a collection of helpers for connecting with Cloud services.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,4 +22,4 @@
22
22
  "mysql": "^2.18.1",
23
23
  "redis": "^4.6.7"
24
24
  }
25
- }
25
+ }
package/src/gcp.js CHANGED
@@ -8,7 +8,7 @@ exports.initialiseGoogleAuth = async () => {
8
8
 
9
9
  try {
10
10
  await readFile ( `${os.tmpdir ()}/gcp.json` );
11
- process.env.GOOGLE_APPLICATION_CREDENTIALS = './gcp.json';
11
+ process.env.GOOGLE_APPLICATION_CREDENTIALS = `${os.tmpdir ()}/gcp.json`;
12
12
 
13
13
  return;
14
14
  }
package/src/s3.js CHANGED
@@ -41,6 +41,7 @@
41
41
  const { S3Client, HeadObjectCommand, GetObjectCommand, PutObjectCommand, DeleteObjectCommand, CopyObjectCommand, DeleteObjectsCommand } = require ( '@aws-sdk/client-s3' );
42
42
  const config = require ( 'config' );
43
43
  const zlib = require ( 'zlib' );
44
+ const crypto = require ( 'crypto' );
44
45
 
45
46
  const S3 = new S3Client ( { region: 'eu-west-1' } );
46
47
 
@@ -114,6 +115,8 @@ exports.get = async ( key ) => {
114
115
  * @param {string} [options.contentType='application/json'] - The content type of the object. Defaults to 'application/json'.
115
116
  * @param {string} [options.acl='public-read'] - The ACL (Access Control List) of the object. Defaults to 'public-read'.
116
117
  * @param {string} [options.cacheControl='max-age=25,s-maxage=30,must-revalidate'] - Sets cache control for the object.
118
+ * @param {boolean} [options.encrypt=false] - When storing JSON parsing this as true will encrypt the data with a random uuid stored in the metadata of the object.
119
+ * @param {object} [options.metadata={}] - Sets metadata for the object.
117
120
  * @returns {Promise} A promise that resolves when the object is successfully set in S3.
118
121
  * @description Sets an object in S3 with the provided key, body, and optional parameters.
119
122
  */
@@ -122,9 +125,34 @@ exports.set = async ( key, body, options = {} ) => {
122
125
  bucket = config.s3.Bucket,
123
126
  contentType = 'application/json',
124
127
  acl = 'public-read',
125
- cacheControl = 'max-age=25,s-maxage=30,must-revalidate'
128
+ cacheControl = 'max-age=25,s-maxage=30,must-revalidate',
129
+ encrypt = false,
130
+ metadata = {}
126
131
  } = options;
127
-
132
+
133
+ if ( encrypt && contentType === 'application/json' ) {
134
+ const encoder = new TextEncoder ();
135
+ const data = encoder.encode ( body );
136
+
137
+ const encryptionKey = await crypto.subtle.generateKey (
138
+ { name: 'AES-CBC', length: 256 },
139
+ true,
140
+ [ 'encrypt', 'decrypt' ]
141
+ );
142
+ const iv = crypto.randomBytes ( 16 );
143
+ const exportedKey = await crypto.subtle.exportKey ( 'raw', encryptionKey );
144
+ const exportedIV = iv.toString ( 'base64' );
145
+ metadata[ 'Tmg-Crypt' ] = Buffer.from ( exportedKey ).toString ( 'base64' );
146
+ metadata[ 'Tmg-Crypt-Vec' ] = exportedIV;
147
+
148
+ const encryptedData = await crypto.subtle.encrypt (
149
+ { name: 'AES-CBC', iv },
150
+ encryptionKey,
151
+ data
152
+ );
153
+ body = Buffer.from ( encryptedData ).toString ( 'base64' );
154
+ }
155
+
128
156
  try {
129
157
  const command = new PutObjectCommand ( {
130
158
  Bucket: bucket,
@@ -132,7 +160,8 @@ exports.set = async ( key, body, options = {} ) => {
132
160
  Body: body,
133
161
  ContentType: contentType,
134
162
  ACL: acl,
135
- CacheControl: cacheControl
163
+ CacheControl: cacheControl,
164
+ Metadata: metadata
136
165
  } );
137
166
 
138
167
  const data = await S3.send ( command );
@@ -140,6 +169,7 @@ exports.set = async ( key, body, options = {} ) => {
140
169
  return data;
141
170
  }
142
171
  catch ( error ) {
172
+ console.log ( error );
143
173
  throw error;
144
174
  }
145
175
  };
@@ -184,7 +214,9 @@ exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
184
214
  const copyCommand = new CopyObjectCommand ( {
185
215
  CopySource: `/${sourceBucket}/${sourceKey}`,
186
216
  Bucket: destinationBucket,
187
- Key: destinationKey
217
+ Key: destinationKey,
218
+ ACL: 'public-read',
219
+ MetadataDirective: 'COPY',
188
220
  } );
189
221
 
190
222
  await S3.send ( copyCommand );