Skip to content

Commit 14faafe

Browse files
chore: migrate ldap-auth e2e test (#271)
Co-authored-by: Ashing Zheng <[email protected]>
1 parent 0d3de24 commit 14faafe

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

.github/workflows/apisix-e2e-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ jobs:
108108
node $(pwd)/adc.js -v
109109
echo "ADC_BIN=node $(pwd)/adc.js" >> $GITHUB_ENV
110110
111+
- name: Start OpenLDAP server
112+
run: make e2e-ldap
113+
111114
- name: Run E2E test suite
112115
shell: bash
113116
env:

.github/workflows/e2e-test-k8s.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ jobs:
107107
node $(pwd)/adc.js -v
108108
echo "ADC_BIN=node $(pwd)/adc.js" >> $GITHUB_ENV
109109
110+
111+
- name: Start OpenLDAP server
112+
run: make e2e-ldap
113+
110114
- name: Run E2E test suite
111115
shell: bash
112116
env:

.github/workflows/e2e-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ jobs:
107107
node $(pwd)/adc.js -v
108108
echo "ADC_BIN=node $(pwd)/adc.js" >> $GITHUB_ENV
109109
110+
- name: Start OpenLDAP server
111+
run: make e2e-ldap
112+
110113
- name: Run E2E test suite
111114
shell: bash
112115
env:

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,18 @@ ifndef ignore-not-found
310310
ignore-not-found = false
311311
endif
312312

313+
.PHONY: e2e-ldap
314+
e2e-ldap:
315+
ifeq ("$(E2E_FOCUS)", "")
316+
chmod +x ./test/e2e/testdata/ldap/cmd.sh && ./test/e2e/testdata/ldap/cmd.sh start
317+
endif
318+
ifneq ("$(E2E_FOCUS)", "")
319+
echo $(E2E_FOCUS) | grep -E 'suite-plugins-authentication|consumer|ldap' || exit 0 \
320+
&& chmod +x ./test/e2e/testdata/ldap/cmd.sh \
321+
&& ./test/e2e/testdata/ldap/cmd.sh start
322+
endif
323+
324+
313325
.PHONY: install-gateway-api
314326
install-gateway-api: ## Install Gateway API CRDs into the K8s cluster specified in ~/.kube/config.
315327
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/$(GATEAY_API_VERSION)/experimental-install.yaml

test/e2e/crds/v2/consumer.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
})

test/e2e/testdata/ldap/cmd.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
cd test/e2e/testdata/ldap/
19+
20+
OPTION=$1
21+
COMPOSE_CMD=""
22+
23+
if command -v "docker-compose" > /dev/null 2>&1; then
24+
COMPOSE_CMD="docker-compose"
25+
elif command -v "docker" > /dev/null 2>&1; then
26+
COMPOSE_CMD="docker compose"
27+
else
28+
echo "docker-compose or docker compose not found"
29+
exit 1
30+
fi
31+
32+
if [ $OPTION = "ip" ]; then
33+
printf '%s' "$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}' openldap)"
34+
elif [ $OPTION = "start" ]; then
35+
$COMPOSE_CMD -f 'docker-compose.yaml' -p 'openldap' down
36+
37+
# start openldap
38+
$COMPOSE_CMD -f 'docker-compose.yaml' -p 'openldap' up -d
39+
40+
elif [ $OPTION = "stop" ]; then
41+
$COMPOSE_CMD -f 'docker-compose.yaml' -p 'openldap' down
42+
else
43+
echo "argument is one of [ip, start, stop]"
44+
fi
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
version: '3'
19+
20+
services:
21+
openldap:
22+
container_name: openldap
23+
image: docker.io/bitnamilegacy/openldap:2.6
24+
ports:
25+
- '1389:1389'
26+
environment:
27+
- LDAP_PORT_NUMBER=1389
28+
- LDAP_ENABLE_TLS=no
29+
- LDAP_ADMIN_USERNAME=admin
30+
- LDAP_ADMIN_PASSWORD=admin
31+
- LDAP_ROOT=dc=ldap,dc=example,dc=org
32+
- LDAP_USERS=jack
33+
- LDAP_PASSWORDS=jackPassword

0 commit comments

Comments
 (0)