Skip to content

Commit

Permalink
Fixes #36827 - Container tokens now return docker-spec fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
qcjames53 committed Oct 25, 2023
1 parent dbc3b67 commit f9470b4
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/smart_proxy_container_gateway/container_gateway_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'smart_proxy_container_gateway/container_gateway_main'
require 'smart_proxy_container_gateway/foreman_api'
require 'sqlite3'
require 'active_support/all'

module Proxy
module ContainerGateway
Expand Down Expand Up @@ -100,18 +101,24 @@ class Api < ::Sinatra::Base
response.headers['Docker-Distribution-API-Version'] = 'registry/2.0'

unless auth_header.present? && auth_header.basic_auth?
one_year = (60 * 60 * 24 * 365)
return { token: AuthorizationHeader::UNAUTHORIZED_TOKEN, issued_at: Time.now.iso8601,
expires_at: (Time.now + one_year).iso8601 }.to_json
return { token: AuthorizationHeader::UNAUTHORIZED_TOKEN, issued_at: Time.now.rfc3339,
expires_in: 1.year.seconds.to_i }.to_json
end

token_response = ForemanApi.new.fetch_token(auth_header.raw_header, request.params)
if token_response.code.to_i != 200
halt token_response.code.to_i, token_response.body
else
# This returned token should follow docker spec. We need some minor conversion
# to store the token with the expires_at time (using rfc3339).
token_response_body = JSON.parse(token_response.body)
ContainerGateway.insert_token(request.params['account'], token_response_body['token'],
token_response_body['expires_at'])
issued_at = token_response_body["issued_at"].to_time
expires_at = issued_at + token_response_body["expires_in"].seconds
ContainerGateway.insert_token(
request.params['account'],
token_response_body['token'],
expires_at.rfc3339
)

repo_response = ForemanApi.new.fetch_user_repositories(auth_header.raw_header, request.params)
if repo_response.code.to_i != 200
Expand All @@ -120,6 +127,8 @@ class Api < ::Sinatra::Base
ContainerGateway.update_user_repositories(request.params['account'],
JSON.parse(repo_response.body)['repositories'])
end

# Return the original token response from Katello
return token_response_body.to_json
end
end
Expand Down

0 comments on commit f9470b4

Please sign in to comment.