addy_lambda 0.1.0 → 0.1.1

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: a40fe7dd18f9436441daacde8ea4ea2a6d0ccfd3878a9d2b03e00e7a76c0d8ce
4
- data.tar.gz: a8a52aeeafbd076c27b1875cb94d8602eea09272f5f28449e937a8ff581d2d9e
3
+ metadata.gz: b6e0c3ac7b853a06d75aed86d86c507c8b26eaa012572ec6e075782fc11c554a
4
+ data.tar.gz: dfd63b81d918faed64600bd88cb5e03773697aa2b6aa2fc09898ebb4923c2b59
5
5
  SHA512:
6
- metadata.gz: 95519ec57cb521f6e9f8cca6ea1ebaeb9a3a72f55cbc52a67d657a11853ef953096df74512cf6e4bd03b384d1813144467a71ff5c21a4290df451715ad1f2653
7
- data.tar.gz: 15db8292c1ea02d3f9868d914dfd2fa381a09dd4bc62a47068fe22c7551f941b6f6676ee47599de36857756c3fa33fca3ab3f9b998b81b7d9bd501e809c02d1c
6
+ metadata.gz: 1ec0bc266dcbeeae6e680437e9172b773ee71856238a1a285d58395883171f585a052614a38b7e05a1f1094b8253b1b0eae57b48cb983995fcb744adf97db959
7
+ data.tar.gz: ee662005af9a3efcf3536b3dee8c9db79718d0dc8840fd00756a2cad094c6d1d416cc5f12abdba149b3303d2da978053c75e61940da7fa1aa574c4df83b9046c
data/.gitignore CHANGED
@@ -7,4 +7,5 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  /vendor
10
- /bin
10
+ /bin
11
+ addy_lambda-*
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  The purpose of this gem, is to make common functionality for Lambdas be available across all of our Lambda implementaitons.
4
4
 
5
5
  Things like:
6
+
6
7
  - calling API endpoints
7
8
  - Posting to slack
8
9
  - Getting a JWT token
@@ -27,9 +28,16 @@ Or install it yourself as:
27
28
 
28
29
  $ gem install addy_lambda
29
30
 
31
+ ## Publishing a new version
32
+
33
+ - Make your changes
34
+ - Bump the version in `lib/addy_lambda/version.rb`
35
+ - Build using `gem build addy_lambda.gemspec`
36
+
30
37
  ## Usage
31
38
 
32
39
  Run the tests:
40
+
33
41
  ```
34
42
  ruby "/home/cabox/workspace/addy_lambda/test/addy_lambda_test.rb"
35
43
  ```
@@ -55,7 +63,7 @@ puts(test)
55
63
 
56
64
  ## Development
57
65
 
