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 +4 -4
- data/lib/idlc-sdk-core/restclient.rb +127 -0
- data/lib/idlc-sdk-core/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf040f7628741d8c3729f2c5ba263c02a235f969
|
4
|
+
data.tar.gz: c56cb22695ba560c139463661c1546162498a0e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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.
|
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
|