This repository was archived by the owner on May 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsession.rb
More file actions
60 lines (47 loc) · 1.28 KB
/
session.rb
File metadata and controls
60 lines (47 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'bundler'
Bundler.require
require 'json'
require 'net/http'
require 'uri'
INVOKE_URL = ENV['INVOKE_URL']
REGION = URI.parse(INVOKE_URL).host.split('.')[2]
BASE_HEADERS = {
'Content-Type' => 'application/json',
'Accept' => 'application/json'
}
def perform(action)
uri = URI.parse("#{INVOKE_URL}/bastion")
method = case action
when :create
'POST'
when :destroy
'DELETE'
end
signer = Aws::Sigv4::Signer.new(
service: 'execute-api',
region: REGION,
credentials_provider: Aws::SharedCredentials.new
)
signature = signer.sign_request(
http_method: method,
url: uri.to_s,
headers: BASE_HEADERS
)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
action_request = case method
when 'POST'
Net::HTTP::Post.new(uri)
when 'DELETE'
Net::HTTP::Delete.new(uri)
end
BASE_HEADERS.merge(signature.headers).each { |k, v|
action_request[k] = v
}
action_request['accept-encoding'] = nil
action_request['user-agent'] = nil
response = http.request(action_request)
if response.code == '201'
puts "#{JSON.pretty_generate(JSON.parse(response.body))}"
end
end