rawscurl 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rawscurl.rb +154 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b2e263d25e119828b69c7b687b625412fd37f284d8b17a54f597879d877b398f
4
+ data.tar.gz: 5f91ba156be746e2ebfc056002f6a8eeb39d737469026e7b43c0f20e50204a33
5
+ SHA512:
6
+ metadata.gz: d0ca7b2acbd4711656d3b75ca9776b4589984d355d2e73a423d67727a620dd17a977eb4b37b89e0aa5b2000f93c06606cc1d106a303965812847b8c2a11f054d
7
+ data.tar.gz: aa3fb55c3d3eec508e54c58d80b552bdbda8715a185e3beeab7ac47fd102504488b389f23bde10c0b143163941d189f86614aa535efd644b0192b16d7e29dd83
data/lib/rawscurl.rb ADDED
@@ -0,0 +1,154 @@
1
+ class AwsCurl
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'digest'
5
+ require 'openssl'
6
+ require 'net/http'
7
+
8
+ def self.url_path_to_dict(path)
9
+ URI.parse(path)
10
+ end
11
+
12
+ def self.make_request(
13
+ method: "GET",
14
+ service: "execute-api",
15
+ region:,
16
+ uri:,
17
+ headers: "",
18
+ data: "",
19
+ access_key:,
20
+ secret_key:,
21
+ security_token: "",
22
+ data_binary: "",
23
+ connection_id:
24
+ )
25
+ uri_dict = url_path_to_dict(uri)
26
+ host = uri_dict.host
27
+ query = uri_dict.query
28
+ #canonical_uri = CGI.escape(uri_dict.path)
29
+ canonical_uri = uri_dict.path + "/" + CGI.escape("@connections") + "/" + CGI.escape(connection_id)
30
+ port = uri_dict.port
31
+
32
+ # puts "host: #{host}"
33
+ # puts "query: #{query}"
34
+ # puts "canonical_uri: #{canonical_uri}"
35
+ # puts "port: #{port}"
36
+
37
+ def self.getSignatureKey key, dateStamp, regionName, serviceName
38
+ kDate = OpenSSL::HMAC.digest('sha256', "AWS4" + key, dateStamp)
39
+ kRegion = OpenSSL::HMAC.digest('sha256', kDate, regionName)
40
+ kService = OpenSSL::HMAC.digest('sha256', kRegion, serviceName)
41
+ kSigning = OpenSSL::HMAC.digest('sha256', kService, "aws4_request")
42
+
43
+ kSigning
44
+ end
45
+
46
+ def self.sha256_hash(val)
47
+ Digest::SHA256.hexdigest(val)
48
+ end
49
+
50
+ def self.sha256_hash_for_binary_data(val)
51
+ Digest::SHA256.hexdigest(val)
52
+ end
53
+
54
+ t = Time.now.utc
55
+ amzdate = t.strftime('%Y%m%dT%H%M%SZ')
56
+ datestamp = t.strftime('%Y%m%d')
57
+
58
+ canonical_querystring = uri_dict.normalize.query.nil? ? "" : uri_dict.normalize.query
59
+ fullhost = port.nil? ? host + ":" + port : host
60
+
61
+ canonical_headers = "host:#{fullhost}\n" +
62
+ "x-amz-date:#{amzdate}\n"
63
+
64
+ unless security_token&.empty?
65
+ canonical_headers += "x-amz-security-token:#{security_token}\n"
66
+ end
67
+
68
+ signed_headers = "host;x-amz-date"
69
+
70
+ unless security_token&.empty?
71
+ signed_headers += ";x-amz-security-token"
72
+ end
73
+
74
+ payload_hash = data_binary&.empty? ? sha256_hash(data) : sha256_hash_for_binary_data(data)
75
+
76
+ canonical_request = method + "\n" +
77
+ canonical_uri + "\n" +
78
+ canonical_querystring + "\n" +
79
+ canonical_headers + "\n" +
80
+ signed_headers + "\n" +
81
+ payload_hash
82
+
83
+ # puts "CANONICAL REQUEST = #{canonical_request}"
84
+
85
+ algorithm = "AWS4-HMAC-SHA256"
86
+ credential_scope = datestamp + "/" +
87
+ region + "/" +
88
+ service + "/" +
89
+ "aws4_request"
90
+
91
+ string_to_sign = algorithm + "\n" +
92
+ amzdate + "\n" +
93
+ credential_scope + "\n" +
94
+ sha256_hash(canonical_request)
95
+
96
+ # puts "STRING TO SIGN = #{string_to_sign}"
97
+
98
+ signing_key = getSignatureKey(secret_key, datestamp, region, service)
99
+
100
+ encoded = string_to_sign.encode("utf-8")
101
+ signature = OpenSSL::HMAC.hexdigest('sha256', signing_key, encoded)
102
+
103
+ # puts "Signature: #{signature}"
104
+
105
+ authorization_header = algorithm + " " +
106
+ "Credential=" + access_key + "/" + credential_scope + ", " +
107
+ "SignedHeaders=" + signed_headers + ", " +
108
+ "Signature=" + signature
109
+
110
+ # puts "AUTHORIZATION HEADER: #{authorization_header}"
111
+
112
+ headers = {
113
+ "Authorization": authorization_header,
114
+ "x-amz-date": amzdate,
115
+ #"x-amz-security-token": security_token,
116
+ "x-amz-content-sha256": payload_hash
117
+ }
118
+
119
+ # puts "Headers: #{headers}"
120
+
121
+ send_request(uri_dict, data, headers, method, connection_id)
122
+ end
123
+
124
+ def self.send_request(uri, data, headers, method, connection_id)
125
+ # puts "************* SENDING REQUEST ***************"
126
+ # puts "URI: #{uri}"
127
+ # puts "DATA: #{data}"
128
+ # puts "HEADERS: #{headers}"
129
+ # puts "METHOD: #{method}"
130
+
131
+ if method == "GET"
132
+ req = Net::HTTP::Get.new(uri.to_s + "/@connections" + "/" + connection_id, initheader = headers)
133
+ elsif method == "POST"
134
+ req = Net::HTTP::Post.new(uri.to_s + "/@connections" + "/" + connection_id, initheader = headers)
135
+ req.body = data
136
+ elsif method == "DELETE"
137
+ req = Net::HTTP::Delete.new(uri.to_s + "/@connections" + "/" + connection_id, initheader = headers)
138
+ end
139
+ https = Net::HTTP.new(uri.host, uri.port)
140
+ https.use_ssl = true
141
+
142
+ res = https.request(req)
143
+
144
+ result = res.body
145
+ status_code = res.code
146
+ # puts "HTTP Status code: #{status_code}, (200=Connected, 204=Disconnected, 400=Invalid connectionId, 410=Gone)"
147
+ # puts res
148
+
149
+ # puts "************* REQUEST SENT ******************"
150
+ return status_code, result
151
+ end
152
+
153
+
154
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rawscurl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Houser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-10-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby AWS WebSocket API library. Ported from awscurl python script.
14
+ email: rob.h@altiwi.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rawscurl.rb
20
+ homepage: https://rubygems.org/gems/rawscurl
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.7.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Ruby AWS WebSocket API library
44
+ test_files: []