Files
threshold/internal/auth/ldap/auth_method.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

320 lines
11 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package ldap
import (
"context"
"fmt"
"net/url"
"github.com/hashicorp/boundary/internal/auth/ldap/store"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/oplog"
"google.golang.org/protobuf/proto"
)
// authMethodTableName defines an AuthMethod's table name.
const authMethodTableName = "auth_ldap_method"
// AuthMethod contains an LDAP auth method configuration. It is owned by a
// scope. AuthMethods MUST have at least one Url. AuthMethods MAY one or zero:
// UserEntrySearchConf, a GroupEntrySearchConf, BindCredential. AuthMethods
// may have zero to many: Accounts, Certificates,
type AuthMethod struct {
*store.AuthMethod
tableName string
}
// NewAuthMethod creates a new in memory AuthMethod assigned to a scopeId. The
// new auth method will have an OperationalState of Inactive.
//
// Supports the options: WithUrls, WithName, WithDescription, WithStartTLS,
// WithInsecureTLS, WithDiscoverDN, WithAnonGroupSearch, WithUpnDomain,
// WithUserSearchConf, WithGroupSearchConf, WithCertificates, WithBindCredential
// are the only valid options and all other options are ignored.
func NewAuthMethod(ctx context.Context, scopeId string, opt ...Option) (*AuthMethod, error) {
const op = "ldap.NewAuthMethod"
switch {
case scopeId == "":
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
opts, err := getOpts(opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
a := &AuthMethod{
AuthMethod: &store.AuthMethod{
ScopeId: scopeId,
Name: opts.withName,
Description: opts.withDescription,
OperationalState: string(opts.withOperationalState), // if no option is specified, a new auth method is initially inactive
Urls: opts.withUrls,
StartTls: opts.withStartTls,
InsecureTls: opts.withInsecureTls,
DiscoverDn: opts.withDiscoverDn,
AnonGroupSearch: opts.withAnonGroupSearch,
UpnDomain: opts.withUpnDomain,
UserDn: opts.withUserDn,
UserAttr: opts.withUserAttr,
UserFilter: opts.withUserFilter,
EnableGroups: opts.withEnableGroups,
UseTokenGroups: opts.withUseTokenGroups,
GroupDn: opts.withGroupDn,
GroupAttr: opts.withGroupAttr,
GroupFilter: opts.withGroupFilter,
BindDn: opts.withBindDn,
BindPassword: opts.withBindPassword,
Certificates: opts.withCertificates,
ClientCertificate: opts.withClientCertificate,
ClientCertificateKey: opts.withClientCertificateKey,
},
}
if len(opts.withAccountAttributeMap) > 0 {
a.AccountAttributeMaps = make([]string, 0, len(opts.withAccountAttributeMap))
for k, v := range opts.withAccountAttributeMap {
a.AccountAttributeMaps = append(a.AccountAttributeMaps, fmt.Sprintf("%s=%s", k, v))
}
}
return a, nil
}
// AllocAuthMethod makes an empty one in memory
func AllocAuthMethod() AuthMethod {
return AuthMethod{
AuthMethod: &store.AuthMethod{},
}
}
// clone an AuthMethod
func (am *AuthMethod) clone() *AuthMethod {
cp := proto.Clone(am.AuthMethod)
return &AuthMethod{
AuthMethod: cp.(*store.AuthMethod),
}
}
// TableName returns the table name (func is required by gorm)
func (am *AuthMethod) TableName() string {
if am.tableName != "" {
return am.tableName
}
return authMethodTableName
}
// SetTableName sets the table name (func is required by oplog)
func (am *AuthMethod) SetTableName(n string) {
am.tableName = n
}
// oplog will create oplog metadata for the AuthMethod.
func (am *AuthMethod) oplog(ctx context.Context, opType oplog.OpType) (oplog.Metadata, error) {
const op = "ldap.(AuthMethod).oplog"
switch {
case am == nil:
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth method")
case opType == oplog.OpType_OP_TYPE_UNSPECIFIED:
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing op type")
case am.PublicId == "":
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing public id")
case am.ScopeId == "":
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
}
metadata := oplog.Metadata{
"resource-public-id": []string{am.PublicId},
"resource-type": []string{"ldap auth method"},
"op-type": []string{opType.String()},
"scope-id": []string{am.ScopeId},
}
return metadata, nil
}
type convertedValues struct {
Urls []any
Certs []any
UserEntrySearchConf any
GroupEntrySearchConf any
ClientCertificate any
BindCredential any
AccountAttributeMaps []any
}
// convertValueObjects converts the embedded value objects. It will return an
// error if the AuthMethod's public id is not set.
func (am *AuthMethod) convertValueObjects(ctx context.Context) (*convertedValues, error) {
const op = "ldap.(AuthMethod).convertValueObjects"
if am.PublicId == "" {
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
var err error
converted := &convertedValues{}
if converted.Urls, err = am.convertUrls(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
if converted.Certs, err = am.convertCertificates(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
if am.UserDn != "" || am.UserAttr != "" || am.UserFilter != "" {
if converted.UserEntrySearchConf, err = am.convertUserEntrySearchConf(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
}
if am.GroupDn != "" || am.GroupAttr != "" || am.GroupFilter != "" {
if converted.GroupEntrySearchConf, err = am.convertGroupEntrySearchConf(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
}
if am.ClientCertificate != "" || am.ClientCertificateKey != nil {
if converted.ClientCertificate, err = am.convertClientCertificate(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
}
if am.BindDn != "" || am.BindPassword != "" {
if converted.BindCredential, err = am.convertBindCredential(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
}
if converted.AccountAttributeMaps, err = am.convertAccountAttributeMaps(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
return converted, nil
}
// convertCertificates converts any embedded URLs from []string
// to []any where each slice element is a *Url. It will return an error if the
// AuthMethod's public id is not set.
func (am *AuthMethod) convertUrls(ctx context.Context) ([]any, error) {
const op = "ldap.(AuthMethod).convertUrls"
if am.PublicId == "" {
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
newValObjs := make([]any, 0, len(am.Urls))
for priority, u := range am.Urls {
parsed, err := url.Parse(u)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
obj, err := NewUrl(ctx, am.PublicId, priority+1, parsed)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
newValObjs = append(newValObjs, obj)
}
return newValObjs, nil
}
// convertCertificates converts any embedded certificates from []string
// to []any where each slice element is a *Certificate. It will return an error
// if the AuthMethod's public id is not set.
func (am *AuthMethod) convertCertificates(ctx context.Context) ([]any, error) {
const op = "ldap.(AuthMethod).convertCertificates"
if am.PublicId == "" {
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
newValObjs := make([]any, 0, len(am.Certificates))
for _, cert := range am.Certificates {
obj, err := NewCertificate(ctx, am.PublicId, cert)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
newValObjs = append(newValObjs, obj)
}
return newValObjs, nil
}
// convertUserEntrySearchConf converts an embedded user entry search fields
// into an any type. It will return an error if the AuthMethod's public id is
// not set.
func (am *AuthMethod) convertUserEntrySearchConf(ctx context.Context) (any, error) {
const op = "ldap.(AuthMethod).convertUserEntrySearchConf"
if am.PublicId == "" {
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
c, err := NewUserEntrySearchConf(ctx, am.PublicId, WithUserDn(ctx, am.UserDn), WithUserAttr(ctx, am.UserAttr), WithUserFilter(ctx, am.UserFilter))
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
return c, nil
}
// convertGroupEntrySearchConf converts an embedded group entry search fields
// into an any type. It will return an error if the AuthMethod's public id is
// not set.
func (am *AuthMethod) convertGroupEntrySearchConf(ctx context.Context) (any, error) {
const op = "ldap.(AuthMethod).convertGroupEntrySearchConf"
if am.PublicId == "" {
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
c, err := NewGroupEntrySearchConf(ctx, am.PublicId, WithGroupDn(ctx, am.GroupDn), WithGroupAttr(ctx, am.GroupAttr), WithGroupFilter(ctx, am.GroupFilter))
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
return c, nil
}
// convertClientCertificate converts an embedded client certificate entry into
// an any type. It will return an error if the AuthMethod's public id is not
// set.
func (am *AuthMethod) convertClientCertificate(ctx context.Context) (any, error) {
const op = "ldap.(AuthMethod).convertClientCertificate"
if am.PublicId == "" {
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing auth method id")
}
cc, err := NewClientCertificate(ctx, am.PublicId, am.ClientCertificateKey, am.ClientCertificate)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
return cc, nil
}
// convertBindCredential converts an embedded bind credential entry into
// an any type. It will return an error if the AuthMethod's public id is not
// set.
func (am *AuthMethod) convertBindCredential(ctx context.Context) (any, error) {
const op = "ldap.(AuthMethod).convertBindCredentials"
if am.PublicId == "" {
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing auth method id")
}
bc, err := NewBindCredential(ctx, am.PublicId, am.BindDn, []byte(am.BindPassword))
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
return bc, nil
}
// convertAccountAttributeMaps converts the embedded account attribute maps from
// []string to []interface{} where each slice element is a *AccountAttributeMap. It
// will return an error if the AuthMethod's public id is not set or it can
// convert the account attribute maps.
func (am *AuthMethod) convertAccountAttributeMaps(ctx context.Context) ([]any, error) {
const op = "ldap.(AuthMethod).convertAccountAttributeMaps"
if am.PublicId == "" {
return nil, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
}
newInterfaces := make([]any, 0, len(am.AccountAttributeMaps))
const (
from = 0
to = 1
)
acms, err := ParseAccountAttributeMaps(ctx, am.AccountAttributeMaps...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
for _, m := range acms {
toClaim, err := ConvertToAccountToAttribute(ctx, m.To)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
obj, err := NewAccountAttributeMap(ctx, am.PublicId, m.From, toClaim)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
newInterfaces = append(newInterfaces, obj)
}
return newInterfaces, nil
}