lambda_convert 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -0
  3. data/lib/lambda_convert/cli.rb +17 -5
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 825e8ca26b989d26a26b063d735e7da3db1fd181
4
- data.tar.gz: 26be24f32c9170cd23931ebfd2790e822f4bd750
3
+ metadata.gz: 25c44e1fb6fa07cb8e1aaf5654100b73721aa0c6
4
+ data.tar.gz: ec7ba833db940fa8dea581cdcd37265e1cea6142
5
5
  SHA512:
6
- metadata.gz: 4e9aa2767b1080468a3ca599e97090004a3e22133dce311534f85187b0baad546feb5d2a5afff57e24091753d31a0a96c1b173dbfb6ea834cf4e233633730c92
7
- data.tar.gz: fa48bfa41584f0ba13ddc0d6494bc69984c25cb61bdd4756a621cdfdb64df6f232b2bba0a98a852ab1e5dcd4545688bd836c6e202d1f91c4bb803d8f7c779b12
6
+ metadata.gz: 12347aac344bf05dd0e0ca8add187610d50d5adf81b962f0136a04c989c5a669db5e78f0ecd075ff5087f32c261c764783714c20304297407c6bfff7a1353aaf
7
+ data.tar.gz: fa4d46a0d15d393f455e20fcd45a2cd68c9ff4585052bb59727550cce5741be24c9be1f02f2f0cadd8abcbc8bbef8387adf46eb6904c59da8074d4a015e0eb5a
data/README.md CHANGED
@@ -1,2 +1,29 @@
1
1
  # lambda-convert
2
2
  AWS Lambda powered drop-in replacement for ImageMagick convert command line tool
3
+
4
+ ## Background
5
+
6
+ At Envoy, we have many image file uploaded by users and will be resized via `convert` (ImageMagick) command line tool. It works fine, the only problems are
7
+
8
+ ### Dealing with big GIF image
9
+
10
+ When user upload a GIF image, to resize it, ImageMagick will need to load the all frames into memory. In that case, even the GIF image file is very small, could posiblly consume huge amount of memory. This brings big impact to our API server, and sometimes the uploading request fails due to this reason.
11
+
12
+ ### Security concerns
13
+
14
+ Despite it's not really easy to perform, it still possible to leverage exploits of certain image file format loading code in ImageMagick.
15
+
16
+ ## Solution
17
+
18
+ To eliminate the big image file uploading issue and the security risk, the idea here is to do image resizing on AWS Lambda instead of localhost. This command line tool is a drop-in replacement for `convert` command, except it upload the input image file to S3, does the resizing on AWS Lambda and finally down the result image back to localhost.
19
+
20
+ ## Environment variables
21
+
22
+ - **CONVERT_S3_REGION** - AWS region for S3, default value will be reading from `AWS_REGION` if this environment variable is not set. (required)
23
+ - **CONVERT_LAMBDA_REGION** - AWS region for Lambda, default value will be reading from `AWS_REGION` if this environment variable is not set. (required)
24
+ - **CONVERT_ACCESS_KEY** - AWS access key, default value will be reading from `AWS_ACCESS_KEY_ID` if this environment variable is not set. (required)
25
+ - **CONVERT_SECRET_ACCESS_KEY** - AWS secret key, default value will be reading from `AWS_SECRET_ACCESS_KEY` if this environment variable is not set. (required)
26
+ - **CONVERT_S3_BUCKET** - AWS S3 bucket. (required)
27
+ - **CONVERT_S3_KEY_PREFIX** - AWS S3 temporary file uploading prefix, default value is `_convert_tmp/`
28
+ - **CONVERT_LAMBDA_FUNCTION** - Name of the AWS Lambda function to invoke, default value is `image-convert-prod`
29
+ - **CONVERT_DISABLE_FALLBACK** - By default, this command line tool fallbacks to local `convert` command if remote operation fails. Set this value to 1 to disable the fallback behavior.
@@ -4,6 +4,14 @@ require 'English'
4
4
 
5
5
  require 'aws-sdk'
6
6
 
7
+ # find command path array matching given `cmd` name in $PATH
8
+ def find_cmd(cmd)
9
+ (ENV['PATH'].split(File::PATH_SEPARATOR).map do |path|
10
+ cmd_path = File.join(path, cmd)
11
+ cmd_path if File.executable?(cmd_path) && !File.directory?(cmd_path)
12
+ end).compact
13
+ end
14
+
7
15
  def parse_input_path(path)
8
16
  # convert command input path could be attached with selecting syntax, let's
9
17
  # parse it and return them in an array of [filename, selecting syntax]
@@ -97,15 +105,19 @@ end
97
105
 
98
106
  def local_convert
99
107
  logger = Logger.new(STDERR)
108
+
100
109
  env = ENV.to_h
101
- # remove Gem bindir from the path, so that we won't invoke ourself
102
- path = ENV['PATH'].split(File::PATH_SEPARATOR) - [Gem.bindir]
103
- env['PATH'] = path.join(File::PATH_SEPARATOR)
110
+ # find the original convert bin path
111
+ original_convert = find_cmd('convert').find do |path|
112
+ # TODO: maybe we need a more robust way to determine whether is given
113
+ # convert path from us or someone else
114
+ File.dirname(path) != Gem.bindir && !path.include?('.rbenv/shims')
115
+ end
104
116
  # we also put a CONVERT_RECURSIVE_FLAG to avoid somehow calling ourself again
105
117
  # by mistake
106
118
  env['CONVERT_RECURSIVE_FLAG'] = '1'
107
- logger.info("Running local convert with args #{ARGV}")
108
- system(env, *(['convert'] + ARGV))
119
+ logger.info("Running local convert #{original_convert} with args #{ARGV}")
120
+ system(env, *([original_convert] + ARGV))
109
121
  abort('Failed to run local convert') unless $CHILD_STATUS.success?
110
122
  logger.info('Done')
111
123
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lambda_convert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fang-Pen Lin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-20 00:00:00.000000000 Z
11
+ date: 2017-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk