Project

General

Profile

Revision 9ea40591

Added by Daniel Lobato Garcia over 6 years ago

Fixes #14181 - Validate registry URL and attempt login

In order to avoid users trying to create containers in an external
registry that doesn't exist, we should provide some preventative
measures.

View differences:

app/models/docker_registry.rb
3 3
  include Taxonomix
4 4
  include Encryptable
5 5

  
6
  attr_accessible :name, :url, :username, :password, :locations, :organizations
7

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

  
11
  attr_accessible :name, :url, :username, :password, :locations, :organizations
9
  attr_accessible :name, :url, :username, :password, :locations, :organizations,
10
    :description
12 11

  
13 12
  validates_lengths_from_database
14 13
  validates :name, :presence => true, :uniqueness => true
15
  validates :url,  :presence => true, :uniqueness => true
14
  validates :url,  :presence => true, :uniqueness => true,
15
    :url_schema => ['http', 'https']
16
  validate :attempt_login
16 17

  
17 18
  scoped_search :on => :name, :complete_value => true
18 19
  scoped_search :on => :url
......
37 38
  def self.humanize_class_name(_name = nil)
38 39
    _("Docker/Registry")
39 40
  end
41

  
42
  private
43

  
44
  def attempt_login
45
    credentials = { 'username' => username, 'password' => password }
46
    connection = ::Docker::Connection.new(
47
      url,
48
      credentials)
49
    ::Docker.authenticate!(credentials, connection)
50
  rescue => e
51
    errors.add(:base, _('Unable to log in to this Docker Registry - %s') % e)
52
  end
40 53
end

Also available in: Unified diff