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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::Internal::Base, feature_category: :system_access do
include GitlabShellHelpers
include APIInternalBaseHelpers
let_it_be(:user, reload: true) { create(:user) }
let_it_be(:project, reload: true) { create(:project, :repository, :wiki_repo) }
let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user) }
let_it_be(:project_snippet) { create(:project_snippet, :repository, author: user, project: project) }
let_it_be(:max_pat_access_token_lifetime) do
PersonalAccessToken::MAX_PERSONAL_ACCESS_TOKEN_LIFETIME_IN_DAYS.days.from_now.to_date.freeze
end
let(:key) { create(:key, user: user) }
let(:secret_token) { Gitlab::Shell.secret_token }
let(:gl_repository) { "project-#{project.id}" }
let(:reference_counter) { double('ReferenceCounter') }
let(:snippet_changes) { "#{TestEnv::BRANCH_SHA['snippet/single-file']} #{TestEnv::BRANCH_SHA['snippet/edit-file']} refs/heads/snippet/edit-file" }
describe "GET /internal/check" do
def perform_request(headers: gitlab_shell_internal_api_request_header)
get api("/internal/check"), headers: headers
end
it do
expect_any_instance_of(Redis).to receive(:ping).and_return('PONG')
perform_request
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['api_version']).to eq(API::API.version)
expect(json_response['redis']).to be(true)
end
it 'returns false for field `redis` when redis is unavailable' do
expect_any_instance_of(Redis).to receive(:ping).and_raise(Errno::ENOENT)
perform_request
expect(json_response['redis']).to be(false)
end
context 'authenticating' do
it 'authenticates using a jwt token in a header' do
perform_request
expect(response).to have_gitlab_http_status(:ok)
end
it 'returns 401 when jwt token is expired' do
headers = gitlab_shell_internal_api_request_header
travel_to(2.minutes.since) do
perform_request(headers: headers)
end
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'returns 401 when jwt issuer is not Gitlab-Shell' do
perform_request(headers: gitlab_shell_internal_api_request_header(issuer: "gitlab-workhorse"))
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'returns 401 when jwt token is not provided, even if plain secret is provided' do
perform_request(headers: { API::Helpers::GITLAB_SHARED_SECRET_HEADER => Base64.encode64(secret_token) })
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
describe 'GET /internal/two_factor_recovery_codes' do
let(:key_id) { key.id }
subject do
post api('/internal/two_factor_recovery_codes'),
params: { key_id: key_id },
headers: gitlab_shell_internal_api_request_header
end
it_behaves_like 'actor key validations'
context 'key is a deploy key' do
let(:key_id) { create(:deploy_key).id }
it 'returns an error message' do
subject
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq('Deploy keys cannot be used to retrieve recovery codes')
end
end
context 'when two-factor is enabled' do
it 'returns new recovery codes when the user exists' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(true)
allow_any_instance_of(User)
.to receive(:generate_otp_backup_codes!).and_return(%w(119135e5a3ebce8e 34bd7b74adbc8861))
subject
expect(json_response['success']).to be_truthy
expect(json_response['recovery_codes']).to match_array(%w(119135e5a3ebce8e 34bd7b74adbc8861))
end
end
context 'when two-factor is not enabled' do
it 'returns an error message' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(false)
subject
expect(json_response['success']).to be_falsey
expect(json_response['recovery_codes']).to be_nil
end
end
end
describe 'POST /internal/personal_access_token' do
let(:key_id) { key.id }
subject do
post api('/internal/personal_access_token'),
params: { key_id: key_id },
headers: gitlab_shell_internal_api_request_header
end
it_behaves_like 'actor key validations'
context 'key is a deploy key' do
let(:key_id) { create(:deploy_key).id }
it 'returns an error message' do
subject
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq('Deploy keys cannot be used to create personal access tokens')
end
end
it 'returns an error message when given an non existent user' do
post api('/internal/personal_access_token'),
params: { user_id: 0 },
headers: gitlab_shell_internal_api_request_header
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq("Could not find the given user")
end
it 'returns an error message when no name parameter is received' do
post api('/internal/personal_access_token'),
params: { key_id: key.id },
headers: gitlab_shell_internal_api_request_header
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq("No token name specified")
end
it 'returns an error message when no scopes parameter is received' do
post api('/internal/personal_access_token'),
params: { key_id: key.id, name: 'newtoken' },
headers: gitlab_shell_internal_api_request_header
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq("No token scopes specified")
end
it 'returns an error message when expires_at contains an invalid date' do
post api('/internal/personal_access_token'),
params: {
key_id: key.id,
name: 'newtoken',
scopes: ['api'],
expires_at: 'invalid-date'
},
headers: gitlab_shell_internal_api_request_header
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq("Invalid token expiry date: 'invalid-date'")
end
it 'returns an error message when it receives an invalid scope' do
post api('/internal/personal_access_token'),
params: {
key_id: key.id,
name: 'newtoken',
scopes: %w(read_api badscope read_repository)
},
headers: gitlab_shell_internal_api_request_header
expect(json_response['success']).to be_falsey
expect(json_response['message']).to match(/\AInvalid scope: 'badscope'. Valid scopes are: /)
end
it 'returns a token with expiry when it receives a valid expires_at parameter' do
freeze_time do
token_size = (PersonalAccessToken.token_prefix || '').size + 20
post api('/internal/personal_access_token'),
params: {
key_id: key.id,
name: 'newtoken',
scopes: %w(read_api read_repository),
expires_at: max_pat_access_token_lifetime
},
headers: gitlab_shell_internal_api_request_header
expect(json_response['success']).to be_truthy
expect(json_response['token']).to match(/\A\S{#{token_size}}\z/)
expect(json_response['scopes']).to match_array(%w(read_api read_repository))
expect(json_response['expires_at']).to eq(max_pat_access_token_lifetime.iso8601)
end
end
it 'returns token with expiry as PersonalAccessToken::MAX_PERSONAL_ACCESS_TOKEN_LIFETIME_IN_DAYS' do
freeze_time do
token_size = (PersonalAccessToken.token_prefix || '').size + 20
post api('/internal/personal_access_token'),
params: {
key_id: key.id,
name: 'newtoken',
scopes: %w(read_api read_repository),
expires_at: 365.days.from_now
},
headers: gitlab_shell_internal_api_request_header
expect(json_response['success']).to be_truthy
expect(json_response['token']).to match(/\A\S{#{token_size}}\z/)
expect(json_response['scopes']).to match_array(%w(read_api read_repository))
expect(json_response['expires_at']).to eq(max_pat_access_token_lifetime.iso8601)
end
end
end
describe "POST /internal/lfs_authenticate" do
before do
stub_lfs_setting(enabled: true)
project.add_developer(user)
end
context 'user key' do
it 'returns the correct information about the key' do
lfs_auth_key(key.id, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['username']).to eq(user.username)
expect(json_response['repository_http_path']).to eq(project.http_url_to_repo)
expect(json_response['expires_in']).to eq(Gitlab::LfsToken::DEFAULT_EXPIRE_TIME)
expect(Gitlab::LfsToken.new(key).token_valid?(json_response['lfs_token'])).to be_truthy
end
it 'returns the correct information about the user' do
lfs_auth_user(user.id, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['username']).to eq(user.username)
expect(json_response['repository_http_path']).to eq(project.http_url_to_repo)
expect(Gitlab::LfsToken.new(user).token_valid?(json_response['lfs_token'])).to be_truthy
end
it 'returns a 404 when no key or user is provided' do
lfs_auth_project(project)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns a 404 when the wrong key is provided' do
lfs_auth_key(non_existing_record_id, project)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns a 404 when the wrong user is provided' do
lfs_auth_user(non_existing_record_id, project)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns a 404 when LFS is disabled on the project' do
project.update!(lfs_enabled: false)
lfs_auth_user(user.id, project)
expect(response).to have_gitlab_http_status(:not_found)
end
context 'other repository types' do
it 'returns the correct information for a project wiki' do
wiki = create(:project_wiki, project: project)
lfs_auth_user(user.id, wiki)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['username']).to eq(user.username)
expect(json_response['repository_http_path']).to eq(wiki.http_url_to_repo)
expect(json_response['expires_in']).to eq(Gitlab::LfsToken::DEFAULT_EXPIRE_TIME)
expect(Gitlab::LfsToken.new(user).token_valid?(json_response['lfs_token'])).to be_truthy
end
it 'returns a 404 when the container does not support LFS' do
snippet = create(:project_snippet)
lfs_auth_user(user.id, snippet)
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'deploy key' do
let(:key) { create(:deploy_key) }
it 'returns the correct information about the key' do
lfs_auth_key(key.id, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['username']).to eq("lfs+deploy-key-#{key.id}")
expect(json_response['repository_http_path']).to eq(project.http_url_to_repo)
expect(Gitlab::LfsToken.new(key).token_valid?(json_response['lfs_token'])).to be_truthy
end
end
end
describe "GET /internal/discover" do
it "finds a user by key id" do
get(api("/internal/discover"), params: { key_id: key.id }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq(user.name)
end
context 'when signing key is passed' do
it 'does not authenticate user' do
key.signing!
get(api("/internal/discover"), params: { key_id: key.id }, headers: gitlab_shell_internal_api_request_header)
expect(json_response).to be_nil
end
end
context 'when auth-only key is passed' do
it 'authenticates user' do
key.auth!
get(api("/internal/discover"), params: { key_id: key.id }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq(user.name)
end
end
it "finds a user by username" do
get(api("/internal/discover"), params: { username: user.username }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['name']).to eq(user.name)
end
it 'responds successfully when a user is not found' do
get(api('/internal/discover'), params: { username: 'noone' }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to eq('null')
end
it 'response successfully when passing invalid params' do
get(api('/internal/discover'), params: { nothing: 'to find a user' }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to eq('null')
end
end
describe "GET /internal/authorized_keys" do
context "using an existing key" do
it "finds the key" do
get(api('/internal/authorized_keys'), params: { key: key.key.split[1] }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['id']).to eq(key.id)
expect(json_response['key'].split[1]).to eq(key.key.split[1])
end
context 'when signing key is passed' do
it 'does not return the key' do
key.signing!
get(api('/internal/authorized_keys'), params: { key: key.key.split[1] }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['id']).to be_nil
end
end
context 'when auth-only key is passed' do
it 'authenticates user' do
key.auth!
get(api('/internal/authorized_keys'), params: { key: key.key.split[1] }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['id']).to eq(key.id)
expect(json_response['key'].split[1]).to eq(key.key.split[1])
end
end
it 'exposes the comment of the key as a simple identifier of username + hostname' do
get(api('/internal/authorized_keys'), params: { key: key.key.split[1] }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['key']).to include("#{key.user_name} (#{Gitlab.config.gitlab.host})")
end
end
it "returns 404 with a partial key" do
get(api('/internal/authorized_keys'), params: { key: key.key.split[1][0...-3] }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:not_found)
end
it "returns 404 with an not valid base64 string" do
get(api('/internal/authorized_keys'), params: { key: "whatever!" }, headers: gitlab_shell_internal_api_request_header)
expect(response).to have_gitlab_http_status(:not_found)
end
end
describe "POST /internal/allowed", :clean_gitlab_redis_shared_state, :clean_gitlab_redis_rate_limiting do
shared_examples 'rate limited request' do
let(:action) { 'git-upload-pack' }
let(:actor) { key }
let(:rate_limiter) { double(:rate_limiter, ip: "127.0.0.1", trusted_ip?: false) }
before do
allow(::Gitlab::Auth::IpRateLimiter).to receive(:new).with("127.0.0.1").and_return(rate_limiter)
end
it 'is throttled by rate limiter' do
allow(::Gitlab::ApplicationRateLimiter).to receive(:threshold).and_return(1)
expect(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).with(:gitlab_shell_operation, scope: [action, project.full_path, actor]).twice.and_call_original
expect(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).with(:gitlab_shell_operation, scope: [action, project.full_path, "127.0.0.1"]).and_call_original
request
expect(response).to have_gitlab_http_status(:ok)
request
expect(response).to have_gitlab_http_status(:too_many_requests)
expect(json_response['message']['error']).to eq('This endpoint has been requested too many times. Try again later.')
end
it 'is not throttled by rate limiter' do
expect(::Gitlab::ApplicationRateLimiter).not_to receive(:throttled?)
subject
end
context 'when the IP is in a trusted range' do
let(:rate_limiter) { double(:rate_limiter, ip: "127.0.0.1", trusted_ip?: true) }
it 'is not throttled by rate limiter' do
expect(::Gitlab::ApplicationRateLimiter).not_to receive(:throttled?)
subject
end
end
end
context "access granted" do
let(:env) { {} }
around do |example|
freeze_time { example.run }
end
before do
project.add_developer(user)
end
shared_context 'with env passed as a JSON' do
let(:obj_dir_relative) { './objects' }
let(:alt_obj_dirs_relative) { ['./alt-objects-1', './alt-objects-2'] }
let(:env) do
{
GIT_OBJECT_DIRECTORY_RELATIVE: obj_dir_relative,
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE: alt_obj_dirs_relative
}
end
end
shared_examples 'sets hook env' do
include_context 'with env passed as a JSON'
it 'sets env in RequestStore' do
expect(Gitlab::Git::HookEnv).to receive(:set).with(gl_repository, env.stringify_keys)
subject
expect(response).to have_gitlab_http_status(:ok)
end
end
shared_examples 'sets hook env and routes to primary' do
include_context 'with env passed as a JSON'
let(:interceptor) do
Class.new(::GRPC::ClientInterceptor) do
def route_to_primary_received?
@route_to_primary_count.to_i > 0
end
def request_response(request:, call:, method:, metadata:) # rubocop:disable Lint/UnusedMethodArgument
@route_to_primary_count ||= 0
@route_to_primary_count += 1 if metadata['gitaly-route-repository-accessor-policy'] == 'primary-only'
yield
end
end.new
end
before do
Gitlab::GitalyClient.clear_stubs!
allow(::Gitlab::GitalyClient).to receive(:interceptors).and_return([interceptor])
end
after do
Gitlab::GitalyClient.clear_stubs!
end
it 'sets env in RequestStore and routes gRPC messages to primary', :request_store do
expect(Gitlab::Git::HookEnv).to receive(:set).with(gl_repository, env.stringify_keys).and_call_original
subject
expect(response).to have_gitlab_http_status(:ok)
expect(interceptor.route_to_primary_received?).to be_truthy
end
end
context "git push with project.wiki" do
subject { push(key, project.wiki, env: env.to_json) }
it 'responds with success' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(project.wiki.full_path)
expect(json_response["gl_repository"]).to eq("wiki-#{project.id}")
expect(json_response["gl_key_type"]).to eq("key")
expect(json_response["gl_key_id"]).to eq(key.id)
expect(user.reload.last_activity_on).to eql(Date.today)
end
# Wiki repositories don't invoke any Gitaly RPCs to check for changes, so we can only test for the
# hook environment being set.
it_behaves_like 'sets hook env' do
let(:gl_repository) { Gitlab::GlRepository::WIKI.identifier_for_container(project.wiki) }
end
end
context "git pull with project.wiki" do
it 'responds with success' do
pull(key, project.wiki)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(project.wiki.full_path)
expect(json_response["gl_repository"]).to eq("wiki-#{project.id}")
expect(user.reload.last_activity_on).to eql(Date.today)
end
end
shared_examples 'snippet success' do
it 'responds with success' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to be_truthy
end
end
context 'git push with personal snippet' do
subject { push(key, personal_snippet, env: env.to_json, changes: snippet_changes) }
it 'responds with success' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(personal_snippet.repository.full_path)
expect(json_response["gl_repository"]).to eq("snippet-#{personal_snippet.id}")
expect(user.reload.last_activity_on).to eql(Date.today)
end
it_behaves_like 'sets hook env and routes to primary' do
let(:gl_repository) { Gitlab::GlRepository::SNIPPET.identifier_for_container(personal_snippet) }
end
end
context 'git pull with personal snippet' do
subject { pull(key, personal_snippet) }
it 'responds with success' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(personal_snippet.repository.full_path)
expect(json_response["gl_repository"]).to eq("snippet-#{personal_snippet.id}")
expect(user.reload.last_activity_on).to eql(Date.today)
end
end
context 'git push with project snippet' do
subject { push(key, project_snippet, env: env.to_json, changes: snippet_changes) }
it 'responds with success' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(project_snippet.repository.full_path)
expect(json_response["gl_repository"]).to eq("snippet-#{project_snippet.id}")
expect(user.reload.last_activity_on).to eql(Date.today)
end
it_behaves_like 'sets hook env and routes to primary' do
let(:gl_repository) { Gitlab::GlRepository::SNIPPET.identifier_for_container(project_snippet) }
end
end
context 'git pull with project snippet' do
it 'responds with success' do
pull(key, project_snippet)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_project_path"]).to eq(project_snippet.repository.full_path)
expect(json_response["gl_repository"]).to eq("snippet-#{project_snippet.id}")
expect(user.reload.last_activity_on).to eql(Date.today)
end
end
context "git pull" do
context "with a feature flag enabled globally" do
before do
stub_feature_flags(gitaly_mep_mep: true)
end
it "has the correct payload" do
pull(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_repository"]).to eq("project-#{project.id}")
expect(json_response["gl_project_path"]).to eq(project.full_path)
expect(json_response["gitaly"]).not_to be_nil
expect(json_response["gitaly"]["repository"]).not_to be_nil
expect(json_response["gitaly"]["repository"]["storage_name"]).to eq(project.repository.gitaly_repository.storage_name)
expect(json_response["gitaly"]["repository"]["relative_path"]).to eq(project.repository.gitaly_repository.relative_path)
expect(json_response["gitaly"]["address"]).to eq(Gitlab::GitalyClient.address(project.repository_storage))
expect(json_response["gitaly"]["token"]).to eq(Gitlab::GitalyClient.token(project.repository_storage))
expect(json_response["gitaly"]["features"]).to eq('gitaly-feature-mep-mep' => 'true')
expect(user.reload.last_activity_on).to eql(Date.today)
end
it_behaves_like 'rate limited request' do
def request
pull(key, project)
end
end
context 'when user_id is passed' do
it_behaves_like 'rate limited request' do
let(:actor) { user }
def request
post(
api("/internal/allowed"),
params: {
user_id: user.id,
project: full_path_for(project),
gl_repository: gl_repository_for(project),
action: 'git-upload-pack',
protocol: 'ssh'
},
headers: gitlab_shell_internal_api_request_header
)
end
it "updates user's activity data" do
expect(::Users::ActivityService).to receive(:new).with(author: user, namespace: project.namespace, project: project)
request
end
end
end
end
context "with a feature flag enabled for a project" do
before do
stub_feature_flags(gitaly_mep_mep: project)
end
it "has the flag set to true for that project" do
pull(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["gl_repository"]).to eq("project-#{project.id}")
expect(json_response["gitaly"]["features"]).to eq('gitaly-feature-mep-mep' => 'true')
end
it "has the flag set to false for other projects" do
other_project = create(:project, :public, :repository)
pull(key, other_project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["gl_repository"]).to eq("project-#{other_project.id}")
expect(json_response["gitaly"]["features"]).to eq('gitaly-feature-mep-mep' => 'false')
end
end
end
context "git push" do
context 'project as namespace/project' do
it do
push(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gl_repository"]).to eq("project-#{project.id}")
expect(json_response["gl_project_path"]).to eq(project.full_path)
expect(json_response["gl_key_type"]).to eq("key")
expect(json_response["gl_key_id"]).to eq(key.id)
expect(json_response["gitaly"]).not_to be_nil
expect(json_response["gitaly"]["repository"]).not_to be_nil
expect(json_response["gitaly"]["repository"]["storage_name"]).to eq(project.repository.gitaly_repository.storage_name)
expect(json_response["gitaly"]["repository"]["relative_path"]).to eq(project.repository.gitaly_repository.relative_path)
expect(json_response["gitaly"]["address"]).to eq(Gitlab::GitalyClient.address(project.repository_storage))
expect(json_response["gitaly"]["token"]).to eq(Gitlab::GitalyClient.token(project.repository_storage))
expect(user.reload.last_activity_on).to eql(Date.today)
end
it_behaves_like 'rate limited request' do
let(:action) { 'git-receive-pack' }
def request
push(key, project)
end
end
end
context 'when receive_max_input_size has been updated' do
before do
allow(Gitlab::CurrentSettings).to receive(:receive_max_input_size) { 1 }
end
it 'returns maxInputSize and partial clone git config' do
push(key, project)
expect(json_response["git_config_options"]).to be_present
expect(json_response["git_config_options"]).to include("receive.maxInputSize=1048576")
expect(json_response["git_config_options"]).to include("uploadpack.allowFilter=true")
expect(json_response["git_config_options"]).to include("uploadpack.allowAnySHA1InWant=true")
end
end
context 'when receive_max_input_size is empty' do
before do
allow(Gitlab::CurrentSettings).to receive(:receive_max_input_size) { nil }
end
it 'returns partial clone git config' do
push(key, project)
expect(json_response["git_config_options"]).to be_present
expect(json_response["git_config_options"]).to include("uploadpack.allowFilter=true")
expect(json_response["git_config_options"]).to include("uploadpack.allowAnySHA1InWant=true")
end
end
end
context 'with Project' do
it_behaves_like 'storing arguments in the application context for the API' do
let(:expected_params) { { user: key.user.username, project: project.full_path, caller_id: "POST /api/:version/internal/allowed" } }
subject { push(key, project) }
end
end
context 'with PersonalSnippet' do
it_behaves_like 'storing arguments in the application context for the API' do
let(:expected_params) { { user: key.user.username, caller_id: "POST /api/:version/internal/allowed" } }
subject { push(key, personal_snippet) }
end
end
context 'with ProjectSnippet' do
it_behaves_like 'storing arguments in the application context for the API' do
let(:expected_params) { { user: key.user.username, project: project_snippet.project.full_path, caller_id: "POST /api/:version/internal/allowed" } }
subject { push(key, project_snippet) }
end
end
end
context "access denied" do
before do
project.add_guest(user)
end
context "git pull" do
it do
pull(key, project)
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response["status"]).to be_falsey
expect(user.reload.last_activity_on).to be_nil
end
end
context "git push" do
it do
push(key, project)
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response["status"]).to be_falsey
expect(user.reload.last_activity_on).to be_nil
end
end
end
context 'with a pending membership' do
let_it_be(:project) { create(:project, :repository) }
before_all do
create(:project_member, :awaiting, :developer, source: project, user: user)
end
it 'returns not found for git pull' do
pull(key, project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
expect(user.reload.last_activity_on).to be_nil
end
it 'returns not found for git push' do
push(key, project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
expect(user.reload.last_activity_on).to be_nil
end
end
context "custom action" do
let(:access_checker) { double(Gitlab::GitAccess) }
let(:payload) do
{
'action' => 'geo_proxy_to_primary',
'data' => {
'api_endpoints' => %w{geo/proxy_git_ssh/info_refs_receive_pack geo/proxy_git_ssh/receive_pack},
'gl_username' => 'testuser',
'primary_repo' => 'http://localhost:3000/testuser/repo.git'
}
}
end
let(:console_messages) { ['informational message'] }
let(:custom_action_result) { Gitlab::GitAccessResult::CustomAction.new(payload, console_messages) }
before do
project.add_guest(user)
expect(Gitlab::GitAccess).to receive(:new).with(
key,
project,
'ssh',
{
authentication_abilities: [:read_project, :download_code, :push_code],
repository_path: "#{project.full_path}.git",
redirected_path: nil
}
).and_return(access_checker)
expect(access_checker).to receive(:check).with(
'git-receive-pack',
'd14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/master'
).and_return(custom_action_result)
end
context "git push" do
it do
push(key, project)
expect(response).to have_gitlab_http_status(:multiple_choices)
expect(json_response['status']).to be_truthy
expect(json_response['payload']).to eql(payload)
expect(json_response['gl_console_messages']).to eql(console_messages)
expect(user.reload.last_activity_on).to eql(Date.today)
end
end
end
context "console message" do
before do
project.add_developer(user)
end
context 'git pull' do
context 'with a key that has expired' do
let(:key) { create(:key, :expired, user: user) }
it 'includes the `key expired` message in the response and fails' do
pull(key, project)
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response['message']).to eq('Your SSH key has expired.')
end
end
context 'with a key that will expire in the next 7 days' do
let(:key) { create(:key, user: user, expires_at: 2.days.from_now) }
it 'includes the `key expiring soon` message in the response' do
pull(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['gl_console_messages']).to eq(['INFO: Your SSH key is expiring soon. Please generate a new key.'])
end
end
context 'with a key that has no expiry' do
it 'does not include any message in the response' do
pull(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['gl_console_messages']).to eq([])
end
end
end
end
context "blocked user" do
let(:personal_project) { create(:project, namespace: user.namespace) }
before do
user.block
end
context "git pull" do
it do
pull(key, personal_project)
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response["status"]).to be_falsey
expect(user.reload.last_activity_on).to be_nil
end
end
context "git push" do
it do
push(key, personal_project)
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response["status"]).to be_falsey
expect(user.reload.last_activity_on).to be_nil
end
end
end
context 'request times out' do
context 'git push' do
it 'responds with a gateway timeout' do
personal_project = create(:project, namespace: user.namespace)
expect_next_instance_of(Gitlab::GitAccess) do |access|
expect(access).to receive(:check).and_raise(Gitlab::GitAccess::TimeoutError, "Foo")
end
push(key, personal_project)
expect(response).to have_gitlab_http_status(:service_unavailable)
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq("Foo")
expect(user.reload.last_activity_on).to be_nil
end
end
end
context "archived project" do
before do
project.add_developer(user)
::Projects::UpdateService.new(project, user, archived: true).execute
end
context "git pull" do
it do
pull(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
end
end
context "git push" do
it do
push(key, project)
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response["status"]).to be_falsey
end
end
end
context "deploy key" do
let(:key) { create(:deploy_key) }
context "added to project" do
before do
key.projects << project
end
it do
archive(key, project)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response["status"]).to be_truthy
expect(json_response["gitaly"]).not_to be_nil
expect(json_response["gl_key_type"]).to eq("deploy_key")
expect(json_response["gl_key_id"]).to eq(key.id)
expect(json_response["gitaly"]["repository"]).not_to be_nil
expect(json_response["gitaly"]["repository"]["storage_name"]).to eq(project.repository.gitaly_repository.storage_name)
expect(json_response["gitaly"]["repository"]["relative_path"]).to eq(project.repository.gitaly_repository.relative_path)
expect(json_response["gitaly"]["address"]).to eq(Gitlab::GitalyClient.address(project.repository_storage))
expect(json_response["gitaly"]["token"]).to eq(Gitlab::GitalyClient.token(project.repository_storage))
end
it_behaves_like 'rate limited request' do
let(:action) { 'git-upload-archive' }
def request
archive(key, project)
end
end
end
context "not added to project" do
it do
archive(key, project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
end
end
end
context 'project does not exist' do
context 'git pull' do
it 'returns a 200 response with status: false' do
project.destroy!
pull(key, project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
end
it 'returns a 200 response when using a project path that does not exist' do
post(
api("/internal/allowed"),
params: {
key_id: key.id,
project: 'project/does-not-exist.git',
action: 'git-upload-pack',
protocol: 'ssh'
},
headers: gitlab_shell_internal_api_request_header
)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
end
end
context 'git push' do
before do
allow(Gitlab::QueryLimiting::Transaction).to receive(:threshold).and_return(120)
end
subject { push_with_path(key, full_path: path, changes: '_any') }
context 'from a user/group namespace' do
let!(:path) { "#{user.namespace.path}/notexist.git" }
it 'creates the project' do
expect do
subject
end.to change { Project.count }.by(1)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to be_truthy
end
end
context 'from the personal snippet path' do
let!(:path) { 'snippets/notexist.git' }
it 'does not create snippet' do
expect do
subject
end.not_to change { Snippet.count }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'from a project path' do
context 'from an non existent project path' do
let!(:path) { "#{user.namespace.path}/notexist/snippets/notexist.git" }
it 'does not create project' do
expect do
subject
end.not_to change { Project.count }
expect(response).to have_gitlab_http_status(:not_found)
end
it 'does not create snippet' do
expect do
subject
end.not_to change { Snippet.count }
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'from an existent project path' do
let!(:path) { "#{project.full_path}/notexist/snippets/notexist.git" }
it 'does not create snippet' do
expect do
subject
end.not_to change { Snippet.count }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
end
context 'user does not exist' do
it do
pull(double('key', id: 0), project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response["status"]).to be_falsey
end
end
context 'ssh access has been disabled' do
before do
stub_application_setting(enabled_git_access_protocol: 'http')
end
it 'rejects the SSH push' do
push(key, project)
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq 'Git access over SSH is not allowed'
end
it 'rejects the SSH pull' do
pull(key, project)
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq 'Git access over SSH is not allowed'
end
end
context 'http access has been disabled' do
before do
stub_application_setting(enabled_git_access_protocol: 'ssh')
end
it 'rejects the HTTP push' do
push(key, project, 'http')
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq 'Git access over HTTP is not allowed'
end
it 'rejects the HTTP pull' do
pull(key, project, 'http')
expect(response).to have_gitlab_http_status(:unauthorized)
expect(json_response['status']).to be_falsey
expect(json_response['message']).to eq 'Git access over HTTP is not allowed'
end
end
context 'web actions are always allowed' do
it 'allows WEB push' do
stub_application_setting(enabled_git_access_protocol: 'ssh')
project.add_developer(user)
push(key, project, 'web')
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['status']).to be_truthy
end
end
context 'the project path was changed' do
let(:project) { create(:project, :repository, :legacy_storage) }
let!(:repository) { project.repository }
before do
project.add_developer(user)
project.path = 'new_path'
project.save!
end
it 'rejects the push' do
push(key, project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['status']).to be_falsy
end
it 'rejects the SSH pull' do
pull(key, project)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['status']).to be_falsy
end
end
context 'for design repositories' do
let(:gl_repository) { Gitlab::GlRepository::DESIGN.identifier_for_container(project) }
it 'does not allow access' do
post(api('/internal/allowed'),
params: {
key_id: key.id,
project: project.full_path,
gl_repository: gl_repository,
protocol: 'ssh'
}, headers: gitlab_shell_internal_api_request_header
)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'admin mode' do
shared_examples 'pushes succeed for ssh and http' do
it 'accepts the SSH push' do
push(key, project)
expect(response).to have_gitlab_http_status(:ok)
end
it 'accepts the HTTP push' do
push(key, project, 'http')
expect(response).to have_gitlab_http_status(:ok)
end
end
shared_examples 'pushes fail for ssh and http' do
it 'rejects the SSH push' do
push(key, project)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'rejects the HTTP push' do
push(key, project, 'http')
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'application setting :admin_mode is enabled' do
context 'with an admin user' do
let(:user) { create(:admin) }
context 'is member of the project' do
before do
project.add_developer(user)
end
it_behaves_like 'pushes succeed for ssh and http'
end
context 'is not member of the project' do
it_behaves_like 'pushes succeed for ssh and http'
end
end
context 'with a regular user' do
context 'is member of the project' do
before do
project.add_developer(user)
end
it_behaves_like 'pushes succeed for ssh and http'
end
context 'is not member of the project' do
it_behaves_like 'pushes fail for ssh and http'
end
end
end
context 'application setting :admin_mode is disabled' do
before do
stub_application_setting(admin_mode: false)
end
context 'with an admin user' do
let(:user) { create(:admin) }
context 'is member of the project' do
before do
project.add_developer(user)
end
it_behaves_like 'pushes succeed for ssh and http'
end
context 'is not member of the project' do
it_behaves_like 'pushes succeed for ssh and http'
end
end
context 'with a regular user' do
context 'is member of the project' do
before do
project.add_developer(user)
end
it_behaves_like 'pushes succeed for ssh and http'
end
context 'is not member of the project' do
it_behaves_like 'pushes fail for ssh and http'
end
end
end
end
end
describe 'POST /internal/post_receive', :clean_gitlab_redis_shared_state do
let(:identifier) { 'key-123' }
let(:branch_name) { 'feature' }
let(:push_options) { ['ci.skip', 'another push option'] }
let(:valid_params) do
{
gl_repository: gl_repository,
identifier: identifier,
changes: changes,
push_options: push_options
}
end
let(:changes) do
"#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{branch_name}"
end
subject { post api('/internal/post_receive'), params: valid_params, headers: gitlab_shell_internal_api_request_header }
before do
project.add_developer(user)
allow_any_instance_of(Gitlab::Identifier).to receive(:identify).and_return(user)
end
shared_examples 'runs post-receive hooks' do
let(:gl_repository) { container.repository.gl_repository }
let(:messages) { [] }
it 'executes PostReceiveService' do
subject
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq({
'messages' => messages,
'reference_counter_decreased' => true
})
end
it 'tries to notify that the container has moved' do
expect(Gitlab::Checks::ContainerMoved).to receive(:fetch_message).with(user, container.repository)
subject
end
it_behaves_like 'storing arguments in the application context for the API' do
let(:expected_params) { expected_context }
end
end
context 'with Project' do
it_behaves_like 'runs post-receive hooks' do
let(:container) { project }
let(:expected_context) { { user: user.username, project: project.full_path } }
let(:messages) do
[
{
'message' => <<~MESSAGE.strip,
To create a merge request for #{branch_name}, visit:
http://#{Gitlab.config.gitlab.host}/#{project.full_path}/-/merge_requests/new?merge_request%5Bsource_branch%5D=#{branch_name}
MESSAGE
'type' => 'basic'
}
]
end
end
end
context 'with PersonalSnippet' do
it_behaves_like 'runs post-receive hooks' do
let(:container) { personal_snippet }
let(:expected_context) { { user: key.user.username } }
end
end
context 'with ProjectSnippet' do
it_behaves_like 'runs post-receive hooks' do
let(:container) { project_snippet }
let(:expected_context) { { user: key.user.username, project: project_snippet.project.full_path } }
end
end
context 'with ProjectWiki' do
it_behaves_like 'runs post-receive hooks' do
let(:container) { project.wiki }
let(:expected_context) { { user: key.user.username, project: project.full_path } }
end
end
context 'with an orphaned write deploy key' do
it 'does not try to notify that project moved' do
allow_any_instance_of(Gitlab::Identifier).to receive(:identify).and_return(nil)
expect(Gitlab::Checks::ContainerMoved).not_to receive(:fetch_message)
subject
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'when container is nil' do
let(:gl_repository) { 'project-foo' }
it 'does not try to notify that project moved' do
allow(Gitlab::GlRepository).to receive(:parse).and_return([nil, nil, Gitlab::GlRepository::PROJECT])
expect(Gitlab::Checks::ContainerMoved).not_to receive(:fetch_message)
subject
expect(response).to have_gitlab_http_status(:ok)
end
end
end
describe 'POST /internal/pre_receive' do
let(:valid_params) do
{ gl_repository: gl_repository }
end
it 'decreases the reference counter and returns the result' do
expect(Gitlab::ReferenceCounter).to receive(:new).with(gl_repository)
.and_return(reference_counter)
expect(reference_counter).to receive(:increase).and_return(true)
post api("/internal/pre_receive"), params: valid_params, headers: gitlab_shell_internal_api_request_header
expect(json_response['reference_counter_increased']).to be(true)
end
end
describe 'POST /internal/two_factor_config' do
let(:key_id) { key.id }
before do
stub_feature_flags(two_factor_for_cli: true)
end
subject do
post api('/internal/two_factor_config'),
params: { key_id: key_id },
headers: gitlab_shell_internal_api_request_header
end
it_behaves_like 'actor key validations'
context 'when the key is a deploy key' do
let(:key) { create(:deploy_key) }
it 'does not required two factor' do
subject
expect(json_response['success']).to be_truthy
expect(json_response['two_factor_required']).to be_falsey
end
end
context 'when two-factor is enabled' do
it 'returns user two factor config' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(true)
subject
expect(json_response['success']).to be_truthy
expect(json_response['two_factor_required']).to be_truthy
end
end
context 'when two-factor is not enabled' do
it 'returns an error message' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(false)
subject
expect(json_response['success']).to be_truthy
expect(json_response['two_factor_required']).to be_falsey
end
end
context 'two_factor_for_cli feature is disabled' do
before do
stub_feature_flags(two_factor_for_cli: false)
end
context 'when two-factor is enabled for the user' do
it 'returns user two factor config' do
allow_any_instance_of(User).to receive(:two_factor_enabled?).and_return(true)
subject
expect(json_response['success']).to be_falsey
end
end
end
end
describe 'POST /internal/two_factor_manual_otp_check' do
let(:key_id) { key.id }
let(:otp) { '123456' }
subject do
post api('/internal/two_factor_manual_otp_check'),
params: {
secret_token: secret_token,
key_id: key_id,
otp_attempt: otp
},
headers: gitlab_shell_internal_api_request_header
end
it 'is not available' do
subject
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq 'Feature is not available'
end
end
describe 'POST /internal/two_factor_push_otp_check' do
let(:key_id) { key.id }
let(:otp) { '123456' }
subject do
post api('/internal/two_factor_push_otp_check'),
params: {
secret_token: secret_token,
key_id: key_id,
otp_attempt: otp
},
headers: gitlab_shell_internal_api_request_header
end
it 'is not available' do
subject
expect(json_response['success']).to be_falsey
expect(json_response['message']).to eq 'Feature is not available'
end
end
describe 'POST /internal/two_factor_manual_otp_check' do
let(:key_id) { key.id }
let(:otp) { '123456' }
subject do
post api('/internal/two_factor_manual_otp_check'),
params: {
secret_token: secret_token,
key_id: key_id,
otp_attempt: otp
},
headers: gitlab_shell_internal_api_request_header
end
it 'is not available' do
subject
expect(json_response['success']).to be_falsey
end
end
describe 'POST /internal/two_factor_push_otp_check' do
let(:key_id) { key.id }
let(:otp) { '123456' }
subject do
post api('/internal/two_factor_push_otp_check'),
params: {
secret_token: secret_token,
key_id: key_id,
otp_attempt: otp
},
headers: gitlab_shell_internal_api_request_header
end
it 'is not available' do
subject
expect(json_response['success']).to be_falsey
end
end
def lfs_auth_project(project)
post(
api("/internal/lfs_authenticate"),
params: { project: project.full_path },
headers: gitlab_shell_internal_api_request_header
)
end
def lfs_auth_key(key_id, project)
post(
api("/internal/lfs_authenticate"),
params: { key_id: key_id, project: project.full_path },
headers: gitlab_shell_internal_api_request_header
)
end
def lfs_auth_user(user_id, project)
post(
api("/internal/lfs_authenticate"),
params: { user_id: user_id, project: project.full_path },
headers: gitlab_shell_internal_api_request_header
)
end
end
|