58
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec ruby test/test_helper.rb` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
67
 
60
68
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
69
 
@@ -63,7 +71,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
63
71
 
64
72
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/addy_lambda. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/addy_lambda/blob/master/CODE_OF_CONDUCT.md).
65
73
 
66
-
67
74
  ## License
68
75
 
69
76
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/deleteme.rb CHANGED
@@ -2,5 +2,14 @@ require_relative 'lib/addy_lambda'
2
2
 
3
3
  lambda_client = Aws::Lambda::Client.new
4
4
 
5
- resp = AddyLambda::Common.post_slack_message(lambda_client, '@alon', 'This is the mess', ':tada:')
6
- puts(resp['message'])
5
+ # resp = AddyLambda::AWS.send_email(lambda_client, ['alon@sabi.me', 'alon@addyinvest.com'], 'alon', 'alon@addyinvest.com',
6
+ # 'This is the subject', 'This is the content')
7
+
8
+ # resp = AddyLambda::AWS.post_slack_message(lambda_client, '@alon', 'This is the mess', ':tada:')
9
+
10
+ # pdf_annotate / subscription
11
+ resp = AddyLambda::AWS.generate_jwt(lambda_client, AddyLambda::JwtIssuers::ADDY, AddyLambda::JwtAudiences::GENERIC,
12
+ 3000, { 'alon' => 'sabi' })
13
+ # resp = AddyLambda::AWS.validate_jwt(lambda_client,
14
+ # 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhbG9uIjoic2FiaSIsImlzcyI6ImFkZHkiLCJhdWQiOiJnZW5lcmljIiwiaWF0IjoxNjM5NDQxMzk5LCJleHAiOjE2Mzk0NDQzOTl9.jj2OhiEZnJ9s6tamHdc0wGNzlgW3Jwq-S8YquTxe_JWhHuOZKYUhlCM1J1AtaLBqwbnFONCI1geK6cDXIA57lwikh2GcBZGecFfn3hq5IuoSqkhSW-PypTsw2dVCPX-W-qOj6ktbVkTU-d9NvTJJAR4nkJdQMBFtDDgMP_huzxldUjkV9i4ruiebev9blinLuwS_pHIOeqpnTkqRxQ9U6oZscYT8dxHUVoTG03_1dbnnfq3xoBjmBzENz9j2CtZO_fnM96iwqPR4EfxpM7TU7o9NxIzunmN4_Z6Jmjh5yQWpRSp5ItnqQ5N9m6a5bE3NyGDfJh3tVHs7wg3VNqxsLQ')
15
+ puts(resp)
@@ -1,6 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'aws-sdk-lambda'
4
+
3
5
  module AddyLambda
6
+ class JwtIssuers
7
+ ADDY = 0
8
+ end
9
+
10
+ class JwtAudiences
11
+ GENERIC = 0
12
+ PDF_ANNOTATE = 1
13
+ SUBSCRIPTION = 2
14
+ end
15
+
16
+ JWT_AUDIENCES = %w[generic pdf_annotate subscription].freeze
17
+ JWT_ISSUERS = ['addy'].freeze
4
18
  # Common functionality for AWS related activities
5
19
  class AWS
6
20
  # ssm_client = Aws::SSM::Client.new
@@ -12,5 +26,64 @@ module AddyLambda
12
26
  response = ssm_client.get_parameter(request)
13
27
  response.parameter.value
14
28
  end
29
+
30
+ def self.invoke_lambda(lambda_client, function_name, payload, syncronous: true)
31
+ resp = lambda_client.invoke({
32
+ function_name: function_name,
33
+ invocation_type: syncronous ? 'RequestResponse' : 'Event',
34
+ payload: payload.to_json
35
+ })
36
+ AddyLambda::Common.valid_json?(resp.payload.string) ? JSON.parse(resp.payload.string) : resp.payload.string
37
+ end
38
+
39
+ # Lambda client can be obtained by running: lambda_client = Aws::Lambda::Client.new
40
+ def self.post_slack_message(lambda_client, channel, message, emoji = ':tada', unfurl_links: false)
41
+ input_params = {
42
+ 'user' => 'Signature Request',
43
+ 'channel' => channel,
44
+ 'message' => message,
45
+ 'emoji' => emoji,
46
+ 'unfurl_links' => unfurl_links
47
+ }
48
+ response_body = AddyLambda::AWS.invoke_lambda(lambda_client, 'slack-client', input_params)
49
+ return response_body unless response_body.key?('error')
50
+
51
+ logger.error(response_body['error'])
52
+ end
53
+
54
+ def self.send_email(lambda_client, recipient_emails, from_name, from_email, subject, content_html)
55
+ input_params = {
56
+ 'recipient_list' => recipient_emails,
57
+ 'from_name' => from_name,
58
+ 'from_email' => from_email,
59
+ 'subject' => subject,
60
+ 'content_html' => content_html
61
+ }
62
+
63
+ response_body = AddyLambda::AWS.invoke_lambda(lambda_client, 'mailgun-client', input_params)
64
+ return response_body unless response_body.key?('error')
65
+
66
+ logger.error(response_body['error'])
67
+ end
68
+
69
+ # NOTE: issuer AddyLambda::JwtIssuers::..., Audience AddyLambda::JwtAudiences::...
70
+ def self.generate_jwt(lambda_client, issuer, audience, expire, payload)
71
+ lambda_payload = {
72
+ 'issuer' => JWT_ISSUERS[issuer],
73
+ 'audience' => JWT_AUDIENCES[audience],
74
+ 'expire' => expire,
75
+ 'payload' => payload
76
+ }
77
+ jwt_response = AddyLambda::AWS.invoke_lambda(lambda_client, 'jwt-generate', lambda_payload)
78
+ jwt_response.key?('jwt') ? jwt_response['jwt'] : jwt_response
79
+ end
80
+
81
+ def self.validate_jwt(lambda_client, jwt)
82
+ lambda_payload = {
83
+ 'jwt' => jwt
84
+ }
85
+ response = AddyLambda::AWS.invoke_lambda(lambda_client, 'jwt-validate', lambda_payload)
86
+ response['statusCode'] == 200 ? JSON.parse(response['body']) : response
87
+ end
15
88
  end
16
89
  end
@@ -1,5 +1,4 @@
1
1
  require 'json'
2
- require 'aws-sdk-lambda'
3
2
 
4
3
  module AddyLambda
5
4
  # Generic methods such as logging, error handling HTTP requests etc.
@@ -51,37 +50,11 @@ module AddyLambda
51
50
  ScoutApm::Agent.instance.install
52
51
  end
53
52
 
54
- # Lambda client can be obtained by running: lambda_client = Aws::Lambda::Client.new
55
- def self.post_slack_message(lambda_client, channel, message, emoji = ':tada', unfurl_links: false)
56
- input_params = {
57
- 'user' => 'Signature Request',
58
- 'channel' => channel,
59
- 'message' => message,
60
- 'emoji' => emoji,
61
- 'unfurl_links' => unfurl_links
62
- }
63
- response_body = invoke_lambda(lambda_client, 'slack-client', input_params)
64
- return response_body unless response_body.key?('error')
65
-
66
- logger.error(response_body['error'])
67
- end
68
-
69
- def self.invoke_lambda(lambda_client, function_name, payload)
70
- resp = lambda_client.invoke({
71
- function_name: function_name,
72
- invocation_type: 'RequestResponse',
73
- payload: payload.to_json
74
- })
75
- valid_json?(resp.payload.string)
76
- response_obj = JSON.parse(resp.payload.string)
77
- valid_json?(resp.payload.string) ? JSON.parse(response_obj['body']) : resp.payload.string
78
- end
79
-
80
53
  def self.get_request(url, params = nil, headers = nil)
81
54
  uri = URI(url)
82
55
  uri.query = URI.encode_www_form(params) if params
83
56
  request = Net::HTTP::Get.new(uri.request_uri)
84
- response = http_request(request, uri, headers)
57
+ http_request(request, uri, headers)
85
58
  end
86
59
 
87
60
  def self.delete_request(url, headers = nil)
@@ -1,3 +1,3 @@
1
1
  module AddyLambda
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/addy_lambda.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'addy_lambda/version'
4
- require 'addy_lambda/aws'
5
- require 'addy_lambda/common'
3
+ require_relative 'addy_lambda/version'
4
+ require_relative 'addy_lambda/aws'
5
+ require_relative 'addy_lambda/common'
6
6
 
7
7
  # Module for general functionality
8
8
  module AddyLambda
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addy_lambda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alon Sabi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-08 00:00:00.000000000 Z
11
+ date: 2021-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-ssm