Skip to content

Commit 3769c52

Browse files
Merge pull request #22 from imnetworku/PIPE-6553
PIPE-6553: Upgrade http-hmac-ruby to Ruby 3.3
2 parents b4b16a9 + 01b407e commit 3769c52

20 files changed

+335
-273
lines changed

.rubocop.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
AllCops:
2+
DisplayCopNames: true
3+
NewCops: disable
4+
TargetRubyVersion: 3.3.6
5+
6+
Style/FrozenStringLiteralComment:
7+
Exclude:
8+
- 'lib/acquia-http-hmac/rack_authenticate.rb'
9+
10+
Metrics/ParameterLists:
11+
Max: 7
12+
13+
Style/ClassVars:
14+
Exclude:
15+
- 'example/app.rb'
16+
- 'lib/acquia-http-hmac/rack_authenticate.rb'
17+
18+
Metrics/AbcSize:
19+
Max: 50
20+
Exclude:
21+
- 'test/acquia_spec_test.rb'
22+
23+
Naming/AccessorMethodName:
24+
Exclude:
25+
- 'test/rack_simple_app_test.rb'
26+
- 'test/rack_sqlite3_app_test.rb'
27+
28+
Metrics/BlockLength:
29+
Max: 150
30+
31+
Metrics/ClassLength:
32+
Max: 1000
33+
34+
Metrics/CyclomaticComplexity:
35+
Max: 12
36+
37+
Layout/LineLength:
38+
Max: 200
39+
40+
Metrics/MethodLength:
41+
Max: 50
42+
43+
Metrics/ModuleLength:
44+
Max: 250
45+
46+
Metrics/PerceivedComplexity:
47+
Max: 10

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
source "https://rubygems.org"
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
24

35
gemspec

Rakefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler/setup'
24

35
begin
46
require 'rake/testtask'
57
Rake::TestTask.new do |t|
6-
t.libs.push "lib"
8+
t.libs.push 'lib'
79
t.test_files = FileList['test/*_test.rb']
810
t.verbose = true
911
end
10-
task(default: [:test])
11-
rescue LoadError
12+
13+
desc 'Run RuboCop'
14+
task :rubocop do
15+
sh 'rubocop'
16+
end
17+
18+
task default: %i[test rubocop]
19+
rescue LoadError => e
20+
warn "Could not load rake/testtask: #{e}"
1221
end

acquia-http-hmac.gemspec

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1+
# frozen_string_literal: true
2+
13
Gem::Specification.new do |s|
24
s.name = 'acquia-http-hmac'
3-
s.version = '2.0.2'
5+
s.version = '2.0.3'
6+
s.required_ruby_version = '>= 3.3.0'
47
s.licenses = ['MIT']
5-
s.summary = "HMAC signing library and rack middleware"
8+
s.summary = 'HMAC signing library and rack middleware'
69
s.description = "HMAC signing library and rack middleware conforming to Acquia's HMAC 2.0 specifications"
7-
s.date = Time.now.strftime("%Y-%m-%d")
8-
s.authors = ["Peter Wolanin", "Marc Seeger"]
10+
s.date = Time.now.strftime('%Y-%m-%d')
11+
s.authors = ['Peter Wolanin', 'Marc Seeger']
912
s.email = 'engineering@acquia.com'
10-
s.homepage = 'https://www.acquia.com/'
11-
s.files = Dir["[A-Z]*", "{bin,etc,lib,test}/**/*"]
13+
s.homepage = 'https://www.acquia.com/'
14+
s.files = Dir['[A-Z]*', '{bin,etc,lib,test}/**/*']
1215
s.bindir = 'bin'
13-
s.require_paths = ["lib"]
16+
s.require_paths = ['lib']
1417
s.executables << 'acq-http-request'
1518

16-
s.add_dependency 'addressable', '~> 2.4'
19+
s.add_dependency 'addressable', '~> 2.8'
1720

18-
s.add_development_dependency('rake')
19-
s.add_development_dependency('grape')
20-
s.add_development_dependency('rack-test')
21-
s.add_development_dependency('multi_json')
22-
s.add_development_dependency('sqlite3')
23-
s.add_development_dependency('webrick')
21+
s.add_development_dependency('grape', '~> 2.2')
22+
s.add_development_dependency('multi_json', '~> 1.15')
23+
s.add_development_dependency 'rack', '~> 2.2.10'
24+
s.add_development_dependency('rack-test', '~> 2.1')
25+
s.add_development_dependency('rake', '~> 13.2')
26+
s.add_development_dependency 'rubocop', '~> 1.69'
27+
s.add_development_dependency('sqlite3', '~> 2.4')
28+
s.add_development_dependency('webrick', '~> 1.9')
2429
end

