gpt4 book ai didi

ruby-on-rails - 本地主机通配符 SSL 与 Puma

转载 作者:行者123 更新时间:2023-12-04 22:41:22 35 4
gpt4 key购买 nike

我有一个带有 Puma 4.3 的 Rails 6.0 应用程序。我需要使用自定义域设置通配符 SSL 证书。我已经查看了一堆不同的资源,但我似乎无法让任何东西正常工作。我在那里看到的大多数东西都适合非通配符设置。

这是我的config/puma.rb .我已经尝试了当前的配置以及被注释掉的东西。

##
# Configure Puma server

require 'fileutils'
workers Integer(ENV.fetch('WEB_CONCURRENCY', 2))
threads_count = Integer(ENV.fetch('MAX_THREADS', 5))
threads threads_count, threads_count

key = %w[. config ssl myapp-local.com.key].join('/')
crt = %w[. config ssl myapp-local.com.crt].join('/')
ssl_bind '127.0.0.1', '9292', {
key: key,
cert: crt,
verify_mode: :peer
}
# if ENV['ENVIRONMENT'] == 'development'
# unless File.exist?(key)
# def generate_root_cert(root_key)
# root_ca = OpenSSL::X509::Certificate.new
# root_ca.version = 2 # cf. RFC 5280 - to make it a "v3" certificate
# root_ca.serial = 0x0
# root_ca.subject = OpenSSL::X509::Name.parse "/C=BE/O=A1/OU=A/CN=*.myapp-local.com"
# root_ca.issuer = root_ca.subject # root CA's are "self-signed"
# root_ca.public_key = root_key.public_key
# root_ca.not_before = Time.now
# root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60 # 2 years validity
#
# root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)
# root_ca
# end
#
# root_key = OpenSSL::PKey::RSA.new(2048)
# file = File.new(key, "wb")
# file.write(root_key)
# file.close
#
# root_cert = generate_root_cert(root_key)
#
# file = File.new(cert, "wb")
# file.write(root_cert)
# file.close
# end
#
# ssl_bind '127.0.0.1', '9292', {
# key: key,
# cert: cert,
# verify_mode: :peer
# }
#
# puts "Using local SSL cert..."
# end

preload_app!

rackup DefaultRackup
port ENV.fetch('PORT', 3000)
environment ENV.fetch('RACK_ENV', 'development')

on_worker_boot do
##
# Force Scout Agent launch. Required because of use of nginx in Procfile
ScoutApm::Agent.instance.start_background_worker
end

on_worker_fork { FileUtils.touch('/tmp/app-initialized') }

我正在使用 foreman .这是我的 Procfile.dev :
redis: (ps aux | grep 6379 | grep redis | awk '{ print $2 }' | xargs kill -s SIGINT) && redis-server --port 6379
resque: rake resque:workers QUEUE='*' COUNT='5'
scheduler: rake resque:scheduler
web: puma

然后在 /etc/hosts我有:
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 app.myapp-local.com

当我去 https://app.myapp-local.com:3000我得到一个 ERR_SSL_PROTOCOL_ERROR来自 Chrome 的错误。

我也尝试过像这样手动生成证书:
name=myapp-local.com
openssl req \
-new \
-newkey rsa:2048 \
-sha256 \
-days 3650 \
-nodes \
-x509 \
-keyout $name.key \
-out $name.crt \
-config <(cat <<-EOF
[req]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
[req_distinguished_name]
CN = $name
[v3_req]
keyUsage = keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = $name
DNS.2 = *.$name
EOF
)

mv myapp-local.com.crt myapp-local.com.key config/ssl
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain config/ssl/myapp-local.com.crt

我已经没有想法了。由于应用程序的配置方式,我不能使用不同的主机名(例如 lvh.me 或 localhost),所以我需要它与 *.myapp-local.com:3000 一起使用.任何帮助是极大的赞赏!

更新

所以我像这样更改了我的配置文件:
if ENV['ENVIRONMENT'] == 'development'
key = %w[. config ssl myapp-local.com.key].join('/')
crt = %w[. config ssl myapp-local.com.pem].join('/')
ca = "/Users/Daniel Bonnell/Library/Application\ Support/Certificate\ Authority/myapp/myapp.certAuthorityConfig"

ssl_bind '127.0.0.1', '3000', {
key: key,
cert: crt,
ca: ca,
verify_mode: :peer
}
end

现在,当我启动应用程序时,我在日志中看到了这一点:
17:12:45 web.1 | [68495] * Listening on ssl://127.0.0.1:3000?cert=./config/ssl/myapp-local.com.pem&key=./config/ssl/myapp-local.com.key&verify_mode=peer&no_tlsv1=false&no_tlsv1_1=false&ca=/Users/Daniel Bonnell/Library/Application Support/Certificate Authority/myapp/myapp.certAuthorityConfig
17:12:45 web.1 | [68495] * Listening on tcp://0.0.0.0:5300

如果我去 https://app.myapp-local.com:3000我看到以下错误:
17:14:19 web.1 | 2020-03-28 17:14:19 -0500: SSL error, peer: 127.0.0.1, peer cert: /O=member: F4B00436-9C90-4941-B053-A83BCB633934 DD31B694-6F02-4C8D-98C2-640DABBEB3F8/CN=member: F4B00436-9C90-4941-B053-A83BCB633934 DD31B694-6F02-4C8D-98C2-640DABBEB3F8, #<Puma::MiniSSL::SSLError: OpenSSL certificate verification error: unable to get local issuer certificate - 20>

如果我去 https://app.myapp-local.com:5300我看到以下错误:
17:15:32 web.1 | 2020-03-28 17:15:32 -0500: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.>

我关注了 these steps使用 mkcert生成证书,然后在我的钥匙串(keychain)(MacOS)中设置本地 CA。

最佳答案

我设法让我的设置正常工作。我认为有两个问题。首先,我将 SSL 绑定(bind)到端口 3000,然后再往下,将 Puma 绑定(bind)到端口 3000。我更改了 port ENV.fetch('PORT', 3000)port ENV.fetch('PORT', 3001) .其次,我指向了错误的 CA。下面的设置现在似乎对我有用。我可以在端口 3000 上访问我的应用程序,并且不再看到那个讨厌的 SSL 错误。希望这可以帮助某人。我花了10个小时搞清楚。 🙃

##
# Configure Puma server

require 'fileutils'
workers Integer(ENV.fetch('WEB_CONCURRENCY', 2))
threads_count = Integer(ENV.fetch('MAX_THREADS', 5))
threads threads_count, threads_count

# Note: Must generate an SSL certificate for local use.
# See: https://blog.filippo.io/mkcert-valid-https-certificates-for-localhost/
if ENV['ENVIRONMENT'] == 'development'
key = File.expand_path('./config/ssl/local_key.pem')
crt = File.expand_path('./config/ssl/local_cert.pem')
ca = File.expand_path('~/Library/Application Support/mkcert/rootCA.pem')

ssl_bind '127.0.0.1', 3000, {
key: key,
cert: crt,
ca: ca,
verify_mode: :peer
}
end

preload_app!

rackup DefaultRackup
port ENV.fetch('PORT', 3001)
environment ENV.fetch('RACK_ENV', 'development')

on_worker_boot do
##
# Force Scout Agent launch. Required because of use of nginx in Procfile
ScoutApm::Agent.instance.start_background_worker
end

on_worker_fork { FileUtils.touch('/tmp/app-initialized') }

关于ruby-on-rails - 本地主机通配符 SSL 与 Puma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60907375/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com