module Proxy::ContainerGateway

Constants

VERSION

Public Class Methods

authorized_for_repo?(repo_name) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 81
def authorized_for_repo?(repo_name)
  conn = initialize_db
  unauthenticated_repo = conn[:unauthenticated_repositories].where(name: repo_name).first
  !unauthenticated_repo.nil?
end
blobs(repository, digest) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 36
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() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 63
def catalog
  unauthenticated_repos
end
initialize_db() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 103
def initialize_db
  conn = Sequel.connect("sqlite://#{Proxy::ContainerGateway::Plugin.settings.sqlite_db_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 94
def insert_token(username, token, expire_at_string, clear_expired_tokens: true)
  tokens = initialize_db[:authentication_tokens]
  checksum = Digest::SHA256.hexdigest(token)

  tokens.where(:token_checksum => checksum).delete
  tokens.insert(username: username, token_checksum: checksum, expire_at: expire_at_string.to_s)
  tokens.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 29
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 24
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 11
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
unauthenticated_repos() click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 67
def unauthenticated_repos
  conn = initialize_db
  conn[:unauthenticated_repositories].order(:name).map(:name)
end
update_unauthenticated_repos(repo_names) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 72
def update_unauthenticated_repos(repo_names)
  conn = initialize_db
  unauthenticated_repos = conn[:unauthenticated_repositories]
  unauthenticated_repos.delete
  repo_names.each do |repo_name|
    unauthenticated_repos.insert(:name => repo_name)
  end
end
valid_token?(token) click to toggle source
# File lib/smart_proxy_container_gateway/container_gateway_main.rb, line 87
def valid_token?(token)
  tokens = initialize_db[:authentication_tokens]
  tokens.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 116
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 120
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 124
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 128
def pulp_key
  OpenSSL::PKey::RSA.new(
    File.read(Proxy::ContainerGateway::Plugin.settings.pulp_client_ssl_key)
  )
end