summaryrefslogtreecommitdiff
path: root/spec/services/auth/container_registry_authentication_service_spec.rb
blob: 67777ad48bc69c45cd164c99f8297ea9c998d502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
require 'spec_helper'

describe Auth::ContainerRegistryAuthenticationService, services: true do
  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 }

  subject { described_class.new(current_project, current_user, current_params).execute }

  before do
    allow(Gitlab.config.registry).to receive_messages(enabled: true, issuer: 'rspec', key: nil)
    allow_any_instance_of(JSONWebToken::RSAToken).to receive(:key).and_return(rsa_key)
  end

  shared_examples 'a valid token' do
    it { is_expected.to include(:token) }
    it { expect(payload).to include('access') }

    context 'a expirable' do
      let(:expires_at) { Time.at(payload['exp']) }
      let(:expire_delay) { 10 }

      context 'for default configuration' do
        it { expect(expires_at).not_to be_within(2.seconds).of(Time.now + expire_delay.minutes) }
      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
  end

  shared_examples 'a accessible' do
    let(:access) do
      [{
         'type' => 'repository',
         'name' => project.path_with_namespace,
         'actions' => actions,
       }]
    end

    it_behaves_like 'a valid token'
    it { expect(payload).to include('access' => access) }
  end

  shared_examples 'an inaccessible' do
    it_behaves_like 'a valid token'
    it { expect(payload).to include('access' => []) }
  end

  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
      let(:actions) { ['pull', 'push'] }
    end
  end

  shared_examples 'a forbidden' do
    it { is_expected.to include(http_status: 403) }
    it { is_expected.not_to include(:token) }
  end

  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

  context 'user authorization' do
    let(:project) { create(:project) }
    let(:current_user) { create(:user) }

    context 'allow to use scope-less authentication' do
      it_behaves_like 'a valid token'
    end

    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'
    end

    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

      it_behaves_like 'a pullable'
    end

    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'
    end

    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
  end

  context 'project authorization' do
    let(:current_project) { create(:empty_project) }

    context 'allow to use scope-less authentication' do
      it_behaves_like 'a valid token'
    end

    context 'allow to pull and push images' do
      let(:current_params) do
        { scope: "repository:#{current_project.path_with_namespace}:pull,push" }
      end

      it_behaves_like 'a pullable and pushable' do
        let(:project) { current_project }
      end
    end

    context 'for other projects' do
      context 'when pulling' do
        let(:current_params) do
          { scope: "repository:#{project.path_with_namespace}:pull" }
        end

        context 'allow for public' do
          let(:project) { create(:empty_project, :public) }
          it_behaves_like 'a pullable'
        end

        context 'disallow for private' do
          let(:project) { create(:empty_project, :private) }
          it_behaves_like 'an inaccessible'
        end
      end

      context 'when pushing' do
        let(:current_params) do
          { scope: "repository:#{project.path_with_namespace}:push" }
        end

        context 'disallow for all' do
          let(:project) { create(:empty_project, :public) }
          it_behaves_like 'an inaccessible'
        end
      end
    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) }

      context 'disallow when pulling' do
        let(:current_params) do
          { scope: "repository:#{project.path_with_namespace}:pull" }
        end

        it_behaves_like 'an inaccessible'
      end
    end
  end

  context 'unauthorized' do
    context 'disallow to use scope-less authentication' do
      it_behaves_like 'a forbidden'
    end

    context 'for invalid scope' do
      let(:current_params) do
        { scope: 'invalid:aa:bb' }
      end

      it_behaves_like 'a forbidden'
    end

    context 'for private project' do
      let(:project) { create(:empty_project, :private) }

      let(:current_params) do
        { scope: "repository:#{project.path_with_namespace}:pull" }
      end

      it_behaves_like 'a forbidden'
    end

    context 'for public project' do
      let(:project) { create(:empty_project, :public) }

      context 'when pulling and pushing' do
        let(:current_params) do
          { scope: "repository:#{project.path_with_namespace}:pull,push" }
        end

        it_behaves_like 'a pullable'
      end

      context 'when pushing' do
        let(:current_params) do
          { scope: "repository:#{project.path_with_namespace}:push" }
        end

        it_behaves_like 'a forbidden'
      end
    end
  end
end