@@ -182,7 +182,7 @@ func Test_listener_detectIncoming(t *testing.T) {
182
182
},
183
183
},
184
184
},
185
- method : "POST" ,
185
+ method : http . MethodPost ,
186
186
queryURL : "/incoming" ,
187
187
queryRepository : "test-good" ,
188
188
querySecret : "verysecrete" ,
@@ -223,7 +223,7 @@ func Test_listener_detectIncoming(t *testing.T) {
223
223
},
224
224
},
225
225
},
226
- method : "POST" ,
226
+ method : http . MethodPost ,
227
227
queryURL : "/incoming" ,
228
228
queryRepository : "test-good" ,
229
229
querySecret : "verysecrete" ,
@@ -235,7 +235,7 @@ func Test_listener_detectIncoming(t *testing.T) {
235
235
{
236
236
name : "invalid incoming body" ,
237
237
args : args {
238
- method : "POST" ,
238
+ method : http . MethodPost ,
239
239
queryURL : "/incoming" ,
240
240
queryRepository : "test-good" ,
241
241
querySecret : "verysecrete" ,
@@ -563,7 +563,7 @@ func Test_listener_detectIncoming(t *testing.T) {
563
563
},
564
564
},
565
565
},
566
- method : "POST" ,
566
+ method : http . MethodPost ,
567
567
queryURL : "/incoming" ,
568
568
queryRepository : "test-good" ,
569
569
querySecret : "verysecrete" ,
@@ -600,7 +600,7 @@ func Test_listener_detectIncoming(t *testing.T) {
600
600
},
601
601
},
602
602
},
603
- method : "POST" ,
603
+ method : http . MethodPost ,
604
604
queryURL : "/incoming" ,
605
605
queryRepository : "test-good" ,
606
606
querySecret : "verysecrete" ,
@@ -641,7 +641,7 @@ func Test_listener_detectIncoming(t *testing.T) {
641
641
},
642
642
},
643
643
},
644
- method : "POST" ,
644
+ method : http . MethodPost ,
645
645
queryURL : "/incoming" ,
646
646
queryRepository : "test-good" ,
647
647
querySecret : "verysecrete" ,
@@ -913,3 +913,159 @@ func TestApplyIncomingParams(t *testing.T) {
913
913
})
914
914
}
915
915
}
916
+
917
+ func Test_detectIncoming_legacy_warning (t * testing.T ) {
918
+ ctx , _ := rtesting .SetupFakeContext (t )
919
+ testNamespace := & corev1.Namespace {
920
+ ObjectMeta : metav1.ObjectMeta {
921
+ Name : "pipelinesascode" ,
922
+ },
923
+ }
924
+ ctx = info .StoreCurrentControllerName (ctx , "default" )
925
+ ctx = info .StoreNS (ctx , testNamespace .GetName ())
926
+ cs , _ := testclient .SeedTestData (t , ctx , testclient.Data {
927
+ Repositories : []* v1alpha1.Repository {
928
+ {
929
+ ObjectMeta : metav1.ObjectMeta {Name : "test-good" },
930
+ Spec : v1alpha1.RepositorySpec {
931
+ URL : "https://matched/by/incoming" ,
932
+ Incomings : & []v1alpha1.Incoming {{
933
+ Targets : []string {"main" },
934
+ Secret : v1alpha1.Secret {Name : "good-secret" },
935
+ Params : []string {"foo" , "bar" },
936
+ }},
937
+ GitProvider : & v1alpha1.GitProvider {Type : "github" },
938
+ },
939
+ },
940
+ },
941
+ })
942
+ client := & params.Run {
943
+ Clients : clients.Clients {
944
+ PipelineAsCode : cs .PipelineAsCode ,
945
+ Kube : cs .Kube ,
946
+ },
947
+ Info : info.Info {
948
+ Controller : & info.ControllerInfo {Secret : info .DefaultPipelinesAscodeSecretName },
949
+ },
950
+ }
951
+ tests := []struct {
952
+ name string
953
+ req * http.Request
954
+ body []byte
955
+ expectWarning bool
956
+ }{
957
+ {
958
+ name : "legacy mode - params in URL" ,
959
+ req : httptest .NewRequest (http .MethodPost ,
960
+ "http://localhost/incoming?repository=test-good&secret=verysecrete&pipelinerun=pipelinerun1&branch=main" ,
961
+ strings .NewReader ("" )),
962
+ body : nil ,
963
+ expectWarning : true ,
964
+ },
965
+ {
966
+ name : "new mode - params in JSON body" ,
967
+ req : func () * http.Request {
968
+ payload := `{
969
+ "repository": "test-good",
970
+ "branch": "main",
971
+ "pipelinerun": "pipelinerun2",
972
+ "secret": "verysecrete",
973
+ "params": {"foo": "bar"}
974
+ }`
975
+ r := httptest .NewRequest (http .MethodPost ,
976
+ "http://localhost/incoming" ,
977
+ strings .NewReader (payload ))
978
+ r .Header .Set ("Content-Type" , "application/json" )
979
+ return r
980
+ }(),
981
+ body : []byte (`{"repository":"test-good","branch":"main","pipelinerun":"pipelinerun2","secret":"verysecrete","params":{"foo":"bar"}}` ),
982
+ expectWarning : false ,
983
+ },
984
+ }
985
+
986
+ for _ , tt := range tests {
987
+ t .Run (tt .name , func (t * testing.T ) {
988
+ observer , observedLogs := zapobserver .New (zap .InfoLevel )
989
+ logger := zap .New (observer ).Sugar ()
990
+ kint := & kubernetestint.KinterfaceTest {GetSecretResult : map [string ]string {"good-secret" : "verysecrete" }}
991
+ l := & listener {
992
+ run : client ,
993
+ logger : logger ,
994
+ kint : kint ,
995
+ event : info .NewEvent (),
996
+ }
997
+ got , _ , err := l .detectIncoming (ctx , tt .req , tt .body )
998
+ assert .NilError (t , err )
999
+ assert .Assert (t , got )
1000
+ found := false
1001
+ for _ , entry := range observedLogs .All () {
1002
+ if strings .Contains (entry .Message , "[SECURITY] Incoming webhook used legacy URL-based secret passing" ) {
1003
+ found = true
1004
+ break
1005
+ }
1006
+ }
1007
+ if tt .expectWarning {
1008
+ assert .Assert (t , found , "expected security warning log for legacy URL-based secret passing" )
1009
+ } else {
1010
+ assert .Assert (t , ! found , "did not expect security warning log for new mode" )
1011
+ }
1012
+ })
1013
+ }
1014
+ }
1015
+
1016
+ func Test_detectIncoming_body_params_are_parsed (t * testing.T ) {
1017
+ ctx , _ := rtesting .SetupFakeContext (t )
1018
+ testNamespace := & corev1.Namespace {
1019
+ ObjectMeta : metav1.ObjectMeta {
1020
+ Name : "pipelinesascode" ,
1021
+ },
1022
+ }
1023
+ ctx = info .StoreCurrentControllerName (ctx , "default" )
1024
+ ctx = info .StoreNS (ctx , testNamespace .GetName ())
1025
+ cs , _ := testclient .SeedTestData (t , ctx , testclient.Data {
1026
+ Repositories : []* v1alpha1.Repository {
1027
+ {
1028
+ ObjectMeta : metav1.ObjectMeta {Name : "test-good" },
1029
+ Spec : v1alpha1.RepositorySpec {
1030
+ URL : "https://matched/by/incoming" ,
1031
+ Incomings : & []v1alpha1.Incoming {{
1032
+ Targets : []string {"main" },
1033
+ Secret : v1alpha1.Secret {Name : "good-secret" },
1034
+ Params : []string {"foo" , "bar" },
1035
+ }},
1036
+ GitProvider : & v1alpha1.GitProvider {Type : "github" },
1037
+ },
1038
+ },
1039
+ },
1040
+ })
1041
+ client := & params.Run {
1042
+ Clients : clients.Clients {
1043
+ PipelineAsCode : cs .PipelineAsCode ,
1044
+ Kube : cs .Kube ,
1045
+ },
1046
+ Info : info.Info {
1047
+ Controller : & info.ControllerInfo {Secret : info .DefaultPipelinesAscodeSecretName },
1048
+ },
1049
+ }
1050
+ payload := `{
1051
+ "repository": "test-good",
1052
+ "branch": "main",
1053
+ "pipelinerun": "pipelinerun2",
1054
+ "secret": "verysecrete",
1055
+ "params": {"foo": "bar", "bar": "baz"}
1056
+ }`
1057
+ req := httptest .NewRequest (http .MethodPost ,
1058
+ "http://localhost/incoming" ,
1059
+ strings .NewReader (payload ))
1060
+ req .Header .Set ("Content-Type" , "application/json" )
1061
+ kint := & kubernetestint.KinterfaceTest {GetSecretResult : map [string ]string {"good-secret" : "verysecrete" }}
1062
+ l := & listener {
1063
+ run : client ,
1064
+ logger : zap .NewNop ().Sugar (),
1065
+ kint : kint ,
1066
+ event : info .NewEvent (),
1067
+ }
1068
+ got , _ , err := l .detectIncoming (ctx , req , []byte (payload ))
1069
+ assert .NilError (t , err )
1070
+ assert .Assert (t , got )
1071
+ }
0 commit comments