├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── VERSION ├── minio ├── .rspec ├── bin │ ├── console │ └── setup ├── lib │ ├── minio.rb │ └── minio │ │ ├── bucket.rb │ │ ├── client.rb │ │ ├── config.rb │ │ ├── digestor.rb │ │ ├── error.rb │ │ ├── signature.rb │ │ ├── signer.rb │ │ └── utils.rb ├── minio.gemspec └── spec │ ├── minio │ ├── bucket_spec.rb │ ├── client_spec.rb │ ├── config_spec.rb │ ├── digestor_spec.rb │ ├── signature_spec.rb │ └── signer_spec.rb │ └── spec_helper.rb └── tasks └── gems.rake /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | *~ 4 | /.config 5 | /coverage/ 6 | /InstalledFiles 7 | /pkg/ 8 | /spec/reports/ 9 | /spec/examples.txt 10 | /test/tmp/ 11 | /test/version_tmp/ 12 | /tmp/ 13 | 14 | # Used by dotenv library to load environment variables. 15 | # .env 16 | 17 | ## Specific to RubyMotion: 18 | .dat* 19 | .repl_history 20 | build/ 21 | *.bridgesupport 22 | build-iPhoneOS/ 23 | build-iPhoneSimulator/ 24 | 25 | ## Specific to RubyMotion (use of CocoaPods): 26 | # 27 | # We recommend against adding the Pods directory to your .gitignore. However 28 | # you should judge for yourself, the pros and cons are mentioned at: 29 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 30 | # 31 | # vendor/Pods/ 32 | 33 | ## Documentation cache and generated files: 34 | /.yardoc/ 35 | /_yardoc/ 36 | /doc/ 37 | /rdoc/ 38 | 39 | ## Environment normalization: 40 | /.bundle/ 41 | /vendor/bundle 42 | /lib/bundler/man/ 43 | 44 | # for a library or gem, you might want to ignore these files since the code is 45 | # intended to run in multiple environments; otherwise, check them in: 46 | Gemfile.lock 47 | .ruby-version 48 | .ruby-gemset 49 | 50 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 51 | .rvmrc 52 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.2.2 4 | before_install: gem install bundler -v 1.10.6 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ### Setup your minio-ruby Github Repository 2 | Fork [minio-ruby upstream](https://github.com/minio/minio-ruby/fork) source repository to your own personal repository. 3 | 4 | ```sh 5 | $ git clone https://github.com/$USER_ID/minio-ruby 6 | $ cd minio-ruby 7 | $ bundle install 8 | $ bundle exec rake test 9 | ... 10 | ``` 11 | 12 | ### Developer Guidelines 13 | 14 | ``minio-ruby`` welcomes your contribution. To make the process as seamless as possible, we ask for the following: 15 | 16 | * Go ahead and fork the project and make your changes. We encourage pull requests to discuss code changes. 17 | - Fork it 18 | - Create your feature branch (git checkout -b my-new-feature) 19 | - Commit your changes (git commit -am 'Add some feature') 20 | - Push to the branch (git push origin my-new-feature) 21 | - Create new Pull Request 22 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec path: 'minio' 4 | gem 'rake', require: false 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Minio Client SDK for Ruby [](https://slack.minio.io) 2 | 3 | The Minio Client SDK for Ruby provides simple APIs to access Minio or any Amazon S3 compatible object storage server. 4 | 5 |
6 | The Minio Ruby SDK is work in progress. Please do not use it in development or production. 7 |8 | 9 | ## Installation 10 | 11 | Add this line to your application's Gemfile: 12 | 13 | ```ruby 14 | gem 'minio-ruby' 15 | ``` 16 | 17 | And then execute to install the deps. 18 | 19 | ``` 20 | bundle 21 | ``` 22 | 23 | Or install it yourself as: 24 | 25 | ``` 26 | gem install minio-ruby 27 | ``` 28 | 29 | ## Development 30 | 31 | To build the minio gem yourself 32 | 33 | ```sh 34 | bundle exec rake gems:build 35 | ``` 36 | 37 | Install the built gem file. 38 | 39 | ```sh 40 | gem install minio-0.0.1.gem 41 | ``` 42 | 43 | ## Contributing 44 | 45 | [Contributors Guide](https://github.com/minio/minio-ruby/blob/master/CONTRIBUTING.md) 46 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | $REPO_ROOT = File.dirname(__FILE__) 2 | 3 | $VERSION = ENV['VERSION'] || File.read(File.join($REPO_ROOT, 'VERSION')).strip 4 | 5 | $GEM_NAMES = [ 6 | 'minio', 7 | ] 8 | 9 | $GEM_NAMES.each do |gem_name| 10 | $LOAD_PATH.unshift(File.join($REPO_ROOT, gem_name, 'lib')) 11 | end 12 | 13 | require 'minio' 14 | 15 | Dir.glob('**/*.rake').each do |task_file| 16 | load task_file 17 | end 18 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.0.1 -------------------------------------------------------------------------------- /minio/.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /minio/bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "minio" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /minio/bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /minio/lib/minio.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'base64' 4 | require 'openssl' 5 | require 'uri' 6 | require 'digest' 7 | require 'rest-client' 8 | require 'time' 9 | require 'pathname' 10 | require 'pry' 11 | require 'set' 12 | require 'cgi' 13 | require 'nokogiri' 14 | 15 | require 'minio/config' 16 | require 'minio/utils' 17 | require 'minio/signature' 18 | require 'minio/signer' 19 | require 'minio/digestor' 20 | require 'minio/bucket' 21 | require 'minio/client' 22 | 23 | module MinioRuby 24 | class Error < StandardError; end 25 | class MissingHttpMethodError < Error; end 26 | class MissingUrlError < Error; end 27 | class InvalidBucketName < Error; end 28 | end 29 | -------------------------------------------------------------------------------- /minio/lib/minio/bucket.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MinioRuby 4 | class Bucket 5 | attr_reader :name, :created_at 6 | 7 | class << self 8 | def bulk_init_from_xml(xml) 9 | Nokogiri::XML(xml) 10 | .document.xpath('//xmlns:Bucket') 11 | .map { |node| init_from_xml(node) } 12 | end 13 | 14 | def init_from_xml(node) 15 | name = node.at_css('Name').text 16 | created_at = node.at_css('CreationDate').text 17 | 18 | new(name: name, created_at: Time.parse(created_at)) 19 | end 20 | end 21 | 22 | def initialize(name:, created_at: nil) 23 | @name = name 24 | @created_at = created_at 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /minio/lib/minio/client.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MinioRuby 4 | class Client 5 | class << self 6 | def configuration 7 | @configuration ||= Config.new 8 | @configuration 9 | end 10 | 11 | def configure 12 | yield(configuration) if block_given? 13 | configuration 14 | end 15 | alias config configure 16 | end 17 | 18 | def bucket_exists?(name) 19 | request_url = "#{config.endpoint}/#{name}" 20 | signature = signer.sign_request(http_method: :head, url: request_url) 21 | response = RestClient.head(request_url, signature.headers) 22 | 23 | response.code == 200 24 | rescue RestClient::NotFound => exception 25 | false 26 | end 27 | 28 | def make_bucket(name) 29 | if name.include?('/') 30 | msg = ':bucket name must not contain a forward-slash (/)' 31 | raise InvalidBucketName, msg 32 | end 33 | 34 | request_url = "#{config.endpoint}/#{name}" 35 | signature = signer.sign_request(http_method: :put, url: request_url) 36 | response = RestClient.put(request_url, {}, signature.headers) 37 | 38 | response.code == 200 39 | end 40 | 41 | def list_buckets 42 | request_url = "#{config.endpoint}/" 43 | signature = signer.sign_request(http_method: :get, url: request_url) 44 | 45 | response = RestClient.get(request_url, signature.headers) 46 | Bucket.bulk_init_from_xml(response.body) 47 | end 48 | 49 | def remove_bucket(name) 50 | request_url = "#{config.endpoint}/#{name}" 51 | signature = signer.sign_request(http_method: :delete, url: request_url) 52 | response = RestClient.delete(request_url, signature.headers) 53 | 54 | response.code == 204 55 | rescue RestClient::NotFound => exception 56 | false 57 | end 58 | 59 | def get_object(bucket, name) 60 | request_url = "#{config.endpoint}/#{bucket}/#{name}" 61 | signature = signer.sign_request(http_method: :get, url: request_url) 62 | response = RestClient.get(request_url, signature.headers) 63 | response.body if response.code == 200 64 | end 65 | 66 | def put_object(bucket, name, data, content_type: 'application/octet-stream') 67 | request_url = "#{config.endpoint}/#{bucket}/#{name}" 68 | signature = signer.sign_request(http_method: :put, url: request_url, body: data, headers: { 'Content-Type' => content_type }) 69 | response = RestClient.put(request_url, data, signature.headers) 70 | response.code == 200 71 | end 72 | 73 | private 74 | 75 | def config 76 | self.class.config 77 | end 78 | 79 | def signer 80 | Signer.new(config: config) 81 | end 82 | end 83 | end 84 | -------------------------------------------------------------------------------- /minio/lib/minio/config.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MinioRuby 4 | class Config 5 | attr_accessor :access_key, :secret_key, :region 6 | attr_accessor :transport, :secure 7 | attr_reader :service, :endpoint 8 | 9 | def initialize(args = {}) 10 | @endpoint = ensure_schema(args[:endpoint] || 'localhost:9000') 11 | @access_key = args[:access_key] 12 | @secret_key = args[:secret_key] 13 | @secure = args[:secure] 14 | @transport = args[:transport] 15 | @region = args[:region] || 'us-east-1' 16 | @service = 's3' 17 | end 18 | 19 | def endpoint=(uri) 20 | @endpoint = ensure_schema(uri) 21 | end 22 | 23 | private 24 | 25 | def ensure_schema(uri) 26 | return unless uri 27 | return uri if uri.start_with?('http://', 'https://') 28 | 29 | "http://#{uri}" 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /minio/lib/minio/digestor.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MinioRuby 4 | class Digestor 5 | class << self 6 | def hexdigest(value) 7 | if value_is_file?(value) 8 | OpenSSL::Digest::SHA256.file(value).hexdigest 9 | elsif value.respond_to?(:read) 10 | fragmented_digest(value) 11 | else 12 | OpenSSL::Digest::SHA256.hexdigest(value) 13 | end 14 | end 15 | 16 | def hmac(key, value) 17 | OpenSSL::HMAC.digest(digest, key, value) 18 | end 19 | 20 | def hexhmac(key, value) 21 | OpenSSL::HMAC.hexdigest(digest, key, value) 22 | end 23 | 24 | def base64(value) 25 | Digest::MD5.base64digest(value) 26 | end 27 | 28 | def signature(secret_key:, service:, region:, date:, string_to_sign:) 29 | k_date = hmac('AWS4' + secret_key, date) 30 | k_region = hmac(k_date, region) 31 | k_service = hmac(k_region, service) 32 | k_credentials = hmac(k_service, 'aws4_request') 33 | hexhmac(k_credentials, string_to_sign) 34 | end 35 | 36 | private 37 | 38 | def value_is_file?(value) 39 | (value.is_a?(File) || value.is_a?(Tempfile)) && 40 | value.path && 41 | File.exist?(value.path) 42 | end 43 | 44 | def fragmented_digest(value) 45 | sha256 = OpenSSL::Digest::SHA256.new 46 | while chunk = value.read(1024 * 1024, buffer ||= ''.dup) # 1MB 47 | sha256.update(chunk) 48 | end 49 | 50 | value.rewind 51 | sha256.hexdigest 52 | end 53 | 54 | def digest 55 | OpenSSL::Digest.new('sha256') 56 | end 57 | end 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /minio/lib/minio/error.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MinioRuby 4 | class InvalidEndpointError < StandardError 5 | attr_reader :str 6 | def initialize(msg = 'Invalid Endpoint Error', str) 7 | @str = str 8 | super 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /minio/lib/minio/signature.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MinioRuby 4 | class Signature 5 | attr_reader :headers, :options 6 | 7 | def initialize(headers: {}, **options) 8 | @headers = headers.to_h 9 | @options = options 10 | end 11 | 12 | def content_sha256 13 | headers['x-amz-content-sha256'] 14 | end 15 | 16 | def authorization 17 | headers['authorization'] 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /minio/lib/minio/signer.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MinioRuby 4 | class Signer 5 | extend Forwardable 6 | def_delegators :@config, :service, :region 7 | def_delegators :@config, :secret_key, :access_key 8 | 9 | def initialize(config:, **options) 10 | @config = config 11 | @options = options 12 | end 13 | 14 | def sign_request(http_method:, url:, headers: {}, **request) 15 | http_method = extract_http_method(http_method) 16 | url = extract_url(url) 17 | headers = downcase_headers(headers) 18 | 19 | datetime = headers['x-amz-date'] 20 | datetime ||= Time.now.utc.iso8601.gsub(/\W/, '') 21 | date = datetime[0, 8] 22 | 23 | content_sha256 = headers['x-amz-content-sha256'] 24 | content_sha256 ||= sha256_hexdigest(request[:body] || '') 25 | 26 | sigv4_headers = {} 27 | sigv4_headers['host'] = Utils.host(url) 28 | sigv4_headers['x-amz-date'] = datetime 29 | sigv4_headers['x-amz-content-sha256'] ||= content_sha256 if apply_checksum_header? 30 | sigv4_headers['content-type'] ||= headers['content-type'] if headers['content-type'] 31 | 32 | headers = headers.merge(sigv4_headers) 33 | signature = compute_signature(http_method: http_method, url: url, headers: headers, content_sha: content_sha256, datetime: datetime) 34 | sigv4_headers['authorization'] = signature_header(date: date, headers: headers, signature: signature) 35 | 36 | Signature.new(headers: sigv4_headers) 37 | end 38 | 39 | private 40 | 41 | def extract_http_method(http_method) 42 | if http_method 43 | http_method.to_s.upcase 44 | else 45 | msg = 'missing required option :http_method' 46 | raise MissingHttpMethodError, msg 47 | end 48 | end 49 | 50 | def extract_url(url) 51 | if url 52 | URI.parse(url.to_s) 53 | else 54 | msg = 'missing required option :url' 55 | raise MissingUrlError, msg 56 | end 57 | end 58 | 59 | def downcase_headers(headers) 60 | headers.to_h.each_with_object({}) do |(key, value), acc| 61 | acc[key.to_s.downcase] = value 62 | end 63 | end 64 | 65 | def apply_checksum_header? 66 | @options[:apply_checksum_header] != false 67 | end 68 | 69 | def compute_signature(http_method:, url:, headers:, content_sha:, datetime:) 70 | request = canonical_request(http_method, url, headers, content_sha) 71 | to_sign = string_to_sign(datetime, request) 72 | 73 | Digestor.signature( 74 | secret_key: secret_key, 75 | service: service, 76 | region: region, 77 | date: datetime[0, 8], 78 | string_to_sign: to_sign 79 | ) 80 | end 81 | 82 | def signature_header(date:, headers:, signature:) 83 | [ 84 | "AWS4-HMAC-SHA256 Credential=#{credential(access_key, date)}", 85 | "SignedHeaders=#{signed_headers(headers)}", 86 | "Signature=#{signature}" 87 | ].join(', ') 88 | end 89 | 90 | def canonical_request(http_method, url, headers, content_sha256) 91 | [ 92 | http_method, 93 | path(url), 94 | normalized_querystring(url.query || ''), 95 | canonical_headers(headers) + "\n", 96 | signed_headers(headers), 97 | content_sha256 98 | ].join("\n") 99 | end 100 | 101 | def string_to_sign(datetime, canonical_request) 102 | [ 103 | 'AWS4-HMAC-SHA256', 104 | datetime, 105 | credential_scope(datetime[0, 8]), 106 | sha256_hexdigest(canonical_request) 107 | ].join("\n") 108 | end 109 | 110 | def credential_scope(date) 111 | [ 112 | date, 113 | region, 114 | service, 115 | 'aws4_request' 116 | ].join('/') 117 | end 118 | 119 | def credential(access_key, date) 120 | "#{access_key}/#{credential_scope(date)}" 121 | end 122 | 123 | def path(url) 124 | path = url.path || '/' 125 | 126 | uri_escape_path(path) 127 | end 128 | 129 | def normalized_querystring(querystring) 130 | params = querystring.split('&') 131 | params = params.map { |p| /=/.match?(p) ? p : p + '=' } 132 | params.each.with_index.sort do |a, b| 133 | a, a_offset = a 134 | a_name = a.split('=')[0] 135 | b, b_offset = b 136 | b_name = b.split('=')[0] 137 | if a_name == b_name 138 | a_offset <=> b_offset 139 | else 140 | a_name <=> b_name 141 | end 142 | end.map(&:first).join('&') 143 | end 144 | 145 | def signed_headers(headers) 146 | headers 147 | .keys 148 | .reject { |header| unsigned_headers.include?(header) } 149 | .sort 150 | .join(';') 151 | end 152 | 153 | def canonical_headers(headers) 154 | headers 155 | .reject { |header, _value| unsigned_headers.include?(header) } 156 | .to_a 157 | .sort_by(&:first) 158 | .map { |k, v| "#{k}:#{canonical_header_value(v.to_s)}" } 159 | .join("\n") 160 | end 161 | 162 | def unsigned_headers 163 | @unsigned_headers ||= Set 164 | .new(@options.fetch(:unsigned_headers, [])) 165 | .map(&:downcase) 166 | .push('authorization') 167 | .push('x-amzn-trace-id') 168 | end 169 | 170 | def canonical_header_value(value) 171 | /^".*"$/.match?(value) ? value : value.gsub(/\s+/, ' ').strip 172 | end 173 | 174 | def sha256_hexdigest(value) 175 | Digestor.hexdigest(value) 176 | end 177 | 178 | def uri_escape(string) 179 | Utils.uri_escape(string) 180 | end 181 | 182 | def uri_escape_path(string) 183 | Utils.uri_escape_path(string) 184 | end 185 | end 186 | end 187 | -------------------------------------------------------------------------------- /minio/lib/minio/utils.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MinioRuby 4 | module Utils 5 | module_function 6 | 7 | def uri_escape_path(path) 8 | path.gsub(%r{[^/]+}) do |part| 9 | uri_escape(part) 10 | end 11 | end 12 | 13 | def uri_escape(string) 14 | return unless string 15 | 16 | CGI 17 | .escape(string.encode('UTF-8')) 18 | .gsub('+', '%20') 19 | .gsub('%7E', '~') 20 | end 21 | 22 | def host(uri) 23 | if standard_port?(uri) 24 | uri.host 25 | else 26 | "#{uri.host}:#{uri.port}" 27 | end 28 | end 29 | 30 | def standard_port?(uri) 31 | (uri.scheme == 'http' && uri.port == 80) || 32 | (uri.scheme == 'https' && uri.port == 443) 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /minio/minio.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | version = File.read(File.expand_path('../../VERSION', __FILE__)).strip 3 | 4 | Gem::Specification.new do |spec| 5 | spec.name = "minio" 6 | spec.version = version 7 | spec.authors = ["Minio, Inc."] 8 | spec.email = ["dev@minio.io"] 9 | 10 | spec.summary = %q{Minio Client SDK for Ruby} 11 | spec.description = %q{The official Minio Client SDK for Ruby.} 12 | spec.homepage = "https://github.com/minio/minio-ruby" 13 | spec.license = 'Apache-2.0' 14 | 15 | spec.require_paths = ["lib"] 16 | 17 | spec.files = Dir['lib/**/*.rb'] 18 | spec.bindir = 'bin' 19 | 20 | spec.add_dependency('rest-client') 21 | spec.add_dependency('nokogiri') 22 | 23 | spec.add_development_dependency('rspec') 24 | spec.add_development_dependency('rubocop') 25 | spec.add_development_dependency('pry') 26 | end 27 | -------------------------------------------------------------------------------- /minio/spec/minio/bucket_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe MinioRuby::Bucket do 6 | subject(:subject) do 7 | described_class.new(name: 'chunky-bacon', created_at: Date.today) 8 | end 9 | 10 | let(:xml_node) do 11 | <<~NODE 12 |