Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ pages](https://github.com/dalibo/ldap2pg/pulls?utf8=%E2%9C%93&q=is%3Apr%20is%3Am

- Fix memory usage value.
- Experimental GSSAPI authentication method.
- Run CI on rockylinux 10 with PostgreSQL19 beta1
- Bump PostgreSQL developpement version to 18
- Run CI on rockylinux 10 with PostgreSQL19 beta1.
- Bump PostgreSQL developpement version to 18.
- Fix duplicate CREATE ROLE.
- Resolve ~ in path.
- Fix empty filter in configuration file leading to panic.
- Strict yaml configuration file decoding.


# ldap2pg 6.5.1
Expand Down
7 changes: 2 additions & 5 deletions internal/cmd/ldap2pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ func Main() {
}

if err != nil {
if errs, ok := err.(interface{ Len() int }); ok {
// Assume error are already logged before.
slog.Error("Some errors occurred. See above for more details.", "err", err, "count", errs.Len())
} else {
slog.Error("Fatal error.", "err", err)
for _, werr := range errorlist.Unwrap(err) {
slog.Error(werr.Error())
}
if internal.CurrentLevel > slog.LevelDebug {
slog.Error("Run ldap2pg with --verbose to get more informations.")
Expand Down
15 changes: 15 additions & 0 deletions internal/config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"log/slog"
"os"
"reflect"
"strings"

"github.com/dalibo/ldap2pg/v6/internal/errorlist"
"github.com/dalibo/ldap2pg/v6/internal/ldap"
"github.com/dalibo/ldap2pg/v6/internal/postgres"
"github.com/dalibo/ldap2pg/v6/internal/pyfmt"
Expand Down Expand Up @@ -83,11 +85,24 @@ func (c *Config) DecodeYaml(yaml any) (err error) {
Metadata: &mapstructure.Metadata{},
Result: c,
WeaklyTypedInput: true,
ErrorUnused: true,
})
if err != nil {
return
}
err = d.Decode(yaml)

// Converting joined error into errorlist.
// Lower the error message to align it with the LDAP parameters.
yamlErrors := errorlist.New("invalid configuration file")
for _, werr := range errorlist.Unwrap(err) {
Comment thread
pirlgon marked this conversation as resolved.
lowerErr := strings.ToLower(werr.Error())
if !yamlErrors.Append(fmt.Errorf("%s", lowerErr)) {
err = yamlErrors.Value()
return
}
}
err = yamlErrors.Value()
return
}

Expand Down
24 changes: 24 additions & 0 deletions internal/config/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/dalibo/ldap2pg/v6/internal/config"
"github.com/dalibo/ldap2pg/v6/internal/errorlist"
"github.com/lithammer/dedent"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -31,3 +32,26 @@ func TestLoadPrivilege(t *testing.T) {
r.Equal("CONNECT", p[0].Type)
r.Equal("DATABASE", p[0].On)
}

func TestStrictYaml(t *testing.T) {
r := require.New(t)

rawYaml := dedent.Dedent(`
postgres:
uri: "postgres://user:xx@localhost"
roles_blacklist_query: [postgres]
databases_query: [nominal]
ldap:
password: "secret"
`)
var value map[string]any
yaml.Unmarshal([]byte(rawYaml), &value) //nolint:errcheck

c := config.New()
err := c.DecodeYaml(value)
r.EqualError(err, "invalid configuration file\n'ldap' has invalid keys: password\n'postgres' has invalid keys: uri")
errs := errorlist.Unwrap(err)
r.Len(errs, 3)
r.EqualError(errs[1], "'ldap' has invalid keys: password")
r.EqualError(errs[2], "'postgres' has invalid keys: uri")
}
4 changes: 2 additions & 2 deletions internal/errorlist/errorlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (errl *List) Value() error {
case 0:
return nil
case 1:
return errl.errors[0]
return fmt.Errorf("%s: %w", errl.message, errl.errors[0])
default:
return errl
return errors.Join(append([]error{errors.New(errl.message)}, errl.errors...)...)
Comment thread
pirlgon marked this conversation as resolved.
}
}
17 changes: 17 additions & 0 deletions internal/errorlist/errorlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,20 @@ func buildErrors(n int) []error {
}
return errors
}

func TestErrorListValue(t *testing.T) {
r := require.New(t)

err := errorlist.New("context")
err.Append(fmt.Errorf("content"))
errs := errorlist.Unwrap(err.Value())
r.Equal(1, len(errs))
r.Equal("context: content", errs[0].Error())

err.Append(fmt.Errorf("content 2"))
errs = errorlist.Unwrap(err.Value())
r.Equal(3, len(errs))
r.Equal("context", errs[0].Error())
r.Equal("content", errs[1].Error())
r.Equal("content 2", errs[2].Error())
}