@@ -345,7 +345,7 @@ func Test_CreateCluster(t *testing.T) {
345345 t .Run ("Returns error when CA secret is missing" , func (t * testing.T ) {
346346 kubeclient := kube .NewFakeClientsetWithResources ()
347347
348- err := CreateCluster (context .Background (), kubeclient , testNamespace , "test-agent" , testResourceProxyAddr )
348+ err := CreateCluster (context .Background (), kubeclient , testNamespace , "test-agent" , testResourceProxyAddr , "" )
349349
350350 require .Error (t , err )
351351 require .Contains (t , err .Error (), "could not generate client certificate" )
@@ -356,7 +356,7 @@ func Test_CreateCluster(t *testing.T) {
356356 createTestCASecret (t , kubeclient , testNamespace )
357357
358358 agentName := "test-agent"
359- err := CreateCluster (context .Background (), kubeclient , testNamespace , agentName , testResourceProxyAddr )
359+ err := CreateCluster (context .Background (), kubeclient , testNamespace , agentName , testResourceProxyAddr , "" )
360360
361361 require .NoError (t , err )
362362
@@ -371,4 +371,124 @@ func Test_CreateCluster(t *testing.T) {
371371 require .Equal (t , agentName , secret .Labels [LabelKeyClusterAgentMapping ])
372372 require .Equal (t , "true" , secret .Labels [LabelKeySelfRegisteredCluster ])
373373 })
374+
375+ t .Run ("Creates cluster secret with shared client cert" , func (t * testing.T ) {
376+ kubeclient := kube .NewFakeClientsetWithResources ()
377+
378+ // Create the shared client cert secret
379+ sharedSecret := & corev1.Secret {
380+ ObjectMeta : metav1.ObjectMeta {
381+ Name : "shared-client-cert" ,
382+ Namespace : testNamespace ,
383+ },
384+ Data : map [string ][]byte {
385+ "tls.crt" : []byte ("shared-cert-data" ),
386+ "tls.key" : []byte ("shared-key-data" ),
387+ "ca.crt" : []byte ("shared-ca-data" ),
388+ },
389+ }
390+ _ , err := kubeclient .CoreV1 ().Secrets (testNamespace ).Create (context .Background (), sharedSecret , metav1.CreateOptions {})
391+ require .NoError (t , err )
392+
393+ agentName := "test-agent-shared"
394+ err = CreateCluster (context .Background (), kubeclient , testNamespace , agentName , testResourceProxyAddr , "shared-client-cert" )
395+
396+ require .NoError (t , err )
397+
398+ // Verify the cluster secret was created with shared cert data
399+ secret , err := kubeclient .CoreV1 ().Secrets (testNamespace ).Get (
400+ context .Background (),
401+ getClusterSecretName (agentName ),
402+ metav1.GetOptions {},
403+ )
404+ require .NoError (t , err )
405+ require .NotNil (t , secret )
406+
407+ // Verify the cluster secret contains the shared cert data (base64 encoded in JSON)
408+ clusterData , ok := secret .Data ["config" ]
409+ require .True (t , ok )
410+ require .Contains (t , string (clusterData ), "c2hhcmVkLWNlcnQtZGF0YQ==" )
411+ require .Contains (t , string (clusterData ), "c2hhcmVkLWtleS1kYXRh" )
412+ require .Contains (t , string (clusterData ), "c2hhcmVkLWNhLWRhdGE=" )
413+ })
414+
415+ t .Run ("Returns error when shared client cert secret does not exist" , func (t * testing.T ) {
416+ kubeclient := kube .NewFakeClientsetWithResources ()
417+
418+ err := CreateCluster (context .Background (), kubeclient , testNamespace , "test-agent" , testResourceProxyAddr , "non-existent-secret" )
419+
420+ require .Error (t , err )
421+ require .Contains (t , err .Error (), "could not read shared client certificate from secret" )
422+ })
423+ }
424+
425+ func Test_CreateClusterWithSharedCert (t * testing.T ) {
426+ const testNamespace = "argocd"
427+ const testResourceProxyAddr = "resource-proxy:8443"
428+
429+ t .Run ("Returns error when shared secret is missing tls.crt" , func (t * testing.T ) {
430+ kubeclient := kube .NewFakeClientsetWithResources ()
431+
432+ secret := & corev1.Secret {
433+ ObjectMeta : metav1.ObjectMeta {
434+ Name : "missing-cert" ,
435+ Namespace : testNamespace ,
436+ },
437+ Data : map [string ][]byte {
438+ "tls.key" : []byte ("key-data" ),
439+ "ca.crt" : []byte ("ca-data" ),
440+ },
441+ }
442+ _ , err := kubeclient .CoreV1 ().Secrets (testNamespace ).Create (context .Background (), secret , metav1.CreateOptions {})
443+ require .NoError (t , err )
444+
445+ err = CreateCluster (context .Background (), kubeclient , testNamespace , "test-agent" , testResourceProxyAddr , "missing-cert" )
446+
447+ require .Error (t , err )
448+ require .Contains (t , err .Error (), "missing tls.crt" )
449+ })
450+
451+ t .Run ("Returns error when shared secret is missing tls.key" , func (t * testing.T ) {
452+ kubeclient := kube .NewFakeClientsetWithResources ()
453+
454+ secret := & corev1.Secret {
455+ ObjectMeta : metav1.ObjectMeta {
456+ Name : "missing-key" ,
457+ Namespace : testNamespace ,
458+ },
459+ Data : map [string ][]byte {
460+ "tls.crt" : []byte ("cert-data" ),
461+ "ca.crt" : []byte ("ca-data" ),
462+ },
463+ }
464+ _ , err := kubeclient .CoreV1 ().Secrets (testNamespace ).Create (context .Background (), secret , metav1.CreateOptions {})
465+ require .NoError (t , err )
466+
467+ err = CreateCluster (context .Background (), kubeclient , testNamespace , "test-agent" , testResourceProxyAddr , "missing-key" )
468+
469+ require .Error (t , err )
470+ require .Contains (t , err .Error (), "missing tls.key" )
471+ })
472+
473+ t .Run ("Returns error when shared secret is missing ca.crt" , func (t * testing.T ) {
474+ kubeclient := kube .NewFakeClientsetWithResources ()
475+
476+ secret := & corev1.Secret {
477+ ObjectMeta : metav1.ObjectMeta {
478+ Name : "missing-ca" ,
479+ Namespace : testNamespace ,
480+ },
481+ Data : map [string ][]byte {
482+ "tls.crt" : []byte ("cert-data" ),
483+ "tls.key" : []byte ("key-data" ),
484+ },
485+ }
486+ _ , err := kubeclient .CoreV1 ().Secrets (testNamespace ).Create (context .Background (), secret , metav1.CreateOptions {})
487+ require .NoError (t , err )
488+
489+ err = CreateCluster (context .Background (), kubeclient , testNamespace , "test-agent" , testResourceProxyAddr , "missing-ca" )
490+
491+ require .Error (t , err )
492+ require .Contains (t , err .Error (), "missing ca.crt" )
493+ })
374494}
0 commit comments