探码开发文档
  • 探码科技-发开日志
  • 代码规范
  • 前端
    • Bootstrap 模板
      • 图表类
      • Profile 类页面
    • JS Chart图表
    • 图片库
    • Icon图标库
    • Css3
      • 字体+背景混合搭配
      • tranform-origin + transform
      • flex
        • 布局左边自适应,右边固定宽度
    • 用Sass颜色函数控制颜色
    • Draggable组件库
  • Javascript
  • Ruby
    • Ruby on Rails
      • 数据库类
      • 日志类
      • 价格字段的单位转换
      • 部署
      • 根据设备类型自动渲染页面
      • 路由
    • Gems
    • Automating your API with JSON Schema
    • 深度冻结变量 Deep Freeze
    • 搭建私有Gem仓库
    • YAML语法
  • 数据库
    • PostgreSQL
      • 基础知识
        • PostgreSQL中插入数据
        • PostgreSQL中更新数据
        • PostgreSQL中删除数据
      • 数据库管理
      • select jsonb
    • PostgreSQL XL
      • Data Definition
      • 查询技巧
  • Docker
    • Docker日志收集最佳实践
    • Harbor搭建私有镜像服务
  • Kubernetes
    • 参考资源
    • Kubeadm方式安装Kubernetes
    • Rancher方式安装Kubernetes
      • RBAC集成
    • rke方式安装Kubernetes
    • RBAC用户管理
    • Traefik配置
    • 创建etcd集群
    • Kubeapps
    • 工具
    • 安装Helm
    • 亲和度配置
  • 文件系统
    • GlusterFS
  • 日志管理
    • Fluentd
  • VirtualBox
  • 工具软件
    • Alfred
    • 代码版本控制工具
    • Atom
    • Bash Shell
    • Vim
    • fzf(Fuzzy Finder)
    • Gitlab
  • Ubuntu
    • 安装 VPN服务
    • 安装DNSMasq
    • Keepalived
    • OpenSSL 使用技巧
  • Git
  • Nginx
    • 自动更新SSL证书
    • 使用stream模块实现负载均衡
  • 机器学习
Powered by GitBook
On this page
  1. Ruby

搭建私有Gem仓库

Previous深度冻结变量 Deep FreezeNextYAML语法

Last updated 6 years ago

Gem私有仓库,我们用Geminabox快速搭建

config.ru
#
# This is a simple rackup file for geminabox. It allows simple role-based authorization.
#
# roles:
# - developer
# - upload
# - delete
# - admin (can do anything)
#
# For example, a developer who can access the service and upload new gems would have the following roles: `%w(developer upload)
#

require "rubygems"
require "geminabox"

Geminabox.rubygems_proxy = false
Geminabox.data = "/geminabox/data"

API_KEYS = {
  ENV['DEVELOPER_API_KEY'] => { password: '', roles: %w(developer) },
  ENV['ADMIN_API_KEY'] => { password: '', roles: %w(admin) }
}

use Rack::Session::Pool, expire_after: 1000 # sec
use Rack::Protection

Geminabox::Server.helpers do
  def protect!(role='developer')
    unless has_role?(role)
      response['WWW-Authenticate'] = %(Basic realm="Gem In a Box")
      halt 401, "Not Authorized.\n"
    end
  end

  def auth
    @auth ||= Rack::Auth::Basic::Request.new(request.env)
  end

  def username
    auth ? auth.credentials.first : nil
  end

  def password
    auth ? auth.credentials.last : nil
  end

  def user_roles
    API_KEYS[username][:roles]
  end

  def authenticated?
    return false unless auth.provided? && auth.basic? && auth.credentials
    api_key = API_KEYS[username]
    !api_key.nil? && password == api_key[:password]
  end

  def current_user_roles
    authenticated? ? user_roles : []
  end

  def has_role?(role)
    current_user_roles.include?('admin') || current_user_roles.include?(role)
  end
end

Geminabox::Server.before '/upload' do
  protect!('upload')
end

Geminabox::Server.before do
  if request.delete?
    protect!('delete')
  else
    protect!('developer')
  end
end

Geminabox::Server.before '/api/v1/gems' do
  unless env['HTTP_AUTHORIZATION'] == 'API_KEY'
    halt 401, "Access Denied. Api_key invalid or missing.\n"
  end
end

run Geminabox::Server

把上面内容保存到config.ru文件,然后运行ADMIN_API_KEY=admin rackup即可启动服务,登录用户名是admin,密码填空

https://github.com/geminabox/geminabox