2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-10-09 17:00:01 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-06-20 18:28:33 -04:00
|
|
|
"bytes"
|
2018-10-09 17:00:01 -04:00
|
|
|
"context"
|
2020-03-17 13:36:13 -04:00
|
|
|
"encoding/base64"
|
2020-04-14 14:28:56 -04:00
|
|
|
"encoding/json"
|
2020-06-09 22:19:03 -04:00
|
|
|
"errors"
|
2020-03-17 13:36:13 -04:00
|
|
|
"fmt"
|
2020-11-02 20:52:13 -05:00
|
|
|
"math/rand"
|
2021-10-18 14:21:57 -04:00
|
|
|
"path"
|
2021-12-02 18:43:39 -05:00
|
|
|
"sort"
|
2020-05-11 16:04:11 -04:00
|
|
|
"strings"
|
2020-11-08 00:03:06 -05:00
|
|
|
"sync"
|
2022-04-03 16:08:59 -04:00
|
|
|
"sync/atomic"
|
2020-06-09 22:19:03 -04:00
|
|
|
"time"
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
libldap "github.com/go-ldap/ldap/v3"
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2020-07-14 12:38:05 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
2021-11-26 22:22:40 -05:00
|
|
|
"github.com/minio/minio/internal/arn"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/auth"
|
2021-11-26 22:22:40 -05:00
|
|
|
"github.com/minio/minio/internal/color"
|
2022-08-08 19:16:27 -04:00
|
|
|
"github.com/minio/minio/internal/config"
|
2022-05-25 21:32:53 -04:00
|
|
|
xldap "github.com/minio/minio/internal/config/identity/ldap"
|
|
|
|
"github.com/minio/minio/internal/config/identity/openid"
|
2022-08-08 19:16:27 -04:00
|
|
|
idplugin "github.com/minio/minio/internal/config/identity/plugin"
|
2023-02-27 02:37:00 -05:00
|
|
|
xtls "github.com/minio/minio/internal/config/identity/tls"
|
2022-08-08 19:16:27 -04:00
|
|
|
"github.com/minio/minio/internal/config/policy/opa"
|
|
|
|
polplugin "github.com/minio/minio/internal/config/policy/plugin"
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2024-01-28 13:04:17 -05:00
|
|
|
xioutil "github.com/minio/minio/internal/ioutil"
|
2022-03-14 12:09:22 -04:00
|
|
|
"github.com/minio/minio/internal/jwt"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2024-04-18 11:15:02 -04:00
|
|
|
"github.com/minio/pkg/v2/ldap"
|
2023-09-14 17:50:16 -04:00
|
|
|
"github.com/minio/pkg/v2/policy"
|
2021-10-20 06:22:35 -04:00
|
|
|
etcd "go.etcd.io/etcd/client/v3"
|
2018-10-09 17:00:01 -04:00
|
|
|
)
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
// UsersSysType - defines the type of users and groups system that is
|
|
|
|
// active on the server.
|
|
|
|
type UsersSysType string
|
|
|
|
|
|
|
|
// Types of users configured in the server.
|
|
|
|
const (
|
|
|
|
// This mode uses the internal users system in MinIO.
|
|
|
|
MinIOUsersSysType UsersSysType = "MinIOUsersSys"
|
|
|
|
|
|
|
|
// This mode uses users and groups from a configured LDAP
|
|
|
|
// server.
|
|
|
|
LDAPUsersSysType UsersSysType = "LDAPUsersSys"
|
|
|
|
)
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
const (
|
|
|
|
statusEnabled = "enabled"
|
|
|
|
statusDisabled = "disabled"
|
|
|
|
)
|
|
|
|
|
2022-05-02 20:56:19 -04:00
|
|
|
const (
|
|
|
|
embeddedPolicyType = "embedded-policy"
|
|
|
|
inheritedPolicyType = "inherited-policy"
|
|
|
|
)
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// IAMSys - config system.
|
|
|
|
type IAMSys struct {
|
2022-04-10 01:19:44 -04:00
|
|
|
// Need to keep them here to keep alignment - ref: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
|
|
|
|
// metrics
|
|
|
|
LastRefreshTimeUnixNano uint64
|
|
|
|
LastRefreshDurationMilliseconds uint64
|
|
|
|
TotalRefreshSuccesses uint64
|
|
|
|
TotalRefreshFailures uint64
|
|
|
|
|
2020-11-08 00:03:06 -05:00
|
|
|
sync.Mutex
|
|
|
|
|
2021-11-01 18:03:07 -04:00
|
|
|
iamRefreshInterval time.Duration
|
2023-02-27 02:37:00 -05:00
|
|
|
|
|
|
|
LDAPConfig xldap.Config // only valid if usersSysType is LDAPUsers
|
|
|
|
OpenIDConfig openid.Config // only valid if OpenID is configured
|
|
|
|
STSTLSConfig xtls.Config // only valid if STS TLS is configured
|
2021-11-01 18:03:07 -04:00
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
usersSysType UsersSysType
|
|
|
|
|
2021-11-26 22:22:40 -05:00
|
|
|
rolesMap map[arn.ARN]string
|
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
// Persistence layer for IAM subsystem
|
2021-11-03 22:47:49 -04:00
|
|
|
store *IAMStoreSys
|
2021-03-02 20:08:25 -05:00
|
|
|
|
|
|
|
// configLoaded will be closed and remain so after first load.
|
|
|
|
configLoaded chan struct{}
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
// IAMUserType represents a user type inside MinIO server
|
|
|
|
type IAMUserType int
|
|
|
|
|
|
|
|
const (
|
2022-08-23 14:11:45 -04:00
|
|
|
unknownIAMUserType IAMUserType = iota - 1
|
|
|
|
regUser
|
2020-03-17 13:36:13 -04:00
|
|
|
stsUser
|
2021-07-09 14:17:21 -04:00
|
|
|
svcUser
|
2020-03-17 13:36:13 -04:00
|
|
|
)
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
// LoadGroup - loads a specific group from storage, and updates the
|
|
|
|
// memberships cache. If the specified group does not exist in
|
|
|
|
// storage, it is removed from in-memory maps as well - this
|
|
|
|
// simplifies the implementation for group removal. This is called
|
|
|
|
// only via IAM notifications.
|
2021-11-15 17:14:22 -05:00
|
|
|
func (sys *IAMSys) LoadGroup(ctx context.Context, objAPI ObjectLayer, group string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-12-10 23:28:22 -05:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:14:22 -05:00
|
|
|
return sys.store.GroupNotificationHandler(ctx, group)
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
// LoadPolicy - reloads a specific canned policy from backend disks or etcd.
|
2021-11-15 17:14:22 -05:00
|
|
|
func (sys *IAMSys) LoadPolicy(ctx context.Context, objAPI ObjectLayer, policyName string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-12-10 23:28:22 -05:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:14:22 -05:00
|
|
|
return sys.store.PolicyNotificationHandler(ctx, policyName)
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-13 16:41:06 -04:00
|
|
|
// LoadPolicyMapping - loads the mapped policy for a user or group
|
|
|
|
// from storage into server memory.
|
2022-08-23 14:11:45 -04:00
|
|
|
func (sys *IAMSys) LoadPolicyMapping(ctx context.Context, objAPI ObjectLayer, userOrGroup string, userType IAMUserType, isGroup bool) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-12-10 23:28:22 -05:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:14:22 -05:00
|
|
|
return sys.store.PolicyMappingNotificationHandler(ctx, userOrGroup, isGroup, userType)
|
2019-08-13 16:41:06 -04:00
|
|
|
}
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
// LoadUser - reloads a specific user from backend disks or etcd.
|
2021-11-15 17:14:22 -05:00
|
|
|
func (sys *IAMSys) LoadUser(ctx context.Context, objAPI ObjectLayer, accessKey string, userType IAMUserType) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-12-10 23:28:22 -05:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:14:22 -05:00
|
|
|
return sys.store.UserNotificationHandler(ctx, accessKey, userType)
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
// LoadServiceAccount - reloads a specific service account from backend disks or etcd.
|
2021-11-15 17:14:22 -05:00
|
|
|
func (sys *IAMSys) LoadServiceAccount(ctx context.Context, accessKey string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-04-24 15:10:09 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-11-15 17:14:22 -05:00
|
|
|
return sys.store.UserNotificationHandler(ctx, accessKey, svcUser)
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 00:03:02 -05:00
|
|
|
// initStore initializes IAM stores
|
|
|
|
func (sys *IAMSys) initStore(objAPI ObjectLayer, etcdClient *etcd.Client) {
|
2023-02-24 21:37:22 -05:00
|
|
|
if sys.LDAPConfig.Enabled() {
|
2022-08-23 14:11:45 -04:00
|
|
|
sys.SetUsersSysType(LDAPUsersSysType)
|
2021-11-03 22:47:49 -04:00
|
|
|
}
|
|
|
|
|
2021-10-20 06:22:35 -04:00
|
|
|
if etcdClient == nil {
|
2022-10-24 20:44:15 -04:00
|
|
|
sys.store = &IAMStoreSys{newIAMObjectStore(objAPI, sys.usersSysType)}
|
2019-08-08 18:10:04 -04:00
|
|
|
} else {
|
2021-11-03 22:47:49 -04:00
|
|
|
sys.store = &IAMStoreSys{newIAMEtcdStore(etcdClient, sys.usersSysType)}
|
2020-04-07 17:26:39 -04:00
|
|
|
}
|
2020-10-19 12:54:40 -04:00
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
// Initialized checks if IAM is initialized
|
2020-11-08 00:03:06 -05:00
|
|
|
func (sys *IAMSys) Initialized() bool {
|
|
|
|
if sys == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
return sys.store != nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
// Load - loads all credentials, policies and policy mappings.
|
2023-08-23 06:07:06 -04:00
|
|
|
func (sys *IAMSys) Load(ctx context.Context, firstTime bool) error {
|
2022-04-03 16:08:59 -04:00
|
|
|
loadStartTime := time.Now()
|
2023-12-01 03:18:04 -05:00
|
|
|
err := sys.store.LoadIAMCache(ctx, firstTime)
|
2021-11-03 22:47:49 -04:00
|
|
|
if err != nil {
|
2022-04-03 16:08:59 -04:00
|
|
|
atomic.AddUint64(&sys.TotalRefreshFailures, 1)
|
2021-11-02 16:51:42 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-04-03 16:08:59 -04:00
|
|
|
loadDuration := time.Since(loadStartTime)
|
|
|
|
|
|
|
|
atomic.StoreUint64(&sys.LastRefreshDurationMilliseconds, uint64(loadDuration.Milliseconds()))
|
|
|
|
atomic.StoreUint64(&sys.LastRefreshTimeUnixNano, uint64(loadStartTime.Add(loadDuration).UnixNano()))
|
|
|
|
atomic.AddUint64(&sys.TotalRefreshSuccesses, 1)
|
2021-11-02 16:51:42 -04:00
|
|
|
|
2024-02-26 16:26:18 -05:00
|
|
|
if !globalSiteReplicatorCred.IsValid() {
|
|
|
|
sa, _, err := sys.getServiceAccount(ctx, siteReplicatorSvcAcc)
|
|
|
|
if err == nil {
|
|
|
|
globalSiteReplicatorCred.Set(sa.Credentials)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 06:07:06 -04:00
|
|
|
if firstTime {
|
|
|
|
bootstrapTraceMsg(fmt.Sprintf("globalIAMSys.Load(): (duration: %s)", loadDuration))
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:08:25 -05:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
default:
|
2024-01-28 13:04:17 -05:00
|
|
|
xioutil.SafeClose(sys.configLoaded)
|
2021-03-02 20:08:25 -05:00
|
|
|
}
|
2021-01-25 23:01:49 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
// Init - initializes config system by reading entries from config/iam
|
2022-02-07 13:39:57 -05:00
|
|
|
func (sys *IAMSys) Init(ctx context.Context, objAPI ObjectLayer, etcdClient *etcd.Client, iamRefreshInterval time.Duration) {
|
2023-08-23 06:07:06 -04:00
|
|
|
bootstrapTraceMsg("IAM initialization started")
|
2022-08-08 19:16:27 -04:00
|
|
|
globalServerConfigMu.RLock()
|
|
|
|
s := globalServerConfig
|
|
|
|
globalServerConfigMu.RUnlock()
|
|
|
|
|
2023-02-26 00:01:37 -05:00
|
|
|
openidConfig, err := openid.LookupConfig(s,
|
2022-10-24 20:44:15 -04:00
|
|
|
NewHTTPTransport(), xhttp.DrainBody, globalSite.Region)
|
2022-08-08 19:16:27 -04:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Unable to initialize OpenID: %w", err), logger.WarningKind)
|
2022-08-08 19:16:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize if LDAP is enabled
|
2023-02-24 21:37:22 -05:00
|
|
|
ldapConfig, err := xldap.Lookup(s, globalRootCAs)
|
2022-08-08 19:16:27 -04:00
|
|
|
if err != nil {
|
2024-04-25 17:28:16 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Unable to load LDAP configuration (LDAP configuration will be disabled!): %w", err), logger.WarningKind)
|
2022-08-08 19:16:27 -04:00
|
|
|
}
|
|
|
|
|
2023-02-27 02:37:00 -05:00
|
|
|
stsTLSConfig, err := xtls.Lookup(s[config.IdentityTLSSubSys][config.Default])
|
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Unable to initialize X.509/TLS STS API: %w", err), logger.WarningKind)
|
2023-02-27 02:37:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if stsTLSConfig.InsecureSkipVerify {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Enabling %s is not recommended in a production environment", xtls.EnvIdentityTLSSkipVerify), logger.WarningKind)
|
2023-02-27 02:37:00 -05:00
|
|
|
}
|
|
|
|
|
2022-08-08 19:16:27 -04:00
|
|
|
authNPluginCfg, err := idplugin.LookupConfig(s[config.IdentityPluginSubSys][config.Default],
|
2022-10-24 20:44:15 -04:00
|
|
|
NewHTTPTransport(), xhttp.DrainBody, globalSite.Region)
|
2022-08-08 19:16:27 -04:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Unable to initialize AuthNPlugin: %w", err), logger.WarningKind)
|
2022-08-08 19:16:27 -04:00
|
|
|
}
|
|
|
|
|
2023-02-27 12:55:18 -05:00
|
|
|
setGlobalAuthNPlugin(idplugin.New(GlobalContext, authNPluginCfg))
|
2022-08-08 19:16:27 -04:00
|
|
|
|
2022-12-13 17:28:48 -05:00
|
|
|
authZPluginCfg, err := polplugin.LookupConfig(s, GetDefaultConnSettings(), xhttp.DrainBody)
|
2022-08-08 19:16:27 -04:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Unable to initialize AuthZPlugin: %w", err), logger.WarningKind)
|
2022-08-08 19:16:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if authZPluginCfg.URL == nil {
|
|
|
|
opaCfg, err := opa.LookupConfig(s[config.PolicyOPASubSys][config.Default],
|
2022-10-24 20:44:15 -04:00
|
|
|
NewHTTPTransport(), xhttp.DrainBody)
|
2022-08-08 19:16:27 -04:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Unable to initialize AuthZPlugin from legacy OPA config: %w", err))
|
2022-08-08 19:16:27 -04:00
|
|
|
} else {
|
|
|
|
authZPluginCfg.URL = opaCfg.URL
|
|
|
|
authZPluginCfg.AuthToken = opaCfg.AuthToken
|
|
|
|
authZPluginCfg.Transport = opaCfg.Transport
|
|
|
|
authZPluginCfg.CloseRespFn = opaCfg.CloseRespFn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setGlobalAuthZPlugin(polplugin.New(authZPluginCfg))
|
|
|
|
|
2021-11-12 00:03:02 -05:00
|
|
|
sys.Lock()
|
2023-02-24 21:37:22 -05:00
|
|
|
sys.LDAPConfig = ldapConfig
|
2023-02-26 00:01:37 -05:00
|
|
|
sys.OpenIDConfig = openidConfig
|
2023-02-27 02:37:00 -05:00
|
|
|
sys.STSTLSConfig = stsTLSConfig
|
2021-11-01 18:03:07 -04:00
|
|
|
sys.iamRefreshInterval = iamRefreshInterval
|
2021-01-06 16:40:20 -05:00
|
|
|
// Initialize IAM store
|
2021-11-12 00:03:02 -05:00
|
|
|
sys.initStore(objAPI, etcdClient)
|
2024-02-07 15:41:58 -05:00
|
|
|
sys.Unlock()
|
2021-01-06 16:40:20 -05:00
|
|
|
|
2020-06-09 22:19:03 -04:00
|
|
|
retryCtx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
// Indicate to our routine to exit cleanly upon return.
|
|
|
|
defer cancel()
|
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2021-11-26 22:22:40 -05:00
|
|
|
// Migrate storage format if needed.
|
2020-11-02 20:52:13 -05:00
|
|
|
for {
|
2020-06-11 17:11:30 -04:00
|
|
|
// Migrate IAM configuration, if necessary.
|
2022-08-05 20:53:23 -04:00
|
|
|
if err := saveIAMFormat(retryCtx, sys.store); err != nil {
|
2021-03-18 17:09:55 -04:00
|
|
|
if configRetriableErrors(err) {
|
2020-06-09 22:19:03 -04:00
|
|
|
logger.Info("Waiting for all MinIO IAM sub-system to be initialized.. possible cause (%v)", err)
|
2023-10-23 15:30:20 -04:00
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(time.Second)))
|
2020-06-09 22:19:03 -04:00
|
|
|
continue
|
|
|
|
}
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("IAM sub-system is partially initialized, unable to write the IAM format: %w", err), logger.WarningKind)
|
2020-06-09 22:19:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
2020-02-01 20:07:43 -05:00
|
|
|
|
2021-11-26 22:22:40 -05:00
|
|
|
// Load IAM data from storage.
|
2020-12-05 16:00:44 -05:00
|
|
|
for {
|
2023-08-23 06:07:06 -04:00
|
|
|
if err := sys.Load(retryCtx, true); err != nil {
|
2021-03-18 17:09:55 -04:00
|
|
|
if configRetriableErrors(err) {
|
2020-12-05 16:00:44 -05:00
|
|
|
logger.Info("Waiting for all MinIO IAM sub-system to be initialized.. possible cause (%v)", err)
|
2023-10-23 15:30:20 -04:00
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(time.Second)))
|
2020-12-05 16:00:44 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Unable to initialize IAM sub-system, some users may not be available: %w", err), logger.WarningKind)
|
2020-12-05 16:00:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2022-05-30 13:58:37 -04:00
|
|
|
refreshInterval := sys.iamRefreshInterval
|
|
|
|
|
2023-09-14 18:25:17 -04:00
|
|
|
go sys.periodicRoutines(ctx, refreshInterval)
|
2021-11-26 22:22:40 -05:00
|
|
|
|
2022-05-26 20:58:09 -04:00
|
|
|
// Load RoleARNs
|
|
|
|
sys.rolesMap = make(map[arn.ARN]string)
|
|
|
|
|
|
|
|
// From OpenID
|
2023-02-26 00:01:37 -05:00
|
|
|
if riMap := sys.OpenIDConfig.GetRoleInfo(); riMap != nil {
|
2022-05-26 20:58:09 -04:00
|
|
|
sys.validateAndAddRolePolicyMappings(ctx, riMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// From AuthN plugin if enabled.
|
2022-08-08 19:16:27 -04:00
|
|
|
if authn := newGlobalAuthNPluginFn(); authn != nil {
|
|
|
|
riMap := authn.GetRoleInfo()
|
2022-05-26 20:58:09 -04:00
|
|
|
sys.validateAndAddRolePolicyMappings(ctx, riMap)
|
2021-11-26 22:22:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.printIAMRoles()
|
2023-08-23 06:07:06 -04:00
|
|
|
|
|
|
|
bootstrapTraceMsg("finishing IAM loading")
|
2021-11-26 22:22:40 -05:00
|
|
|
}
|
|
|
|
|
2023-09-14 18:25:17 -04:00
|
|
|
func (sys *IAMSys) periodicRoutines(ctx context.Context, baseInterval time.Duration) {
|
|
|
|
// Watch for IAM config changes for iamStorageWatcher.
|
|
|
|
watcher, isWatcher := sys.store.IAMStorageAPI.(iamStorageWatcher)
|
|
|
|
if isWatcher {
|
|
|
|
go func() {
|
|
|
|
ch := watcher.watch(ctx, iamConfigPrefix)
|
|
|
|
for event := range ch {
|
|
|
|
if err := sys.loadWatchedEvent(ctx, event); err != nil {
|
|
|
|
// we simply log errors
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Failure in loading watch event: %v", err), logger.WarningKind)
|
2023-09-14 18:25:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
2024-03-28 19:43:50 -04:00
|
|
|
|
|
|
|
// Calculate the waitInterval between periodic refreshes so that each server
|
|
|
|
// independently picks a (uniformly distributed) random time in an interval
|
|
|
|
// of size = baseInterval.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// - if baseInterval=10s, then 5s <= waitInterval() < 15s
|
|
|
|
//
|
|
|
|
// - if baseInterval=10m, then 5m <= waitInterval() < 15m
|
|
|
|
waitInterval := func() time.Duration {
|
|
|
|
// Calculate a random value such that 0 <= value < baseInterval
|
|
|
|
randAmt := time.Duration(r.Float64() * float64(baseInterval))
|
|
|
|
return baseInterval/2 + randAmt
|
2023-09-14 18:25:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var maxDurationSecondsForLog float64 = 5
|
2024-03-28 19:43:50 -04:00
|
|
|
timer := time.NewTimer(waitInterval())
|
2023-09-14 18:25:17 -04:00
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
// Load all IAM items (except STS creds) periodically.
|
|
|
|
refreshStart := time.Now()
|
|
|
|
if err := sys.Load(ctx, false); err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Failure in periodic refresh for IAM (took %.2fs): %v", time.Since(refreshStart).Seconds(), err), logger.WarningKind)
|
2023-09-14 18:25:17 -04:00
|
|
|
} else {
|
|
|
|
took := time.Since(refreshStart).Seconds()
|
|
|
|
if took > maxDurationSecondsForLog {
|
|
|
|
// Log if we took a lot of time to load.
|
|
|
|
logger.Info("IAM refresh took %.2fs", took)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 19:43:50 -04:00
|
|
|
// Purge expired STS credentials.
|
|
|
|
purgeStart := time.Now()
|
|
|
|
if err := sys.store.PurgeExpiredSTS(ctx); err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, fmt.Errorf("Failure in periodic STS purge for IAM (took %.2fs): %v", time.Since(purgeStart).Seconds(), err))
|
2024-03-28 19:43:50 -04:00
|
|
|
} else {
|
|
|
|
took := time.Since(purgeStart).Seconds()
|
|
|
|
if took > maxDurationSecondsForLog {
|
|
|
|
// Log if we took a lot of time to load.
|
|
|
|
logger.Info("IAM expired STS purge took %.2fs", took)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 18:25:17 -04:00
|
|
|
// The following actions are performed about once in 4 times that
|
|
|
|
// IAM is refreshed:
|
|
|
|
if r.Intn(4) == 0 {
|
|
|
|
// Poll and remove accounts for those users who were removed
|
|
|
|
// from LDAP/OpenID.
|
|
|
|
if sys.LDAPConfig.Enabled() {
|
|
|
|
sys.purgeExpiredCredentialsForLDAP(ctx)
|
|
|
|
sys.updateGroupMembershipsForLDAP(ctx)
|
|
|
|
}
|
|
|
|
if sys.OpenIDConfig.ProviderEnabled() {
|
|
|
|
sys.purgeExpiredCredentialsForExternalSSO(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 19:43:50 -04:00
|
|
|
timer.Reset(waitInterval())
|
2023-09-14 18:25:17 -04:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 20:58:09 -04:00
|
|
|
func (sys *IAMSys) validateAndAddRolePolicyMappings(ctx context.Context, m map[arn.ARN]string) {
|
|
|
|
// Validate that policies associated with roles are defined. If
|
|
|
|
// authZ plugin is set, role policies are just claims sent to
|
|
|
|
// the plugin and they need not exist.
|
|
|
|
//
|
|
|
|
// If some mapped policies do not exist, we print some error
|
|
|
|
// messages but continue any way - they can be fixed in the
|
|
|
|
// running server by creating the policies after start up.
|
|
|
|
for arn, rolePolicies := range m {
|
|
|
|
specifiedPoliciesSet := newMappedPolicy(rolePolicies).policySet()
|
|
|
|
validPolicies, _ := sys.store.FilterPolicies(rolePolicies, "")
|
|
|
|
knownPoliciesSet := newMappedPolicy(validPolicies).policySet()
|
|
|
|
unknownPoliciesSet := specifiedPoliciesSet.Difference(knownPoliciesSet)
|
|
|
|
if len(unknownPoliciesSet) > 0 {
|
2022-08-08 19:16:27 -04:00
|
|
|
authz := newGlobalAuthZPluginFn()
|
|
|
|
if authz == nil {
|
2022-05-26 20:58:09 -04:00
|
|
|
// Print a warning that some policies mapped to a role are not defined.
|
|
|
|
errMsg := fmt.Errorf(
|
|
|
|
"The policies \"%s\" mapped to role ARN %s are not defined - this role may not work as expected.",
|
|
|
|
unknownPoliciesSet.ToSlice(), arn.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
authZLogIf(ctx, errMsg, logger.WarningKind)
|
2022-05-26 20:58:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sys.rolesMap[arn] = rolePolicies
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:22:40 -05:00
|
|
|
// Prints IAM role ARNs.
|
|
|
|
func (sys *IAMSys) printIAMRoles() {
|
2021-12-02 18:43:39 -05:00
|
|
|
if len(sys.rolesMap) == 0 {
|
2021-11-26 22:22:40 -05:00
|
|
|
return
|
|
|
|
}
|
2021-12-02 18:43:39 -05:00
|
|
|
var arns []string
|
|
|
|
for arn := range sys.rolesMap {
|
|
|
|
arns = append(arns, arn.String())
|
|
|
|
}
|
|
|
|
sort.Strings(arns)
|
2021-11-26 22:22:40 -05:00
|
|
|
msgs := make([]string, 0, len(arns))
|
|
|
|
for _, arn := range arns {
|
|
|
|
msgs = append(msgs, color.Bold(arn))
|
|
|
|
}
|
|
|
|
|
2022-03-03 16:21:16 -05:00
|
|
|
logger.Info(fmt.Sprintf("%s %s", color.Blue("IAM Roles:"), strings.Join(msgs, " ")))
|
2021-10-18 14:21:57 -04:00
|
|
|
}
|
|
|
|
|
2021-10-20 06:22:35 -04:00
|
|
|
// HasWatcher - returns if the IAM system has a watcher to be notified of
|
|
|
|
// changes.
|
|
|
|
func (sys *IAMSys) HasWatcher() bool {
|
2021-11-03 22:47:49 -04:00
|
|
|
return sys.store.HasWatcher()
|
2021-10-20 06:22:35 -04:00
|
|
|
}
|
|
|
|
|
2021-11-15 17:14:22 -05:00
|
|
|
func (sys *IAMSys) loadWatchedEvent(ctx context.Context, event iamWatchEvent) (err error) {
|
2021-10-18 14:21:57 -04:00
|
|
|
usersPrefix := strings.HasPrefix(event.keyPath, iamConfigUsersPrefix)
|
|
|
|
groupsPrefix := strings.HasPrefix(event.keyPath, iamConfigGroupsPrefix)
|
|
|
|
stsPrefix := strings.HasPrefix(event.keyPath, iamConfigSTSPrefix)
|
|
|
|
svcPrefix := strings.HasPrefix(event.keyPath, iamConfigServiceAccountsPrefix)
|
|
|
|
policyPrefix := strings.HasPrefix(event.keyPath, iamConfigPoliciesPrefix)
|
|
|
|
policyDBUsersPrefix := strings.HasPrefix(event.keyPath, iamConfigPolicyDBUsersPrefix)
|
|
|
|
policyDBSTSUsersPrefix := strings.HasPrefix(event.keyPath, iamConfigPolicyDBSTSUsersPrefix)
|
|
|
|
policyDBGroupsPrefix := strings.HasPrefix(event.keyPath, iamConfigPolicyDBGroupsPrefix)
|
|
|
|
|
2021-11-15 17:14:22 -05:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2021-10-18 14:21:57 -04:00
|
|
|
defer cancel()
|
|
|
|
|
2021-11-16 12:28:29 -05:00
|
|
|
switch {
|
|
|
|
case usersPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(event.keyPath, iamConfigUsersPrefix))
|
|
|
|
err = sys.store.UserNotificationHandler(ctx, accessKey, regUser)
|
|
|
|
case stsPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(event.keyPath, iamConfigSTSPrefix))
|
|
|
|
err = sys.store.UserNotificationHandler(ctx, accessKey, stsUser)
|
|
|
|
case svcPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(event.keyPath, iamConfigServiceAccountsPrefix))
|
|
|
|
err = sys.store.UserNotificationHandler(ctx, accessKey, svcUser)
|
|
|
|
case groupsPrefix:
|
|
|
|
group := path.Dir(strings.TrimPrefix(event.keyPath, iamConfigGroupsPrefix))
|
|
|
|
err = sys.store.GroupNotificationHandler(ctx, group)
|
|
|
|
case policyPrefix:
|
|
|
|
policyName := path.Dir(strings.TrimPrefix(event.keyPath, iamConfigPoliciesPrefix))
|
|
|
|
err = sys.store.PolicyNotificationHandler(ctx, policyName)
|
|
|
|
case policyDBUsersPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(event.keyPath, iamConfigPolicyDBUsersPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
|
|
|
err = sys.store.PolicyMappingNotificationHandler(ctx, user, false, regUser)
|
|
|
|
case policyDBSTSUsersPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(event.keyPath, iamConfigPolicyDBSTSUsersPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
|
|
|
err = sys.store.PolicyMappingNotificationHandler(ctx, user, false, stsUser)
|
|
|
|
case policyDBGroupsPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(event.keyPath, iamConfigPolicyDBGroupsPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
|
|
|
err = sys.store.PolicyMappingNotificationHandler(ctx, user, true, regUser)
|
2021-10-18 14:21:57 -04:00
|
|
|
}
|
|
|
|
return err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:43:39 -05:00
|
|
|
// HasRolePolicy - returns if a role policy is configured for IAM.
|
|
|
|
func (sys *IAMSys) HasRolePolicy() bool {
|
|
|
|
return len(sys.rolesMap) > 0
|
2021-11-26 22:22:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRolePolicy - returns policies associated with a role ARN.
|
2022-04-28 21:27:09 -04:00
|
|
|
func (sys *IAMSys) GetRolePolicy(arnStr string) (arn.ARN, string, error) {
|
|
|
|
roleArn, err := arn.Parse(arnStr)
|
2021-11-26 22:22:40 -05:00
|
|
|
if err != nil {
|
2022-04-28 21:27:09 -04:00
|
|
|
return arn.ARN{}, "", fmt.Errorf("RoleARN parse err: %v", err)
|
2021-11-26 22:22:40 -05:00
|
|
|
}
|
2022-04-28 21:27:09 -04:00
|
|
|
rolePolicy, ok := sys.rolesMap[roleArn]
|
2021-11-26 22:22:40 -05:00
|
|
|
if !ok {
|
2022-04-28 21:27:09 -04:00
|
|
|
return arn.ARN{}, "", fmt.Errorf("RoleARN %s is not defined.", arnStr)
|
2021-11-26 22:22:40 -05:00
|
|
|
}
|
2022-04-28 21:27:09 -04:00
|
|
|
return roleArn, rolePolicy, nil
|
2021-11-26 22:22:40 -05:00
|
|
|
}
|
|
|
|
|
2024-02-07 23:39:53 -05:00
|
|
|
// DeletePolicy - deletes a canned policy from backend. `notifyPeers` is true
|
|
|
|
// whenever this is called via the API. It is false when called via a
|
|
|
|
// notification from another peer. This is to avoid infinite loops.
|
2021-11-29 17:38:57 -05:00
|
|
|
func (sys *IAMSys) DeletePolicy(ctx context.Context, policyName string, notifyPeers bool) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-09 17:00:01 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
for _, v := range policy.DefaultPolicies {
|
2023-06-13 14:06:17 -04:00
|
|
|
if v.Name == policyName {
|
|
|
|
if err := checkConfig(ctx, globalObjectAPI, getPolicyDocPath(policyName)); err != nil && err == errConfigNotFound {
|
|
|
|
return fmt.Errorf("inbuilt policy `%s` not allowed to be deleted", policyName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 23:39:53 -05:00
|
|
|
err := sys.store.DeletePolicy(ctx, policyName, !notifyPeers)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !notifyPeers || sys.HasWatcher() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to delete policy
|
2022-02-07 13:39:57 -05:00
|
|
|
for _, nerr := range globalNotificationSys.DeletePolicy(policyName) {
|
2021-11-29 17:38:57 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:03:39 -05:00
|
|
|
// InfoPolicy - returns the policy definition with some metadata.
|
|
|
|
func (sys *IAMSys) InfoPolicy(policyName string) (*madmin.PolicyInfo, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2021-12-11 12:03:39 -05:00
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := sys.store.GetPolicyDoc(policyName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pdata, err := json.Marshal(d.Policy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-26 14:23:13 -04:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:03:39 -05:00
|
|
|
return &madmin.PolicyInfo{
|
|
|
|
PolicyName: policyName,
|
|
|
|
Policy: pdata,
|
|
|
|
CreateDate: d.CreateDate,
|
|
|
|
UpdateDate: d.UpdateDate,
|
|
|
|
}, nil
|
2019-09-26 14:23:13 -04:00
|
|
|
}
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
// ListPolicies - lists all canned policies.
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) ListPolicies(ctx context.Context, bucketName string) (map[string]policy.Policy, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-16 15:48:19 -04:00
|
|
|
return nil, errServerNotInitialized
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2023-08-23 06:07:06 -04:00
|
|
|
return sys.store.ListPolicies(ctx, bucketName)
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2022-04-24 05:36:31 -04:00
|
|
|
// ListPolicyDocs - lists all canned policy docs.
|
|
|
|
func (sys *IAMSys) ListPolicyDocs(ctx context.Context, bucketName string) (map[string]PolicyDoc, error) {
|
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2023-08-23 06:07:06 -04:00
|
|
|
return sys.store.ListPolicyDocs(ctx, bucketName)
|
2022-04-24 05:36:31 -04:00
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
// SetPolicy - sets a new named policy.
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) SetPolicy(ctx context.Context, policyName string, p policy.Policy) (time.Time, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return time.Time{}, errServerNotInitialized
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err := sys.store.SetPolicy(ctx, policyName, p)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, err
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !sys.HasWatcher() {
|
|
|
|
// Notify all other MinIO peers to reload policy
|
2022-02-07 13:39:57 -05:00
|
|
|
for _, nerr := range globalNotificationSys.LoadPolicy(policyName) {
|
2021-11-29 17:38:57 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// DeleteUser - delete user (only for long-term users not STS users).
|
2021-12-21 05:16:50 -05:00
|
|
|
func (sys *IAMSys) DeleteUser(ctx context.Context, accessKey string, notifyPeers bool) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-09 17:00:01 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-12-21 05:16:50 -05:00
|
|
|
if err := sys.store.DeleteUser(ctx, accessKey, regUser); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to delete user.
|
|
|
|
if notifyPeers && !sys.HasWatcher() {
|
2022-02-07 13:39:57 -05:00
|
|
|
for _, nerr := range globalNotificationSys.DeleteUser(accessKey) {
|
2021-12-21 05:16:50 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2021-12-21 05:16:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2020-11-08 00:03:06 -05:00
|
|
|
// CurrentPolicies - returns comma separated policy string, from
|
|
|
|
// an input policy after validating if there are any current
|
|
|
|
// policies which exist on MinIO corresponding to the input.
|
|
|
|
func (sys *IAMSys) CurrentPolicies(policyName string) string {
|
|
|
|
if !sys.Initialized() {
|
2020-10-21 00:28:24 -04:00
|
|
|
return ""
|
|
|
|
}
|
2020-11-08 00:03:06 -05:00
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
policies, _ := sys.store.FilterPolicies(policyName, "")
|
|
|
|
return policies
|
2020-07-19 18:34:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:38:57 -05:00
|
|
|
func (sys *IAMSys) notifyForUser(ctx context.Context, accessKey string, isTemp bool) {
|
|
|
|
// Notify all other MinIO peers to reload user.
|
|
|
|
if !sys.HasWatcher() {
|
2022-02-07 13:39:57 -05:00
|
|
|
for _, nerr := range globalNotificationSys.LoadUser(accessKey, isTemp) {
|
2021-11-29 17:38:57 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Map policy to parent for STS (#13884)
When STS credentials are created for a user, a unique (hopefully stable) parent
user value exists for the credential, which corresponds to the user for whom the
credentials are created. The access policy is mapped to this parent-user and is
persisted. This helps ensure that all STS credentials of a user have the same
policy assignment at all times.
Before this change, for an OIDC STS credential, when the policy claim changes in
the provider (when not using RoleARNs), the change would not take effect on
existing credentials, but only on new ones.
To support existing STS credentials without parent-user policy mappings, we
lookup the policy in the policy claim value. This behavior should be deprecated
when such support is no longer required, as it can still lead to stale
policy mappings.
Additionally this change also simplifies the implementation for all non-RoleARN
STS credentials. Specifically, for AssumeRole (internal IDP) STS credentials,
policies are picked up from the parent user's policies; for
AssumeRoleWithCertificate STS credentials, policies are picked up from the
parent user mapping created when the STS credential is generated.
AssumeRoleWithLDAP already picks up policies mapped to the virtual parent user.
2021-12-17 03:46:30 -05:00
|
|
|
// SetTempUser - set temporary user credentials, these credentials have an
|
|
|
|
// expiry. The permissions for these STS credentials is determined in one of the
|
|
|
|
// following ways:
|
|
|
|
//
|
|
|
|
// - RoleARN - if a role-arn is specified in the request, the STS credential's
|
|
|
|
// policy is the role's policy.
|
|
|
|
//
|
|
|
|
// - inherited from parent - this is the case for AssumeRole API, where the
|
|
|
|
// parent user is an actual real user with their own (permanent) credentials and
|
|
|
|
// policy association.
|
|
|
|
//
|
|
|
|
// - inherited from "virtual" parent - this is the case for AssumeRoleWithLDAP
|
|
|
|
// where the parent user is the DN of the actual LDAP user. The parent user
|
|
|
|
// itself cannot login, but the policy associated with them determines the base
|
|
|
|
// policy for the STS credential. The policy mapping can be updated by the
|
|
|
|
// administrator.
|
|
|
|
//
|
|
|
|
// - from `Subject.CommonName` field from the STS request for
|
|
|
|
// AssumeRoleWithCertificate. In this case, the policy for the STS credential
|
|
|
|
// has the same name as the value of this field.
|
|
|
|
//
|
|
|
|
// - from special JWT claim from STS request for AssumeRoleWithOIDC API (when
|
|
|
|
// not using RoleARN). The claim value can be a string or a list and refers to
|
|
|
|
// the names of access policies.
|
|
|
|
//
|
|
|
|
// For all except the RoleARN case, the implementation is the same - the policy
|
|
|
|
// for the STS credential is associated with a parent user. For the
|
|
|
|
// AssumeRoleWithCertificate case, the "virtual" parent user is the value of the
|
|
|
|
// `Subject.CommonName` field. For the OIDC (without RoleARN) case the "virtual"
|
|
|
|
// parent is derived as a concatenation of the `sub` and `iss` fields. The
|
|
|
|
// policies applicable to the STS credential are associated with this "virtual"
|
|
|
|
// parent.
|
|
|
|
//
|
|
|
|
// When a policyName is given to this function, the policy association is
|
|
|
|
// created and stored in the IAM store. Thus, it should NOT be given for the
|
|
|
|
// role-arn case (because the role-to-policy mapping is separately stored
|
|
|
|
// elsewhere), the AssumeRole case (because the parent user is real and their
|
|
|
|
// policy is associated via policy-set API) and the AssumeRoleWithLDAP case
|
|
|
|
// (because the policy association is made via policy-set API).
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) SetTempUser(ctx context.Context, accessKey string, cred auth.Credentials, policyName string) (time.Time, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return time.Time{}, errServerNotInitialized
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2022-05-30 13:58:37 -04:00
|
|
|
if newGlobalAuthZPluginFn() != nil {
|
2021-11-03 22:47:49 -04:00
|
|
|
// If OPA is set, we do not need to set a policy mapping.
|
|
|
|
policyName = ""
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err := sys.store.SetTempUser(ctx, accessKey, cred, policyName)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return time.Time{}, err
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.notifyForUser(ctx, cred.AccessKey, true)
|
2022-01-06 18:52:43 -05:00
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2021-05-27 13:15:02 -04:00
|
|
|
// ListBucketUsers - list all users who can access this 'bucket'
|
2022-04-19 12:00:19 -04:00
|
|
|
func (sys *IAMSys) ListBucketUsers(ctx context.Context, bucket string) (map[string]madmin.UserInfo, error) {
|
2021-11-03 22:47:49 -04:00
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
2021-05-27 13:15:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-19 12:00:19 -04:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
return sys.store.GetBucketUsers(bucket)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2021-05-27 13:15:02 -04:00
|
|
|
}
|
|
|
|
|
2018-10-13 03:18:43 -04:00
|
|
|
// ListUsers - list all users.
|
2022-04-19 12:00:19 -04:00
|
|
|
func (sys *IAMSys) ListUsers(ctx context.Context) (map[string]madmin.UserInfo, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-13 03:18:43 -04:00
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
2022-04-19 12:00:19 -04:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
return sys.store.GetUsers(), nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2018-10-13 03:18:43 -04:00
|
|
|
}
|
|
|
|
|
2022-04-16 00:26:02 -04:00
|
|
|
// ListLDAPUsers - list LDAP users which has
|
2022-10-04 13:41:47 -04:00
|
|
|
func (sys *IAMSys) ListLDAPUsers(ctx context.Context) (map[string]madmin.UserInfo, error) {
|
2022-04-16 00:26:02 -04:00
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
if sys.usersSysType != LDAPUsersSysType {
|
|
|
|
return nil, errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2022-10-04 13:41:47 -04:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
ldapUsers := make(map[string]madmin.UserInfo)
|
|
|
|
for user, policy := range sys.store.GetUsersWithMappedPolicies() {
|
|
|
|
ldapUsers[user] = madmin.UserInfo{
|
|
|
|
PolicyName: policy,
|
|
|
|
Status: madmin.AccountEnabled,
|
|
|
|
}
|
2022-04-16 00:26:02 -04:00
|
|
|
}
|
2022-10-04 13:41:47 -04:00
|
|
|
return ldapUsers, nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
2022-04-16 00:26:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 17:35:09 -05:00
|
|
|
// QueryLDAPPolicyEntities - queries policy associations for LDAP users/groups/policies.
|
|
|
|
func (sys *IAMSys) QueryLDAPPolicyEntities(ctx context.Context, q madmin.PolicyEntitiesQuery) (*madmin.PolicyEntitiesResult, error) {
|
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2023-05-26 01:31:05 -04:00
|
|
|
if !sys.LDAPConfig.Enabled() {
|
2022-11-07 17:35:09 -05:00
|
|
|
return nil, errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
2023-05-26 01:31:05 -04:00
|
|
|
pe := sys.store.ListPolicyMappings(q, sys.LDAPConfig.IsLDAPUserDN, sys.LDAPConfig.IsLDAPGroupDN)
|
2022-11-07 17:35:09 -05:00
|
|
|
pe.Timestamp = UTCNow()
|
|
|
|
return &pe, nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 01:44:50 -04:00
|
|
|
// IsTempUser - returns if given key is a temporary user and parent user.
|
2021-02-25 16:49:59 -05:00
|
|
|
func (sys *IAMSys) IsTempUser(name string) (bool, string, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2021-02-25 16:49:59 -05:00
|
|
|
return false, "", errServerNotInitialized
|
2019-12-19 17:21:21 -05:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
u, found := sys.store.GetUser(name)
|
2019-12-19 17:21:21 -05:00
|
|
|
if !found {
|
2021-02-25 16:49:59 -05:00
|
|
|
return false, "", errNoSuchUser
|
|
|
|
}
|
2022-07-01 16:19:13 -04:00
|
|
|
cred := u.Credentials
|
2021-02-25 16:49:59 -05:00
|
|
|
if cred.IsTemp() {
|
|
|
|
return true, cred.ParentUser, nil
|
2019-12-19 17:21:21 -05:00
|
|
|
}
|
|
|
|
|
2021-02-25 16:49:59 -05:00
|
|
|
return false, "", nil
|
2019-12-19 17:21:21 -05:00
|
|
|
}
|
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
// IsServiceAccount - returns if given key is a service account
|
|
|
|
func (sys *IAMSys) IsServiceAccount(name string) (bool, string, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-03-17 13:36:13 -04:00
|
|
|
return false, "", errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
u, found := sys.store.GetUser(name)
|
2020-03-17 13:36:13 -04:00
|
|
|
if !found {
|
|
|
|
return false, "", errNoSuchUser
|
|
|
|
}
|
2022-07-01 16:19:13 -04:00
|
|
|
cred := u.Credentials
|
2020-04-28 15:49:56 -04:00
|
|
|
if cred.IsServiceAccount() {
|
|
|
|
return true, cred.ParentUser, nil
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, "", nil
|
|
|
|
}
|
|
|
|
|
2019-08-13 16:41:06 -04:00
|
|
|
// GetUserInfo - get info on a user.
|
2021-11-15 17:14:22 -05:00
|
|
|
func (sys *IAMSys) GetUserInfo(ctx context.Context, name string) (u madmin.UserInfo, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-08-13 16:41:06 -04:00
|
|
|
return u, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2023-09-13 15:43:46 -04:00
|
|
|
loadUserCalled := false
|
2021-03-02 20:08:25 -05:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
default:
|
2021-11-15 17:14:22 -05:00
|
|
|
sys.store.LoadUser(ctx, name)
|
2023-09-13 15:43:46 -04:00
|
|
|
loadUserCalled = true
|
2021-11-02 16:51:42 -04:00
|
|
|
}
|
|
|
|
|
2023-09-13 15:43:46 -04:00
|
|
|
userInfo, err := sys.store.GetUserInfo(name)
|
|
|
|
if err == errNoSuchUser && !loadUserCalled {
|
|
|
|
sys.store.LoadUser(ctx, name)
|
|
|
|
userInfo, err = sys.store.GetUserInfo(name)
|
|
|
|
}
|
|
|
|
return userInfo, err
|
2019-08-13 16:41:06 -04:00
|
|
|
}
|
|
|
|
|
2022-12-19 13:37:03 -05:00
|
|
|
// QueryPolicyEntities - queries policy associations for builtin users/groups/policies.
|
|
|
|
func (sys *IAMSys) QueryPolicyEntities(ctx context.Context, q madmin.PolicyEntitiesQuery) (*madmin.PolicyEntitiesResult, error) {
|
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
2023-05-26 01:31:05 -04:00
|
|
|
var userPredicate, groupPredicate func(string) bool
|
|
|
|
if sys.LDAPConfig.Enabled() {
|
|
|
|
userPredicate = func(s string) bool {
|
|
|
|
return !sys.LDAPConfig.IsLDAPUserDN(s)
|
|
|
|
}
|
|
|
|
groupPredicate = func(s string) bool {
|
|
|
|
return !sys.LDAPConfig.IsLDAPGroupDN(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pe := sys.store.ListPolicyMappings(q, userPredicate, groupPredicate)
|
2022-12-19 13:37:03 -05:00
|
|
|
pe.Timestamp = UTCNow()
|
|
|
|
return &pe, nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:55:23 -04:00
|
|
|
// SetUserStatus - sets current user status, supports disabled or enabled.
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) SetUserStatus(ctx context.Context, accessKey string, status madmin.AccountStatus) (updatedAt time.Time, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2018-10-16 17:55:23 -04:00
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errIAMActionNotAllowed
|
2021-01-25 23:01:49 -05:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err = sys.store.SetUserStatus(ctx, accessKey, status)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.notifyForUser(ctx, accessKey, false)
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *IAMSys) notifyForServiceAccount(ctx context.Context, accessKey string) {
|
|
|
|
// Notify all other Minio peers to reload the service account
|
|
|
|
if !sys.HasWatcher() {
|
2022-02-07 13:39:57 -05:00
|
|
|
for _, nerr := range globalNotificationSys.LoadServiceAccount(accessKey) {
|
2021-11-29 17:38:57 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 17:55:23 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
type newServiceAccountOpts struct {
|
2023-09-14 17:50:16 -04:00
|
|
|
sessionPolicy *policy.Policy
|
2023-04-28 15:24:14 -04:00
|
|
|
accessKey string
|
|
|
|
secretKey string
|
2023-05-17 20:05:36 -04:00
|
|
|
name, description string
|
2023-04-28 15:24:14 -04:00
|
|
|
expiration *time.Time
|
|
|
|
allowSiteReplicatorAccount bool // allow creating internal service account for site-replication.
|
2021-07-24 14:57:36 -04:00
|
|
|
|
2021-10-05 14:49:33 -04:00
|
|
|
claims map[string]interface{}
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
// NewServiceAccount - create a new service account
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) NewServiceAccount(ctx context.Context, parentUser string, groups []string, opts newServiceAccountOpts) (auth.Credentials, time.Time, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return auth.Credentials{}, time.Time{}, errServerNotInitialized
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2021-08-27 00:57:30 -04:00
|
|
|
if parentUser == "" {
|
2022-07-01 16:19:13 -04:00
|
|
|
return auth.Credentials{}, time.Time{}, errInvalidArgument
|
2021-08-27 00:57:30 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 05:31:43 -05:00
|
|
|
if len(opts.accessKey) > 0 && len(opts.secretKey) == 0 {
|
|
|
|
return auth.Credentials{}, time.Time{}, auth.ErrNoSecretKeyWithAccessKey
|
|
|
|
}
|
|
|
|
if len(opts.secretKey) > 0 && len(opts.accessKey) == 0 {
|
|
|
|
return auth.Credentials{}, time.Time{}, auth.ErrNoAccessKeyWithSecretKey
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
var policyBuf []byte
|
2021-04-15 01:51:14 -04:00
|
|
|
if opts.sessionPolicy != nil {
|
|
|
|
err := opts.sessionPolicy.Validate()
|
2020-04-14 14:28:56 -04:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return auth.Credentials{}, time.Time{}, err
|
2020-04-14 14:28:56 -04:00
|
|
|
}
|
2021-04-15 01:51:14 -04:00
|
|
|
policyBuf, err = json.Marshal(opts.sessionPolicy)
|
2020-03-23 17:17:18 -04:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return auth.Credentials{}, time.Time{}, err
|
2020-03-23 17:17:18 -04:00
|
|
|
}
|
2023-05-09 03:53:08 -04:00
|
|
|
if len(policyBuf) > 2048 {
|
|
|
|
return auth.Credentials{}, time.Time{}, errSessionPolicyTooLarge
|
2020-03-23 17:17:18 -04:00
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2021-08-27 00:57:30 -04:00
|
|
|
// found newly requested service account, to be same as
|
|
|
|
// parentUser, reject such operations.
|
|
|
|
if parentUser == opts.accessKey {
|
2022-07-01 16:19:13 -04:00
|
|
|
return auth.Credentials{}, time.Time{}, errIAMActionNotAllowed
|
2021-08-27 00:57:30 -04:00
|
|
|
}
|
2023-04-28 15:24:14 -04:00
|
|
|
if siteReplicatorSvcAcc == opts.accessKey && !opts.allowSiteReplicatorAccount {
|
|
|
|
return auth.Credentials{}, time.Time{}, errIAMActionNotAllowed
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
m := make(map[string]interface{})
|
|
|
|
m[parentClaim] = parentUser
|
2020-03-23 17:17:18 -04:00
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
if len(policyBuf) > 0 {
|
2023-09-14 17:50:16 -04:00
|
|
|
m[policy.SessionPolicyName] = base64.StdEncoding.EncodeToString(policyBuf)
|
2022-05-02 20:56:19 -04:00
|
|
|
m[iamPolicyClaimNameSA()] = embeddedPolicyType
|
2020-03-23 17:17:18 -04:00
|
|
|
} else {
|
2022-05-02 20:56:19 -04:00
|
|
|
m[iamPolicyClaimNameSA()] = inheritedPolicyType
|
2020-03-23 17:17:18 -04:00
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2024-01-31 13:56:45 -05:00
|
|
|
// Add all the necessary claims for the service account.
|
2021-10-05 14:49:33 -04:00
|
|
|
for k, v := range opts.claims {
|
|
|
|
_, ok := m[k]
|
|
|
|
if !ok {
|
|
|
|
m[k] = v
|
|
|
|
}
|
2021-07-24 14:57:36 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 12:09:22 -04:00
|
|
|
var accessKey, secretKey string
|
2021-11-03 22:47:49 -04:00
|
|
|
var err error
|
2024-02-13 05:31:43 -05:00
|
|
|
if len(opts.accessKey) > 0 || len(opts.secretKey) > 0 {
|
2022-03-14 12:09:22 -04:00
|
|
|
accessKey, secretKey = opts.accessKey, opts.secretKey
|
2021-04-15 01:51:14 -04:00
|
|
|
} else {
|
2022-03-14 12:09:22 -04:00
|
|
|
accessKey, secretKey, err = auth.GenerateCredentials()
|
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return auth.Credentials{}, time.Time{}, err
|
2022-03-14 12:09:22 -04:00
|
|
|
}
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
2022-03-14 12:09:22 -04:00
|
|
|
cred, err := auth.CreateNewCredentialsWithMetadata(accessKey, secretKey, m, secretKey)
|
2020-03-17 13:36:13 -04:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return auth.Credentials{}, time.Time{}, err
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
cred.ParentUser = parentUser
|
2021-02-25 16:49:59 -05:00
|
|
|
cred.Groups = groups
|
2021-04-15 17:47:58 -04:00
|
|
|
cred.Status = string(auth.AccountOn)
|
2023-05-17 20:05:36 -04:00
|
|
|
cred.Name = opts.name
|
|
|
|
cred.Description = opts.description
|
2020-04-28 15:49:56 -04:00
|
|
|
|
2023-02-27 13:10:22 -05:00
|
|
|
if opts.expiration != nil {
|
|
|
|
expirationInUTC := opts.expiration.UTC()
|
|
|
|
if err := validateSvcExpirationInUTC(expirationInUTC); err != nil {
|
|
|
|
return auth.Credentials{}, time.Time{}, err
|
|
|
|
}
|
|
|
|
cred.Expiration = expirationInUTC
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err := sys.store.AddServiceAccount(ctx, cred)
|
2021-11-03 22:47:49 -04:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return auth.Credentials{}, time.Time{}, err
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
2021-11-29 17:38:57 -05:00
|
|
|
|
|
|
|
sys.notifyForServiceAccount(ctx, cred.AccessKey)
|
2022-07-01 16:19:13 -04:00
|
|
|
return cred, updatedAt, nil
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
type updateServiceAccountOpts struct {
|
2023-09-14 17:50:16 -04:00
|
|
|
sessionPolicy *policy.Policy
|
2023-05-17 20:05:36 -04:00
|
|
|
secretKey string
|
|
|
|
status string
|
|
|
|
name, description string
|
|
|
|
expiration *time.Time
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateServiceAccount - edit a service account
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) UpdateServiceAccount(ctx context.Context, accessKey string, opts updateServiceAccountOpts) (updatedAt time.Time, err error) {
|
2021-04-15 01:51:14 -04:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err = sys.store.UpdateServiceAccount(ctx, accessKey, opts)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, err
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.notifyForServiceAccount(ctx, accessKey)
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
|
|
|
|
2023-12-15 16:00:43 -05:00
|
|
|
// ListServiceAccounts - lists all service accounts associated to a specific user
|
2021-04-15 01:51:14 -04:00
|
|
|
func (sys *IAMSys) ListServiceAccounts(ctx context.Context, accessKey string) ([]auth.Credentials, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-04-24 15:10:09 -04:00
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:00:19 -04:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
return sys.store.ListServiceAccounts(ctx, accessKey)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
2023-12-15 16:00:43 -05:00
|
|
|
// ListTempAccounts - lists all temporary service accounts associated to a specific user
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) ListTempAccounts(ctx context.Context, accessKey string) ([]UserIdentity, error) {
|
2022-04-28 05:39:00 -04:00
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
return sys.store.ListTempAccounts(ctx, accessKey)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 16:00:43 -05:00
|
|
|
// ListSTSAccounts - lists all STS accounts associated to a specific user
|
|
|
|
func (sys *IAMSys) ListSTSAccounts(ctx context.Context, accessKey string) ([]auth.Credentials, error) {
|
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
return sys.store.ListSTSAccounts(ctx, accessKey)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 16:16:37 -05:00
|
|
|
// GetServiceAccount - wrapper method to get information about a service account
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) GetServiceAccount(ctx context.Context, accessKey string) (auth.Credentials, *policy.Policy, error) {
|
2021-11-30 16:16:37 -05:00
|
|
|
sa, embeddedPolicy, err := sys.getServiceAccount(ctx, accessKey)
|
|
|
|
if err != nil {
|
2022-11-22 10:26:33 -05:00
|
|
|
return auth.Credentials{}, nil, err
|
2021-11-30 16:16:37 -05:00
|
|
|
}
|
|
|
|
// Hide secret & session keys
|
2022-07-01 16:19:13 -04:00
|
|
|
sa.Credentials.SecretKey = ""
|
|
|
|
sa.Credentials.SessionToken = ""
|
|
|
|
return sa.Credentials, embeddedPolicy, nil
|
2021-11-30 16:16:37 -05:00
|
|
|
}
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) getServiceAccount(ctx context.Context, accessKey string) (UserIdentity, *policy.Policy, error) {
|
2022-12-13 11:38:50 -05:00
|
|
|
sa, jwtClaims, err := sys.getAccountWithClaims(ctx, accessKey)
|
2022-11-22 10:26:33 -05:00
|
|
|
if err != nil {
|
|
|
|
if err == errNoSuchAccount {
|
|
|
|
return UserIdentity{}, nil, errNoSuchServiceAccount
|
|
|
|
}
|
|
|
|
return UserIdentity{}, nil, err
|
|
|
|
}
|
|
|
|
if !sa.Credentials.IsServiceAccount() {
|
|
|
|
return UserIdentity{}, nil, errNoSuchServiceAccount
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
var embeddedPolicy *policy.Policy
|
2022-12-13 11:38:50 -05:00
|
|
|
|
|
|
|
pt, ptok := jwtClaims.Lookup(iamPolicyClaimNameSA())
|
2023-09-14 17:50:16 -04:00
|
|
|
sp, spok := jwtClaims.Lookup(policy.SessionPolicyName)
|
2022-12-13 11:38:50 -05:00
|
|
|
if ptok && spok && pt == embeddedPolicyType {
|
|
|
|
policyBytes, err := base64.StdEncoding.DecodeString(sp)
|
|
|
|
if err != nil {
|
|
|
|
return UserIdentity{}, nil, err
|
|
|
|
}
|
2023-09-14 17:50:16 -04:00
|
|
|
embeddedPolicy, err = policy.ParseConfig(bytes.NewReader(policyBytes))
|
2022-12-13 11:38:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return UserIdentity{}, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 10:26:33 -05:00
|
|
|
return sa, embeddedPolicy, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 11:38:50 -05:00
|
|
|
// GetTemporaryAccount - wrapper method to get information about a temporary account
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) GetTemporaryAccount(ctx context.Context, accessKey string) (auth.Credentials, *policy.Policy, error) {
|
2022-12-13 11:38:50 -05:00
|
|
|
tmpAcc, embeddedPolicy, err := sys.getTempAccount(ctx, accessKey)
|
|
|
|
if err != nil {
|
|
|
|
return auth.Credentials{}, nil, err
|
|
|
|
}
|
|
|
|
// Hide secret & session keys
|
|
|
|
tmpAcc.Credentials.SecretKey = ""
|
|
|
|
tmpAcc.Credentials.SessionToken = ""
|
|
|
|
return tmpAcc.Credentials, embeddedPolicy, nil
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) getTempAccount(ctx context.Context, accessKey string) (UserIdentity, *policy.Policy, error) {
|
2022-12-13 11:38:50 -05:00
|
|
|
tmpAcc, claims, err := sys.getAccountWithClaims(ctx, accessKey)
|
2022-11-22 10:26:33 -05:00
|
|
|
if err != nil {
|
|
|
|
if err == errNoSuchAccount {
|
|
|
|
return UserIdentity{}, nil, errNoSuchTempAccount
|
|
|
|
}
|
|
|
|
return UserIdentity{}, nil, err
|
|
|
|
}
|
|
|
|
if !tmpAcc.Credentials.IsTemp() {
|
|
|
|
return UserIdentity{}, nil, errNoSuchTempAccount
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
var embeddedPolicy *policy.Policy
|
2022-12-13 11:38:50 -05:00
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
sp, spok := claims.Lookup(policy.SessionPolicyName)
|
2022-12-13 11:38:50 -05:00
|
|
|
if spok {
|
|
|
|
policyBytes, err := base64.StdEncoding.DecodeString(sp)
|
|
|
|
if err != nil {
|
|
|
|
return UserIdentity{}, nil, err
|
|
|
|
}
|
2023-09-14 17:50:16 -04:00
|
|
|
embeddedPolicy, err = policy.ParseConfig(bytes.NewReader(policyBytes))
|
2022-12-13 11:38:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return UserIdentity{}, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 10:26:33 -05:00
|
|
|
return tmpAcc, embeddedPolicy, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 11:38:50 -05:00
|
|
|
// getAccountWithClaims - gets information about an account with claims
|
|
|
|
func (sys *IAMSys) getAccountWithClaims(ctx context.Context, accessKey string) (UserIdentity, *jwt.MapClaims, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-12-13 11:38:50 -05:00
|
|
|
return UserIdentity{}, nil, errServerNotInitialized
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:38:50 -05:00
|
|
|
acc, ok := sys.store.GetUser(accessKey)
|
2022-11-22 10:26:33 -05:00
|
|
|
if !ok {
|
2022-12-13 11:38:50 -05:00
|
|
|
return UserIdentity{}, nil, errNoSuchAccount
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
2021-04-15 01:51:14 -04:00
|
|
|
|
2023-02-27 13:10:22 -05:00
|
|
|
jwtClaims, err := extractJWTClaims(acc)
|
2022-01-10 17:26:26 -05:00
|
|
|
if err != nil {
|
2023-02-27 13:10:22 -05:00
|
|
|
return UserIdentity{}, nil, err
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
|
|
|
|
2022-12-13 11:38:50 -05:00
|
|
|
return acc, jwtClaims, nil
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
2021-10-06 19:36:31 -04:00
|
|
|
// GetClaimsForSvcAcc - gets the claims associated with the service account.
|
|
|
|
func (sys *IAMSys) GetClaimsForSvcAcc(ctx context.Context, accessKey string) (map[string]interface{}, error) {
|
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
if sys.usersSysType != LDAPUsersSysType {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
sa, ok := sys.store.GetUser(accessKey)
|
2022-07-01 16:19:13 -04:00
|
|
|
if !ok || !sa.Credentials.IsServiceAccount() {
|
2021-10-06 19:36:31 -04:00
|
|
|
return nil, errNoSuchServiceAccount
|
|
|
|
}
|
|
|
|
|
2023-02-27 13:10:22 -05:00
|
|
|
jwtClaims, err := extractJWTClaims(sa)
|
2021-10-06 19:36:31 -04:00
|
|
|
if err != nil {
|
2023-02-27 13:10:22 -05:00
|
|
|
return nil, err
|
2021-10-06 19:36:31 -04:00
|
|
|
}
|
2023-02-27 13:10:22 -05:00
|
|
|
|
2021-10-06 19:36:31 -04:00
|
|
|
return jwtClaims.Map(), nil
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
// DeleteServiceAccount - delete a service account
|
2021-12-21 05:16:50 -05:00
|
|
|
func (sys *IAMSys) DeleteServiceAccount(ctx context.Context, accessKey string, notifyPeers bool) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-04-24 15:10:09 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
sa, ok := sys.store.GetUser(accessKey)
|
2022-07-01 16:19:13 -04:00
|
|
|
if !ok || !sa.Credentials.IsServiceAccount() {
|
2020-05-01 11:05:14 -04:00
|
|
|
return nil
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
2021-12-21 05:16:50 -05:00
|
|
|
if err := sys.store.DeleteUser(ctx, accessKey, svcUser); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if notifyPeers && !sys.HasWatcher() {
|
2022-02-07 13:39:57 -05:00
|
|
|
for _, nerr := range globalNotificationSys.DeleteServiceAccount(accessKey) {
|
2021-12-21 05:16:50 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2021-12-21 05:16:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
// CreateUser - create new user credentials and policy, if user already exists
|
|
|
|
// they shall be rewritten with new inputs.
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) CreateUser(ctx context.Context, accessKey string, ureq madmin.AddOrUpdateUserReq) (updatedAt time.Time, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2019-05-29 16:18:46 -04:00
|
|
|
}
|
|
|
|
|
2021-05-05 19:36:39 -04:00
|
|
|
if !auth.IsAccessKeyValid(accessKey) {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, auth.ErrInvalidAccessKeyLength
|
2021-05-05 19:36:39 -04:00
|
|
|
}
|
|
|
|
|
2021-12-23 12:21:21 -05:00
|
|
|
if !auth.IsSecretKeyValid(ureq.SecretKey) {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, auth.ErrInvalidSecretKeyLength
|
2021-11-02 16:51:42 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err = sys.store.AddUser(ctx, accessKey, ureq)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, err
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.notifyForUser(ctx, accessKey, false)
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2021-11-02 16:51:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetUserSecretKey - sets user secret key
|
2021-11-15 17:14:22 -05:00
|
|
|
func (sys *IAMSys) SetUserSecretKey(ctx context.Context, accessKey string, secretKey string) error {
|
2021-11-02 16:51:42 -04:00
|
|
|
if !sys.Initialized() {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
if !auth.IsAccessKeyValid(accessKey) {
|
|
|
|
return auth.ErrInvalidAccessKeyLength
|
2021-01-25 23:01:49 -05:00
|
|
|
}
|
2020-10-19 12:54:40 -04:00
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
if !auth.IsSecretKeyValid(secretKey) {
|
|
|
|
return auth.ErrInvalidSecretKeyLength
|
2021-01-25 23:01:49 -05:00
|
|
|
}
|
|
|
|
|
2021-11-15 17:14:22 -05:00
|
|
|
return sys.store.UpdateUserSecretKey(ctx, accessKey, secretKey)
|
2021-01-25 23:01:49 -05:00
|
|
|
}
|
2020-10-19 12:54:40 -04:00
|
|
|
|
2021-07-09 14:17:21 -04:00
|
|
|
// purgeExpiredCredentialsForExternalSSO - validates if local credentials are still valid
|
|
|
|
// by checking remote IDP if the relevant users are still active and present.
|
|
|
|
func (sys *IAMSys) purgeExpiredCredentialsForExternalSSO(ctx context.Context) {
|
2022-04-28 21:27:09 -04:00
|
|
|
parentUsersMap := sys.store.GetAllParentUsers()
|
2021-11-03 22:47:49 -04:00
|
|
|
var expiredUsers []string
|
2022-04-28 21:27:09 -04:00
|
|
|
for parentUser, puInfo := range parentUsersMap {
|
|
|
|
// There are multiple role ARNs for parent user only when there
|
|
|
|
// are multiple openid provider configurations with the same ID
|
|
|
|
// provider. We lookup the provider associated with some one of
|
|
|
|
// the roleARNs to check if the user still exists. If they don't
|
|
|
|
// we can safely remove credentials for this parent user
|
|
|
|
// associated with any of the provider configurations.
|
|
|
|
//
|
|
|
|
// If there is no roleARN mapped to the user, the user may be
|
|
|
|
// coming from a policy claim based openid provider.
|
|
|
|
roleArns := puInfo.roleArns.ToSlice()
|
|
|
|
var roleArn string
|
|
|
|
if len(roleArns) == 0 {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext,
|
2022-04-28 21:27:09 -04:00
|
|
|
fmt.Errorf("parentUser: %s had no roleArns mapped!", parentUser))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
roleArn = roleArns[0]
|
2023-02-26 00:01:37 -05:00
|
|
|
u, err := sys.OpenIDConfig.LookupUser(roleArn, puInfo.subClaimValue)
|
2021-07-09 14:17:21 -04:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, err)
|
2021-07-09 14:17:21 -04:00
|
|
|
continue
|
|
|
|
}
|
2021-11-03 22:47:49 -04:00
|
|
|
// If user is set to "disabled", we will remove them
|
|
|
|
// subsequently.
|
2021-07-09 14:17:21 -04:00
|
|
|
if !u.Enabled {
|
2022-04-28 21:27:09 -04:00
|
|
|
expiredUsers = append(expiredUsers, parentUser)
|
2021-07-09 14:17:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
// We ignore any errors
|
|
|
|
_ = sys.store.DeleteUsers(ctx, expiredUsers)
|
2021-07-09 14:17:21 -04:00
|
|
|
}
|
|
|
|
|
2021-07-21 02:33:12 -04:00
|
|
|
// purgeExpiredCredentialsForLDAP - validates if local credentials are still
|
|
|
|
// valid by checking LDAP server if the relevant users are still present.
|
|
|
|
func (sys *IAMSys) purgeExpiredCredentialsForLDAP(ctx context.Context) {
|
2021-11-03 22:47:49 -04:00
|
|
|
parentUsers := sys.store.GetAllParentUsers()
|
|
|
|
var allDistNames []string
|
2022-04-28 21:27:09 -04:00
|
|
|
for parentUser := range parentUsers {
|
2023-02-24 21:37:22 -05:00
|
|
|
if !sys.LDAPConfig.IsLDAPUserDN(parentUser) {
|
2021-11-03 22:47:49 -04:00
|
|
|
continue
|
2021-07-21 02:33:12 -04:00
|
|
|
}
|
2021-11-03 22:47:49 -04:00
|
|
|
|
2022-04-28 21:27:09 -04:00
|
|
|
allDistNames = append(allDistNames, parentUser)
|
2021-07-21 02:33:12 -04:00
|
|
|
}
|
|
|
|
|
2023-02-24 21:37:22 -05:00
|
|
|
expiredUsers, err := sys.LDAPConfig.GetNonEligibleUserDistNames(allDistNames)
|
2021-07-21 02:33:12 -04:00
|
|
|
if err != nil {
|
|
|
|
// Log and return on error - perhaps it'll work the next time.
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, err)
|
2021-07-21 02:33:12 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
// We ignore any errors
|
|
|
|
_ = sys.store.DeleteUsers(ctx, expiredUsers)
|
2021-07-21 02:33:12 -04:00
|
|
|
}
|
|
|
|
|
2021-07-24 14:57:36 -04:00
|
|
|
// updateGroupMembershipsForLDAP - updates the list of groups associated with the credential.
|
|
|
|
func (sys *IAMSys) updateGroupMembershipsForLDAP(ctx context.Context) {
|
|
|
|
// 1. Collect all LDAP users with active creds.
|
2021-11-03 22:47:49 -04:00
|
|
|
allCreds := sys.store.GetSTSAndServiceAccounts()
|
2021-07-24 14:57:36 -04:00
|
|
|
// List of unique LDAP (parent) user DNs that have active creds
|
2021-11-03 22:47:49 -04:00
|
|
|
var parentUsers []string
|
2021-07-24 14:57:36 -04:00
|
|
|
// Map of LDAP user to list of active credential objects
|
2021-11-03 22:47:49 -04:00
|
|
|
parentUserToCredsMap := make(map[string][]auth.Credentials)
|
2021-07-24 14:57:36 -04:00
|
|
|
// DN to ldap username mapping for each LDAP user
|
2021-11-03 22:47:49 -04:00
|
|
|
parentUserToLDAPUsernameMap := make(map[string]string)
|
|
|
|
for _, cred := range allCreds {
|
2024-01-09 15:25:18 -05:00
|
|
|
// Expired credentials don't need parent user updates.
|
|
|
|
if cred.IsExpired() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:37:22 -05:00
|
|
|
if !sys.LDAPConfig.IsLDAPUserDN(cred.ParentUser) {
|
2021-11-03 22:47:49 -04:00
|
|
|
continue
|
|
|
|
}
|
2024-01-09 15:25:18 -05:00
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
// Check if this is the first time we are
|
|
|
|
// encountering this LDAP user.
|
|
|
|
if _, ok := parentUserToCredsMap[cred.ParentUser]; !ok {
|
|
|
|
// Try to find the ldapUsername for this
|
|
|
|
// parentUser by extracting JWT claims
|
2022-03-14 12:09:22 -04:00
|
|
|
var (
|
|
|
|
jwtClaims *jwt.MapClaims
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if cred.SessionToken == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if cred.IsServiceAccount() {
|
|
|
|
jwtClaims, err = auth.ExtractClaims(cred.SessionToken, cred.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
jwtClaims, err = auth.ExtractClaims(cred.SessionToken, globalActiveCred.SecretKey)
|
|
|
|
}
|
|
|
|
} else {
|
2024-02-26 16:26:18 -05:00
|
|
|
var secretKey string
|
|
|
|
secretKey, err = getTokenSigningKey()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
jwtClaims, err = auth.ExtractClaims(cred.SessionToken, secretKey)
|
2022-03-14 12:09:22 -04:00
|
|
|
}
|
2021-11-03 22:47:49 -04:00
|
|
|
if err != nil {
|
2022-03-14 12:09:22 -04:00
|
|
|
// skip this cred - session token seems invalid
|
2021-11-03 22:47:49 -04:00
|
|
|
continue
|
|
|
|
}
|
2022-03-14 12:09:22 -04:00
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
ldapUsername, ok := jwtClaims.Lookup(ldapUserN)
|
|
|
|
if !ok {
|
|
|
|
// skip this cred - we dont have the
|
|
|
|
// username info needed
|
|
|
|
continue
|
2021-07-24 14:57:36 -04:00
|
|
|
}
|
2021-11-03 22:47:49 -04:00
|
|
|
|
|
|
|
// Collect each new cred.ParentUser into parentUsers
|
|
|
|
parentUsers = append(parentUsers, cred.ParentUser)
|
|
|
|
|
|
|
|
// Update the ldapUsernameMap
|
|
|
|
parentUserToLDAPUsernameMap[cred.ParentUser] = ldapUsername
|
2021-07-24 14:57:36 -04:00
|
|
|
}
|
2021-11-03 22:47:49 -04:00
|
|
|
parentUserToCredsMap[cred.ParentUser] = append(parentUserToCredsMap[cred.ParentUser], cred)
|
|
|
|
|
2021-07-24 14:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Query LDAP server for groups of the LDAP users collected.
|
2023-02-24 21:37:22 -05:00
|
|
|
updatedGroups, err := sys.LDAPConfig.LookupGroupMemberships(parentUsers, parentUserToLDAPUsernameMap)
|
2021-07-24 14:57:36 -04:00
|
|
|
if err != nil {
|
|
|
|
// Log and return on error - perhaps it'll work the next time.
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, err)
|
2021-07-24 14:57:36 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Update creds for those users whose groups are changed
|
|
|
|
for _, parentUser := range parentUsers {
|
|
|
|
currGroupsSet := updatedGroups[parentUser]
|
|
|
|
currGroups := currGroupsSet.ToSlice()
|
|
|
|
for _, cred := range parentUserToCredsMap[parentUser] {
|
|
|
|
gSet := set.CreateStringSet(cred.Groups...)
|
|
|
|
if gSet.Equals(currGroupsSet) {
|
|
|
|
// No change to groups memberships for this
|
|
|
|
// credential.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-09 15:25:18 -05:00
|
|
|
// Expired credentials don't need group membership updates.
|
|
|
|
if cred.IsExpired() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-24 14:57:36 -04:00
|
|
|
cred.Groups = currGroups
|
2021-11-03 22:47:49 -04:00
|
|
|
if err := sys.store.UpdateUserIdentity(ctx, cred); err != nil {
|
2021-07-24 14:57:36 -04:00
|
|
|
// Log and continue error - perhaps it'll work the next time.
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, err)
|
2021-07-24 14:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
// NormalizeLDAPAccessKeypairs - normalize the access key pairs (service
|
|
|
|
// accounts) for LDAP users. This normalizes the parent user and the group names
|
|
|
|
// whenever the parent user parses validly as a DN.
|
|
|
|
func (sys *IAMSys) NormalizeLDAPAccessKeypairs(ctx context.Context, accessKeyMap map[string]madmin.SRSvcAccCreate,
|
|
|
|
) (err error) {
|
|
|
|
conn, err := sys.LDAPConfig.LDAP.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Bind to the lookup user account
|
|
|
|
if err = sys.LDAPConfig.LDAP.LookupBind(conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var collectedErrors []error
|
|
|
|
updatedKeysMap := make(map[string]madmin.SRSvcAccCreate)
|
|
|
|
for ak, createReq := range accessKeyMap {
|
|
|
|
parent := createReq.Parent
|
|
|
|
groups := createReq.Groups
|
|
|
|
|
|
|
|
_, err := ldap.NormalizeDN(parent)
|
|
|
|
if err != nil {
|
|
|
|
// not a valid DN, ignore.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hasDiff := false
|
|
|
|
|
2024-04-25 11:50:16 -04:00
|
|
|
// For the parent value, we require that the parent exists in the LDAP
|
|
|
|
// server and is under a configured base DN.
|
|
|
|
validatedParent, isUnderBaseDN, err := sys.LDAPConfig.GetValidatedUserDN(conn, parent)
|
2024-04-18 11:15:02 -04:00
|
|
|
if err != nil {
|
|
|
|
collectedErrors = append(collectedErrors, fmt.Errorf("could not validate `%s` exists in LDAP directory: %w", parent, err))
|
|
|
|
continue
|
|
|
|
}
|
2024-04-25 11:50:16 -04:00
|
|
|
if validatedParent == "" || !isUnderBaseDN {
|
2024-04-18 11:15:02 -04:00
|
|
|
err := fmt.Errorf("DN `%s` was not found in the LDAP directory", parent)
|
|
|
|
collectedErrors = append(collectedErrors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if validatedParent != parent {
|
|
|
|
hasDiff = true
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:50:16 -04:00
|
|
|
var normalizedGroups []string
|
2024-04-18 11:15:02 -04:00
|
|
|
for _, group := range groups {
|
2024-04-25 11:50:16 -04:00
|
|
|
// For a group, we store the normalized DN even if it not under a
|
|
|
|
// configured base DN.
|
|
|
|
validatedGroup, _, err := sys.LDAPConfig.GetValidatedGroupDN(conn, group)
|
2024-04-18 11:15:02 -04:00
|
|
|
if err != nil {
|
|
|
|
collectedErrors = append(collectedErrors, fmt.Errorf("could not validate `%s` exists in LDAP directory: %w", group, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if validatedGroup == "" {
|
|
|
|
err := fmt.Errorf("DN `%s` was not found in the LDAP directory", group)
|
|
|
|
collectedErrors = append(collectedErrors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if validatedGroup != group {
|
|
|
|
hasDiff = true
|
|
|
|
}
|
2024-04-25 11:50:16 -04:00
|
|
|
normalizedGroups = append(normalizedGroups, validatedGroup)
|
2024-04-18 11:15:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if hasDiff {
|
|
|
|
updatedCreateReq := createReq
|
|
|
|
updatedCreateReq.Parent = validatedParent
|
2024-04-25 11:50:16 -04:00
|
|
|
updatedCreateReq.Groups = normalizedGroups
|
2024-04-18 11:15:02 -04:00
|
|
|
|
|
|
|
updatedKeysMap[ak] = updatedCreateReq
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there are any errors, return a collected error.
|
|
|
|
if len(collectedErrors) > 0 {
|
|
|
|
return fmt.Errorf("errors validating LDAP DN: %w", errors.Join(collectedErrors...))
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range updatedKeysMap {
|
|
|
|
// Replace the map values with the updated ones
|
|
|
|
accessKeyMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
fix: IAM import for LDAP should replace mappings (#19607)
Existing IAM import logic for LDAP creates new mappings when the
normalized form of the mapping key differs from the existing mapping key
in storage. This change effectively replaces the existing mapping key by
first deleting it and then recreating with the normalized form of the
mapping key.
For e.g. if an older deployment had a policy mapped to a user DN -
`UID=alice1,OU=people,OU=hwengg,DC=min,DC=io`
instead of adding a mapping for the normalized form -
`uid=alice1,ou=people,ou=hwengg,dc=min,dc=io`
we should replace the existing mapping.
This ensures that duplicates mappings won't remain after the import.
Some additional cleanup cases are also covered. If there are multiple
mappings for the name normalized key such as:
`UID=alice1,OU=people,OU=hwengg,DC=min,DC=io`
`uid=alice1,ou=people,ou=hwengg,DC=min,DC=io`
`uid=alice1,ou=people,ou=hwengg,dc=min,dc=io`
we check if the list of policies mapped to all these keys are exactly
the same, and if so remove all of them and create a single mapping with
the normalized key. However, if the policies mapped to such keys differ,
the import operation returns an error as the server cannot automatically
pick the "right" list of policies to map.
2024-04-25 11:49:53 -04:00
|
|
|
func (sys *IAMSys) getStoredLDAPPolicyMappingKeys(ctx context.Context, isGroup bool) set.StringSet {
|
|
|
|
entityKeysInStorage := set.NewStringSet()
|
|
|
|
if iamOS, ok := sys.store.IAMStorageAPI.(*IAMObjectStore); ok {
|
|
|
|
// Load existing mapping keys from the cached listing for
|
|
|
|
// `IAMObjectStore`.
|
|
|
|
iamFilesListing := iamOS.cachedIAMListing.Load().(map[string][]string)
|
|
|
|
listKey := policyDBSTSUsersListKey
|
|
|
|
if isGroup {
|
|
|
|
listKey = policyDBGroupsListKey
|
|
|
|
}
|
|
|
|
for _, item := range iamFilesListing[listKey] {
|
|
|
|
stsUserName := strings.TrimSuffix(item, ".json")
|
|
|
|
entityKeysInStorage.Add(stsUserName)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// For non-iam object store, we copy the mapping keys from the cache.
|
|
|
|
cache := sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
cachedPolicyMap := cache.iamSTSPolicyMap
|
|
|
|
if isGroup {
|
|
|
|
cachedPolicyMap = cache.iamGroupPolicyMap
|
|
|
|
}
|
|
|
|
cachedPolicyMap.Range(func(k string, v MappedPolicy) bool {
|
|
|
|
entityKeysInStorage.Add(k)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return entityKeysInStorage
|
|
|
|
}
|
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
// NormalizeLDAPMappingImport - validates the LDAP policy mappings. Keys in the
|
|
|
|
// given map may not correspond to LDAP DNs - these keys are ignored.
|
|
|
|
//
|
|
|
|
// For validated mappings, it updates the key in the given map to be in
|
|
|
|
// normalized form.
|
|
|
|
func (sys *IAMSys) NormalizeLDAPMappingImport(ctx context.Context, isGroup bool,
|
|
|
|
policyMap map[string]MappedPolicy,
|
|
|
|
) error {
|
|
|
|
conn, err := sys.LDAPConfig.LDAP.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Bind to the lookup user account
|
|
|
|
if err = sys.LDAPConfig.LDAP.LookupBind(conn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We map keys that correspond to LDAP DNs and validate that they exist in
|
|
|
|
// the LDAP server.
|
2024-04-25 11:50:16 -04:00
|
|
|
var dnValidator func(*libldap.Conn, string) (string, bool, error) = sys.LDAPConfig.GetValidatedUserDN
|
2024-04-18 11:15:02 -04:00
|
|
|
if isGroup {
|
|
|
|
dnValidator = sys.LDAPConfig.GetValidatedGroupDN
|
|
|
|
}
|
|
|
|
|
|
|
|
// map of normalized DN keys to original keys.
|
|
|
|
normalizedDNKeysMap := make(map[string][]string)
|
|
|
|
var collectedErrors []error
|
|
|
|
for k := range policyMap {
|
|
|
|
_, err := ldap.NormalizeDN(k)
|
|
|
|
if err != nil {
|
|
|
|
// not a valid DN, ignore.
|
|
|
|
continue
|
|
|
|
}
|
2024-04-25 11:50:16 -04:00
|
|
|
validatedDN, underBaseDN, err := dnValidator(conn, k)
|
2024-04-18 11:15:02 -04:00
|
|
|
if err != nil {
|
|
|
|
collectedErrors = append(collectedErrors, fmt.Errorf("could not validate `%s` exists in LDAP directory: %w", k, err))
|
|
|
|
continue
|
|
|
|
}
|
2024-04-25 11:50:16 -04:00
|
|
|
if validatedDN == "" || !underBaseDN {
|
2024-04-18 11:15:02 -04:00
|
|
|
err := fmt.Errorf("DN `%s` was not found in the LDAP directory", k)
|
|
|
|
collectedErrors = append(collectedErrors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if validatedDN != k {
|
|
|
|
normalizedDNKeysMap[validatedDN] = append(normalizedDNKeysMap[validatedDN], k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there are any errors, return a collected error.
|
|
|
|
if len(collectedErrors) > 0 {
|
|
|
|
return fmt.Errorf("errors validating LDAP DN: %w", errors.Join(collectedErrors...))
|
|
|
|
}
|
|
|
|
|
fix: IAM import for LDAP should replace mappings (#19607)
Existing IAM import logic for LDAP creates new mappings when the
normalized form of the mapping key differs from the existing mapping key
in storage. This change effectively replaces the existing mapping key by
first deleting it and then recreating with the normalized form of the
mapping key.
For e.g. if an older deployment had a policy mapped to a user DN -
`UID=alice1,OU=people,OU=hwengg,DC=min,DC=io`
instead of adding a mapping for the normalized form -
`uid=alice1,ou=people,ou=hwengg,dc=min,dc=io`
we should replace the existing mapping.
This ensures that duplicates mappings won't remain after the import.
Some additional cleanup cases are also covered. If there are multiple
mappings for the name normalized key such as:
`UID=alice1,OU=people,OU=hwengg,DC=min,DC=io`
`uid=alice1,ou=people,ou=hwengg,DC=min,DC=io`
`uid=alice1,ou=people,ou=hwengg,dc=min,dc=io`
we check if the list of policies mapped to all these keys are exactly
the same, and if so remove all of them and create a single mapping with
the normalized key. However, if the policies mapped to such keys differ,
the import operation returns an error as the server cannot automatically
pick the "right" list of policies to map.
2024-04-25 11:49:53 -04:00
|
|
|
entityKeysInStorage := sys.getStoredLDAPPolicyMappingKeys(ctx, isGroup)
|
|
|
|
|
2024-04-18 11:15:02 -04:00
|
|
|
for normKey, origKeys := range normalizedDNKeysMap {
|
|
|
|
if len(origKeys) > 1 {
|
2024-04-18 15:09:19 -04:00
|
|
|
// If there are multiple DN keys that normalize to the same value,
|
|
|
|
// check if the policy mappings are equal, if they are we don't need
|
|
|
|
// to return an error.
|
|
|
|
policiesDiffer := false
|
|
|
|
firstMappedPolicies := policyMap[origKeys[0]].policySet()
|
|
|
|
for i := 1; i < len(origKeys); i++ {
|
|
|
|
otherMappedPolicies := policyMap[origKeys[i]].policySet()
|
|
|
|
if !firstMappedPolicies.Equals(otherMappedPolicies) {
|
|
|
|
policiesDiffer = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if policiesDiffer {
|
|
|
|
return fmt.Errorf("multiple DNs map to the same LDAP DN[%s]: %v; please remove DNs that are not needed",
|
|
|
|
normKey, origKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policies mapped to the DN's are the same, so we remove the extra
|
|
|
|
// ones from the map.
|
|
|
|
for i := 1; i < len(origKeys); i++ {
|
|
|
|
delete(policyMap, origKeys[i])
|
fix: IAM import for LDAP should replace mappings (#19607)
Existing IAM import logic for LDAP creates new mappings when the
normalized form of the mapping key differs from the existing mapping key
in storage. This change effectively replaces the existing mapping key by
first deleting it and then recreating with the normalized form of the
mapping key.
For e.g. if an older deployment had a policy mapped to a user DN -
`UID=alice1,OU=people,OU=hwengg,DC=min,DC=io`
instead of adding a mapping for the normalized form -
`uid=alice1,ou=people,ou=hwengg,dc=min,dc=io`
we should replace the existing mapping.
This ensures that duplicates mappings won't remain after the import.
Some additional cleanup cases are also covered. If there are multiple
mappings for the name normalized key such as:
`UID=alice1,OU=people,OU=hwengg,DC=min,DC=io`
`uid=alice1,ou=people,ou=hwengg,DC=min,DC=io`
`uid=alice1,ou=people,ou=hwengg,dc=min,dc=io`
we check if the list of policies mapped to all these keys are exactly
the same, and if so remove all of them and create a single mapping with
the normalized key. However, if the policies mapped to such keys differ,
the import operation returns an error as the server cannot automatically
pick the "right" list of policies to map.
2024-04-25 11:49:53 -04:00
|
|
|
|
|
|
|
// Remove the mapping from storage by setting the policy to "".
|
|
|
|
if entityKeysInStorage.Contains(origKeys[i]) {
|
|
|
|
// Ignore any deletion error.
|
|
|
|
_, _ = sys.PolicyDBSet(ctx, origKeys[i], "", stsUser, isGroup)
|
|
|
|
}
|
2024-04-18 15:09:19 -04:00
|
|
|
}
|
2024-04-18 11:15:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replacing origKeys[0] with normKey in the policyMap
|
|
|
|
|
|
|
|
// len(origKeys) is always > 0, so here len(origKeys) == 1
|
|
|
|
mappingValue := policyMap[origKeys[0]]
|
|
|
|
delete(policyMap, origKeys[0])
|
|
|
|
policyMap[normKey] = mappingValue
|
fix: IAM import for LDAP should replace mappings (#19607)
Existing IAM import logic for LDAP creates new mappings when the
normalized form of the mapping key differs from the existing mapping key
in storage. This change effectively replaces the existing mapping key by
first deleting it and then recreating with the normalized form of the
mapping key.
For e.g. if an older deployment had a policy mapped to a user DN -
`UID=alice1,OU=people,OU=hwengg,DC=min,DC=io`
instead of adding a mapping for the normalized form -
`uid=alice1,ou=people,ou=hwengg,dc=min,dc=io`
we should replace the existing mapping.
This ensures that duplicates mappings won't remain after the import.
Some additional cleanup cases are also covered. If there are multiple
mappings for the name normalized key such as:
`UID=alice1,OU=people,OU=hwengg,DC=min,DC=io`
`uid=alice1,ou=people,ou=hwengg,DC=min,DC=io`
`uid=alice1,ou=people,ou=hwengg,dc=min,dc=io`
we check if the list of policies mapped to all these keys are exactly
the same, and if so remove all of them and create a single mapping with
the normalized key. However, if the policies mapped to such keys differ,
the import operation returns an error as the server cannot automatically
pick the "right" list of policies to map.
2024-04-25 11:49:53 -04:00
|
|
|
// Remove the mapping from storage by setting the policy to "".
|
|
|
|
if entityKeysInStorage.Contains(origKeys[0]) {
|
|
|
|
// Ignore any deletion error.
|
|
|
|
_, _ = sys.PolicyDBSet(ctx, origKeys[0], "", stsUser, isGroup)
|
|
|
|
}
|
2024-04-18 11:15:02 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
// GetUser - get user credentials
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) GetUser(ctx context.Context, accessKey string) (u UserIdentity, ok bool) {
|
2021-01-25 23:01:49 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return u, false
|
2020-06-11 17:11:30 -04:00
|
|
|
}
|
|
|
|
|
2023-09-13 15:43:46 -04:00
|
|
|
if accessKey == globalActiveCred.AccessKey {
|
|
|
|
return newUserIdentity(globalActiveCred), true
|
|
|
|
}
|
|
|
|
|
|
|
|
loadUserCalled := false
|
2021-03-02 20:08:25 -05:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
default:
|
2021-11-15 17:14:22 -05:00
|
|
|
sys.store.LoadUser(ctx, accessKey)
|
2023-09-13 15:43:46 -04:00
|
|
|
loadUserCalled = true
|
2021-01-19 13:05:41 -05:00
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
u, ok = sys.store.GetUser(accessKey)
|
2023-09-13 15:43:46 -04:00
|
|
|
if !ok && !loadUserCalled {
|
2021-11-15 17:14:22 -05:00
|
|
|
sys.store.LoadUser(ctx, accessKey)
|
2022-07-01 16:19:13 -04:00
|
|
|
u, ok = sys.store.GetUser(accessKey)
|
2021-01-19 13:05:41 -05:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
return u, ok && u.Credentials.IsValid()
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:38:57 -05:00
|
|
|
// Notify all other MinIO peers to load group.
|
|
|
|
func (sys *IAMSys) notifyForGroup(ctx context.Context, group string) {
|
|
|
|
if !sys.HasWatcher() {
|
2022-02-07 13:39:57 -05:00
|
|
|
for _, nerr := range globalNotificationSys.LoadGroup(group) {
|
2021-11-29 17:38:57 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
// AddUsersToGroup - adds users to a group, creating the group if
|
|
|
|
// needed. No error if user(s) already are in the group.
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) AddUsersToGroup(ctx context.Context, group string, members []string) (updatedAt time.Time, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err = sys.store.AddUsersToGroup(ctx, group, members)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, err
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.notifyForGroup(ctx, group)
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUsersFromGroup - remove users from group. If no users are
|
|
|
|
// given, and the group is empty, deletes the group as well.
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) RemoveUsersFromGroup(ctx context.Context, group string, members []string) (updatedAt time.Time, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errIAMActionNotAllowed
|
2019-09-09 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err = sys.store.RemoveUsersFromGroup(ctx, group, members)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, err
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.notifyForGroup(ctx, group)
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetGroupStatus - enable/disabled a group
|
2022-07-01 16:19:13 -04:00
|
|
|
func (sys *IAMSys) SetGroupStatus(ctx context.Context, group string, enabled bool) (updatedAt time.Time, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errIAMActionNotAllowed
|
2019-09-09 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err = sys.store.SetGroupStatus(ctx, group, enabled)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, err
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.notifyForGroup(ctx, group)
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetGroupDescription - builds up group description
|
|
|
|
func (sys *IAMSys) GetGroupDescription(group string) (gd madmin.GroupDesc, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-04-07 17:26:39 -04:00
|
|
|
return gd, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
return sys.store.GetGroupDescription(group)
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
// ListGroups - lists groups.
|
2021-11-15 17:14:22 -05:00
|
|
|
func (sys *IAMSys) ListGroups(ctx context.Context) (r []string, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-04-07 17:26:39 -04:00
|
|
|
return r, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:00:19 -04:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
return sys.store.ListGroups(ctx)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2024-04-23 21:23:08 -04:00
|
|
|
// PolicyDBSet - sets a policy for a user or group in the PolicyDB. This does
|
|
|
|
// not validate if the user/group exists - that is the responsibility of the
|
|
|
|
// caller.
|
2022-08-23 14:11:45 -04:00
|
|
|
func (sys *IAMSys) PolicyDBSet(ctx context.Context, name, policy string, userType IAMUserType, isGroup bool) (updatedAt time.Time, err error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
updatedAt, err = sys.store.PolicyDBSet(ctx, name, policy, userType, isGroup)
|
2021-11-29 17:38:57 -05:00
|
|
|
if err != nil {
|
2022-07-01 16:19:13 -04:00
|
|
|
return
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to reload policy
|
|
|
|
if !sys.HasWatcher() {
|
2022-08-23 14:11:45 -04:00
|
|
|
for _, nerr := range globalNotificationSys.LoadPolicyMapping(name, userType, isGroup) {
|
2021-11-29 17:38:57 -05:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2021-11-29 17:38:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
return updatedAt, nil
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 01:44:50 -04:00
|
|
|
// PolicyDBUpdateBuiltin - adds or removes policies from a user or a group
|
|
|
|
// verified to be an internal IDP user.
|
|
|
|
func (sys *IAMSys) PolicyDBUpdateBuiltin(ctx context.Context, isAttach bool,
|
|
|
|
r madmin.PolicyAssociationReq,
|
|
|
|
) (updatedAt time.Time, addedOrRemoved, effectivePolicies []string, err error) {
|
|
|
|
if !sys.Initialized() {
|
|
|
|
err = errServerNotInitialized
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userOrGroup := r.User
|
|
|
|
var isGroup bool
|
|
|
|
if userOrGroup == "" {
|
|
|
|
isGroup = true
|
|
|
|
userOrGroup = r.Group
|
|
|
|
}
|
|
|
|
|
|
|
|
if isGroup {
|
|
|
|
_, err = sys.GetGroupDescription(userOrGroup)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var isTemp bool
|
|
|
|
isTemp, _, err = sys.IsTempUser(userOrGroup)
|
|
|
|
if err != nil && err != errNoSuchUser {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if isTemp {
|
|
|
|
err = errIAMActionNotAllowed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the user is root credential you are not allowed to
|
|
|
|
// add policies for root user.
|
|
|
|
if userOrGroup == globalActiveCred.AccessKey {
|
|
|
|
err = errIAMActionNotAllowed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate that user exists.
|
|
|
|
var userExists bool
|
|
|
|
_, userExists = sys.GetUser(ctx, userOrGroup)
|
|
|
|
if !userExists {
|
|
|
|
err = errNoSuchUser
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedAt, addedOrRemoved, effectivePolicies, err = sys.store.PolicyDBUpdate(ctx, userOrGroup, isGroup,
|
|
|
|
regUser, r.Policies, isAttach)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to reload policy
|
|
|
|
if !sys.HasWatcher() {
|
|
|
|
for _, nerr := range globalNotificationSys.LoadPolicyMapping(userOrGroup, regUser, isGroup) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2023-06-22 01:44:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 08:04:40 -04:00
|
|
|
replLogIf(ctx, globalSiteReplicationSys.IAMChangeHook(ctx, madmin.SRIAMItem{
|
2023-06-22 01:44:50 -04:00
|
|
|
Type: madmin.SRIAMItemPolicyMapping,
|
|
|
|
PolicyMapping: &madmin.SRPolicyMapping{
|
|
|
|
UserOrGroup: userOrGroup,
|
|
|
|
UserType: int(regUser),
|
|
|
|
IsGroup: isGroup,
|
|
|
|
Policy: strings.Join(effectivePolicies, ","),
|
|
|
|
},
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
}))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:08:33 -05:00
|
|
|
// PolicyDBUpdateLDAP - adds or removes policies from a user or a group verified
|
|
|
|
// to be in the LDAP directory.
|
|
|
|
func (sys *IAMSys) PolicyDBUpdateLDAP(ctx context.Context, isAttach bool,
|
|
|
|
r madmin.PolicyAssociationReq,
|
2023-06-22 01:44:50 -04:00
|
|
|
) (updatedAt time.Time, addedOrRemoved, effectivePolicies []string, err error) {
|
2022-12-09 16:08:33 -05:00
|
|
|
if !sys.Initialized() {
|
2023-06-22 01:44:50 -04:00
|
|
|
err = errServerNotInitialized
|
|
|
|
return
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var dn string
|
|
|
|
var isGroup bool
|
|
|
|
if r.User != "" {
|
2024-03-28 02:45:26 -04:00
|
|
|
dn, err = sys.LDAPConfig.GetValidatedDNForUsername(r.User)
|
2022-12-09 16:08:33 -05:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, err)
|
2023-06-22 01:44:50 -04:00
|
|
|
return
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
if dn == "" {
|
2023-11-09 17:44:43 -05:00
|
|
|
// Still attempt to detach if provided user is a DN.
|
|
|
|
if !isAttach && sys.LDAPConfig.IsLDAPUserDN(r.User) {
|
|
|
|
dn = r.User
|
|
|
|
} else {
|
|
|
|
err = errNoSuchUser
|
|
|
|
return
|
|
|
|
}
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
isGroup = false
|
|
|
|
} else {
|
2023-11-09 17:44:43 -05:00
|
|
|
if isAttach {
|
2024-03-28 02:45:26 -04:00
|
|
|
var foundGroupDN string
|
2024-04-25 11:50:16 -04:00
|
|
|
var underBaseDN bool
|
|
|
|
if foundGroupDN, underBaseDN, err = sys.LDAPConfig.GetValidatedGroupDN(nil, r.Group); err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, err)
|
2023-11-09 17:44:43 -05:00
|
|
|
return
|
2024-04-25 11:50:16 -04:00
|
|
|
} else if foundGroupDN == "" || !underBaseDN {
|
2023-11-09 17:44:43 -05:00
|
|
|
err = errNoSuchGroup
|
|
|
|
return
|
|
|
|
}
|
2024-03-28 02:45:26 -04:00
|
|
|
// We use the group DN returned by the LDAP server (this may not
|
|
|
|
// equal the input group name, but we assume it is canonical).
|
|
|
|
dn = foundGroupDN
|
|
|
|
} else {
|
|
|
|
dn = r.Group
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
isGroup = true
|
|
|
|
}
|
|
|
|
|
2024-04-16 20:36:43 -04:00
|
|
|
// Backward compatibility in detaching non-normalized DNs.
|
|
|
|
if !isAttach {
|
|
|
|
var oldDN string
|
|
|
|
if isGroup {
|
|
|
|
oldDN = r.Group
|
|
|
|
} else {
|
|
|
|
oldDN = r.User
|
|
|
|
}
|
|
|
|
if oldDN != dn {
|
|
|
|
sys.store.PolicyDBUpdate(ctx, oldDN, isGroup, stsUser, r.Policies, isAttach)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:08:33 -05:00
|
|
|
userType := stsUser
|
2024-03-28 02:45:26 -04:00
|
|
|
updatedAt, addedOrRemoved, effectivePolicies, err = sys.store.PolicyDBUpdate(
|
|
|
|
ctx, dn, isGroup, userType, r.Policies, isAttach)
|
2022-12-09 16:08:33 -05:00
|
|
|
if err != nil {
|
2023-06-22 01:44:50 -04:00
|
|
|
return
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to reload policy
|
|
|
|
if !sys.HasWatcher() {
|
|
|
|
for _, nerr := range globalNotificationSys.LoadPolicyMapping(dn, userType, isGroup) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(ctx, nerr.Err)
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 08:04:40 -04:00
|
|
|
replLogIf(ctx, globalSiteReplicationSys.IAMChangeHook(ctx, madmin.SRIAMItem{
|
2023-06-22 01:44:50 -04:00
|
|
|
Type: madmin.SRIAMItemPolicyMapping,
|
|
|
|
PolicyMapping: &madmin.SRPolicyMapping{
|
|
|
|
UserOrGroup: dn,
|
|
|
|
UserType: int(userType),
|
|
|
|
IsGroup: isGroup,
|
|
|
|
Policy: strings.Join(effectivePolicies, ","),
|
|
|
|
},
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
}))
|
|
|
|
|
|
|
|
return
|
2022-12-09 16:08:33 -05:00
|
|
|
}
|
|
|
|
|
2021-03-25 03:38:15 -04:00
|
|
|
// PolicyDBGet - gets policy set on a user or group. If a list of groups is
|
|
|
|
// given, policies associated with them are included as well.
|
2023-11-29 19:07:35 -05:00
|
|
|
func (sys *IAMSys) PolicyDBGet(name string, groups ...string) ([]string, error) {
|
2021-03-23 18:15:51 -04:00
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2023-11-29 19:07:35 -05:00
|
|
|
return sys.store.PolicyDBGet(name, groups...)
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
const sessionPolicyNameExtracted = policy.SessionPolicyName + "-extracted"
|
2022-05-02 20:56:19 -04:00
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
// IsAllowedServiceAccount - checks if the given service account is allowed to perform
|
|
|
|
// actions. The permission of the parent user is checked first
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) IsAllowedServiceAccount(args policy.Args, parentUser string) bool {
|
2022-07-26 22:06:55 -04:00
|
|
|
// Verify if the parent claim matches the parentUser.
|
2020-03-17 13:36:13 -04:00
|
|
|
p, ok := args.Claims[parentClaim]
|
|
|
|
if ok {
|
|
|
|
parentInClaim, ok := p.(string)
|
|
|
|
if !ok {
|
|
|
|
// Reject malformed/malicious requests.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// The parent claim in the session token should be equal
|
|
|
|
// to the parent detected in the backend
|
2021-07-11 20:39:52 -04:00
|
|
|
if parentInClaim != parentUser {
|
2020-03-17 13:36:13 -04:00
|
|
|
return false
|
|
|
|
}
|
2020-03-23 17:17:18 -04:00
|
|
|
} else {
|
|
|
|
// This is needed so a malicious user cannot
|
|
|
|
// use a leaked session key of another user
|
|
|
|
// to widen its privileges.
|
|
|
|
return false
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 22:06:55 -04:00
|
|
|
isOwnerDerived := parentUser == globalActiveCred.AccessKey
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2022-07-26 22:06:55 -04:00
|
|
|
var err error
|
|
|
|
var svcPolicies []string
|
|
|
|
roleArn := args.GetRoleArn()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isOwnerDerived:
|
|
|
|
// All actions are allowed by default and no policy evaluation is
|
|
|
|
// required.
|
|
|
|
|
|
|
|
case roleArn != "":
|
|
|
|
arn, err := arn.Parse(roleArn)
|
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, fmt.Errorf("error parsing role ARN %s: %v", roleArn, err))
|
2022-07-26 22:06:55 -04:00
|
|
|
return false
|
2021-11-18 18:38:54 -05:00
|
|
|
}
|
2022-07-26 22:06:55 -04:00
|
|
|
svcPolicies = newMappedPolicy(sys.rolesMap[arn]).toSlice()
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Check policy for parent user of service account.
|
2023-11-29 19:07:35 -05:00
|
|
|
svcPolicies, err = sys.PolicyDBGet(parentUser, args.Groups...)
|
2022-07-26 22:06:55 -04:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, err)
|
2021-11-18 18:38:54 -05:00
|
|
|
return false
|
|
|
|
}
|
2022-07-26 22:06:55 -04:00
|
|
|
|
|
|
|
// Finally, if there is no parent policy, check if a policy claim is
|
|
|
|
// present.
|
|
|
|
if len(svcPolicies) == 0 {
|
2023-09-14 17:50:16 -04:00
|
|
|
policySet, _ := policy.GetPoliciesFromClaims(args.Claims, iamPolicyClaimNameOpenID())
|
2022-07-26 22:06:55 -04:00
|
|
|
svcPolicies = policySet.ToSlice()
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 22:06:55 -04:00
|
|
|
// Defensive code: Do not allow any operation if no policy is found.
|
|
|
|
if !isOwnerDerived && len(svcPolicies) == 0 {
|
2020-03-17 13:36:13 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
var combinedPolicy policy.Policy
|
2022-07-26 22:06:55 -04:00
|
|
|
// Policies were found, evaluate all of them.
|
|
|
|
if !isOwnerDerived {
|
|
|
|
availablePoliciesStr, c := sys.store.FilterPolicies(strings.Join(svcPolicies, ","), "")
|
|
|
|
if availablePoliciesStr == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
combinedPolicy = c
|
|
|
|
}
|
|
|
|
|
2020-03-23 17:17:18 -04:00
|
|
|
parentArgs := args
|
2021-07-11 20:39:52 -04:00
|
|
|
parentArgs.AccountName = parentUser
|
2020-04-14 14:28:56 -04:00
|
|
|
|
|
|
|
saPolicyClaim, ok := args.Claims[iamPolicyClaimNameSA()]
|
|
|
|
if !ok {
|
2020-03-17 13:36:13 -04:00
|
|
|
return false
|
|
|
|
}
|
2020-03-23 17:17:18 -04:00
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
saPolicyClaimStr, ok := saPolicyClaim.(string)
|
|
|
|
if !ok {
|
|
|
|
// Sub policy if set, should be a string reject
|
|
|
|
// malformed/malicious requests.
|
|
|
|
return false
|
|
|
|
}
|
2020-03-23 17:17:18 -04:00
|
|
|
|
2022-05-02 20:56:19 -04:00
|
|
|
if saPolicyClaimStr == inheritedPolicyType {
|
2022-07-26 22:06:55 -04:00
|
|
|
return isOwnerDerived || combinedPolicy.IsAllowed(parentArgs)
|
2020-03-23 17:17:18 -04:00
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2024-01-31 13:56:45 -05:00
|
|
|
// 3. If an inline session-policy is present, evaluate it.
|
2024-02-01 17:45:03 -05:00
|
|
|
hasSessionPolicy, isAllowedSP := isAllowedBySessionPolicyForServiceAccount(args)
|
2024-01-31 13:56:45 -05:00
|
|
|
if hasSessionPolicy {
|
|
|
|
return isAllowedSP && (isOwnerDerived || combinedPolicy.IsAllowed(parentArgs))
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2024-01-31 13:56:45 -05:00
|
|
|
// Sub policy not set. Evaluate only the parent policies.
|
|
|
|
return (isOwnerDerived || combinedPolicy.IsAllowed(parentArgs))
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 14:33:35 -04:00
|
|
|
// IsAllowedSTS is meant for STS based temporary credentials,
|
|
|
|
// which implements claims validation and verification other than
|
|
|
|
// applying policies.
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) IsAllowedSTS(args policy.Args, parentUser string) bool {
|
2022-07-26 22:06:55 -04:00
|
|
|
// 1. Determine mapped policies
|
|
|
|
|
|
|
|
isOwnerDerived := parentUser == globalActiveCred.AccessKey
|
2021-11-26 22:22:40 -05:00
|
|
|
var policies []string
|
|
|
|
roleArn := args.GetRoleArn()
|
2022-07-26 22:06:55 -04:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case isOwnerDerived:
|
|
|
|
// All actions are allowed by default and no policy evaluation is
|
|
|
|
// required.
|
|
|
|
|
|
|
|
case roleArn != "":
|
|
|
|
// If a roleARN is present, the role policy is applied.
|
2021-11-26 22:22:40 -05:00
|
|
|
arn, err := arn.Parse(roleArn)
|
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, fmt.Errorf("error parsing role ARN %s: %v", roleArn, err))
|
2021-11-26 22:22:40 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
policies = newMappedPolicy(sys.rolesMap[arn]).toSlice()
|
2022-07-26 22:06:55 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
// Otherwise, inherit parent user's policy
|
2021-12-20 17:07:16 -05:00
|
|
|
var err error
|
2024-04-05 17:26:41 -04:00
|
|
|
policies, err = sys.PolicyDBGet(parentUser, args.Groups...)
|
2021-12-20 17:07:16 -05:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, fmt.Errorf("error fetching policies on %s: %v", parentUser, err))
|
2021-12-20 17:07:16 -05:00
|
|
|
return false
|
|
|
|
}
|
2022-07-26 22:06:55 -04:00
|
|
|
|
|
|
|
// Finally, if there is no parent policy, check if a policy claim is
|
|
|
|
// present in the session token.
|
2021-12-20 17:07:16 -05:00
|
|
|
if len(policies) == 0 {
|
Map policy to parent for STS (#13884)
When STS credentials are created for a user, a unique (hopefully stable) parent
user value exists for the credential, which corresponds to the user for whom the
credentials are created. The access policy is mapped to this parent-user and is
persisted. This helps ensure that all STS credentials of a user have the same
policy assignment at all times.
Before this change, for an OIDC STS credential, when the policy claim changes in
the provider (when not using RoleARNs), the change would not take effect on
existing credentials, but only on new ones.
To support existing STS credentials without parent-user policy mappings, we
lookup the policy in the policy claim value. This behavior should be deprecated
when such support is no longer required, as it can still lead to stale
policy mappings.
Additionally this change also simplifies the implementation for all non-RoleARN
STS credentials. Specifically, for AssumeRole (internal IDP) STS credentials,
policies are picked up from the parent user's policies; for
AssumeRoleWithCertificate STS credentials, policies are picked up from the
parent user mapping created when the STS credential is generated.
AssumeRoleWithLDAP already picks up policies mapped to the virtual parent user.
2021-12-17 03:46:30 -05:00
|
|
|
// If there is no parent policy mapping, we fall back to
|
|
|
|
// using policy claim from JWT.
|
|
|
|
policySet, ok := args.GetPolicies(iamPolicyClaimNameOpenID())
|
|
|
|
if !ok {
|
|
|
|
// When claims are set, it should have a policy claim field.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
policies = policySet.ToSlice()
|
2021-11-26 22:22:40 -05:00
|
|
|
}
|
2022-07-26 22:06:55 -04:00
|
|
|
|
2019-06-20 18:28:33 -04:00
|
|
|
}
|
|
|
|
|
2022-05-19 14:06:55 -04:00
|
|
|
// Defensive code: Do not allow any operation if no policy is found in the session token
|
2022-07-26 22:06:55 -04:00
|
|
|
if !isOwnerDerived && len(policies) == 0 {
|
2022-05-19 14:06:55 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-26 22:06:55 -04:00
|
|
|
// 2. Combine the mapped policies into a single combined policy.
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
var combinedPolicy policy.Policy
|
2022-07-26 22:06:55 -04:00
|
|
|
if !isOwnerDerived {
|
|
|
|
var err error
|
|
|
|
combinedPolicy, err = sys.store.GetPolicy(strings.Join(policies, ","))
|
2022-11-14 10:15:46 -05:00
|
|
|
if errors.Is(err, errNoSuchPolicy) {
|
2022-07-26 22:06:55 -04:00
|
|
|
for _, pname := range policies {
|
|
|
|
_, err := sys.store.GetPolicy(pname)
|
2022-11-14 10:15:46 -05:00
|
|
|
if errors.Is(err, errNoSuchPolicy) {
|
2022-07-26 22:06:55 -04:00
|
|
|
// all policies presented in the claim should exist
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, fmt.Errorf("expected policy (%s) missing from the JWT claim %s, rejecting the request", pname, iamPolicyClaimNameOpenID()))
|
2022-07-26 22:06:55 -04:00
|
|
|
return false
|
|
|
|
}
|
2021-11-03 22:47:49 -04:00
|
|
|
}
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, fmt.Errorf("all policies were unexpectedly present!"))
|
2022-07-26 22:06:55 -04:00
|
|
|
return false
|
2020-05-11 16:04:11 -04:00
|
|
|
}
|
2022-07-26 22:06:55 -04:00
|
|
|
|
2020-05-11 16:04:11 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 22:06:55 -04:00
|
|
|
// 3. If an inline session-policy is present, evaluate it.
|
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
// Now check if we have a sessionPolicy.
|
|
|
|
hasSessionPolicy, isAllowedSP := isAllowedBySessionPolicy(args)
|
|
|
|
if hasSessionPolicy {
|
2022-07-26 22:06:55 -04:00
|
|
|
return isAllowedSP && (isOwnerDerived || combinedPolicy.IsAllowed(args))
|
2021-07-15 18:27:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sub policy not set, this is most common since subPolicy
|
|
|
|
// is optional, use the inherited policies.
|
2022-07-26 22:06:55 -04:00
|
|
|
return isOwnerDerived || combinedPolicy.IsAllowed(args)
|
2021-07-15 18:27:34 -04:00
|
|
|
}
|
|
|
|
|
2024-02-01 17:45:03 -05:00
|
|
|
func isAllowedBySessionPolicyForServiceAccount(args policy.Args) (hasSessionPolicy bool, isAllowed bool) {
|
|
|
|
hasSessionPolicy = false
|
|
|
|
isAllowed = false
|
|
|
|
|
|
|
|
// Now check if we have a sessionPolicy.
|
|
|
|
spolicy, ok := args.Claims[sessionPolicyNameExtracted]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hasSessionPolicy = true
|
|
|
|
|
|
|
|
spolicyStr, ok := spolicy.(string)
|
|
|
|
if !ok {
|
|
|
|
// Sub policy if set, should be a string reject
|
|
|
|
// malformed/malicious requests.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if policy is parseable.
|
|
|
|
subPolicy, err := policy.ParseConfig(bytes.NewReader([]byte(spolicyStr)))
|
|
|
|
if err != nil {
|
|
|
|
// Log any error in input session policy config.
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, err)
|
2024-02-01 17:45:03 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SPECIAL CASE: For service accounts, any valid JSON is allowed as a
|
|
|
|
// policy, regardless of whether the number of statements is 0, this
|
|
|
|
// includes `null`, `{}` and `{"Statement": null}`. In fact, MinIO Console
|
|
|
|
// sends `null` when no policy is set and the intended behavior is that the
|
|
|
|
// service account should inherit parent policy.
|
|
|
|
//
|
|
|
|
// However, for a policy like `{"Statement":[]}`, the intention is to not
|
|
|
|
// provide any permissions via the session policy - i.e. the service account
|
|
|
|
// can do nothing (such a JSON could be generated by an external application
|
|
|
|
// as the policy for the service account). Inheriting the parent policy in
|
|
|
|
// such a case, is a security issue. Ideally, we should not allow such
|
|
|
|
// behavior, but for compatibility with the Console, we currently allow it.
|
|
|
|
//
|
|
|
|
// TODO:
|
|
|
|
//
|
|
|
|
// 1. fix console behavior and allow this inheritance for service accounts
|
|
|
|
// created before a certain (TBD) future date.
|
|
|
|
//
|
|
|
|
// 2. do not allow empty statement policies for service accounts.
|
|
|
|
if subPolicy.Version == "" && subPolicy.Statements == nil && subPolicy.ID == "" {
|
|
|
|
hasSessionPolicy = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the session policy exists, even if the parent is the root account, it
|
|
|
|
// must be restricted by it. So, we set `.IsOwner` to false here
|
|
|
|
// unconditionally.
|
|
|
|
sessionPolicyArgs := args
|
|
|
|
sessionPolicyArgs.IsOwner = false
|
|
|
|
|
|
|
|
// Sub policy is set and valid.
|
|
|
|
return hasSessionPolicy, subPolicy.IsAllowed(sessionPolicyArgs)
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
func isAllowedBySessionPolicy(args policy.Args) (hasSessionPolicy bool, isAllowed bool) {
|
2021-07-15 18:27:34 -04:00
|
|
|
hasSessionPolicy = false
|
|
|
|
isAllowed = false
|
|
|
|
|
2019-06-20 18:28:33 -04:00
|
|
|
// Now check if we have a sessionPolicy.
|
2022-05-02 20:56:19 -04:00
|
|
|
spolicy, ok := args.Claims[sessionPolicyNameExtracted]
|
2021-07-15 18:27:34 -04:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-06-20 18:28:33 -04:00
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
hasSessionPolicy = true
|
2019-06-20 18:28:33 -04:00
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
spolicyStr, ok := spolicy.(string)
|
|
|
|
if !ok {
|
|
|
|
// Sub policy if set, should be a string reject
|
|
|
|
// malformed/malicious requests.
|
|
|
|
return
|
|
|
|
}
|
2019-06-20 18:28:33 -04:00
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
// Check if policy is parseable.
|
2023-09-14 17:50:16 -04:00
|
|
|
subPolicy, err := policy.ParseConfig(bytes.NewReader([]byte(spolicyStr)))
|
2021-07-15 18:27:34 -04:00
|
|
|
if err != nil {
|
|
|
|
// Log any error in input session policy config.
|
2024-04-04 08:04:40 -04:00
|
|
|
iamLogIf(GlobalContext, err)
|
2021-07-15 18:27:34 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policy without Version string value reject it.
|
|
|
|
if subPolicy.Version == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-31 13:56:45 -05:00
|
|
|
// As the session policy exists, even if the parent is the root account, it
|
|
|
|
// must be restricted by it. So, we set `.IsOwner` to false here
|
|
|
|
// unconditionally.
|
|
|
|
sessionPolicyArgs := args
|
|
|
|
sessionPolicyArgs.IsOwner = false
|
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
// Sub policy is set and valid.
|
2024-01-31 13:56:45 -05:00
|
|
|
return hasSessionPolicy, subPolicy.IsAllowed(sessionPolicyArgs)
|
2019-06-20 18:28:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
// GetCombinedPolicy returns a combined policy combining all policies
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) GetCombinedPolicy(policies ...string) policy.Policy {
|
2021-11-03 22:47:49 -04:00
|
|
|
_, policy := sys.store.FilterPolicies(strings.Join(policies, ","), "")
|
|
|
|
return policy
|
2020-11-27 20:23:57 -05:00
|
|
|
}
|
|
|
|
|
2024-02-05 14:44:23 -05:00
|
|
|
// doesPolicyAllow - checks if the given policy allows the passed action with given args. This is rarely needed.
|
|
|
|
// Notice there is no account name involved, so this is a dangerous function.
|
|
|
|
func (sys *IAMSys) doesPolicyAllow(policy string, args policy.Args) bool {
|
|
|
|
// Policies were found, evaluate all of them.
|
|
|
|
return sys.GetCombinedPolicy(policy).IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:28:33 -04:00
|
|
|
// IsAllowed - checks given policy args is allowed to continue the Rest API.
|
2023-09-14 17:50:16 -04:00
|
|
|
func (sys *IAMSys) IsAllowed(args policy.Args) bool {
|
2018-10-15 15:44:03 -04:00
|
|
|
// If opa is configured, use OPA always.
|
2022-05-30 13:58:37 -04:00
|
|
|
if authz := newGlobalAuthZPluginFn(); authz != nil {
|
|
|
|
ok, err := authz.IsAllowed(args)
|
2019-07-27 23:03:25 -04:00
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
authZLogIf(GlobalContext, err)
|
2019-07-27 23:03:25 -04:00
|
|
|
}
|
|
|
|
return ok
|
2018-10-15 15:44:03 -04:00
|
|
|
}
|
|
|
|
|
2019-08-13 16:41:06 -04:00
|
|
|
// Policies don't apply to the owner.
|
|
|
|
if args.IsOwner {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-22 04:39:46 -05:00
|
|
|
// If the credential is temporary, perform STS related checks.
|
2021-02-25 16:49:59 -05:00
|
|
|
ok, parentUser, err := sys.IsTempUser(args.AccountName)
|
2020-01-22 04:39:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ok {
|
2021-02-25 16:49:59 -05:00
|
|
|
return sys.IsAllowedSTS(args, parentUser)
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
// If the credential is for a service account, perform related check
|
2021-02-25 16:49:59 -05:00
|
|
|
ok, parentUser, err = sys.IsServiceAccount(args.AccountName)
|
2020-03-17 13:36:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return sys.IsAllowedServiceAccount(args, parentUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue with the assumption of a regular user
|
2023-11-29 19:07:35 -05:00
|
|
|
policies, err := sys.PolicyDBGet(args.AccountName, args.Groups...)
|
2019-08-13 16:41:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(policies) == 0 {
|
|
|
|
// No policy found.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policies were found, evaluate all of them.
|
2020-11-27 20:23:57 -05:00
|
|
|
return sys.GetCombinedPolicy(policies...).IsAllowed(args)
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2022-08-23 14:11:45 -04:00
|
|
|
// SetUsersSysType - sets the users system type, regular or LDAP.
|
|
|
|
func (sys *IAMSys) SetUsersSysType(t UsersSysType) {
|
|
|
|
sys.usersSysType = t
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsersSysType - returns the users system type for this IAM
|
|
|
|
func (sys *IAMSys) GetUsersSysType() UsersSysType {
|
|
|
|
return sys.usersSysType
|
2020-03-22 01:47:26 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// NewIAMSys - creates new config system object.
|
|
|
|
func NewIAMSys() *IAMSys {
|
|
|
|
return &IAMSys{
|
2021-11-03 22:47:49 -04:00
|
|
|
usersSysType: MinIOUsersSysType,
|
|
|
|
configLoaded: make(chan struct{}),
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
}
|