bin/acq-http-request

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,71 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

34
require 'acquia-http-hmac'
45
require 'optparse'
56
require 'ostruct'
67

7-
if ARGV[0]
8-
url = ARGV[0]
9-
end
8+
url = ARGV[0] if ARGV[0]
109

1110
# Argument processing
1211
options = OpenStruct.new
1312
options.realm = 'Test'
1413
options.http_method = 'GET'
1514
o = OptionParser.new do |opts|
16-
opts.banner = "Usage: #{$0} URL -u ID:PASSWORD"
17-
opts.on("-r", "--realm [REALM]", "Server auth realm. Default 'Test'.") { |v| options.realm = v }
18-
opts.on("-u", "--user [ID:PASSWORD]", "HMAC creds") { |v| options.user = v }
19-
opts.on("-d", "--data [DATA]", "Data to POST.") { |v| options.data = v }
20-
opts.on("-X", "--request [METHOD]", "HTTP method. Defaults to GET, or POST if --data is specified.") { |v| options.http_method = v.upcase }
15+
opts.banner = "Usage: #{$PROGRAM_NAME} URL -u ID:PASSWORD"
16+
opts.on('-r', '--realm [REALM]', "Server auth realm. Default 'Test'.") { |v| options.realm = v }
17+
opts.on('-u', '--user [ID:PASSWORD]', 'HMAC creds') { |v| options.user = v }
18+
opts.on('-d', '--data [DATA]', 'Data to POST.') { |v| options.data = v }
19+
opts.on('-X', '--request [METHOD]', 'HTTP method. Defaults to GET, or POST if --data is specified.') { |v| options.http_method = v.upcase }
2120
end
2221
begin
2322
o.parse!
24-
rescue Exception => e
23+
rescue StandardError => e
2524
puts e.message
2625
puts o.help
2726
exit 1
2827
end
2928

30-
if ARGV.empty? or !options.user
29+
if ARGV.empty? || !options.user
3130
puts o.help
3231
exit
3332
end
3433

3534
uri = URI(Addressable::URI.escape.encode(url))
3635

37-
if uri.path == ''
38-
uri.path = '/'
39-
end
36+
uri.path = '/' if uri.path == ''
4037

4138
host = uri.host
42-
if uri.port && uri.port != '443'
43-
host << ':' + uri.port
44-
end
39+
host << ":#{uri.port}" if uri.port && uri.port != '443'
4540

4641
id, password = options.user.split(':', 2)
4742

4843
mac = Acquia::HTTPHmac::Auth.new(options.realm, password)
4944

50-
51-
5245
args = {
5346
http_method: options.http_method,
5447
host: host,
5548
id: id,
56-
path_info: uri.path,
49+
path_info: uri.path
5750
}
5851

59-
case
60-
when options.http_method == 'GET'
61-
req = Net::HTTP::GET.new(uri)
62-
when options.http_method == 'HEAD'
63-
req = Net::HTTP::HEAD.new(uri)
64-
when options.http_method == 'POST'
65-
req = Net::HTTP::POST.new(uri)
66-
when options.http_method == 'PUT'
67-
req = Net::HTTP::PUT.new(uri)
68-
when options.http_method == 'DELETE'
69-
req = Net::HTTP::DELETE.new(uri)
52+
case options.http_method
53+
when 'GET'
54+
Net::HTTP::GET.new(uri)
55+
when 'HEAD'
56+
Net::HTTP::HEAD.new(uri)
57+
when 'POST'
58+
Net::HTTP::POST.new(uri)
59+
when 'PUT'
60+
Net::HTTP::PUT.new(uri)
61+
when 'DELETE'
62+
Net::HTTP::DELETE.new(uri)
7063
else
71-
fail("Unsupported HTTP verb #{options.http_method}")
64+
raise("Unsupported HTTP verb #{options.http_method}")
7265
end
7366
mac.prepare_request_headers(args).each do |name, value|
74-
#header(name, value)
67+
# header(name, value)
7568
end
7669

7770
net = Net::HTTP.new(uri.host, uri.port)
78-
net.use_ssl= uri.host != 'localhost'
79-
71+
net.use_ssl = uri.host != 'localhost'

example-sqlite3/setup.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
# frozen_string_literal: true
2+
13
require 'sqlite3'
24
require 'base64'
35
require 'openssl'
46
require 'yaml'
57

