Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Ruby

Tự Host Ruby Gems An Hơn Vậy Phụ Thuộc RubyGems.org Trong Production

Một bài phê phán + screencast chỉ bạn cách host gems của riêng bạn

Để rõ ràng ngay: gems awesome, và RubyGems.org là great service.

...Nhưng gần đây tôi cảm thấy queasy mỗi lần thêm gem mới vào app. Càng suy nghĩ, càng thấy cách chúng ta dùng gems không chỉ flawed. Đó là disaster waiting to happen.

Social engineering FTW

Vài ngày trước tại RubyNation, Ben Smith talk hay "Hacking With Gems".

Không phải hacking on gems, mà dùng gems as cách hack... as attack vector.

Anh ấy perform cool experiment.

Nếu bạn ở conference, có thể thấy stacks printed cards đẹp bên cạnh github stickers và swag khác.

Card chỉ nói một điều: gem install rubynation. Và khoảng 10% attendees làm theo.

Tự Host Ruby Gems An Hơn Vậy Phụ Thuộc RubyGems.org Trong Production

Gem không làm gì

Gem làm vài điều useful. Nhưng điều nó không làm thú vị hơn.

Nó không:

  • Steal RubyGems.org credentials từ gemcutter dotfile của bạn

  • Intercept plaintext passwords và store trong rails /public directory

  • Thêm secret SSH account vào system của bạn

Nhưng nó có thể. Hỏi Ben nếu không tin.

Điều này nên làm bạn sợ hãi

Để tôi backup và nói lại, để nó sink in.

Khoảng 25 người - LẬP TRÌNH VIÊN MÁY TÍNH - bị manipulated chạy arbitrary code trên development machines. Một vài thậm chí chạy as root.

Tôi không mean pick on họ. Đây chỉ là norm trong Ruby community.

Tại sao xảy ra?

Người tin gems.

  • Vì chúng usually work.

  • Vì Rails là gem.

  • Vì well-known folks như Aaron Patterson, Steve Klabnik, Yehuda Katz maintain chúng.

Nhưng vấn đề khi bạn bắt đầu trust người bạn không biết.

Vấn đề khi bạn gõ gem install rubynation với không ngần ngại hơn gem install rails.

Và honest. Ai chưa làm này ít nhất một lần? Chúng ta lười và gems dễ.

RubyGems.org không phải blame

Nó chỉ make extremely easy làm wrong thing. Chạy untrusted code trên servers. Uninformed về changes trong code chạy trên servers.

Đó là lý do tôi suggest chúng ta stop using it in production.

Solutions

Signatures không đủ

Mọi người nên sign gems. Nhưng signatures không đủ. Signature chỉ verify identity của publisher. Không nói họ good, evil, hay chaotic neutral.

Không ai coming to the rescue

Trong perfect world, chúng ta có trusted organization distribute versions của gems known to be good.

Nếu rubs you the wrong way, nghĩ về organizations như Debian làm similar cho Linux. Nếu vẫn rubs you wrong way....well tôi honored bạn đọc blog tôi, Mr. Stallman.

Tại sao nói perfect world? Vì alternatives khác ton of work, và tôi rather not do it.

...có ai YC hopefuls get on this please?

Only real option: DIY

Unfortunately tôi phải sống trong smelly old real world này. Only real way total control over gems là:

  • Review code cho treachery

  • Host gems yourself

May mắn, host own gems dễ. Tôi show bạn how.

How to host your own gems

Tự Host Ruby Gems An Hơn Vậy Phụ Thuộc RubyGems.org Trong Production

Không phải magic

Gem "server" chỉ là bunch flat files on the web. Host anywhere, thậm chí S3 hoặc dropbox.

Tạo Rails app as testbench

Chỉ reason tôi dùng rails ở đây vì nó cho premade Gemfile chơi. App này không làm gì.

`$ rails new myapp
$ cd myapp `

Download .gem files

Bundler command này download tất cả .gem files referenced by Gemfile.lock và put chúng vào /vendor/cache

`$ bundle package
$ mkdir /tmp/gem_server
$ mv vendor/cache/ /tmp/gem_server/gems `

Generate 'server' files

Để take directory of .gem files và make ready for serving, chạy gem generate_index.

`$ gem generate_index -d /tmp/gem_server/ `

Put it all online

Now tôi không recommend này production, nhưng...

`$ mv /tmp/gem_server /Users/snhorne/Dropbox/Public/ `

Edit Gemfile và bundle update

Update "source" line point đến new host thay vì rubygems

`# Gemfile
source 'https://dl.dropboxusercontent.com/u/12345/gem_server'
...
gem 'rails', '3.2.13' `

Cool! Nó pulling gems từ dropbox của tôi.

`$ bundle update
Fetching gem metadata from https://dl.dropboxusercontent.com/u/12345/gem_server/.
Fetching full source index from https://dl.dropboxusercontent.com/u/12345/gem_server/
Using rake (10.1.0)
Using i18n (0.6.1)
Using multi_json (1.7.7)
Using activesupport (3.2.13) `

Bonus: Private gems có thể protected với HTTP basic authentication

Chỉ làm Gemfile trông như:

`# Gemfile
source 'https://username:password@example.com/' `

Alternatives

Approach này không work cho bạn? Có free và commercial gem hosting solutions cho few more bells and whistles.

Geminabox là open source application eliminate nhiều command line work.

Gemfury là private gem hosting company. Tôi used them in the past host proprietary gems cho client. Easy setup và never had problem.