oci_config_reader 0.0.4 → 0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e3b5fb4f78faaadc5381c354c396c6a335c08f2ebe4d18796dd45971ef14da2
4
- data.tar.gz: 26366d52b1695f201cd8a7113d094cad2f050fd74fba2116172f33008a92d67b
3
+ metadata.gz: e5bedcdf788e14ef6eae5a6a14c0a16fccac84dda5cade87d780a61a8850e62a
4
+ data.tar.gz: fe7c7a53a84cf1e00fbc1d7ad7fc11be73e23ecfc537ff297fe59ab2f0430562
5
5
  SHA512:
6
- metadata.gz: 27c3f0017b67049c0e6036f7d71b93eed96c85b1cba9e588afb9fdba47e2c35574dc509377315031e2eec4027561676556c404d51368be40646f27e6b19e8672
7
- data.tar.gz: 87faaa69e6461b355f7780c045e1485d40e2c94865511f4d903f45331c31345a1b9fca755cc8bda8ecfa1e5cfb0d7307fc9a05e6b036cba259085cdb118a2749
6
+ metadata.gz: 03d43091dada9c53dc7e598340826444ee4b90a069ea78d77f54d2c9c64b797a8333966618b7cc46079f338bb31ac76e539de96ba5900cb153e02acf88761eb6
7
+ data.tar.gz: 74a341d441e26962f16aae02506773700adbff1bead48d71996544a2163ee58423940fd27969f313788ddf304f23859cabdfc784131b4ed96aacc93e69537126
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # OciConfigReader
1
+ # OCIConfigReader
2
2
 
3
3
  A simple gem to read data from oci config file. So in Ruby sample codes you don't need to write down/copy-paste the same information again-and-again.
4
4
 
@@ -21,7 +21,14 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
- OciConfigReader.oci_config_data["user/fingerprint/key_file/tenancy/region"]
24
+ The gem will try to read the data from: ```~/.oci/config```
25
+ ```ruby
26
+ user = OciConfigReader.oci_config_data["user"]
27
+ fingerprint = OciConfigReader.oci_config_data["fingerprint"]
28
+ key_file = OciConfigReader.oci_config_data["key_file"]
29
+ tenancy = OciConfigReader.oci_config_data["tenancy"]
30
+ ```
31
+
25
32
 
26
33
 
27
34
  ## Contributing
