module Proxy::ContainerGateway

Constants

VERSION

Public Class Methods

authorized_for_repo?(repo_name, user_token_is_valid, username = nil) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 124
def authorized_for_repo?(repo_name, user_token_is_valid, username = nil)
  repository = Repository.where(name: repo_name).first

  # Repository doesn't exist
  return false if repository.nil?

  # Repository doesn't require auth
  return true unless repository.auth_required

  if username && user_token_is_valid && repository.auth_required
    # User is logged in and has access to the repository
    user = User.find(name: username)
    return !user.repositories_dataset.where(name: repo_name).first.nil?
  end

  false
end
blobs(repository, digest) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 37
def blobs(repository, digest)
  uri = URI.parse(
    "#{Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}/pulpcore_registry/v2/#{repository}/blobs/#{digest}"
  )
  pulp_registry_request(uri)['location']
end
catalog(user = nil) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 65
def catalog(user = nil)
  if user.nil?
    unauthenticated_repos
  else
    (unauthenticated_repos + user.repositories_dataset.map(:name)).sort
  end
end
initialize_db() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 161
def initialize_db
  file_path = Proxy::ContainerGateway::Plugin.settings.sqlite_db_path
  conn = Sequel.connect("sqlite://#{file_path}")
  container_gateway_path = $LOAD_PATH.detect { |path| path.include? 'smart_proxy_container_gateway' }
  begin
    Sequel::Migrator.check_current(conn, "#{container_gateway_path}/smart_proxy_container_gateway/sequel_migrations")
  rescue Sequel::Migrator::NotCurrentError
    migrate_db(conn, container_gateway_path)
  end
  conn
end
insert_token(username, token, expire_at_string, clear_expired_tokens: true) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 152
def insert_token(username, token, expire_at_string, clear_expired_tokens: true)
  checksum = Digest::SHA256.hexdigest(token)
  user = User.find_or_create(name: username)

  AuthenticationToken.where(:token_checksum => checksum).delete
  AuthenticationToken.create(token_checksum: checksum, expire_at: expire_at_string.to_s, user_id: user.id)
  AuthenticationToken.where { expire_at < Sequel::CURRENT_TIMESTAMP }.delete if clear_expired_tokens
end
manifests(repository, tag) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 30
def manifests(repository, tag)
  uri = URI.parse(
    "#{Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}/pulpcore_registry/v2/#{repository}/manifests/#{tag}"
  )
  pulp_registry_request(uri)['location']
end
ping() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 25
def ping
  uri = URI.parse("#{Proxy::ContainerGateway::Plugin.settings.pulp_endpoint}/pulpcore_registry/v2/")
  pulp_registry_request(uri).body
end
pulp_registry_request(uri) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 12
def pulp_registry_request(uri)
  http_client = Net::HTTP.new(uri.host, uri.port)
  http_client.ca_file = pulp_ca
  http_client.cert = pulp_cert
  http_client.key = pulp_key
  http_client.use_ssl = true

  http_client.start do |http|
    request = Net::HTTP::Get.new uri
    http.request request
  end
end
token_user(token) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 142
def token_user(token)
  User[AuthenticationToken.find(token_checksum: Digest::SHA256.hexdigest(token)).user_id]
end
unauthenticated_repos() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 73
def unauthenticated_repos
  Repository.where(auth_required: false).order(:name).map(:name)
end
update_repository_list(repo_list) click to toggle source

Replaces the entire list of repositories

# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 78
def update_repository_list(repo_list)
  RepositoryUser.dataset.delete
  Repository.dataset.delete
  repo_list.each do |repo|
    Repository.find_or_create(name: repo['repository'],
                              auth_required: repo['auth_required'].to_s.downcase == "true")
  end
end
update_user_repo_mapping(user_repo_maps) click to toggle source

Replaces the entire user-repo mapping for all logged-in users

# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 88
def update_user_repo_mapping(user_repo_maps)
  # Get hash map of all users and their repositories
  # Ex: {"users"=> [{"admin"=>[{"repository"=>"repo", "auth_required"=>"true"}]}]}
  # Go through list of repositories and add them to the DB
  RepositoryUser.dataset.delete
  user_repo_maps['users'].each do |user_repo_map|
    user_repo_map.each do |user, repos|
      next if repos.nil?

      repos.each do |repo|
        found_repo = Repository.find(name: repo['repository'],
                                     auth_required: repo['auth_required'].to_s.downcase == "true")
        if found_repo.nil?
          logger.warn("#{repo['repository']} does not exist in this smart proxy's environments")
        elsif found_repo.auth_required
          found_repo.add_user(User.find(name: user))
        end
      end
    end
  end
end
update_user_repositories(username, repositories) click to toggle source

Replaces the user-repo mapping for a single user

# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 111
def update_user_repositories(username, repositories)
  user = User.where(name: username).first
  user.remove_all_repositories
  repositories.each do |repo_name|
    found_repo = Repository.find(name: repo_name)
    if found_repo.nil?
      logger.warn("#{repo_name} does not exist in this smart proxy's environments")
    elsif user.repositories_dataset.where(name: repo_name).first.nil? && found_repo.auth_required
      user.add_repository(found_repo)
    end
  end
end
valid_token?(token) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 146
def valid_token?(token)
  AuthenticationToken.where(token_checksum: Digest::SHA256.hexdigest(token)).where do
    expire_at > Sequel::CURRENT_TIMESTAMP
  end.count.positive?
end

Private Class Methods

migrate_db(db_connection, container_gateway_path) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 175
def migrate_db(db_connection, container_gateway_path)
  Sequel::Migrator.run(db_connection, "#{container_gateway_path}/smart_proxy_container_gateway/sequel_migrations")
end
pulp_ca() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 179
def pulp_ca
  Proxy::ContainerGateway::Plugin.settings.pulp_client_ssl_ca
end
pulp_cert() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 183
def pulp_cert
  OpenSSL::X509::Certificate.new(File.read(Proxy::ContainerGateway::Plugin.settings.pulp_client_ssl_cert))
end
pulp_key() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 187
def pulp_key
  OpenSSL::PKey::RSA.new(
    File.read(Proxy::ContainerGateway::Plugin.settings.pulp_client_ssl_key)
  )
end