@@ -847,3 +847,199 @@ func TestTerminalState(t *testing.T) {
847847
848848 assert .Equal (t , expected , actual , " Terminal state error is not equal" )
849849}
850+
851+ func TestValidateWIPCredentialConfig (t * testing.T ) {
852+ tests := []struct {
853+ name string
854+ config * drivers.GCPWIPCredential
855+ expectError bool
856+ errorMsg string
857+ }{
858+ {
859+ name : "Valid_AllFieldsPresent" ,
860+ config : & drivers.GCPWIPCredential {
861+ Type : "external_account" ,
862+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
863+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
864+ TokenURL : "https://sts.googleapis.com/v1/token" ,
865+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
866+ CredentialSource : & drivers.GCPWIPCredentialSource {
867+ File : "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
868+ },
869+ },
870+ expectError : false ,
871+ },
872+ {
873+ name : "Valid_WithOptionalFields" ,
874+ config : & drivers.GCPWIPCredential {
875+ UniverseDomain : "googleapis.com" ,
876+ Type : "external_account" ,
877+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
878+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
879+ TokenURL : "https://sts.googleapis.com/v1/token" ,
880+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
881+ CredentialSource : & drivers.GCPWIPCredentialSource {
882+ File : "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
883+ },
884+ QuotaProjectID : "quota-project" ,
885+ },
886+ expectError : false ,
887+ },
888+ {
889+ name : "Error_MissingType" ,
890+ config : & drivers.GCPWIPCredential {
891+ Type : "" ,
892+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
893+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
894+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
895+ TokenURL : "https://sts.googleapis.com/v1/token" ,
896+ CredentialSource : & drivers.GCPWIPCredentialSource {
897+ File : "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
898+ },
899+ },
900+ expectError : true ,
901+ errorMsg : "type" ,
902+ },
903+ {
904+ name : "Error_MissingAudience" ,
905+ config : & drivers.GCPWIPCredential {
906+ Type : "external_account" ,
907+ Audience : "" ,
908+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
909+ TokenURL : "https://sts.googleapis.com/v1/token" ,
910+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
911+ CredentialSource : & drivers.GCPWIPCredentialSource {
912+ File : "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
913+ },
914+ },
915+ expectError : true ,
916+ errorMsg : "audience" ,
917+ },
918+ {
919+ name : "Error_MissingSubjectTokenType" ,
920+ config : & drivers.GCPWIPCredential {
921+ Type : "external_account" ,
922+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
923+ SubjectTokenType : "" ,
924+ TokenURL : "https://sts.googleapis.com/v1/token" ,
925+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
926+ CredentialSource : & drivers.GCPWIPCredentialSource {
927+ File : "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
928+ },
929+ },
930+ expectError : true ,
931+ errorMsg : "subject_token_type" ,
932+ },
933+ {
934+ name : "Error_MissingTokenURL" ,
935+ config : & drivers.GCPWIPCredential {
936+ Type : "external_account" ,
937+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
938+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
939+ TokenURL : "" ,
940+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
941+ CredentialSource : & drivers.GCPWIPCredentialSource {
942+ File : "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
943+ },
944+ },
945+ expectError : true ,
946+ errorMsg : "token_url" ,
947+ },
948+ {
949+ name : "Error_NilCredentialSource" ,
950+ config : & drivers.GCPWIPCredential {
951+ Type : "external_account" ,
952+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
953+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
954+ TokenURL : "https://sts.googleapis.com/v1/token" ,
955+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
956+ CredentialSource : nil ,
957+ },
958+ expectError : true ,
959+ errorMsg : "credential_source" ,
960+ },
961+ {
962+ name : "Error_MissingCredentialSourceFile" ,
963+ config : & drivers.GCPWIPCredential {
964+ Type : "external_account" ,
965+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
966+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
967+ TokenURL : "https://sts.googleapis.com/v1/token" ,
968+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
969+ CredentialSource : & drivers.GCPWIPCredentialSource {
970+ File : "" ,
971+ },
972+ },
973+ expectError : true ,
974+ errorMsg : "credential_source.file" ,
975+ },
976+ {
977+ name : "Error_MultipleFieldsMissing" ,
978+ config : & drivers.GCPWIPCredential {
979+ Type : "" ,
980+ Audience : "" ,
981+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
982+ TokenURL : "" ,
983+ CredentialSource : nil ,
984+ ServiceAccountImpersonationURL : "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/sa@project.iam.gserviceaccount.com:generateAccessToken" ,
985+ },
986+ expectError : true ,
987+ errorMsg : "type, audience, token_url, credential_source" ,
988+ },
989+ {
990+ name : "Error_AllFieldsMissing" ,
991+ config : & drivers.GCPWIPCredential {
992+ Type : "" ,
993+ Audience : "" ,
994+ SubjectTokenType : "" ,
995+ TokenURL : "" ,
996+ CredentialSource : nil ,
997+ },
998+ expectError : true ,
999+ errorMsg : "type, audience, subject_token_type, token_url, service_account_impersonation_url, credential_source" ,
1000+ },
1001+ {
1002+ name : "Error_OnlyCredentialSourceFileMissing" ,
1003+ config : & drivers.GCPWIPCredential {
1004+ Type : "external_account" ,
1005+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
1006+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
1007+ TokenURL : "https://sts.googleapis.com/v1/token" ,
1008+ CredentialSource : & drivers.GCPWIPCredentialSource {
1009+ File : "" ,
1010+ },
1011+ },
1012+ expectError : true ,
1013+ },
1014+ {
1015+ name : "Error_MissingServiceAccountImpersonationURL" ,
1016+ config : & drivers.GCPWIPCredential {
1017+ UniverseDomain : "googleapis.com" ,
1018+ Type : "external_account" ,
1019+ Audience : "//iam.googleapis.com/projects/123/locations/global/workloadIdentityPools/pool/providers/provider" ,
1020+ SubjectTokenType : "urn:ietf:params:oauth:token-type:jwt" ,
1021+ TokenURL : "https://sts.googleapis.com/v1/token" ,
1022+ CredentialSource : & drivers.GCPWIPCredentialSource {
1023+ File : "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
1024+ },
1025+ QuotaProjectID : "quota-project" ,
1026+ },
1027+ expectError : true ,
1028+ errorMsg : "service_account_impersonation_url" ,
1029+ },
1030+ }
1031+
1032+ for _ , tt := range tests {
1033+ t .Run (tt .name , func (t * testing.T ) {
1034+ err := validateWIPCredentialConfig (tt .config )
1035+
1036+ if tt .expectError {
1037+ assert .Error (t , err , "Expected an error but got none" )
1038+ assert .Contains (t , err .Error (), "missing required WIP credential fields" )
1039+ assert .Contains (t , err .Error (), tt .errorMsg , "Error message should contain expected missing field(s)" )
1040+ } else {
1041+ assert .NoError (t , err , "Expected no error but got: %v" , err )
1042+ }
1043+ })
1044+ }
1045+ }
0 commit comments