@@ -23,10 +23,14 @@ import (
2323 "encoding/base64"
2424 "fmt"
2525 "net/http"
26+ "os/exec"
27+ "path/filepath"
28+ "runtime"
2629 "time"
2730
2831 . "github.com/onsi/ginkgo/v2"
2932 . "github.com/onsi/gomega"
33+ "github.com/stretchr/testify/assert"
3034 "k8s.io/apimachinery/pkg/types"
3135
3236 apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
@@ -585,4 +589,90 @@ spec:
585589 Expect (err ).ShouldNot (HaveOccurred (), "deleting Secret" )
586590 })
587591 })
592+ Context ("Test LDAPAuth" , func () {
593+ getLDAPServerURL := func () (string , error ) {
594+ // Get current file's directory using runtime.Caller
595+ _ , filename , _ , _ := runtime .Caller (0 )
596+ currentDir := filepath .Dir (filename )
597+ path := filepath .Join (currentDir , ".." , ".." , "testdata" , "ldap" , "cmd.sh" )
598+
599+ cmd := exec .Command ("sh" , path , "ip" )
600+ ip , err := cmd .Output ()
601+ if err != nil {
602+ return "" , fmt .Errorf ("%s failed: %v" , path , err )
603+ }
604+ if len (ip ) == 0 {
605+ return "" , fmt .Errorf ("ldap-server start failed" )
606+ }
607+ return fmt .Sprintf ("%s:1389" , string (ip )), nil
608+ }
609+ request := func (path string , username , password string ) int {
610+ return s .NewAPISIXClient ().GET (path ).WithBasicAuth (username , password ).WithHost ("httpbin" ).Expect ().Raw ().StatusCode
611+ }
612+ It ("ApisixRoute with ldapAuth consumer using secret" , func () {
613+ secret := `
614+ apiVersion: v1
615+ kind: Secret
616+ metadata:
617+ name: ldap
618+ data:
619+ user_dn: Y249amFjayxvdT11c2VycyxkYz1sZGFwLGRjPWV4YW1wbGUsZGM9b3Jn
620+ `
621+ assert .Nil (GinkgoT (), s .CreateResourceFromString (secret ), "creating ldapAuth secret for ApisixConsumer" )
622+
623+ ac := `
624+ apiVersion: apisix.apache.org/v2
625+ kind: ApisixConsumer
626+ metadata:
627+ name: jack
628+ spec:
629+ ingressClassName: %s
630+ authParameter:
631+ ldapAuth:
632+ secretRef:
633+ name: ldap
634+ `
635+
636+ By ("apply ApisixConsumer" )
637+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "jack" },
638+ & apiv2.ApisixConsumer {}, fmt .Sprintf (ac , s .Namespace ()))
639+
640+ ldapSvr , err := getLDAPServerURL ()
641+ assert .Nil (GinkgoT (), err , "check ldap server" )
642+ ar := fmt .Sprintf (`
643+ apiVersion: apisix.apache.org/v2
644+ kind: ApisixRoute
645+ metadata:
646+ name: httpbin-route
647+ spec:
648+ ingressClassName: %s
649+ http:
650+ - name: rule1
651+ match:
652+ hosts:
653+ - httpbin
654+ paths:
655+ - /get
656+ backends:
657+ - serviceName: httpbin-service-e2e-test
658+ servicePort: 80
659+ authentication:
660+ enable: true
661+ type: ldapAuth
662+ ldapAuth:
663+ ldap_uri: %s
664+ base_dn: "ou=users,dc=ldap,dc=example,dc=org"
665+ use_tls: false
666+ uid: "cn"
667+ ` , s .Namespace (), ldapSvr )
668+ applier .MustApplyAPIv2 (types.NamespacedName {Namespace : s .Namespace (), Name : "httpbin-route" },
669+ & apiv2.ApisixRoute {}, ar )
670+
671+ By ("verify ApisixRoute with ApisixConsumer" )
672+ Eventually (request ).WithArguments ("/get" , "" , "" ).WithTimeout (5 * time .Second ).ProbeEvery (time .Second ).Should (Equal (http .StatusUnauthorized ))
673+
674+ By ("verify ApisixRoute with ApisixConsumer" )
675+ Eventually (request ).WithArguments ("/get" , "jack" , "jackPassword" ).WithTimeout (5 * time .Second ).ProbeEvery (time .Second ).Should (Equal (http .StatusOK ))
676+ })
677+ })
588678})
0 commit comments