idlc-sdk-core 1.0.3 → 1.0.4

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
  SHA1:
3
- metadata.gz: 95cc41404c5997e024a316429a0cdc89987663fb
4
- data.tar.gz: cf0ab62790e938d7b57d3e8ab5eb838f0446a31c
3
+ metadata.gz: bf040f7628741d8c3729f2c5ba263c02a235f969
4
+ data.tar.gz: c56cb22695ba560c139463661c1546162498a0e4
5
5
  SHA512:
6
- metadata.gz: 02e2844ae529a22542cbb173f9ce125a09e042674aefe5a1c6d75110f33563dbbf529506dda8fc959eb6aa027fd0063d86bea469e8fa7c8fa53e8c674ecf6064
7
- data.tar.gz: 57eb5bea5ca830f52cbed4a05a3b0c2ec31bfa774d819ead9ac2f8122dc4e81e79ad2263204345e8f251ea675a38c5fa7560b4d35c40fea921f0d44db42177d4
6
+ metadata.gz: ed17b2302676a828c9a7cedab99c0d8f00a9e53348206056d613be41302bc305683dc54d03961b37c856c000242fa17612f144bfd54f95eb6d3604627df8474c
7
+ data.tar.gz: 050374d872d4575efdaa42aeaaa6c4fc3efc9648c0a23519c7a550e6a90703f01da274170f1ba237d9aa28fbc7ed05d68d570bfaab4c7ab30b10a6de22d0a912
@@ -0,0 +1,127 @@
1
+ require 'aws-sigv4'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'aws-sdk-lambda'
5
+
6
+ # Idlc::SERVICES object is loaded from core lib
7
+
8
+ module Idlc
9
+ class AWSLambdaProxy
10
+ def fetch(request)
11
+ client = Aws::Lambda::Client.new()
12
+
13
+ request[:function] = "#{request[:service]}-" + Idlc::SERVICES[request[:service]]['stage'] + "-#{request[:lambda]}"
14
+ request[:httpMethod] = request[:method]
15
+
16
+ resp = client.invoke({
17
+ function_name: "service-lambda-proxy",
18
+ invocation_type: "RequestResponse",
19
+ log_type: "None",
20
+ payload: request.to_json,
21
+ })
22
+
23
+ JSON.parse(JSON.parse(JSON.parse(resp.payload.string)['Payload'])['body'])
24
+ end
25
+ end
26
+
27
+ class AWSRestClient
28
+ def initialize(credentials= {
29
+ access_key_id: ENV['AWS_ACCESS_KEY_ID'],
30
+ secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
31
+ },
32
+ region=ENV['AWS_REGION']
33
+ )
34
+ @service_name = 'execute-api'
35
+ @credentials = credentials
36
+ @region = region
37
+ end
38
+
39
+ def fetch(request)
40
+ request = JSON.parse(request)
41
+
42
+ endpoint = 'https://' + Idlc::SERVICES[request['service']]['endpoint'] + '/' + Idlc::SERVICES[request['service']]['stage']
43
+
44
+ body = ''
45
+ body = request['body'].to_json if request['body']
46
+
47
+ resp = send_signed_request(
48
+ request['method'],
49
+ "#{endpoint.strip}#{request['path']}",
50
+ body
51
+ )
52
+
53
+ # if request has 'outfile' param, write response to file
54
+ to_file(resp, request['outfile']) if request['outfile']
55
+
56
+ # return response obj
57
+ resp
58
+ end
59
+
60
+ def to_file(obj, filename)
61
+ File.open(filename, 'w') do |f|
62
+ f.write(obj.to_json)
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def send_signed_request(method, url, payload)
69
+ uri = URI.parse(url)
70
+ https = Net::HTTP.new(uri.host, uri.port)
71
+ https.use_ssl = true
72
+ signature = sigv4_signature(method, url, payload)
73
+ request = http_request(method, uri.path, signature, payload)
74
+
75
+ response = https.request(request)
76
+ JSON.parse(response.body)
77
+ end
78
+
79
+ def set_headers(request, signature)
80
+ request.add_field 'host', signature.headers['host']
81
+ request.add_field 'content-type', 'application/json'
82
+ request.add_field 'x-amz-content-sha256', signature.headers['x-amz-content-sha256']
83
+ request.add_field 'x-amz-date', signature.headers['x-amz-date']
84
+ request.add_field 'authorization', signature.headers['authorization']
85
+ end
86
+
87
+ def http_request(method, path, signature, payload)
88
+ case method.downcase
89
+ when 'put'
90
+ request = Net::HTTP::Put.new(path)
91
+ when 'post'
92
+ request = Net::HTTP::Post.new(path)
93
+ when 'get'
94
+ request = Net::HTTP::Get.new(path)
95
+ when 'delete'
96
+ request = Net::HTTP::Delete.new(path)
97
+ else
98
+ request = Net::HTTP::Put.new(path)
99
+ end
100
+
101
+ set_headers(request, signature)
102
+ request.body = payload
103
+
104
+ request
105
+ end
106
+
107
+ def signer
108
+ Aws::Sigv4::Signer.new(
109
+ service: @service_name,
110
+ region: @region,
111
+ access_key_id: @credentials[:access_key_id],
112
+ secret_access_key: @credentials[:secret_access_key]
113
+ )
114
+ end
115
+
116
+ def sigv4_signature(method, url, payload)
117
+ signer.sign_request(
118
+ http_method: method,
119
+ url: url,
120
+ headers: {
121
+ 'content-type' => 'application/json'
122
+ },
123
+ body: payload
124
+ )
125
+ end
126
+ end
127
+ end
@@ -1,3 +1,3 @@
1
1
  module Idlc
2
- VERSION = '1.0.3'.freeze
2
+ VERSION = '1.0.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idlc-sdk-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Cazell
@@ -156,6 +156,7 @@ files:
156
156
  - idlc-sdk-core.gemspec
157
157
  - lib/idlc-sdk-core.rb
158
158
  - lib/idlc-sdk-core/helpers.rb
159
+ - lib/idlc-sdk-core/restclient.rb
159
160
  - lib/idlc-sdk-core/utility.rb
160
161
  - lib/idlc-sdk-core/version.rb
161
162
  - lib/idlc-sdk-core/workspace.rb