Bump the vendored reva dependency to pick up the opt-in bounded LDAP connection pool (owncloud/reva#658, #665), and wire it into oCIS: add pool_enabled/pool_size/pool_checkout_timeout config to auth-basic, users, groups and graph, forward it into the reva config map for the first three, and unify graph's identity backend onto reva's LDAP client (utils.GetLDAPClientFromConfig) instead of its own hand-rolled reconnecting client, now that reva implements PasswordModify and ModifyWithResult on both LDAP clients. Pooling is off by default and fully backwards compatible. Signed-off-by: Lukas Hirt <info@hirt.cz> test(ldap): [OCISDEV-1031] run graph LDAP suites with pooling enabled in CI Add a dedicated ldap-pool-tests job to acceptance-tests.yml that reruns apiGraphUser/apiGraph/apiGraphGroup with OCIS_LDAP_POOL_ENABLED=true, so concurrent behat requests exercise checkout/release, eviction and the pooled PasswordModify/ModifyWithResult path end-to-end, in addition to the existing pooling-disabled runs of the same suites. Signed-off-by: Lukas Hirt <info@hirt.cz> fix(ldap): [OCISDEV-1031] set real pool_size/pool_checkout_timeout defaults Only PoolEnabled: false was set in the four defaultconfig.go files; PoolSize/PoolCheckoutTimeout were left as Go zero values (0/0s), relying on reva's NewLDAPPool substituting 5/30s when <= 0. That reads as "zero connections" to an operator in the generated yaml config rather than "default 5". Set the real defaults (5, 30s) so documented defaults match what's emitted. Signed-off-by: Lukas Hirt <info@hirt.cz> chore(deps): [OCISDEV-1031] bump reva to pick up LDAP TLS fixes Bumps the vendored reva dependency to owncloud/reva@8ee2020, picking up owncloud/reva#672 (restore MinVersion: tls.VersionTLS12 on LDAP TLS connections) and owncloud/reva#674 (restore pinned-CA-only trust and PEM validation for the CA-cert path), both flagged during review of this PR. Signed-off-by: Lukas Hirt <info@hirt.cz> docs: point changelog fragment at forward-port PR #12689
156 lines
5.3 KiB
Go
156 lines
5.3 KiB
Go
// Copyright 2021 CERN
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
// In applying this license, CERN does not waive the privileges and immunities
|
|
// granted to it by virtue of its status as an Intergovernmental Organization
|
|
// or submit itself to any jurisdiction.
|
|
|
|
package utils
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
"github.com/owncloud/reva/v2/pkg/logger"
|
|
ldapReconnect "github.com/owncloud/reva/v2/pkg/utils/ldap"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// LDAPConn holds the basic parameter for setting up an
|
|
// LDAP connection.
|
|
type LDAPConn struct {
|
|
URI string `mapstructure:"uri"`
|
|
Insecure bool `mapstructure:"insecure"`
|
|
CACert string `mapstructure:"cacert"`
|
|
BindDN string `mapstructure:"bind_username"`
|
|
BindPassword string `mapstructure:"bind_password"`
|
|
|
|
RetryMaxCount int `mapstructure:"retry_max_count"`
|
|
RetryBaseDelay time.Duration `mapstructure:"retry_base_delay"`
|
|
RetryMaxDelay time.Duration `mapstructure:"retry_max_delay"`
|
|
|
|
// PoolEnabled switches GetLDAPClientFromConfig to a bounded connection pool instead of
|
|
// the single long-lived reconnecting connection. Off by default.
|
|
PoolEnabled bool `mapstructure:"pool_enabled"`
|
|
// PoolSize caps the number of concurrently open pooled connections. Defaults to 5 when unset.
|
|
PoolSize int `mapstructure:"pool_size"`
|
|
// PoolCheckoutTimeout bounds how long a checkout waits for a connection to become available
|
|
// once the pool is at PoolSize. Defaults to 30s when unset.
|
|
PoolCheckoutTimeout time.Duration `mapstructure:"pool_checkout_timeout"`
|
|
}
|
|
|
|
// tlsConfigFromLDAPConn builds the *tls.Config shared by all GetLDAPClient* constructors below.
|
|
func tlsConfigFromLDAPConn(c *LDAPConn) (*tls.Config, error) {
|
|
if c.Insecure {
|
|
logger.New().Warn().Msg("SSL Certificate verification is disabled. This is strongly discouraged for production environments.")
|
|
return &tls.Config{
|
|
MinVersion: tls.VersionTLS12,
|
|
//nolint:gosec // We need the ability to run with "insecure" (dev/testing)
|
|
InsecureSkipVerify: true,
|
|
}, nil
|
|
}
|
|
if c.CACert != "" {
|
|
pemBytes, err := os.ReadFile(c.CACert)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "Error reading LDAP CA Cert '%s.'", c.CACert)
|
|
}
|
|
rpool := x509.NewCertPool()
|
|
if !rpool.AppendCertsFromPEM(pemBytes) {
|
|
return nil, errors.Errorf("Error adding LDAP CA Cert '%s': no valid certificates found", c.CACert)
|
|
}
|
|
return &tls.Config{
|
|
MinVersion: tls.VersionTLS12,
|
|
RootCAs: rpool,
|
|
}, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// GetLDAPClientWithReconnect initializes a long-lived LDAP connection that
|
|
// automatically reconnects on connection errors. It allows to set TLS options
|
|
// e.g. to add trusted Certificates or disable Certificate verification
|
|
func GetLDAPClientWithReconnect(c *LDAPConn) (ldap.Client, error) {
|
|
tlsConf, err := tlsConfigFromLDAPConn(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
conn := ldapReconnect.NewLDAPWithReconnect(
|
|
ldapReconnect.Config{
|
|
URI: c.URI,
|
|
BindDN: c.BindDN,
|
|
BindPassword: c.BindPassword,
|
|
TLSConfig: tlsConf,
|
|
RetryMaxCount: c.RetryMaxCount,
|
|
RetryBaseDelay: c.RetryBaseDelay,
|
|
RetryMaxDelay: c.RetryMaxDelay,
|
|
},
|
|
)
|
|
return conn, nil
|
|
}
|
|
|
|
// GetLDAPClientWithPool initializes a bounded pool of authenticated LDAP connections, dialed and
|
|
// bound lazily on first use. It is a drop-in alternative to GetLDAPClientWithReconnect intended for
|
|
// backends that need to serve concurrent requests without serializing on a single connection.
|
|
func GetLDAPClientWithPool(c *LDAPConn) (ldap.Client, error) {
|
|
tlsConf, err := tlsConfigFromLDAPConn(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pool := ldapReconnect.NewLDAPPool(
|
|
ldapReconnect.Config{
|
|
URI: c.URI,
|
|
BindDN: c.BindDN,
|
|
BindPassword: c.BindPassword,
|
|
TLSConfig: tlsConf,
|
|
RetryMaxCount: c.RetryMaxCount,
|
|
RetryBaseDelay: c.RetryBaseDelay,
|
|
RetryMaxDelay: c.RetryMaxDelay,
|
|
PoolSize: c.PoolSize,
|
|
PoolCheckoutTimeout: c.PoolCheckoutTimeout,
|
|
},
|
|
logger.New(),
|
|
)
|
|
return pool, nil
|
|
}
|
|
|
|
// GetLDAPClientFromConfig returns a connected ldap.Client for c: a bounded pool when
|
|
// c.PoolEnabled, otherwise the single long-lived reconnecting connection.
|
|
func GetLDAPClientFromConfig(c *LDAPConn) (ldap.Client, error) {
|
|
if c.PoolEnabled {
|
|
return GetLDAPClientWithPool(c)
|
|
}
|
|
return GetLDAPClientWithReconnect(c)
|
|
}
|
|
|
|
// GetLDAPClientForAuth initializes an LDAP connection. The connection is not authenticated
|
|
// when returned. The main purpose for GetLDAPClientForAuth is to get and LDAP connection that
|
|
// can be used to issue a single bind request to authenticate a user.
|
|
func GetLDAPClientForAuth(c *LDAPConn) (ldap.Client, error) {
|
|
tlsConf, err := tlsConfigFromLDAPConn(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
l, err := ldap.DialURL(c.URI, ldap.DialWithTLSConfig(tlsConf))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return l, nil
|
|
}
|