* feature (auth): required schema changes for auth ldap method (#2669) chore (auth/ldap): move schema changes to next avail migration number * feature (auth/ldap): define AuthMethod and all its value objects (#2703) * feature (auth/ldap): storage protos * feature (auth/ldap): define AuthMethod and all its value objects * feature (auth/ldap): add repo and reading an auth method (#2718) * feature (auth/ldap): add Repository.CreateAuthMethod(...) and Repository.DeleteAuthMethod(...) (#2724) * feature (auth/ldap): add Repository.UpdateAuthMethod(...) (#2739) * feature (auth/ldap): add Account * feature (auth/ldap): add AuthMethod.EnableGroups * fix (auth/ldap): refactor AuthMethod.oplog to enforce proper constraints * feature (auth/ldap): add Account repo functions * refactor (auth/ldap): remove entry attributes from account Realized the entry attributes could have absolutely anything in them (including binary data) and since we absolutely don't have to have them there's just no reason to take on the risk. * feature (auth/ldap): add AuthMethod.UseTokenGroups * feature (auth/ldap): add Authenticate(...) * refactor (auth/ldap): ensure options take a context as the 1st parameter * feature (auth/ldap): add Account attribute maps * chore (auth/ldap): make fmt deltas * feature (auth/ldap): add managed groups (#2760) * tests (auth/ldap): add missing unit test to Repository.DeleteAccount(...) Add bits to test the delete operation when you're not able to generate oplog metadata * feature (auth/ldap): add managed groups fixup! feature (auth/ldap): add managed groups (#2760) * feature (auth/ldap): service proto definition (#2761) * feature (handlers/authmethods): add handlers for ldap auth method operations (#2794) * feature (auth/ldap): add Account attribute maps * chore (auth/ldap): update cap/ldap to latest version * feature (auth/ldap): add ldap api generation definitions * feature (authmethods): add ldap repo NewService(...) * feature (authmethods/ldap): add proper mask_mapping to protobufs * feature (authmethods): add support to get an ldap auth method * refactor (auth/ldap): export TestGenerateCA(...) * feature (authmethods): add support to create an ldap auth method * feature (authmethods): add support to delete an ldap auth method * feature (authmethods): add support to list ldap auth methods * refactor (auth/ldap): make urls optional for NewAuthMethod(...) * refactor (auth/ldap): export ldap.TestInvalidPem * chore: make fmt changes * fix (auth/ldap): properly handle group search config Add constraints and tests to ensure when an ldap AuthMethod.EnableGroups is true, and UseTokenGroups is false; that there's a GroupDn configured for finding a user's associated groups * feature (authmethods): add support to update an ldap auth methods * chore (db/ldap): tmp mv migrations so there's no conflict with ongoing work * feature (verifier): add ldap auth method to verifier bits * fix (controller): prevent panic when controller stops when there's no listener * feature (authmethods): add support to authenticate via ldap auth methods * chore (migrations): fix whitespace in stmt * chore: fmt fixup * tests (auth/ldap): invalid err msg * feature (cli/authmethods): add support for ldap auth-methods CRUD and authenticate (#2810) * feature (authmethods): add CLI support for ldap auth methods CRUD * tests (api/auth): ldap auth method classification tests * feature (authmethods): add CLI support for ldap auth authenticate * feature (auth/ldap): set request timeouts for ldap server connections * feature (handlers/authmethods): handle u_anon listing properly. * feature (account/handers): ldap account and managed group CRUDL APIs (#2852) * feature (auth/ldap) add repository Listing of ManagedGroupMemberAccount * feature (controller/handlers): add ldapRepoFn to accounts service * feature (auth/ldap) register ldap managed group subtype * feature (account/handlers): ldap account CRUDL APIs * feature (controller/handlers): add ldapRepo to managed groups service * feature (account/handlers): ldap managed group CRUDL APIs * docs (domain): add LDAP accounts, auth-methods and managed groups (#2857) * feature (ldap/cli) add ldap accounts and managed groups CRUDL commands (#2856) * fix (handlers/authmethods): fix ldap authorized actions (#2892) * feature (cli/ldap/authenticate): use primary auth method if none is provided (#2890) * fix (auth/ldap): support setting the state attribute * feature (cli/ldap/authenticate): use primary auth method if none is provided * feature (wh/ldap) add tests for new ldap auth method and accounts (#2919) * refactor (migrations/ldap): mv to correct directory * chore: add copyright headers * fix (api/authmethods/ldap): renumber new LdapAuthMethodAttributes field * fix (auth/ldap): allow the auth method state to be updated (#2951) * chore: update sdk and api versions for llb - this is tmp until merging * tests (managed groups): add required errContains for new test
302 lines
8.2 KiB
Go
302 lines
8.2 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package ldap
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/json"
|
|
"encoding/pem"
|
|
"math/big"
|
|
"net"
|
|
"net/url"
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const TestInvalidPem = `-----BEGIN CERTIFICATE-----
|
|
MIICUTCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADBXMQswCQYDVQQGEwJDTjEL
|
|
-----END CERTIFICATE-----`
|
|
|
|
var testGrpNames = []string{"test-admin", "test-users"}
|
|
|
|
// TestEncodeGrpNames will json marshal group names
|
|
func TestEncodedGrpNames(t *testing.T, names ...string) string {
|
|
encoded, err := json.Marshal(names)
|
|
require.NoError(t, err)
|
|
return string(encoded)
|
|
}
|
|
|
|
// TestAuthMethod creates a new auth method and it's persisted in the database.
|
|
// See NewAuthMethod for list of supported options.
|
|
func TestAuthMethod(t testing.TB,
|
|
conn *db.DB,
|
|
databaseWrapper wrapping.Wrapper,
|
|
scopeId string,
|
|
urls []string,
|
|
opt ...Option,
|
|
) *AuthMethod {
|
|
t.Helper()
|
|
require.Greater(t, len(urls), 0)
|
|
testCtx := context.TODO()
|
|
require := require.New(t)
|
|
rw := db.New(conn)
|
|
|
|
opt = append(opt, WithUrls(testCtx, TestConvertToUrls(t, urls...)...))
|
|
|
|
opts, err := getOpts(opt...)
|
|
require.NoError(err)
|
|
am, err := NewAuthMethod(testCtx, scopeId, opt...)
|
|
require.NoError(err)
|
|
id, err := newAuthMethodId(testCtx)
|
|
require.NoError(err)
|
|
am.PublicId = id
|
|
_, err = rw.DoTx(testCtx, 0, db.ConstBackoff{}, func(r db.Reader, w db.Writer) error {
|
|
if err := w.Create(testCtx, am); err != nil {
|
|
return err
|
|
}
|
|
for priority, u := range urls {
|
|
storeUrl, err := NewUrl(testCtx, am.PublicId, priority+1, TestConvertToUrls(t, u)[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := w.Create(testCtx, storeUrl); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if len(opts.withCertificates) > 0 {
|
|
for _, pem := range opts.withCertificates {
|
|
c, err := NewCertificate(testCtx, am.PublicId, pem)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := w.Create(testCtx, c); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if len(opts.withAccountAttributeMap) > 0 {
|
|
for from, to := range opts.withAccountAttributeMap {
|
|
aam, err := NewAccountAttributeMap(testCtx, am.PublicId, from, AccountToAttribute(string(to)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := w.Create(testCtx, aam); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if opts.withUserDn != "" || opts.withUserAttr != "" || opts.withUserFilter != "" {
|
|
uc, err := NewUserEntrySearchConf(testCtx, am.PublicId, opt...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = w.Create(testCtx, uc); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if opts.withGroupDn != "" || opts.withGroupAttr != "" || opts.withGroupFilter != "" {
|
|
uc, err := NewGroupEntrySearchConf(testCtx, am.PublicId, opt...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = w.Create(testCtx, uc); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if opts.withClientCertificate != "" || len(opts.withClientCertificateKey) != 0 {
|
|
cc, err := NewClientCertificate(testCtx, am.PublicId, opts.withClientCertificateKey, opts.withClientCertificate)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := cc.encrypt(testCtx, databaseWrapper); err != nil {
|
|
return err
|
|
}
|
|
if err := w.Create(testCtx, cc); err != nil {
|
|
return err
|
|
}
|
|
am.ClientCertificateKeyHmac = cc.CertificateKeyHmac
|
|
}
|
|
if opts.withBindDn != "" || opts.withBindPassword != "" {
|
|
bc, err := NewBindCredential(testCtx, am.PublicId, opts.withBindDn, []byte(opts.withBindPassword))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := bc.encrypt(testCtx, databaseWrapper); err != nil {
|
|
return err
|
|
}
|
|
if err := w.Create(testCtx, bc); err != nil {
|
|
return err
|
|
}
|
|
am.BindPasswordHmac = bc.PasswordHmac
|
|
}
|
|
return nil
|
|
})
|
|
require.NoError(err)
|
|
|
|
return am
|
|
}
|
|
|
|
// TestAccount creates a test ldap auth account.
|
|
func TestAccount(t testing.TB, conn *db.DB, am *AuthMethod, loginName string, opt ...Option) *Account {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
rw := db.New(conn)
|
|
ctx := context.Background()
|
|
|
|
a, err := NewAccount(ctx, am.ScopeId, am.PublicId, loginName, opt...)
|
|
require.NoError(err)
|
|
|
|
id, err := newAccountId(ctx, am.PublicId, loginName)
|
|
require.NoError(err)
|
|
a.PublicId = id
|
|
|
|
require.NoError(rw.Create(ctx, a))
|
|
return a
|
|
}
|
|
|
|
// TestManagedGroup creates a test ldap managed group.
|
|
func TestManagedGroup(t testing.TB, conn *db.DB, am *AuthMethod, grpNames []string, opt ...Option) *ManagedGroup {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
rw := db.New(conn)
|
|
ctx := context.Background()
|
|
|
|
mg, err := NewManagedGroup(ctx, am.PublicId, grpNames, opt...)
|
|
require.NoError(err)
|
|
|
|
id, err := newManagedGroupId(ctx)
|
|
require.NoError(err)
|
|
mg.PublicId = id
|
|
|
|
require.NoError(rw.Create(ctx, mg, db.WithLookup(true)))
|
|
return mg
|
|
}
|
|
|
|
// TestGenerateCA will generate a test x509 CA cert, along with it encoded in a
|
|
// PEM format.
|
|
func TestGenerateCA(t testing.TB, hosts ...string) (*x509.Certificate, string) {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
|
require.NoError(err)
|
|
|
|
// ECDSA, ED25519 and RSA subject keys should have the DigitalSignature
|
|
// KeyUsage bits set in the x509.Certificate template
|
|
keyUsage := x509.KeyUsageDigitalSignature
|
|
|
|
validFor := 2 * time.Minute
|
|
notBefore := time.Now()
|
|
notAfter := notBefore.Add(validFor)
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
require.NoError(err)
|
|
|
|
template := x509.Certificate{
|
|
SerialNumber: serialNumber,
|
|
Subject: pkix.Name{
|
|
Organization: []string{"Acme Co"},
|
|
},
|
|
NotBefore: notBefore,
|
|
NotAfter: notAfter,
|
|
|
|
KeyUsage: keyUsage,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
BasicConstraintsValid: true,
|
|
}
|
|
|
|
for _, h := range hosts {
|
|
if ip := net.ParseIP(h); ip != nil {
|
|
template.IPAddresses = append(template.IPAddresses, ip)
|
|
} else {
|
|
template.DNSNames = append(template.DNSNames, h)
|
|
}
|
|
}
|
|
|
|
template.IsCA = true
|
|
template.KeyUsage |= x509.KeyUsageCertSign
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
|
require.NoError(err)
|
|
|
|
c, err := x509.ParseCertificate(derBytes)
|
|
require.NoError(err)
|
|
|
|
return c, string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}))
|
|
}
|
|
|
|
// TestConvertToUrls will convert URL string representations to a slice of
|
|
// *url.URL
|
|
func TestConvertToUrls(t testing.TB, urls ...string) []*url.URL {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
require.NotEmpty(urls)
|
|
var convertedUrls []*url.URL
|
|
for _, u := range urls {
|
|
parsed, err := url.Parse(u)
|
|
require.NoError(err)
|
|
require.Contains([]string{"ldap", "ldaps"}, parsed.Scheme)
|
|
convertedUrls = append(convertedUrls, parsed)
|
|
}
|
|
return convertedUrls
|
|
}
|
|
|
|
// TestSortAuthMethods will sort the provided auth methods by public id and it
|
|
// will sort each auth method's embedded value objects
|
|
func TestSortAuthMethods(t testing.TB, methods []*AuthMethod) {
|
|
// sort them by public id first...
|
|
sort.Slice(methods, func(a, b int) bool {
|
|
return methods[a].PublicId < methods[b].PublicId
|
|
})
|
|
|
|
// sort all the embedded value objects...
|
|
for _, am := range methods {
|
|
sort.Slice(am.Urls, func(a, b int) bool {
|
|
return am.Urls[a] < am.Urls[b]
|
|
})
|
|
sort.Slice(am.Certificates, func(a, b int) bool {
|
|
return am.Certificates[a] < am.Certificates[b]
|
|
})
|
|
sort.Slice(am.AccountAttributeMaps, func(a, b int) bool {
|
|
return am.AccountAttributeMaps[a] < am.AccountAttributeMaps[b]
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestGetAcctManagedGroups will retrieve the managed groups associated with an account.
|
|
func TestGetAcctManagedGroups(t testing.TB, conn *db.DB, acctId string) []string {
|
|
t.Helper()
|
|
testCtx := context.Background()
|
|
rw := db.New(conn)
|
|
var memberAccts []*grpMemberAccts
|
|
require.NoError(t, rw.SearchWhere(testCtx, &memberAccts, "member_id = ?", []any{acctId}))
|
|
grpIds := make([]string, 0, len(memberAccts))
|
|
for _, mbrAcct := range memberAccts {
|
|
grpIds = append(grpIds, mbrAcct.ManagedGroupId)
|
|
}
|
|
return grpIds
|
|
}
|
|
|
|
type grpMemberAccts struct {
|
|
CreateTime time.Time
|
|
MemberId string
|
|
ManagedGroupId string
|
|
ManagedGroupName string
|
|
}
|
|
|
|
func (*grpMemberAccts) TableName() string {
|
|
return "auth_ldap_managed_group_member_account"
|
|
}
|