2016-05-09 15:06:32 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-05-14 12:15:19 -04:00
|
|
|
describe Auth::ContainerRegistryAuthenticationService, services: true do
|
2016-05-09 15:06:32 -04:00
|
|
|
let(:current_project) { nil }
|
|
|
|
let(:current_user) { nil }
|
|
|
|
let(:current_params) { {} }
|
|
|
|
let(:rsa_key) { OpenSSL::PKey::RSA.generate(512) }
|
|
|
|
let(:payload) { JWT.decode(subject[:token], rsa_key).first }
|
2016-09-16 03:59:10 -04:00
|
|
|
let(:authentication_abilities) do
|
2016-09-15 07:49:11 -04:00
|
|
|
[
|
|
|
|
:read_container_image,
|
|
|
|
:create_container_image
|
|
|
|
]
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-09-16 03:59:10 -04:00
|
|
|
subject { described_class.new(current_project, current_user, current_params).execute(authentication_abilities: authentication_abilities) }
|
2016-05-09 15:06:32 -04:00
|
|
|
|
|
|
|
before do
|
2016-05-20 19:43:11 -04:00
|
|
|
allow(Gitlab.config.registry).to receive_messages(enabled: true, issuer: 'rspec', key: nil)
|
2016-05-14 19:23:31 -04:00
|
|
|
allow_any_instance_of(JSONWebToken::RSAToken).to receive(:key).and_return(rsa_key)
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
|
2016-05-30 10:57:39 -04:00
|
|
|
shared_examples 'a valid token' do
|
2016-05-09 15:06:32 -04:00
|
|
|
it { is_expected.to include(:token) }
|
|
|
|
it { expect(payload).to include('access') }
|
2016-05-31 07:48:21 -04:00
|
|
|
|
|
|
|
context 'a expirable' do
|
|
|
|
let(:expires_at) { Time.at(payload['exp']) }
|
|
|
|
let(:expire_delay) { 10 }
|
|
|
|
|
|
|
|
context 'for default configuration' do
|
2016-05-31 09:38:42 -04:00
|
|
|
it { expect(expires_at).not_to be_within(2.seconds).of(Time.now + expire_delay.minutes) }
|
2016-05-31 07:48:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for changed configuration' do
|
|
|
|
before { stub_application_setting(container_registry_token_expire_delay: expire_delay) }
|
|
|
|
|
|
|
|
it { expect(expires_at).to be_within(2.seconds).of(Time.now + expire_delay.minutes) }
|
|
|
|
end
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a accessible' do
|
2016-05-12 13:47:55 -04:00
|
|
|
let(:access) do
|
2016-05-09 15:06:32 -04:00
|
|
|
[{
|
|
|
|
'type' => 'repository',
|
|
|
|
'name' => project.path_with_namespace,
|
|
|
|
'actions' => actions,
|
|
|
|
}]
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-05-30 10:57:39 -04:00
|
|
|
it_behaves_like 'a valid token'
|
2016-05-09 15:06:32 -04:00
|
|
|
it { expect(payload).to include('access' => access) }
|
|
|
|
end
|
|
|
|
|
2016-05-30 10:57:39 -04:00
|
|
|
shared_examples 'an inaccessible' do
|
|
|
|
it_behaves_like 'a valid token'
|
|
|
|
it { expect(payload).to include('access' => []) }
|
|
|
|
end
|
|
|
|
|
2016-05-09 15:06:32 -04:00
|
|
|
shared_examples 'a pullable' do
|
|
|
|
it_behaves_like 'a accessible' do
|
|
|
|
let(:actions) { ['pull'] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a pushable' do
|
|
|
|
it_behaves_like 'a accessible' do
|
|
|
|
let(:actions) { ['push'] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a pullable and pushable' do
|
|
|
|
it_behaves_like 'a accessible' do
|
2017-02-22 12:46:57 -05:00
|
|
|
let(:actions) { %w(pull push) }
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-15 09:52:26 -04:00
|
|
|
shared_examples 'a forbidden' do
|
|
|
|
it { is_expected.to include(http_status: 403) }
|
2016-05-23 19:37:59 -04:00
|
|
|
it { is_expected.not_to include(:token) }
|
2016-05-15 09:52:26 -04:00
|
|
|
end
|
|
|
|
|
2016-05-20 19:43:11 -04:00
|
|
|
describe '#full_access_token' do
|
|
|
|
let(:project) { create(:empty_project) }
|
|
|
|
let(:token) { described_class.full_access_token(project.path_with_namespace) }
|
|
|
|
|
|
|
|
subject { { token: token } }
|
|
|
|
|
|
|
|
it_behaves_like 'a accessible' do
|
|
|
|
let(:actions) { ['*'] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-09 15:06:32 -04:00
|
|
|
context 'user authorization' do
|
|
|
|
let(:current_user) { create(:user) }
|
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'for private project' do
|
|
|
|
let(:project) { create(:empty_project) }
|
2016-05-15 09:52:26 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'allow to use scope-less authentication' do
|
|
|
|
it_behaves_like 'a valid token'
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'allow developer to push images' do
|
|
|
|
before { project.team << [current_user, :developer] }
|
|
|
|
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:push" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a pushable'
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'allow reporter to pull images' do
|
|
|
|
before { project.team << [current_user, :reporter] }
|
|
|
|
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull" }
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
it_behaves_like 'a pullable'
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'return a least of privileges' do
|
|
|
|
before { project.team << [current_user, :reporter] }
|
|
|
|
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:push,pull" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a pullable'
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'disallow guest to pull or push images' do
|
|
|
|
before { project.team << [current_user, :guest] }
|
|
|
|
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull,push" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an inaccessible'
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'for public project' do
|
|
|
|
let(:project) { create(:empty_project, :public) }
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'allow anyone to pull images' do
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a pullable'
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'disallow anyone to push images' do
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:push" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an inaccessible'
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'for internal project' do
|
|
|
|
let(:project) { create(:empty_project, :internal) }
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'for internal user' do
|
|
|
|
context 'allow anyone to pull images' do
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a pullable'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'disallow anyone to push images' do
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:push" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an inaccessible'
|
|
|
|
end
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-07-15 11:05:41 -04:00
|
|
|
context 'for external user' do
|
|
|
|
let(:current_user) { create(:user, external: true) }
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull,push" }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an inaccessible'
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-15 10:36:39 -04:00
|
|
|
context 'build authorized as user' do
|
2016-05-09 15:06:32 -04:00
|
|
|
let(:current_project) { create(:empty_project) }
|
2016-09-15 10:36:39 -04:00
|
|
|
let(:current_user) { create(:user) }
|
2016-09-16 03:59:10 -04:00
|
|
|
let(:authentication_abilities) do
|
2016-09-15 09:40:53 -04:00
|
|
|
[
|
|
|
|
:build_read_container_image,
|
|
|
|
:build_create_container_image
|
|
|
|
]
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-09-15 10:36:39 -04:00
|
|
|
before do
|
|
|
|
current_project.team << [current_user, :developer]
|
2016-05-15 09:52:26 -04:00
|
|
|
end
|
|
|
|
|
2016-09-15 10:36:39 -04:00
|
|
|
it_behaves_like 'a valid token'
|
|
|
|
|
2016-05-09 15:06:32 -04:00
|
|
|
context 'allow to pull and push images' do
|
2016-05-12 13:47:55 -04:00
|
|
|
let(:current_params) do
|
2016-05-09 15:06:32 -04:00
|
|
|
{ scope: "repository:#{current_project.path_with_namespace}:pull,push" }
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
|
|
|
it_behaves_like 'a pullable and pushable' do
|
|
|
|
let(:project) { current_project }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for other projects' do
|
|
|
|
context 'when pulling' do
|
2016-05-12 13:47:55 -04:00
|
|
|
let(:current_params) do
|
2016-05-09 15:06:32 -04:00
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull" }
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
|
|
|
context 'allow for public' do
|
|
|
|
let(:project) { create(:empty_project, :public) }
|
2016-09-15 10:36:39 -04:00
|
|
|
|
2016-05-09 15:06:32 -04:00
|
|
|
it_behaves_like 'a pullable'
|
|
|
|
end
|
|
|
|
|
2016-09-15 10:36:39 -04:00
|
|
|
shared_examples 'pullable for being team member' do
|
|
|
|
context 'when you are not member' do
|
|
|
|
it_behaves_like 'an inaccessible'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when you are member' do
|
|
|
|
before do
|
|
|
|
project.team << [current_user, :developer]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a pullable'
|
|
|
|
end
|
2016-10-17 11:23:51 -04:00
|
|
|
|
|
|
|
context 'when you are owner' do
|
|
|
|
let(:project) { create(:empty_project, namespace: current_user.namespace) }
|
|
|
|
|
|
|
|
it_behaves_like 'a pullable'
|
|
|
|
end
|
2016-09-15 10:36:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for private' do
|
2016-05-09 15:06:32 -04:00
|
|
|
let(:project) { create(:empty_project, :private) }
|
2016-09-15 10:36:39 -04:00
|
|
|
|
|
|
|
it_behaves_like 'pullable for being team member'
|
|
|
|
|
|
|
|
context 'when you are admin' do
|
|
|
|
let(:current_user) { create(:admin) }
|
|
|
|
|
2016-09-16 03:59:10 -04:00
|
|
|
context 'when you are not member' do
|
|
|
|
it_behaves_like 'an inaccessible'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when you are member' do
|
|
|
|
before do
|
|
|
|
project.team << [current_user, :developer]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a pullable'
|
|
|
|
end
|
2016-10-17 11:23:51 -04:00
|
|
|
|
|
|
|
context 'when you are owner' do
|
|
|
|
let(:project) { create(:empty_project, namespace: current_user.namespace) }
|
|
|
|
|
|
|
|
it_behaves_like 'a pullable'
|
|
|
|
end
|
2016-09-15 10:36:39 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pushing' do
|
2016-05-12 13:47:55 -04:00
|
|
|
let(:current_params) do
|
2016-05-09 15:06:32 -04:00
|
|
|
{ scope: "repository:#{project.path_with_namespace}:push" }
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
|
|
|
context 'disallow for all' do
|
2016-10-17 11:23:51 -04:00
|
|
|
context 'when you are member' do
|
|
|
|
let(:project) { create(:empty_project, :public) }
|
2016-09-15 10:36:39 -04:00
|
|
|
|
2016-10-17 11:23:51 -04:00
|
|
|
before do
|
|
|
|
project.team << [current_user, :developer]
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'an inaccessible'
|
2016-09-15 10:36:39 -04:00
|
|
|
end
|
|
|
|
|
2016-10-17 11:23:51 -04:00
|
|
|
context 'when you are owner' do
|
|
|
|
let(:project) { create(:empty_project, :public, namespace: current_user.namespace) }
|
|
|
|
|
|
|
|
it_behaves_like 'an inaccessible'
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
end
|
2016-05-13 13:20:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for project without container registry' do
|
|
|
|
let(:project) { create(:empty_project, :public, container_registry_enabled: false) }
|
|
|
|
|
|
|
|
before { project.update(container_registry_enabled: false) }
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-05-13 13:20:23 -04:00
|
|
|
context 'disallow when pulling' do
|
|
|
|
let(:current_params) do
|
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull" }
|
|
|
|
end
|
|
|
|
|
2016-05-30 10:57:39 -04:00
|
|
|
it_behaves_like 'an inaccessible'
|
2016-05-13 13:20:23 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized' do
|
2016-05-30 10:57:39 -04:00
|
|
|
context 'disallow to use scope-less authentication' do
|
|
|
|
it_behaves_like 'a forbidden'
|
2016-05-15 09:52:26 -04:00
|
|
|
end
|
|
|
|
|
2016-05-09 15:06:32 -04:00
|
|
|
context 'for invalid scope' do
|
2016-05-12 13:47:55 -04:00
|
|
|
let(:current_params) do
|
2016-05-09 15:06:32 -04:00
|
|
|
{ scope: 'invalid:aa:bb' }
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-05-15 11:46:54 -04:00
|
|
|
it_behaves_like 'a forbidden'
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for private project' do
|
|
|
|
let(:project) { create(:empty_project, :private) }
|
|
|
|
|
2016-05-12 13:47:55 -04:00
|
|
|
let(:current_params) do
|
2016-05-09 15:06:32 -04:00
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull" }
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-05-15 11:46:54 -04:00
|
|
|
it_behaves_like 'a forbidden'
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for public project' do
|
|
|
|
let(:project) { create(:empty_project, :public) }
|
|
|
|
|
|
|
|
context 'when pulling and pushing' do
|
2016-05-12 13:47:55 -04:00
|
|
|
let(:current_params) do
|
2016-05-09 15:06:32 -04:00
|
|
|
{ scope: "repository:#{project.path_with_namespace}:pull,push" }
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
|
|
|
it_behaves_like 'a pullable'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pushing' do
|
2016-05-12 13:47:55 -04:00
|
|
|
let(:current_params) do
|
2016-05-09 15:06:32 -04:00
|
|
|
{ scope: "repository:#{project.path_with_namespace}:push" }
|
2016-05-12 13:47:55 -04:00
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2016-05-15 11:46:54 -04:00
|
|
|
it_behaves_like 'a forbidden'
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|