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
Gemfile fixes and cleaning up activesupport imports.

Modified GH Ruby version, small comment tweak.

Rubocop fixes
  • Loading branch information
qcjames53 committed Oct 30, 2023
1 parent dbc3b67 commit 9f823eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.5
ruby-version: 2.7
bundler-cache: true
- name: Run Rubocop
run: bundle exec rubocop
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ group :development do
gem 'rack-test'
gem 'webmock'
gem 'mocha'
gem 'activesupport'
gem 'smart_proxy', :github => "theforeman/smart-proxy", :branch => 'develop'
end

Expand Down
22 changes: 17 additions & 5 deletions lib/smart_proxy_container_gateway/container_gateway_api.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'active_support'
require 'active_support/core_ext/integer'
require 'active_support/core_ext/string'
require 'active_support/time_with_zone'
require 'sinatra'
require 'smart_proxy_container_gateway/container_gateway'
require 'smart_proxy_container_gateway/container_gateway_main'
Expand Down Expand Up @@ -100,18 +104,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 OAuth2 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 +130,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
1 change: 1 addition & 0 deletions smart_proxy_container_gateway.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Gem::Specification.new do |s|
s.required_ruby_version = '~> 2.5'
s.add_dependency 'sequel'
s.add_dependency 'sqlite3'
s.add_dependency 'activesupport'
end

0 comments on commit 9f823eb

Please sign in to comment.