mirror of
https://github.com/project-zot/zot.git
synced 2026-06-17 12:58:02 +08:00
feat(ldap): add option to load ldap from file (#1778)
Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -1447,6 +1448,88 @@ func TestScrub(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateLDAPConfig(t *testing.T) {
|
||||
Convey("updateLDAPConfig errors while unmarshaling ldap config", t, func() {
|
||||
tempDir := t.TempDir()
|
||||
ldapConfigContent := "bad-json"
|
||||
ldapConfigPath := filepath.Join(tempDir, "ldap.json")
|
||||
|
||||
err := os.WriteFile(ldapConfigPath, []byte(ldapConfigContent), 0o000)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
configStr := fmt.Sprintf(`
|
||||
{
|
||||
"Storage": {
|
||||
"RootDirectory": "%s"
|
||||
},
|
||||
"HTTP": {
|
||||
"Address": "%s",
|
||||
"Port": "%s",
|
||||
"Auth": {
|
||||
"LDAP": {
|
||||
"CredentialsFile": "%s",
|
||||
"BaseDN": "%v",
|
||||
"UserAttribute": "uid",
|
||||
"UserGroupAttribute": "memberOf",
|
||||
"Insecure": true,
|
||||
"Address": "%v",
|
||||
"Port": %v
|
||||
}
|
||||
}
|
||||
}
|
||||
}`, tempDir, "127.0.0.1", "8000", ldapConfigPath, "LDAPBaseDN", "LDAPAddress", 1000)
|
||||
|
||||
configPath := filepath.Join(tempDir, "config.json")
|
||||
|
||||
err = os.WriteFile(configPath, []byte(configStr), 0o0600)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
server := cli.NewServerRootCmd()
|
||||
server.SetArgs([]string{"serve", configPath})
|
||||
So(func() { err = server.Execute() }, ShouldPanic)
|
||||
|
||||
err = os.Chmod(ldapConfigPath, 0o600)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
server = cli.NewServerRootCmd()
|
||||
server.SetArgs([]string{"serve", configPath})
|
||||
So(func() { err = server.Execute() }, ShouldPanic)
|
||||
})
|
||||
|
||||
Convey("unauthenticated LDAP config", t, func() {
|
||||
tempDir := t.TempDir()
|
||||
|
||||
configStr := fmt.Sprintf(`
|
||||
{
|
||||
"Storage": {
|
||||
"RootDirectory": "%s"
|
||||
},
|
||||
"HTTP": {
|
||||
"Address": "%s",
|
||||
"Port": "%s",
|
||||
"Auth": {
|
||||
"LDAP": {
|
||||
"BaseDN": "%v",
|
||||
"UserAttribute": "uid",
|
||||
"UserGroupAttribute": "memberOf",
|
||||
"Insecure": true,
|
||||
"Address": "%v",
|
||||
"Port": %v
|
||||
}
|
||||
}
|
||||
}
|
||||
}`, tempDir, "127.0.0.1", "8000", "LDAPBaseDN", "LDAPAddress", 1000)
|
||||
|
||||
configPath := filepath.Join(tempDir, "config.json")
|
||||
|
||||
err := os.WriteFile(configPath, []byte(configStr), 0o0600)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = cli.LoadConfiguration(config.New(), configPath)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
// run cli and return output.
|
||||
func runCLIWithConfig(tempDir string, config string) (string, error) {
|
||||
port := GetFreePort()
|
||||
|
||||
Reference in New Issue
Block a user