Skip to content

Commit

Permalink
Implemented net/http helper to use bundled openssl certificates (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx authored May 15, 2024
1 parent 738e620 commit 1f29e65
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 8 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/test-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ 'ubuntu-20.04', macos-12 ]
ruby_ver: [ '3.0.6', '3.1.4', '3.2.2' ]
os: [ 'ubuntu-20.04' ]
ruby_ver: [ '2.7.8', '3.0.7', '3.1.5', '3.2.4', '3.3.1' ]
include:
- os: windows-latest
ruby_ver: '3.1.4'
ruby_ver: '3.1.5'
- os: macos-12
ruby_ver: '3.1.5'
- os: macos-14
ruby_ver: '3.1.5'

runs-on: ${{ matrix.os }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/pkg/
/spec/reports/
/tmp/
/lib/cert/

Gemfile.lock

Expand Down
29 changes: 26 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) 2023 [Ribose Inc](https://www.ribose.com).
# Copyright (c) 2023-2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
Expand Down Expand Up @@ -29,7 +29,30 @@ require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rubocop/rake_task"

require "net/http"
require "fileutils"

namespace :build do
desc "Download cacert.pem"
task :download_cacert do
url = URI("https://curl.se/ca/cacert.pem")
FileUtils.mkdir_p("cert")
Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == "https") do |http|
request = Net::HTTP::Get.new url
http.request request do |response|
open "cert/cacert.pem.mozilla", "w" do |io|
response.read_body do |chunk|
io.write chunk
end
end
end
end
end
end

task build: "build:download_cacert"

task default: ["build:download_cacert", :spec]

RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new

task default: %i[spec]
3 changes: 2 additions & 1 deletion lib/tebako-runtime.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Copyright (c) 2023 [Ribose Inc](https://www.ribose.com).
# Copyright (c) 2023-2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
Expand Down Expand Up @@ -45,6 +45,7 @@ module TebakoRuntime
"jing" => "tebako-runtime/adapters/jing",
"mn2pdf" => "tebako-runtime/adapters/mn2pdf",
"mnconvert" => "tebako-runtime/adapters/mnconvert",
"net/http" => "tebako-runtime/adapters/net-http",
"sassc" => "tebako-runtime/adapters/sassc"
}.freeze

Expand Down
39 changes: 39 additions & 0 deletions lib/tebako-runtime/adapters/net-http.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

# Copyright (c) 2024 [Ribose Inc](https://www.ribose.com).
# All rights reserved.
# This file is a part of tebako
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

CACERT_PEM = File.expand_path("#{__dir__}/../../cert/cacert.pem.mozilla")
CACERT_PEM_TMP = TebakoRuntime.extract_memfs(CACERT_PEM)

Net::HTTP.class_eval do
alias_method :_use_ssl=, :use_ssl=

def use_ssl=(boolean)
self.ca_file = CACERT_PEM_TMP
self.verify_mode = OpenSSL::SSL::VERIFY_PEER
self._use_ssl = boolean
end
end
2 changes: 1 addition & 1 deletion lib/tebako-runtime/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
# POSSIBILITY OF SUCH DAMAGE.

module TebakoRuntime
VERSION = "0.3.1"
VERSION = "0.4.0"
end
16 changes: 16 additions & 0 deletions spec/runtime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ def tmpdir_name
require "mnconvert"
end

it "provides an adapter for net/http gem" do
tfile = File.join(TebakoRuntime.full_gem_path("tebako-runtime"), "lib", "cert", "cacert.pem.mozilla")
expect(TebakoRuntime).to receive(:extract_memfs).with(tfile).and_call_original
require "net/http"

uri = URI("https://github.com/tamatebako/tebako-runtime/archive/refs/tags/v0.2.0.tar.gz")
http = Net::HTTP.new(uri.host, uri.port)

expect(http).to receive(:use_ssl=).with(true).and_call_original

http.use_ssl = true

expect(http.ca_file).to eq(tfile)
expect(http.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
end

it "provides an adapter for sassc gem" do
TebakoRuntime.send(:remove_const, :COMPILER_MEMFS)
TebakoRuntime::COMPILER_MEMFS = __dir__
Expand Down

0 comments on commit 1f29e65

Please sign in to comment.