Project

General

Profile

Revision 64f6a1f2

Added by Dmitri Dolguikh about 8 years ago

Fixes #8393: added support for basic auth for standalone registries

View differences:

app/controllers/image_search_controller.rb
46 46
  end
47 47

  
48 48
  def registry_image_exists?(term)
49
    result = ::Service::RegistryApi.new(:url => @registry.url).search(term)
49
    result = ::Service::RegistryApi.new(:url => @registry.url,
50
                                        :user => @registry.username,
51
                                        :password => @registry.password).search(term)
50 52
    registry_name = term.split('/').size > 1 ? term :
51 53
        'library/' + term
52 54
    result['results'].any? { |r| r['name'] == registry_name }
53 55
  end
54 56

  
55 57
  def registry_auto_complete_image_tags(terms)
56
    ::Service::RegistryApi.new(:url => @registry.url).list_repository_tags(terms).keys
58
    ::Service::RegistryApi.new(:url => @registry.url,
59
                               :user => @registry.username,
60
                               :password => @registry.password).list_repository_tags(terms).keys
57 61
  end
58 62

  
59 63
  def registry_search_image(terms)
60
    r = ::Service::RegistryApi.new(:url => @registry.url).search(terms)
64
    r = ::Service::RegistryApi.new(:url => @registry.url,
65
                                   :user => @registry.username,
66
                                   :password => @registry.password).search(terms)
61 67
    r['results']
62 68
  end
63 69

  
app/models/docker_registry.rb
1 1
class DockerRegistry < ActiveRecord::Base
2 2
  include Authorizable
3 3
  include Taxonomix
4
  include Encryptable
4 5

  
5 6
  has_many :containers, :foreign_key => "registry_id", :dependent => :destroy
7
  encrypts :password
6 8

  
7 9
  scoped_search :on => :name, :complete_value => true
8 10
  scoped_search :on => :url
app/models/service/registry_api.rb
4 4
    attr_reader :config
5 5

  
6 6
    def initialize(params = {})
7
      @config = DEFAULTS.merge(params)
7
      config = DEFAULTS.merge(params)
8
      uri = URI(config.delete(:url))
9
      uri.user = config.delete(:user)
10
      uri.password = config.delete(:password)
11
      @config = config.merge(:url => uri.to_s)
8 12
    end
9 13

  
10 14
    def search(aquery)
app/views/registries/_form.html.erb
15 15
        <%= text_f   f, :name, :help_inline => _("Registry name") %>
16 16
        <%= text_f   f, :url, :help_inline => _("Registry url") %>
17 17
        <%= text_f   f, :description, :help_inline => _("Describing of the registry") %>
18
        <%= text_f   f, :username, :help_inline => _("Username used to access the registry") %>
19
        <%= password_f   f, :password, :help_inline => _("Password used for authentication to the registry") %>
18 20
      </div>
19 21

  
20 22
      <%= render 'taxonomies/loc_org_tabs', :f => f, :obj => @registry %>
db/migrate/20141120123003_add_user_credentials_to_docker_registries.rb
1
class AddUserCredentialsToDockerRegistries < ActiveRecord::Migration
2
  def change
3
    add_column :docker_registries, :username, :string
4
    add_column :docker_registries, :password, :string
5
  end
6
end
test/factories/docker_registry.rb
2 2
  factory :docker_registry do
3 3
    sequence(:name) { |n| "hub#{n}" }
4 4
    sequence(:url) { |n| "http://localhost/#{n}" }
5
    sequence(:username) { |n| "username#{n}" }
6
    sequence(:password) { |n| "password#{n}" }
5 7
  end
6 8

  
7 9
  trait :with_location do
test/units/docker_registry_test.rb
16 16
    end
17 17
    assert r.used_organization_ids.include?(organization.id)
18 18
  end
19

  
20
  test 'password is stored encrypted' do
21
    r = as_admin { FactoryGirl.create(:docker_registry) }
22
    assert r.is_decryptable?(r.password_in_db)
23
  end
19 24
end

Also available in: Unified diff