cfn-model 0.1.32 → 0.1.33

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82a2ad87afed68b9eee1deb1084a18812a2433cb8dbfbb041c805b84ea9e3d55
4
- data.tar.gz: 89aa11931767791f101933cf9c19e0c7cd91830695c9129852b20f6a709793ee
3
+ metadata.gz: a9079fef39b645007961bf5837699e02f76b86a587c79f74faf390165a8dcf56
4
+ data.tar.gz: 6b3563c7239a31ad68ddd705649d8ca38b6db1adc061632de39faea256bc77b6
5
5
  SHA512:
6
- metadata.gz: 17d77fa70360798f4a62abf45d22b9615d5ef9c948ccfcff3240c43898127075f669d18294db41e85219039f0710390e1aec24a151a251eddc0c385598a5d39e
7
- data.tar.gz: d2aa19b4049b8fa73b4b6dd2c9ae786e693e8394e6daa23d0b5255283e8997de205854093575cd816ab9225e9d11424ce8faf693c4eaed38674422410b5284ff
6
+ metadata.gz: 0e38f76f6f5b2d6bddad02c994b492ab7a98211e5049dc62840540acd4435af501fbedb08978d558d76fbfc03757cd3e2253eabd3cbcbbfc41fc4d2b3a6bf270
7
+ data.tar.gz: 434fc11caf6c25f449ad4dcf6bd2c6b63c9ad9bb86ffe1a1a01bdea53a2e3df98763695d98d632940441d755512592a88be634e4fb23645f3c69bdb7baa59a08
@@ -29,35 +29,75 @@ class CfnModel
29
29
  uri.split('/')[3..-1].join('/')
30
30
  end
31
31
 
32
- # Is a URL a S3 URL
33
- def is_s3_uri?(uri)
34
- uri[0..4].eql? 's3://'
32
+ def s3_uri?(uri)
33
+ if uri.is_a? String
34
+ uri[0..4].eql? 's3://'
35
+ else
36
+ false
37
+ end
35
38
  end
36
39
 
37
- # rubocop:disable Metrics/AbcSize
38
- # rubocop:disable Metrics/MethodLength
40
+ def resolve_globals_function_property(cfn_hash, property_name)
41
+ cfn_hash['Globals']['Function'][property_name]
42
+ end
39
43
 
40
- def replace_serverless_function(cfn_hash, resource_name)
41
- resource = cfn_hash['Resources'][resource_name]
42
- code_uri = resource['Properties']['CodeUri']
44
+ def serverless_function_property(serverless_function, cfn_hash, property_name)
45
+ serverless_function['Properties'][property_name] || \
46
+ resolve_globals_function_property(cfn_hash, property_name)
47
+ end
43
48
 
44
- lambda_fn_params = {
45
- handler: resource['Properties']['Handler'],
46
- runtime: resource['Properties']['Runtime']
47
- }
48
- if is_s3_uri? code_uri
49
+ # i question whether we need to carry out the transform this far given cfn_nag
50
+ # likely won't ever opine on bucket names or object keys
51
+ def transform_code_uri(lambda_fn_params, code_uri)
52
+ puts code_uri
53
+ if s3_uri? code_uri
49
54
  lambda_fn_params[:code_bucket] = bucket_from_uri code_uri
50
55
  lambda_fn_params[:code_key] = object_key_from_uri code_uri
56
+ elsif code_uri.is_a? Hash
57
+ lambda_fn_params[:code_bucket] = code_uri['Bucket']
58
+ lambda_fn_params[:code_key] = code_uri['Key']
51
59
  end
