@@ -658,56 +658,74 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
658658 assert .Equal (t , VenafiConnection , got .OutputMode )
659659 })
660660
661- const arkUsername = "cluster-1-region-1-cloud-1@cyberark.cloud.123456"
662-
663661 t .Run ("--machine-hub selects MachineHub mode" , func (t * testing.T ) {
664662 t .Setenv ("POD_NAMESPACE" , "venafi" )
665663 t .Setenv ("KUBECONFIG" , withFile (t , fakeKubeconfig ))
666664 t .Setenv ("ARK_SUBDOMAIN" , "tlspk" )
667- t .Setenv ("ARK_USERNAME" , arkUsername )
668- t .Setenv ("ARK_SECRET" , "test-secret" )
669665 got , cl , err := ValidateAndCombineConfig (discardLogs (),
670- withConfig ("" ),
666+ withConfig (testutil .Undent (`
667+ cluster_name: my-cluster
668+ cyberark:
669+ service_id: dev-cluster
670+ ` )),
671+ withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
672+ require .NoError (t , err )
673+ assert .Equal (t , MachineHub , got .OutputMode )
674+ assert .Equal (t , "my-cluster" , got .ClusterName )
675+ assert .Equal (t , "dev-cluster" , got .CyberArk .ServiceID )
676+ assert .IsType (t , & client.CyberArkClient {}, cl )
677+ })
678+
679+ t .Run ("--machine-hub with cluster_id fallback when cluster_name is empty" , func (t * testing.T ) {
680+ t .Setenv ("POD_NAMESPACE" , "venafi" )
681+ t .Setenv ("KUBECONFIG" , withFile (t , fakeKubeconfig ))
682+ t .Setenv ("ARK_SUBDOMAIN" , "tlspk" )
683+ got , cl , err := ValidateAndCombineConfig (discardLogs (),
684+ withConfig (testutil .Undent (`
685+ cluster_id: my-cluster-id
686+ cyberark:
687+ service_id: dev-cluster
688+ ` )),
671689 withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
672690 require .NoError (t , err )
673691 assert .Equal (t , MachineHub , got .OutputMode )
674- assert .Equal (t , arkUsername , got .ClusterName ,
675- "the ClusterName should default to the ARK_USERNAME value if the cluster_name in the config file is empty" )
692+ assert .Equal (t , "my-cluster-id" , got .ClusterName ,
693+ "cluster_id should be used as cluster_name when cluster_name is empty" )
676694 assert .IsType (t , & client.CyberArkClient {}, cl )
677695 })
678696
679697 t .Run ("--machine-hub with cluster_name override" , func (t * testing.T ) {
680698 t .Setenv ("POD_NAMESPACE" , "venafi" )
681699 t .Setenv ("KUBECONFIG" , withFile (t , fakeKubeconfig ))
682700 t .Setenv ("ARK_SUBDOMAIN" , "tlspk" )
683- t .Setenv ("ARK_USERNAME" , arkUsername )
684- t .Setenv ("ARK_SECRET" , "test-secret" )
685701 got , cl , err := ValidateAndCombineConfig (discardLogs (),
686702 withConfig (testutil .Undent (`
687703 cluster_name: override-cluster-name
688- ` )),
704+ cyberark:
705+ service_id: dev-cluster
706+ ` )),
689707 withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
690708 require .NoError (t , err )
691709 assert .Equal (t , MachineHub , got .OutputMode )
692- assert .Equal (t , "override-cluster-name" , got .ClusterName ,
693- "the cluster_name in the config file should be used if not empty, even if ARK_USERNAME is set" )
710+ assert .Equal (t , "override-cluster-name" , got .ClusterName )
694711 assert .IsType (t , & client.CyberArkClient {}, cl )
695712 })
696713
697- t .Run ("--machine-hub without required environment variables " , func (t * testing.T ) {
714+ t .Run ("--machine-hub without ARK_SUBDOMAIN environment variable " , func (t * testing.T ) {
698715 t .Setenv ("POD_NAMESPACE" , "venafi" )
699716 t .Setenv ("KUBECONFIG" , withFile (t , fakeKubeconfig ))
700717 t .Setenv ("ARK_SUBDOMAIN" , "" )
701- t .Setenv ("ARK_USERNAME" , "" )
702- t .Setenv ("ARK_SECRET" , "" )
703718 got , cl , err := ValidateAndCombineConfig (discardLogs (),
704- withConfig ("" ),
719+ withConfig (testutil .Undent (`
720+ cyberark:
721+ service_id: dev-cluster
722+ ` )),
705723 withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
706724 assert .Equal (t , CombinedConfig {}, got )
707725 assert .Nil (t , cl )
708726 assert .EqualError (t , err , testutil .Undent (`
709727 validating creds: failed loading config using the MachineHub mode: 1 error occurred:
710- * missing environment variables: ARK_SUBDOMAIN, ARK_USERNAME, ARK_SECRET
728+ * missing environment variables: ARK_SUBDOMAIN
711729
712730 ` ))
713731 })
@@ -1303,6 +1321,119 @@ func Test_ValidateAndCombineConfig_NGTS(t *testing.T) {
13031321 })
13041322}
13051323
1324+ func TestConfig_CyberArk_Validation (t * testing.T ) {
1325+ // Common env setup: ARK_SUBDOMAIN is the only required env var for MachineHub mode.
1326+ setEnv := func (t * testing.T ) {
1327+ t .Helper ()
1328+ t .Setenv ("POD_NAMESPACE" , "venafi" )
1329+ t .Setenv ("KUBECONFIG" , withFile (t , fakeKubeconfig ))
1330+ t .Setenv ("ARK_SUBDOMAIN" , "tlspk" )
1331+ }
1332+
1333+ // service_id is no longer required at config-validation time: the agent
1334+ // also supports the legacy username/password method (ARK_USERNAME/ARK_SECRET,
1335+ // set via env, not config), and cyberark.selectAuthenticator fails closed at
1336+ // runtime (ErrNoAuthMethod) if neither method ends up configured. See the
1337+ // comment on this validation block in config.go.
1338+ t .Run ("empty service_id is valid at config time" , func (t * testing.T ) {
1339+ setEnv (t )
1340+ combined , _ , err := ValidateAndCombineConfig (discardLogs (),
1341+ withConfig (testutil .Undent (`
1342+ cyberark:
1343+ service_id: ""
1344+ ` )),
1345+ withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
1346+ require .NoError (t , err )
1347+ assert .Equal (t , "" , combined .CyberArk .ServiceID )
1348+ })
1349+
1350+ t .Run ("missing cyberark block is valid at config time" , func (t * testing.T ) {
1351+ setEnv (t )
1352+ combined , _ , err := ValidateAndCombineConfig (discardLogs (),
1353+ withConfig ("" ),
1354+ withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
1355+ require .NoError (t , err )
1356+ assert .Equal (t , "" , combined .CyberArk .ServiceID )
1357+ })
1358+
1359+ t .Run ("jwt_source spiffe is rejected" , func (t * testing.T ) {
1360+ setEnv (t )
1361+ _ , _ , err := ValidateAndCombineConfig (discardLogs (),
1362+ withConfig (testutil .Undent (`
1363+ cyberark:
1364+ service_id: dev-cluster
1365+ jwt_source: spiffe
1366+ ` )),
1367+ withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
1368+ require .Error (t , err )
1369+ assert .Contains (t , err .Error (), `cyberark.jwt_source "spiffe" is not supported` )
1370+ })
1371+
1372+ t .Run ("jwt_source file is accepted" , func (t * testing.T ) {
1373+ setEnv (t )
1374+ got , cl , err := ValidateAndCombineConfig (discardLogs (),
1375+ withConfig (testutil .Undent (`
1376+ cyberark:
1377+ service_id: dev-cluster
1378+ jwt_source: file
1379+ jwt_file_path: /var/run/secrets/tokens/agent-token
1380+ ` )),
1381+ withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
1382+ require .NoError (t , err )
1383+ assert .Equal (t , "dev-cluster" , got .CyberArk .ServiceID )
1384+ assert .Equal (t , "file" , got .CyberArk .JWTSource )
1385+ assert .Equal (t , "/var/run/secrets/tokens/agent-token" , got .CyberArk .JWTFilePath )
1386+ assert .IsType (t , & client.CyberArkClient {}, cl )
1387+ })
1388+
1389+ t .Run ("jwt_source empty string is accepted" , func (t * testing.T ) {
1390+ setEnv (t )
1391+ got , cl , err := ValidateAndCombineConfig (discardLogs (),
1392+ withConfig (testutil .Undent (`
1393+ cyberark:
1394+ service_id: dev-cluster
1395+ ` )),
1396+ withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
1397+ require .NoError (t , err )
1398+ assert .Equal (t , "dev-cluster" , got .CyberArk .ServiceID )
1399+ assert .Equal (t , "" , got .CyberArk .JWTSource )
1400+ assert .IsType (t , & client.CyberArkClient {}, cl )
1401+ })
1402+
1403+ t .Run ("account and jwt_file_path are optional" , func (t * testing.T ) {
1404+ setEnv (t )
1405+ got , _ , err := ValidateAndCombineConfig (discardLogs (),
1406+ withConfig (testutil .Undent (`
1407+ cyberark:
1408+ service_id: dev-cluster
1409+ account: myaccount
1410+ jwt_file_path: /tmp/token
1411+ ` )),
1412+ withCmdLineFlags ("--period" , "1m" , "--machine-hub" ))
1413+ require .NoError (t , err )
1414+ assert .Equal (t , "myaccount" , got .CyberArk .Account )
1415+ assert .Equal (t , "/tmp/token" , got .CyberArk .JWTFilePath )
1416+ })
1417+
1418+ t .Run ("cyberark block is ignored in non-MachineHub modes" , func (t * testing.T ) {
1419+ t .Setenv ("POD_NAMESPACE" , "venafi" )
1420+ fakeCredsPath := withFile (t , `{"user_id":"foo","user_secret":"bar","client_id": "baz","client_secret": "foobar","auth_server_domain":"bazbar"}` )
1421+ got , _ , err := ValidateAndCombineConfig (discardLogs (),
1422+ withConfig (testutil .Undent (`
1423+ server: https://preflight.jetstack.io
1424+ organization_id: my-org
1425+ cluster_id: my-cluster
1426+ period: 1h
1427+ cyberark:
1428+ service_id: should-be-ignored
1429+ ` )),
1430+ withCmdLineFlags ("--credentials-file" , fakeCredsPath ))
1431+ require .NoError (t , err )
1432+ // CyberArk config is not copied into CombinedConfig for non-MachineHub modes.
1433+ assert .Equal (t , CyberArkConfig {}, got .CyberArk )
1434+ })
1435+ }
1436+
13061437const fakePrivKeyPEM = `-----BEGIN PRIVATE KEY-----
13071438MHcCAQEEIFptpPXOvEWDrYkiMhyEH1+FB1GwtwX2tyXH4KtBO6g7oAoGCCqGSM49
13081439AwEHoUQDQgAE/BsIwagYc4YUjSSFyqcStj2qliAkdVGlMoJbMuXupzQ9Qs4TX5Pl
0 commit comments