summaryrefslogtreecommitdiff
path: root/spec/support/proxy_server.rb
blob: 0ef676d35ac82e3effac410f3f63d3ca775d57d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

class ProxyServer
  DOMAINS = [
    'composer.test',
    'goproxy.test',
    'maven.test',
    'npm.test',
    'nuget.test',
    'pypi.test',
    'rubygems.test'
  ].freeze

  include Singleton

  attr_accessor :pid

  def start
    DOMAINS.each { |domain| add_host(domain, '127.0.0.1') }
    Dir.chdir License::Management.root.join('tmp') do
      host = 'wildcard.test'
      subject_alternative_names = DOMAINS.map { |x| "DNS:#{x}" }.join(',')
      system([
        "rm -f #{host}.*",
        "/usr/bin/openssl req -x509 -newkey rsa:4096 -keyout #{host}.key -out #{host}.crt -days 999 -nodes -subj '/C=/ST=/L=/O=/OU=/CN=*.test' -addext 'subjectAltName=#{subject_alternative_names}'",
        "cat #{host}.* > #{host}.pem"
      ].join("&&"))
    end
    config_file = License::Management.root.join("spec/fixtures/haproxy.cfg")
    self.pid = spawn("/usr/sbin/haproxy -f #{config_file}")
    wait_for_server
    pid
  end

  def stop(pid = self.pid)
    return unless pid

    Process.kill("TERM", pid)
    Process.wait(pid)
    system("rm -f /usr/local/share/ca-certificates/custom.*")
    system("rm -f /usr/lib/ssl/certs/custom.*")
    system("update-ca-certificates -v")
    system("c_rehash -v")
    system("/opt/asdf/installs/mono/6.8.0.123/bin/cert-sync /etc/ssl/certs/ca-certificates.crt")
  end

  private

  def add_host(name, ip)
    return if system("grep #{name} /etc/hosts")

    system("echo '#{ip} #{name}' >> /etc/hosts")
  end

  def wait_for_server
    DOMAINS.each do |domain|
      puts "Warming up #{domain}..."
      print "." until system("curl -s -k https://#{domain} > /dev/null")
      puts "#{domain} is ready!"
    end
  end
end