6-
8+
# Class: ExampleSQLite3Setup
79
class ExampleSQLite3Setup
8-
910
def initialize(dbfile, passwords_file)
1011
@dbfile = dbfile
1112
File.unlink(@dbfile) if File.exist?(@dbfile)
1213
@passwords_file = passwords_file
1314
end
1415

1516
def create_t1
16-
return <<-SQL
17+
<<-SQL
1718
CREATE TABLE passwords (
1819
id VARCHAR(50),
1920
request_date CHAR(10),
@@ -24,7 +25,7 @@ def create_t1
2425
end
2526

2627
def create_t2
27-
return <<-SQL
28+
<<-SQL
2829
CREATE TABLE password_data (
2930
id VARCHAR(50),
3031
request_method VARCHAR(10),
@@ -46,51 +47,51 @@ def write_database
4647

4748
dates = [
4849
today.strftime('%F'),
49-
tomorrow.strftime('%F'),
50+
tomorrow.strftime('%F')
5051
]
5152
realm = 'Test'
5253

5354
creds = YAML.safe_load(File.read(@passwords_file))
5455
passwords = {}
55-
creds.each do |id,data|
56+
creds.each do |id, data|
5657
passwords[id] = data['password']
5758
end
5859

5960
data = {
6061
'testadmin' => [
6162
['GET', '/'],
62-
['POST', '/'],
63+
['POST', '/']
6364
],
6465
'testuser' => [],
6566
'curltest' => [
66-
['GET', '/'],
67-
],
67+
['GET', '/']
68+
]
6869
}
6970

70-
sha256 = OpenSSL::Digest::SHA256.new
71+
sha256 = OpenSSL::Digest.new('SHA256')
7172

72-
passwords.each do |id,pass|
73+
passwords.each do |id, pass|
7374
# Run a 2-step HMAC KDF using date and realm
7475
binary_pass = Base64.decode64(pass)
7576
dates.each do |date|
7677
derived_pass1 = OpenSSL::HMAC.digest(sha256, binary_pass, date)
7778
derived_pass2 = OpenSSL::HMAC.digest(sha256, derived_pass1, realm)
78-
db.execute("INSERT INTO passwords (id, request_date, base64_password) VALUES ( ?, ?, ? )", [id, date, Base64.strict_encode64(derived_pass2)])
79+
db.execute('INSERT INTO passwords (id, request_date, base64_password) VALUES ( ?, ?, ? )', [id, date, Base64.strict_encode64(derived_pass2)])
7980
end
8081
end
8182

8283
data.each do |id, values|
8384
values.each do |row|
84-
row.unshift(id)
85-
db.execute("INSERT INTO password_data VALUES ( ?, ?, ? )", row)
85+
row.unshift(id)
86+
db.execute('INSERT INTO password_data VALUES ( ?, ?, ? )', row)
8687
end
8788
end
8889

8990
db.close
9091
end
9192
end
9293

93-
if $0 == __FILE__
94+
if $PROGRAM_NAME == __FILE__
9495

9596
mypath = File.dirname(__FILE__)
9697
dbfile = File.join(mypath, 'passwords.sqlite3')

example/app.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler/setup'
24
require 'securerandom'
35
require 'grape'
46
require 'json'
57

68
module Example
9+
# Class: App
710
class App < Grape::API
811
version 'v1', using: :header, vendor: 'acquia'
912

@@ -14,28 +17,28 @@ class App < Grape::API
1417
helpers do
1518
def hellos
1619
# Store data in memory for simple testing.
17-
@@hellos ||= {SecureRandom.uuid => "world"}
20+
@@hellos ||= { SecureRandom.uuid => 'world' }
1821
@@hellos
1922
end
2023
end
2124

2225
resource :hello do
2326
get do
24-
{hello: hellos}
27+
{ hello: hellos }
2528
end
2629

27-
desc "Return a single hello."
30+
desc 'Return a single hello.'
2831
get ':id' do
29-
{hello: hellos[params[:id]]}
32+
{ hello: hellos[params[:id]] }
3033
end
3134

3235
params do
33-
requires :hello, type: String, desc: "A hello."
36+
requires :hello, type: String, desc: 'A hello.'
3437
end
3538
post do
3639
id = SecureRandom.uuid
3740
hellos[id] = params[:hello]
38-
{id => params[:hello]}
41+
{ id => params[:hello] }
3942
end
4043
end
4144

0 commit comments

Comments
 (0)