52
- cfn_hash['Resources'][resource_name] = \
53
- lambda_function lambda_fn_params
60
+ lambda_fn_params
61
+ end
62
+
63
+ def serverless_function_properties(cfn_hash, serverless_function)
64
+ code_uri = serverless_function_property(serverless_function, cfn_hash, 'CodeUri')
65
+
66
+ lambda_fn_params = {
67
+ handler: serverless_function_property(serverless_function, cfn_hash, 'Handler'),
68
+ runtime: serverless_function_property(serverless_function, cfn_hash, 'Runtime')
69
+ }
70
+
71
+ lambda_fn_params = transform_code_uri(
72
+ lambda_fn_params,
73
+ code_uri
74
+ )
75
+
76
+ lambda_fn_params
77
+ end
78
+
79
+ def replace_serverless_function(cfn_hash, resource_name)
80
+ serverless_function = cfn_hash['Resources'][resource_name]
81
+
82
+ lambda_fn_params = serverless_function_properties(cfn_hash, serverless_function)
83
+
84
+ cfn_hash['Resources'][resource_name] = lambda_function lambda_fn_params
54
85
 
55
86
  cfn_hash['Resources']['FunctionNameRole'] = function_name_role
56
87
  end
57
- # rubocop:enable Metrics/AbcSize
58
- # rubocop:enable Metrics/MethodLength
59
88
 
60
- # rubocop:disable Metrics/MethodLength
89
+ def lambda_service_can_assume_role
90
+ {
91
+ 'Version' => '2012-10-17',
92
+ 'Statement' => [
93
+ {
94
+ 'Action' => ['sts:AssumeRole'],
95
+ 'Effect' => 'Allow',
96
+ 'Principal' => { 'Service' => ['lambda.amazonaws.com'] }
97
+ }
98
+ ]
99
+ }
100
+ end
61
101
 
62
102
  # Return the hash structure of the 'FunctionNameRole'
63
103
  # AWS::IAM::Role resource as created by Serverless transform
@@ -65,20 +105,23 @@ class CfnModel
65
105
  {
66
106
  'Type' => 'AWS::IAM::Role',
67
107
  'Properties' => {
68
- 'ManagedPolicyArns' =>
69
- ['arn:aws:iam::aws:policy/service-role/' \
70
- 'AWSLambdaBasicExecutionRole'],
71
- 'AssumeRolePolicyDocument' => {
72
- 'Version' => '2012-10-17',
73
- 'Statement' => [{
74
- 'Action' => ['sts:AssumeRole'], 'Effect' => 'Allow',
75
- 'Principal' => { 'Service' => ['lambda.amazonaws.com'] }
76
- }]
77
- }
108
+ 'ManagedPolicyArns' => [
109
+ 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
110
+ ],
111
+ 'AssumeRolePolicyDocument' => lambda_service_can_assume_role
78
112
  }
79
113
  }
80
114
  end
81
- # rubocop:enable Metrics/MethodLength
115
+
116
+ def lambda_function_code(fn_resource, code_bucket, code_key)
117
+ if code_bucket && code_key
118
+ fn_resource['Properties']['Code'] = {
119
+ 'S3Bucket' => code_bucket,
120
+ 'S3Key' => code_key
121
+ }
122
+ end
123
+ fn_resource
124
+ end
82
125
 
83
126
  # Return the hash structure of a AWS::Lambda::Function as created
84
127
  # by Serverless transform
@@ -86,20 +129,15 @@ class CfnModel
86
129
  code_bucket: nil,
87
130
  code_key: nil,
88
131
  runtime:)
89
- fn_resource = \
90
- { 'Type' => 'AWS::Lambda::Function',
132
+ fn_resource = {
133
+ 'Type' => 'AWS::Lambda::Function',
91
134
  'Properties' => {
92
135
  'Handler' => handler,
93
136
  'Role' => { 'Fn::GetAtt' => %w[FunctionNameRole Arn] },
94
137
  'Runtime' => runtime
95
- } }
96
- if code_bucket && code_key
97
- fn_resource['Properties']['Code'] = {
98
- 'S3Bucket' => code_bucket,
99
- 'S3Key' => code_key
100
138
  }
101
- end
102
- fn_resource
139
+ }
140
+ lambda_function_code(fn_resource, code_bucket, code_key)
103
141
  end
104
142
  end
105
143
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfn-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.32
4
+ version: 0.1.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Kascic