@@ -0,0 +1,159 @@
1
+ #
2
+ #
3
+ # Origin of the file: https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/signingrequests.htm#RubySigningSample
4
+ #
5
+ #
6
+
7
+ require 'base64'
8
+ require 'digest'
9
+ require 'openssl'
10
+ require 'time'
11
+ require 'uri'
12
+ require 'json'
13
+
14
+ # gem 'oci_config_reader'
15
+ require 'oci_config_reader'
16
+
17
+ # gem 'terminal-table'
18
+ require 'terminal-table'
19
+
20
+ # gem 'httparty', '~> 0.13.0'
21
+ require 'httparty'
22
+
23
+ user = OciConfigReader.oci_config_data["user"]
24
+ fingerprint = OciConfigReader.oci_config_data["fingerprint"]
25
+ key_file = OciConfigReader.oci_config_data["key_file"]
26
+ tenancy = OciConfigReader.oci_config_data["tenancy"]
27
+
28
+ # Version 1.0.1
29
+ class Client
30
+ include HTTParty
31
+ attr_reader :signer
32
+
33
+ def initialize(key_id, private_key)
34
+ @signer = Signer.new(key_id, private_key)
35
+ end
36
+
37
+ # nothing to sign for :options
38
+
39
+ [:get, :head, :delete].each do |method|
40
+ define_method(method) do |uri, headers: {}|
41
+ self.signer.sign(method, uri, headers, body: nil)
42
+ self.class.send(method, uri, :headers => headers)
43
+ end
44
+ end
45
+
46
+ [:put, :post].each do |method|
47
+ define_method(method) do |uri, headers: {}, body: ""|
48
+ self.signer.sign(method, uri, headers, body)
49
+ self.class.send(method, uri, :headers => headers, :body => body)
50
+ end
51
+ end
52
+ end
53
+
54
+
55
+ class Signer
56
+ class << self
57
+ attr_reader :headers
58
+ end
59
+
60
+ attr_reader :key_id, :private_key
61
+
62
+ generic_headers = [:"date", :"(request-target)", :"host"]
63
+ body_headers = [
64
+ :"content-length", :"content-type", :"x-content-sha256"]
65
+ @headers = {
66
+ get: generic_headers,
67
+ head: generic_headers,
68
+ delete: generic_headers,
69
+ put: generic_headers + body_headers,
70
+ post: generic_headers + body_headers
71
+ }
72
+
73
+ def initialize(key_id, private_key)
74
+ @key_id = key_id
75
+ @private_key = private_key
76
+ end
77
+
78
+ def sign(method, uri, headers, body)
79
+ uri = URI(uri)
80
+ path = uri.query.nil? ? uri.path : "#{uri.path}?#{uri.query}"
81
+ self.inject_missing_headers(headers, method, body, uri)
82
+ signature = self.compute_signature(headers, method, path)
83
+ unless signature.nil?
84
+ self.inject_authorization_header(headers, method, signature)
85
+ end
86
+ end
87
+
88
+ def inject_missing_headers(headers, method, body, uri)
89
+ headers["content-type"] ||= "application/json"
90
+ headers["date"] ||= Time.now.utc.httpdate
91
+ headers["accept"] ||= "*/*"
92
+ headers["host"] ||= uri.host
93
+ if method == :put or method == :post
94
+ body ||= ""
95
+ headers["content-length"] ||= body.length.to_s
96
+ headers["x-content-sha256"] ||= Digest::SHA256.base64digest(body)
97
+ end
98
+ end
99
+
100
+ def inject_authorization_header(headers, method, signature)
101
+ signed_headers = self.class.headers[method].map(&:to_s).join(" ")
102
+ headers["authorization"] = [
103
+ %(Signature version="1"),
104
+ %(headers="#{signed_headers}"),
105
+ %(keyId="#{self.key_id}"),
106
+ %(algorithm="rsa-sha256"),
107
+ %(signature="#{signature}")
108
+ ].join(",")
109
+ end
110
+
111
+ def compute_signature(headers, method, path)
112
+ return if self.class.headers[method].empty?
113
+ signing_string = self.class.headers[method].map do |header|
114
+ if header == :"(request-target)"
115
+ "#{header}: #{method.downcase} #{path}"
116
+ else
117
+ "#{header}: #{headers[header.to_s]}"
118
+ end
119
+ end.join("\n")
120
+ signature = self.private_key.sign(
121
+ OpenSSL::Digest::SHA256.new,
122
+ signing_string.encode("ascii"))
123
+ Base64.strict_encode64(signature)
124
+ end
125
+ end
126
+
127
+
128
+ api_key = [tenancy, user, fingerprint].join("/")
129
+ private_key = OpenSSL::PKey::RSA.new(File.read(key_file))
130
+ client = Client.new(api_key, private_key)
131
+
132
+ headers = {
133
+ # Uncomment to use a fixed date
134
+ # "date" => "Thu, 05 Jan 2014 21:31:40 GMT"
135
+ }
136
+
137
+ # GET with query parameters
138
+ uri = "https://iaas.eu-frankfurt-1.oraclecloud.com/20160918/instances?compartmentId=%{compartment_id}"
139
+ uri = uri % {
140
+ :compartment_id => "ocid1.compartment.oc1..aaaaaaaa4ifadwjtf3uez2sawspmre5yartoh3jq6afwvihv3zddug3m3nda".sub(":", "%3A")
141
+ }
142
+ response = client.get(uri, headers: headers)
143
+
144
+ # Create a table
145
+ response_data = JSON.parse(response&.body || "{}")
146
+ AD = response_data[0]["availabilityDomain"]
147
+ server_name = response_data[0]["displayName"]
148
+ fault_domain = response_data[0]["faultDomain"]
149
+ region = response_data[0]["region"]
150
+ shape = response_data[0]["shape"]
151
+ ocpus = response_data[0]["shapeConfig"]["ocpus"]
152
+ memory = response_data[0]["shapeConfig"]["memoryInGBs"]
153
+ cpu_type = response_data[0]["shapeConfig"]["processorDescription"]
154
+
155
+ rows = []
156
+ rows << [server_name, fault_domain, region, shape, ocpus, cpu_type, memory]
157
+ table = Terminal::Table.new :title => AD, :headings => ["Server name", "Fault domain", "Region", "Shape", "OCPU", "OCPU type", "Memory in GB"], :rows => rows
158
+
159
+ puts table
@@ -1,3 +1,3 @@
1
1
  module OciConfigReader
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oci_config_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: '0.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Szabolcs Toth
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-16 00:00:00.000000000 Z
11
+ date: 2023-03-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple gem to read data from oci config file. So in Ruby sample codes
14
14
  you don't need to write down/copy-paste the same information again-and-again.
@@ -26,6 +26,7 @@ files:
26
26
  - Rakefile
27
27
  - bin/console
28
28
  - bin/setup
29
+ - examples/example1.rb
29
30
  - lib/oci_config_reader.rb
30
31
  - lib/oci_config_reader/version.rb
31
32
  - oci_config_reader.gemspec
@@ -36,7 +37,7 @@ metadata:
36
37
  homepage_uri: https://github.com/kicsipixel/oci_config_reader
37
38
  source_code_uri: https://github.com/kicsipixel/oci_config_reader
38
39
  changelog_uri: https://github.com/kicsipixel/oci_config_reader
39
- post_install_message:
40
+ post_install_message:
40
41
  rdoc_options: []
41
42
  require_paths:
42
43
  - lib
@@ -51,8 +52,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
52
  - !ruby/object:Gem::Version
52
53
  version: '0'
53
54
  requirements: []
54
- rubygems_version: 3.0.3
55
- signing_key:
55
+ rubygems_version: 3.4.6
56
+ signing_key:
56
57
  specification_version: 4
57
58
  summary: A Ruby gem to read out oci config data.
58
59
  test_files: []