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"
|
2020-05-11 16:04:11 -04:00
|
|
|
"strings"
|
2020-11-08 00:03:06 -05:00
|
|
|
"sync"
|
2020-06-09 22:19:03 -04:00
|
|
|
"time"
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2020-08-05 16:08:40 -04:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2021-05-06 11:52:02 -04:00
|
|
|
"github.com/minio/madmin-go"
|
2020-07-14 12:38:05 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/auth"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
2021-05-30 00:16:42 -04:00
|
|
|
iampolicy "github.com/minio/pkg/iam/policy"
|
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"
|
|
|
|
)
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
const (
|
|
|
|
// IAM configuration directory.
|
|
|
|
iamConfigPrefix = minioConfigPrefix + "/iam"
|
|
|
|
|
|
|
|
// IAM users directory.
|
|
|
|
iamConfigUsersPrefix = iamConfigPrefix + "/users/"
|
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
// IAM service accounts directory.
|
|
|
|
iamConfigServiceAccountsPrefix = iamConfigPrefix + "/service-accounts/"
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
// IAM groups directory.
|
|
|
|
iamConfigGroupsPrefix = iamConfigPrefix + "/groups/"
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
// IAM policies directory.
|
|
|
|
iamConfigPoliciesPrefix = iamConfigPrefix + "/policies/"
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// IAM sts directory.
|
|
|
|
iamConfigSTSPrefix = iamConfigPrefix + "/sts/"
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// IAM Policy DB prefixes.
|
2020-03-17 13:36:13 -04:00
|
|
|
iamConfigPolicyDBPrefix = iamConfigPrefix + "/policydb/"
|
|
|
|
iamConfigPolicyDBUsersPrefix = iamConfigPolicyDBPrefix + "users/"
|
|
|
|
iamConfigPolicyDBSTSUsersPrefix = iamConfigPolicyDBPrefix + "sts-users/"
|
|
|
|
iamConfigPolicyDBServiceAccountsPrefix = iamConfigPolicyDBPrefix + "service-accounts/"
|
|
|
|
iamConfigPolicyDBGroupsPrefix = iamConfigPolicyDBPrefix + "groups/"
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// IAM identity file which captures identity credentials.
|
|
|
|
iamIdentityFile = "identity.json"
|
|
|
|
|
|
|
|
// IAM policy file which provides policies for each users.
|
|
|
|
iamPolicyFile = "policy.json"
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
// IAM group members file
|
|
|
|
iamGroupMembersFile = "members.json"
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// IAM format file
|
|
|
|
iamFormatFile = "format.json"
|
|
|
|
|
|
|
|
iamFormatVersion1 = 1
|
2018-10-09 17:00:01 -04:00
|
|
|
)
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
const (
|
|
|
|
statusEnabled = "enabled"
|
|
|
|
statusDisabled = "disabled"
|
|
|
|
)
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
type iamFormat struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
}
|
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
func newIAMFormatVersion1() iamFormat {
|
2019-07-24 20:34:23 -04:00
|
|
|
return iamFormat{Version: iamFormatVersion1}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIAMFormatFilePath() string {
|
2019-08-06 15:08:58 -04:00
|
|
|
return iamConfigPrefix + SlashSeparator + iamFormatFile
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
func getUserIdentityPath(user string, userType IAMUserType) string {
|
|
|
|
var basePath string
|
|
|
|
switch userType {
|
2021-07-09 14:17:21 -04:00
|
|
|
case svcUser:
|
2020-03-17 13:36:13 -04:00
|
|
|
basePath = iamConfigServiceAccountsPrefix
|
|
|
|
case stsUser:
|
2019-07-24 20:34:23 -04:00
|
|
|
basePath = iamConfigSTSPrefix
|
2020-03-17 13:36:13 -04:00
|
|
|
default:
|
|
|
|
basePath = iamConfigUsersPrefix
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
return pathJoin(basePath, user, iamIdentityFile)
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
func getGroupInfoPath(group string) string {
|
|
|
|
return pathJoin(iamConfigGroupsPrefix, group, iamGroupMembersFile)
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
func getPolicyDocPath(name string) string {
|
|
|
|
return pathJoin(iamConfigPoliciesPrefix, name, iamPolicyFile)
|
|
|
|
}
|
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
func getMappedPolicyPath(name string, userType IAMUserType, isGroup bool) string {
|
|
|
|
if isGroup {
|
2019-08-02 17:25:00 -04:00
|
|
|
return pathJoin(iamConfigPolicyDBGroupsPrefix, name+".json")
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
switch userType {
|
2021-07-09 14:17:21 -04:00
|
|
|
case svcUser:
|
2020-03-17 13:36:13 -04:00
|
|
|
return pathJoin(iamConfigPolicyDBServiceAccountsPrefix, name+".json")
|
|
|
|
case stsUser:
|
|
|
|
return pathJoin(iamConfigPolicyDBSTSUsersPrefix, name+".json")
|
2019-08-02 17:25:00 -04:00
|
|
|
default:
|
|
|
|
return pathJoin(iamConfigPolicyDBUsersPrefix, name+".json")
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserIdentity represents a user's secret key and their status
|
|
|
|
type UserIdentity struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
Credentials auth.Credentials `json:"credentials"`
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
func newUserIdentity(cred auth.Credentials) UserIdentity {
|
|
|
|
return UserIdentity{Version: 1, Credentials: cred}
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
// GroupInfo contains info about a group
|
|
|
|
type GroupInfo struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Members []string `json:"members"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGroupInfo(members []string) GroupInfo {
|
|
|
|
return GroupInfo{Version: 1, Status: statusEnabled, Members: members}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MappedPolicy represents a policy name mapped to a user or group
|
|
|
|
type MappedPolicy struct {
|
2020-05-20 14:33:35 -04:00
|
|
|
Version int `json:"version"`
|
|
|
|
Policies string `json:"policy"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// converts a mapped policy into a slice of distinct policies
|
|
|
|
func (mp MappedPolicy) toSlice() []string {
|
|
|
|
var policies []string
|
|
|
|
for _, policy := range strings.Split(mp.Policies, ",") {
|
|
|
|
policy = strings.TrimSpace(policy)
|
|
|
|
if policy == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
policies = append(policies, policy)
|
|
|
|
}
|
|
|
|
return policies
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2020-05-11 16:04:11 -04:00
|
|
|
func (mp MappedPolicy) policySet() set.StringSet {
|
2020-05-20 14:33:35 -04:00
|
|
|
var policies []string
|
|
|
|
for _, policy := range strings.Split(mp.Policies, ",") {
|
|
|
|
policy = strings.TrimSpace(policy)
|
|
|
|
if policy == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
policies = append(policies, policy)
|
|
|
|
}
|
|
|
|
return set.CreateStringSet(policies...)
|
2020-05-11 16:04:11 -04:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
func newMappedPolicy(policy string) MappedPolicy {
|
2020-05-20 14:33:35 -04:00
|
|
|
return MappedPolicy{Version: 1, Policies: policy}
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// IAMSys - config system.
|
|
|
|
type IAMSys struct {
|
2020-11-08 00:03:06 -05:00
|
|
|
sync.Mutex
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
usersSysType UsersSysType
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// map of policy names to policy definitions
|
|
|
|
iamPolicyDocsMap map[string]iampolicy.Policy
|
2019-08-02 17:25:00 -04:00
|
|
|
// map of usernames to credentials
|
|
|
|
iamUsersMap map[string]auth.Credentials
|
|
|
|
// map of group names to group info
|
|
|
|
iamGroupsMap map[string]GroupInfo
|
|
|
|
// map of user names to groups they are a member of
|
|
|
|
iamUserGroupMemberships map[string]set.StringSet
|
2019-07-24 20:34:23 -04:00
|
|
|
// map of usernames/temporary access keys to policy names
|
|
|
|
iamUserPolicyMap map[string]MappedPolicy
|
2019-08-02 17:25:00 -04:00
|
|
|
// map of group names to policy names
|
|
|
|
iamGroupPolicyMap map[string]MappedPolicy
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
// Persistence layer for IAM subsystem
|
2021-03-02 20:08:25 -05:00
|
|
|
store IAMStorageAPI
|
|
|
|
|
|
|
|
// 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 (
|
2021-07-09 14:17:21 -04:00
|
|
|
regUser IAMUserType = iota
|
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
|
|
|
)
|
|
|
|
|
2020-11-04 16:06:05 -05:00
|
|
|
// key options
|
|
|
|
type options struct {
|
|
|
|
ttl int64 //expiry in seconds
|
|
|
|
}
|
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
// IAMStorageAPI defines an interface for the IAM persistence layer
|
|
|
|
type IAMStorageAPI interface {
|
2020-04-07 17:26:39 -04:00
|
|
|
lock()
|
|
|
|
unlock()
|
|
|
|
|
|
|
|
rlock()
|
|
|
|
runlock()
|
|
|
|
|
|
|
|
migrateBackendFormat(context.Context) error
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
loadPolicyDoc(ctx context.Context, policy string, m map[string]iampolicy.Policy) error
|
2020-04-07 17:26:39 -04:00
|
|
|
loadPolicyDocs(ctx context.Context, m map[string]iampolicy.Policy) error
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
loadUser(ctx context.Context, user string, userType IAMUserType, m map[string]auth.Credentials) error
|
2020-04-07 17:26:39 -04:00
|
|
|
loadUsers(ctx context.Context, userType IAMUserType, m map[string]auth.Credentials) error
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
loadGroup(ctx context.Context, group string, m map[string]GroupInfo) error
|
2020-04-07 17:26:39 -04:00
|
|
|
loadGroups(ctx context.Context, m map[string]GroupInfo) error
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
loadMappedPolicy(ctx context.Context, name string, userType IAMUserType, isGroup bool, m map[string]MappedPolicy) error
|
2020-04-07 17:26:39 -04:00
|
|
|
loadMappedPolicies(ctx context.Context, userType IAMUserType, isGroup bool, m map[string]MappedPolicy) error
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
loadAll(context.Context, *IAMSys) error
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2020-11-04 16:06:05 -05:00
|
|
|
saveIAMConfig(ctx context.Context, item interface{}, path string, opts ...options) error
|
2020-10-19 12:54:40 -04:00
|
|
|
loadIAMConfig(ctx context.Context, item interface{}, path string) error
|
|
|
|
deleteIAMConfig(ctx context.Context, path string) error
|
2019-08-02 17:25:00 -04:00
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
savePolicyDoc(ctx context.Context, policyName string, p iampolicy.Policy) error
|
2020-11-04 16:06:05 -05:00
|
|
|
saveMappedPolicy(ctx context.Context, name string, userType IAMUserType, isGroup bool, mp MappedPolicy, opts ...options) error
|
|
|
|
saveUserIdentity(ctx context.Context, name string, userType IAMUserType, u UserIdentity, opts ...options) error
|
2020-10-19 12:54:40 -04:00
|
|
|
saveGroupInfo(ctx context.Context, group string, gi GroupInfo) error
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
deletePolicyDoc(ctx context.Context, policyName string) error
|
|
|
|
deleteMappedPolicy(ctx context.Context, name string, userType IAMUserType, isGroup bool) error
|
|
|
|
deleteUserIdentity(ctx context.Context, name string, userType IAMUserType) error
|
|
|
|
deleteGroupInfo(ctx context.Context, name string) error
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
watch(context.Context, *IAMSys)
|
2018-10-09 17:00:01 -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.
|
|
|
|
func (sys *IAMSys) LoadGroup(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
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
if globalEtcdClient != nil {
|
|
|
|
// Watch APIs cover this case, so nothing to do.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
err := sys.store.loadGroup(context.Background(), group, sys.iamGroupsMap)
|
2020-05-09 16:59:12 -04:00
|
|
|
if err != nil && err != errNoSuchGroup {
|
2019-08-02 17:25:00 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:59:12 -04:00
|
|
|
if err == errNoSuchGroup {
|
2019-08-02 17:25:00 -04:00
|
|
|
// group does not exist - so remove from memory.
|
|
|
|
sys.removeGroupFromMembershipsMap(group)
|
|
|
|
delete(sys.iamGroupsMap, group)
|
|
|
|
delete(sys.iamGroupPolicyMap, group)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gi := sys.iamGroupsMap[group]
|
|
|
|
|
|
|
|
// Updating the group memberships cache happens in two steps:
|
|
|
|
//
|
|
|
|
// 1. Remove the group from each user's list of memberships.
|
|
|
|
// 2. Add the group to each member's list of memberships.
|
|
|
|
//
|
|
|
|
// This ensures that regardless of members being added or
|
|
|
|
// removed, the cache stays current.
|
|
|
|
sys.removeGroupFromMembershipsMap(group)
|
|
|
|
sys.updateGroupMembershipsMap(group, &gi)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
// LoadPolicy - reloads a specific canned policy from backend disks or etcd.
|
|
|
|
func (sys *IAMSys) LoadPolicy(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
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
if globalEtcdClient == nil {
|
2020-10-19 12:54:40 -04:00
|
|
|
return sys.store.loadPolicyDoc(context.Background(), policyName, sys.iamPolicyDocsMap)
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// When etcd is set, we use watch APIs so this code is not needed.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-13 16:41:06 -04:00
|
|
|
// LoadPolicyMapping - loads the mapped policy for a user or group
|
|
|
|
// from storage into server memory.
|
|
|
|
func (sys *IAMSys) LoadPolicyMapping(objAPI ObjectLayer, userOrGroup string, isGroup bool) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-12-10 23:28:22 -05:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2019-08-13 16:41:06 -04:00
|
|
|
if globalEtcdClient == nil {
|
|
|
|
var err error
|
2021-07-09 14:17:21 -04:00
|
|
|
userType := regUser
|
2021-04-23 18:11:01 -04:00
|
|
|
if sys.usersSysType == LDAPUsersSysType {
|
|
|
|
userType = stsUser
|
|
|
|
}
|
|
|
|
|
2019-08-13 16:41:06 -04:00
|
|
|
if isGroup {
|
2021-04-23 18:11:01 -04:00
|
|
|
err = sys.store.loadMappedPolicy(context.Background(), userOrGroup, userType, isGroup, sys.iamGroupPolicyMap)
|
2019-08-13 16:41:06 -04:00
|
|
|
} else {
|
2021-04-23 18:11:01 -04:00
|
|
|
err = sys.store.loadMappedPolicy(context.Background(), userOrGroup, userType, isGroup, sys.iamUserPolicyMap)
|
2019-08-13 16:41:06 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 18:11:01 -04:00
|
|
|
if err == errNoSuchPolicy {
|
|
|
|
if isGroup {
|
|
|
|
delete(sys.iamGroupPolicyMap, userOrGroup)
|
|
|
|
} else {
|
|
|
|
delete(sys.iamUserPolicyMap, userOrGroup)
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 16:41:06 -04:00
|
|
|
// Ignore policy not mapped error
|
2020-05-09 16:59:12 -04:00
|
|
|
if err != nil && err != errNoSuchPolicy {
|
2019-08-13 16:41:06 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// When etcd is set, we use watch APIs so this code is not needed.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
// LoadUser - reloads a specific user from backend disks or etcd.
|
2020-03-17 13:36:13 -04:00
|
|
|
func (sys *IAMSys) LoadUser(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
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
if globalEtcdClient == nil {
|
2020-10-19 12:54:40 -04:00
|
|
|
err := sys.store.loadUser(context.Background(), accessKey, userType, sys.iamUsersMap)
|
2019-07-24 20:34:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-19 12:54:40 -04:00
|
|
|
err = sys.store.loadMappedPolicy(context.Background(), accessKey, userType, false, sys.iamUserPolicyMap)
|
2019-08-08 18:10:04 -04:00
|
|
|
// Ignore policy not mapped error
|
2020-05-09 16:59:12 -04:00
|
|
|
if err != nil && err != errNoSuchPolicy {
|
2019-07-24 20:34:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2021-05-13 19:21:06 -04:00
|
|
|
// We are on purpose not persisting the policy map for parent
|
|
|
|
// user, although this is a hack, it is a good enough hack
|
|
|
|
// at this point in time - we need to overhaul our OIDC
|
|
|
|
// usage with service accounts with a more cleaner implementation
|
|
|
|
//
|
|
|
|
// This mapping is necessary to ensure that valid credentials
|
|
|
|
// have necessary ParentUser present - this is mainly for only
|
|
|
|
// webIdentity based STS tokens.
|
|
|
|
cred, ok := sys.iamUsersMap[accessKey]
|
|
|
|
if ok {
|
|
|
|
if cred.IsTemp() && cred.ParentUser != "" && cred.ParentUser != globalActiveCred.AccessKey {
|
|
|
|
if _, ok := sys.iamUserPolicyMap[cred.ParentUser]; !ok {
|
|
|
|
sys.iamUserPolicyMap[cred.ParentUser] = sys.iamUserPolicyMap[accessKey]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
|
|
|
// When etcd is set, we use watch APIs so this code is not needed.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
// LoadServiceAccount - reloads a specific service account from backend disks or etcd.
|
|
|
|
func (sys *IAMSys) LoadServiceAccount(accessKey string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-04-24 15:10:09 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
|
|
|
if globalEtcdClient == nil {
|
2021-07-09 14:17:21 -04:00
|
|
|
err := sys.store.loadUser(context.Background(), accessKey, svcUser, sys.iamUsersMap)
|
2020-04-24 15:10:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// When etcd is set, we use watch APIs so this code is not needed.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// Perform IAM configuration migration.
|
2020-04-07 17:26:39 -04:00
|
|
|
func (sys *IAMSys) doIAMConfigMigration(ctx context.Context) error {
|
|
|
|
return sys.store.migrateBackendFormat(ctx)
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
// InitStore initializes IAM stores
|
|
|
|
func (sys *IAMSys) InitStore(objAPI ObjectLayer) {
|
2020-11-08 00:03:06 -05:00
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
if globalEtcdClient == nil {
|
2021-07-10 11:32:52 -04:00
|
|
|
if globalIsGateway {
|
|
|
|
sys.store = &iamDummyStore{}
|
|
|
|
} else {
|
|
|
|
sys.store = newIAMObjectStore(objAPI)
|
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
} else {
|
2020-10-19 12:54:40 -04:00
|
|
|
sys.store = newIAMEtcdStore()
|
2020-04-07 17:26:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if globalLDAPConfig.Enabled {
|
|
|
|
sys.EnableLDAPSys()
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
2020-10-19 12:54:40 -04:00
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
|
2020-11-08 00:03:06 -05:00
|
|
|
// Initialized check if IAM is initialized
|
|
|
|
func (sys *IAMSys) Initialized() bool {
|
|
|
|
if sys == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
return sys.store != nil
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
// Load - loads all credentials
|
|
|
|
func (sys *IAMSys) Load(ctx context.Context, store IAMStorageAPI) error {
|
|
|
|
iamUsersMap := make(map[string]auth.Credentials)
|
|
|
|
iamGroupsMap := make(map[string]GroupInfo)
|
|
|
|
iamUserPolicyMap := make(map[string]MappedPolicy)
|
|
|
|
iamGroupPolicyMap := make(map[string]MappedPolicy)
|
|
|
|
iamPolicyDocsMap := make(map[string]iampolicy.Policy)
|
|
|
|
|
|
|
|
store.rlock()
|
|
|
|
isMinIOUsersSys := sys.usersSysType == MinIOUsersSysType
|
|
|
|
store.runlock()
|
|
|
|
|
|
|
|
if err := store.loadPolicyDocs(ctx, iamPolicyDocsMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets default canned policies, if none are set.
|
|
|
|
setDefaultCannedPolicies(iamPolicyDocsMap)
|
|
|
|
|
|
|
|
if isMinIOUsersSys {
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := store.loadUsers(ctx, regUser, iamUsersMap); err != nil {
|
2021-01-25 23:01:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := store.loadGroups(ctx, iamGroupsMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load polices mapped to users
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := store.loadMappedPolicies(ctx, regUser, false, iamUserPolicyMap); err != nil {
|
2021-01-25 23:01:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// load policies mapped to groups
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := store.loadMappedPolicies(ctx, regUser, true, iamGroupPolicyMap); err != nil {
|
2021-01-25 23:01:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := store.loadUsers(ctx, svcUser, iamUsersMap); err != nil {
|
2021-01-25 23:01:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// load STS temp users
|
|
|
|
if err := store.loadUsers(ctx, stsUser, iamUsersMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// load STS policy mappings
|
|
|
|
if err := store.loadMappedPolicies(ctx, stsUser, false, iamUserPolicyMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
store.lock()
|
|
|
|
defer store.unlock()
|
|
|
|
|
|
|
|
for k, v := range iamPolicyDocsMap {
|
|
|
|
sys.iamPolicyDocsMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the new reloaded entries into global map.
|
|
|
|
// See issue https://github.com/minio/minio/issues/9651
|
|
|
|
// where the present list of entries on disk are not yet
|
|
|
|
// latest, there is a small window where this can make
|
|
|
|
// valid users invalid.
|
|
|
|
for k, v := range iamUsersMap {
|
|
|
|
sys.iamUsersMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range iamUserPolicyMap {
|
|
|
|
sys.iamUserPolicyMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// purge any expired entries which became expired now.
|
|
|
|
for k, v := range sys.iamUsersMap {
|
|
|
|
if v.IsExpired() {
|
|
|
|
delete(sys.iamUsersMap, k)
|
|
|
|
delete(sys.iamUserPolicyMap, k)
|
2021-07-09 14:17:21 -04:00
|
|
|
// deleting will be done in the next cycle.
|
2021-01-25 23:01:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range iamGroupPolicyMap {
|
|
|
|
sys.iamGroupPolicyMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range iamGroupsMap {
|
|
|
|
sys.iamGroupsMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.buildUserGroupMemberships()
|
2021-03-02 20:08:25 -05:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
default:
|
|
|
|
close(sys.configLoaded)
|
|
|
|
}
|
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
|
|
|
|
func (sys *IAMSys) Init(ctx context.Context, objAPI ObjectLayer) {
|
2021-01-06 16:40:20 -05:00
|
|
|
// Initialize IAM store
|
|
|
|
sys.InitStore(objAPI)
|
|
|
|
|
2020-06-09 22:19:03 -04:00
|
|
|
retryCtx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
// Indicate to our routine to exit cleanly upon return.
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Hold the lock for migration only.
|
2020-11-04 11:25:42 -05:00
|
|
|
txnLk := objAPI.NewNSLock(minioMetaBucket, minioConfigPrefix+"/iam.lock")
|
2020-06-09 22:19:03 -04:00
|
|
|
|
2020-08-17 14:29:58 -04:00
|
|
|
// allocate dynamic timeout once before the loop
|
2020-09-18 12:18:18 -04:00
|
|
|
iamLockTimeout := newDynamicTimeout(5*time.Second, 3*time.Second)
|
2020-08-17 14:29:58 -04:00
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
|
|
|
for {
|
2020-06-09 22:19:03 -04:00
|
|
|
// let one of the server acquire the lock, if not let them timeout.
|
|
|
|
// which shall be retried again by this loop.
|
2021-04-29 23:55:21 -04:00
|
|
|
lkctx, err := txnLk.GetLock(retryCtx, iamLockTimeout)
|
2021-04-27 19:12:50 -04:00
|
|
|
if err != nil {
|
2020-06-09 22:19:03 -04:00
|
|
|
logger.Info("Waiting for all MinIO IAM sub-system to be initialized.. trying to acquire lock")
|
2020-11-02 20:52:13 -05:00
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(5*time.Second)))
|
2020-06-09 22:19:03 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if globalEtcdClient != nil {
|
|
|
|
// **** WARNING ****
|
|
|
|
// Migrating to encrypted backend on etcd should happen before initialization of
|
|
|
|
// IAM sub-system, make sure that we do not move the above codeblock elsewhere.
|
2021-05-17 19:50:47 -04:00
|
|
|
if err := migrateIAMConfigsEtcdToEncrypted(retryCtx, globalEtcdClient); err != nil {
|
2021-04-29 23:55:21 -04:00
|
|
|
txnLk.Unlock(lkctx.Cancel)
|
2020-06-11 17:11:30 -04:00
|
|
|
logger.LogIf(ctx, fmt.Errorf("Unable to decrypt an encrypted ETCD backend for IAM users and policies: %w", err))
|
|
|
|
logger.LogIf(ctx, errors.New("IAM sub-system is partially initialized, some users may not be available"))
|
2020-06-09 22:19:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These messages only meant primarily for distributed setup, so only log during distributed setup.
|
2020-06-12 23:04:01 -04:00
|
|
|
if globalIsDistErasure {
|
2020-06-09 22:19:03 -04:00
|
|
|
logger.Info("Waiting for all MinIO IAM sub-system to be initialized.. lock acquired")
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:11:30 -04:00
|
|
|
// Migrate IAM configuration, if necessary.
|
2021-05-17 19:50:47 -04:00
|
|
|
if err := sys.doIAMConfigMigration(retryCtx); err != nil {
|
2021-04-29 23:55:21 -04:00
|
|
|
txnLk.Unlock(lkctx.Cancel)
|
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)
|
|
|
|
continue
|
|
|
|
}
|
2020-06-11 17:11:30 -04:00
|
|
|
logger.LogIf(ctx, fmt.Errorf("Unable to migrate IAM users and policies to new format: %w", err))
|
|
|
|
logger.LogIf(ctx, errors.New("IAM sub-system is partially initialized, some users may not be available"))
|
2020-06-09 22:19:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:11:30 -04:00
|
|
|
// Successfully migrated, proceed to load the users.
|
2021-04-29 23:55:21 -04:00
|
|
|
txnLk.Unlock(lkctx.Cancel)
|
2020-06-09 22:19:03 -04:00
|
|
|
break
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
2020-02-01 20:07:43 -05:00
|
|
|
|
2020-12-05 16:00:44 -05:00
|
|
|
for {
|
2021-05-17 19:50:47 -04:00
|
|
|
if err := sys.store.loadAll(retryCtx, sys); 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)
|
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(5*time.Second)))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, fmt.Errorf("Unable to initialize IAM sub-system, some users may not be available %w", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2021-07-09 14:17:21 -04:00
|
|
|
if globalOpenIDConfig.ProviderEnabled() {
|
|
|
|
go func() {
|
|
|
|
// Purge expired credentials
|
|
|
|
for {
|
|
|
|
time.Sleep(globalRefreshIAMInterval)
|
|
|
|
sys.purgeExpiredCredentialsForExternalSSO(ctx)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-04-08 22:00:39 -04:00
|
|
|
go sys.store.watch(ctx, sys)
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
// DeletePolicy - deletes a canned policy from backend or etcd.
|
|
|
|
func (sys *IAMSys) DeletePolicy(policyName string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-09 17:00:01 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
if policyName == "" {
|
|
|
|
return errInvalidArgument
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
2019-12-10 23:28:22 -05:00
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
err := sys.store.deletePolicyDoc(context.Background(), policyName)
|
2020-04-16 19:22:34 -04:00
|
|
|
if err == errNoSuchPolicy {
|
2019-06-06 20:46:22 -04:00
|
|
|
// Ignore error if policy is already deleted.
|
|
|
|
err = nil
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
delete(sys.iamPolicyDocsMap, policyName)
|
2019-12-20 17:42:08 -05:00
|
|
|
|
|
|
|
// Delete user-policy mappings that will no longer apply
|
|
|
|
for u, mp := range sys.iamUserPolicyMap {
|
2020-05-20 14:33:35 -04:00
|
|
|
pset := mp.policySet()
|
|
|
|
if pset.Contains(policyName) {
|
2019-12-20 17:42:08 -05:00
|
|
|
cr, ok := sys.iamUsersMap[u]
|
|
|
|
if !ok {
|
2021-04-15 11:51:01 -04:00
|
|
|
// This case can happen when an temporary account
|
|
|
|
// is deleted or expired, removed it from userPolicyMap.
|
|
|
|
delete(sys.iamUserPolicyMap, u)
|
|
|
|
continue
|
2019-12-20 17:42:08 -05:00
|
|
|
}
|
2020-05-20 14:33:35 -04:00
|
|
|
pset.Remove(policyName)
|
2020-04-28 15:49:56 -04:00
|
|
|
// User is from STS if the cred are temporary
|
2020-03-17 13:36:13 -04:00
|
|
|
if cr.IsTemp() {
|
2020-05-20 14:33:35 -04:00
|
|
|
sys.policyDBSet(u, strings.Join(pset.ToSlice(), ","), stsUser, false)
|
2020-03-17 13:36:13 -04:00
|
|
|
} else {
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.policyDBSet(u, strings.Join(pset.ToSlice(), ","), regUser, false)
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
2019-12-20 17:42:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete group-policy mappings that will no longer apply
|
|
|
|
for g, mp := range sys.iamGroupPolicyMap {
|
2020-05-20 14:33:35 -04:00
|
|
|
pset := mp.policySet()
|
|
|
|
if pset.Contains(policyName) {
|
|
|
|
pset.Remove(policyName)
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.policyDBSet(g, strings.Join(pset.ToSlice(), ","), regUser, true)
|
2019-12-20 17:42:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-26 14:23:13 -04:00
|
|
|
// InfoPolicy - expands the canned policy into its JSON structure.
|
2020-04-07 22:30:59 -04:00
|
|
|
func (sys *IAMSys) InfoPolicy(policyName string) (iampolicy.Policy, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-04-07 22:30:59 -04:00
|
|
|
return iampolicy.Policy{}, errServerNotInitialized
|
2019-09-26 14:23:13 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
2019-09-26 14:23:13 -04:00
|
|
|
|
2021-05-29 12:22:22 -04:00
|
|
|
var combinedPolicy iampolicy.Policy
|
|
|
|
for _, policy := range strings.Split(policyName, ",") {
|
|
|
|
if policy == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v, ok := sys.iamPolicyDocsMap[policy]
|
|
|
|
if !ok {
|
|
|
|
return iampolicy.Policy{}, errNoSuchPolicy
|
|
|
|
}
|
|
|
|
combinedPolicy = combinedPolicy.Merge(v)
|
2019-09-26 14:23:13 -04:00
|
|
|
}
|
2021-05-29 12:22:22 -04:00
|
|
|
return combinedPolicy, nil
|
2019-09-26 14:23:13 -04:00
|
|
|
}
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
// ListPolicies - lists all canned policies.
|
2021-05-27 13:15:02 -04:00
|
|
|
func (sys *IAMSys) ListPolicies(bucketName string) (map[string]iampolicy.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
|
|
|
}
|
|
|
|
|
2021-03-02 20:08:25 -05:00
|
|
|
<-sys.configLoaded
|
2020-06-11 17:11:30 -04:00
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
policyDocsMap := make(map[string]iampolicy.Policy, len(sys.iamPolicyDocsMap))
|
2019-07-24 20:34:23 -04:00
|
|
|
for k, v := range sys.iamPolicyDocsMap {
|
2021-05-27 13:15:02 -04:00
|
|
|
if bucketName != "" && v.MatchResource(bucketName) {
|
|
|
|
policyDocsMap[k] = v
|
|
|
|
} else {
|
|
|
|
policyDocsMap[k] = v
|
|
|
|
}
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
return policyDocsMap, nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// SetPolicy - sets a new name policy.
|
2019-06-06 20:46:22 -04:00
|
|
|
func (sys *IAMSys) SetPolicy(policyName string, p iampolicy.Policy) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-09 17:00:01 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
if p.IsEmpty() || policyName == "" {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
2019-12-10 23:28:22 -05:00
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
if err := sys.store.savePolicyDoc(context.Background(), policyName, p); err != nil {
|
2018-10-16 15:48:19 -04:00
|
|
|
return err
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
sys.iamPolicyDocsMap[policyName] = p
|
2018-10-09 17:00:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// DeleteUser - delete user (only for long-term users not STS users).
|
2018-10-09 17:00:01 -04:00
|
|
|
func (sys *IAMSys) DeleteUser(accessKey string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-09 17:00:01 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2019-12-20 17:42:08 -05:00
|
|
|
// First we remove the user from their groups.
|
|
|
|
userInfo, getErr := sys.GetUserInfo(accessKey)
|
|
|
|
if getErr != nil {
|
|
|
|
return getErr
|
|
|
|
}
|
2020-04-07 17:26:39 -04:00
|
|
|
|
2019-12-20 17:42:08 -05:00
|
|
|
for _, group := range userInfo.MemberOf {
|
|
|
|
removeErr := sys.RemoveUsersFromGroup(group, []string{accessKey})
|
|
|
|
if removeErr != nil {
|
|
|
|
return removeErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next we can remove the user from memory and IAM store
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
for _, u := range sys.iamUsersMap {
|
2021-01-25 23:01:49 -05:00
|
|
|
// Delete any service accounts if any first.
|
2020-04-28 15:49:56 -04:00
|
|
|
if u.IsServiceAccount() {
|
|
|
|
if u.ParentUser == accessKey {
|
2021-07-09 14:17:21 -04:00
|
|
|
_ = sys.store.deleteUserIdentity(context.Background(), u.AccessKey, svcUser)
|
2020-04-28 15:49:56 -04:00
|
|
|
delete(sys.iamUsersMap, u.AccessKey)
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 23:01:49 -05:00
|
|
|
// Delete any associated STS users.
|
|
|
|
if u.IsTemp() {
|
|
|
|
if u.ParentUser == accessKey {
|
|
|
|
_ = sys.store.deleteUserIdentity(context.Background(), u.AccessKey, stsUser)
|
|
|
|
delete(sys.iamUsersMap, u.AccessKey)
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 15:49:56 -04:00
|
|
|
}
|
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
// It is ok to ignore deletion error on the mapped policy
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.store.deleteMappedPolicy(context.Background(), accessKey, regUser, false)
|
|
|
|
err := sys.store.deleteUserIdentity(context.Background(), accessKey, regUser)
|
2020-04-16 19:22:34 -04:00
|
|
|
if err == errNoSuchUser {
|
2019-06-06 20:46:22 -04:00
|
|
|
// ignore if user is already deleted.
|
|
|
|
err = nil
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
delete(sys.iamUsersMap, accessKey)
|
2019-07-24 20:34:23 -04:00
|
|
|
delete(sys.iamUserPolicyMap, accessKey)
|
2018-10-16 15:48:19 -04:00
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-07-19 18:34:01 -04:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
|
|
|
var policies []string
|
|
|
|
mp := newMappedPolicy(policyName)
|
|
|
|
for _, policy := range mp.toSlice() {
|
|
|
|
_, found := sys.iamPolicyDocsMap[policy]
|
|
|
|
if found {
|
|
|
|
policies = append(policies, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(policies, ",")
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// SetTempUser - set temporary user credentials, these credentials have an expiry.
|
2018-10-29 14:08:59 -04:00
|
|
|
func (sys *IAMSys) SetTempUser(accessKey string, cred auth.Credentials, policyName string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-09 17:00:01 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:24:01 -05:00
|
|
|
ttl := int64(cred.Expiration.Sub(UTCNow()).Seconds())
|
2020-11-04 16:06:05 -05:00
|
|
|
|
2018-10-29 14:08:59 -04:00
|
|
|
// If OPA is not set we honor any policy claims for this
|
|
|
|
// temporary user which match with pre-configured canned
|
|
|
|
// policies for this server.
|
|
|
|
if globalPolicyOPA == nil && policyName != "" {
|
2020-05-20 14:33:35 -04:00
|
|
|
mp := newMappedPolicy(policyName)
|
2021-04-15 01:35:42 -04:00
|
|
|
combinedPolicy := sys.GetCombinedPolicy(mp.toSlice()...)
|
2020-05-11 16:04:11 -04:00
|
|
|
|
|
|
|
if combinedPolicy.IsEmpty() {
|
2021-04-15 01:35:42 -04:00
|
|
|
return fmt.Errorf("specified policy %s, not found %w", policyName, errNoSuchPolicy)
|
2018-10-29 14:08:59 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 01:35:42 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2021-05-13 19:21:06 -04:00
|
|
|
if err := sys.store.saveMappedPolicy(context.Background(), accessKey, stsUser, false, mp, options{ttl: ttl}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.iamUserPolicyMap[accessKey] = mp
|
|
|
|
|
2021-04-29 16:01:42 -04:00
|
|
|
// We are on purpose not persisting the policy map for parent
|
2021-05-13 19:21:06 -04:00
|
|
|
// user, although this is a hack, it is a good enough hack
|
2021-04-29 16:01:42 -04:00
|
|
|
// at this point in time - we need to overhaul our OIDC
|
|
|
|
// usage with service accounts with a more cleaner implementation
|
|
|
|
//
|
|
|
|
// This mapping is necessary to ensure that valid credentials
|
|
|
|
// have necessary ParentUser present - this is mainly for only
|
|
|
|
// webIdentity based STS tokens.
|
2021-05-04 14:58:19 -04:00
|
|
|
if cred.IsTemp() && cred.ParentUser != "" && cred.ParentUser != globalActiveCred.AccessKey {
|
2021-04-29 16:01:42 -04:00
|
|
|
if _, ok := sys.iamUserPolicyMap[cred.ParentUser]; !ok {
|
|
|
|
sys.iamUserPolicyMap[cred.ParentUser] = mp
|
|
|
|
}
|
|
|
|
}
|
2021-04-15 01:35:42 -04:00
|
|
|
} else {
|
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
2018-10-29 14:08:59 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
u := newUserIdentity(cred)
|
2020-11-04 16:06:05 -05:00
|
|
|
if err := sys.store.saveUserIdentity(context.Background(), accessKey, stsUser, u, options{ttl: ttl}); err != nil {
|
2018-10-16 15:48:19 -04:00
|
|
|
return err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.iamUsersMap[accessKey] = cred
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-27 13:15:02 -04:00
|
|
|
// ListBucketUsers - list all users who can access this 'bucket'
|
|
|
|
func (sys *IAMSys) ListBucketUsers(bucket string) (map[string]madmin.UserInfo, error) {
|
|
|
|
if bucket == "" {
|
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
|
|
|
var users = make(map[string]madmin.UserInfo)
|
|
|
|
|
|
|
|
for k, v := range sys.iamUsersMap {
|
|
|
|
if v.IsTemp() || v.IsServiceAccount() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var policies []string
|
|
|
|
mp, ok := sys.iamUserPolicyMap[k]
|
|
|
|
if ok {
|
|
|
|
policies = append(policies, mp.toSlice()...)
|
|
|
|
for _, group := range sys.iamUserGroupMemberships[k].ToSlice() {
|
|
|
|
if nmp, ok := sys.iamGroupPolicyMap[group]; ok {
|
|
|
|
policies = append(policies, nmp.toSlice()...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var matchesPolices []string
|
|
|
|
for _, p := range policies {
|
|
|
|
if sys.iamPolicyDocsMap[p].MatchResource(bucket) {
|
|
|
|
matchesPolices = append(matchesPolices, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(matchesPolices) > 0 {
|
|
|
|
users[k] = madmin.UserInfo{
|
|
|
|
PolicyName: strings.Join(matchesPolices, ","),
|
|
|
|
Status: func() madmin.AccountStatus {
|
|
|
|
if v.IsValid() {
|
|
|
|
return madmin.AccountEnabled
|
|
|
|
}
|
|
|
|
return madmin.AccountDisabled
|
|
|
|
}(),
|
|
|
|
MemberOf: sys.iamUserGroupMemberships[k].ToSlice(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2018-10-13 03:18:43 -04:00
|
|
|
// ListUsers - list all users.
|
|
|
|
func (sys *IAMSys) ListUsers() (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
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:08:25 -05:00
|
|
|
<-sys.configLoaded
|
2020-06-11 17:11:30 -04:00
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
|
|
|
var users = make(map[string]madmin.UserInfo)
|
|
|
|
|
2018-10-13 03:18:43 -04:00
|
|
|
for k, v := range sys.iamUsersMap {
|
2020-03-17 13:36:13 -04:00
|
|
|
if !v.IsTemp() && !v.IsServiceAccount() {
|
2019-12-19 17:21:21 -05:00
|
|
|
users[k] = madmin.UserInfo{
|
2020-05-20 14:33:35 -04:00
|
|
|
PolicyName: sys.iamUserPolicyMap[k].Policies,
|
2019-12-19 17:21:21 -05:00
|
|
|
Status: func() madmin.AccountStatus {
|
|
|
|
if v.IsValid() {
|
|
|
|
return madmin.AccountEnabled
|
|
|
|
}
|
|
|
|
return madmin.AccountDisabled
|
|
|
|
}(),
|
2021-05-27 13:15:02 -04:00
|
|
|
MemberOf: sys.iamUserGroupMemberships[k].ToSlice(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sys.usersSysType == LDAPUsersSysType {
|
|
|
|
for k, v := range sys.iamUserPolicyMap {
|
|
|
|
users[k] = madmin.UserInfo{
|
|
|
|
PolicyName: v.Policies,
|
|
|
|
Status: madmin.AccountEnabled,
|
2019-12-19 17:21:21 -05:00
|
|
|
}
|
2018-10-13 03:18:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:21:21 -05:00
|
|
|
// IsTempUser - returns if given key is a temporary 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
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
2019-12-19 17:21:21 -05:00
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
cred, found := sys.iamUsersMap[name]
|
2019-12-19 17:21:21 -05:00
|
|
|
if !found {
|
2021-02-25 16:49:59 -05:00
|
|
|
return false, "", errNoSuchUser
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
cred, found := sys.iamUsersMap[name]
|
2020-03-17 13:36:13 -04:00
|
|
|
if !found {
|
|
|
|
return false, "", errNoSuchUser
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (sys *IAMSys) GetUserInfo(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
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:08:25 -05:00
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
default:
|
2021-01-25 23:01:49 -05:00
|
|
|
sys.loadUserFromStore(name)
|
|
|
|
}
|
2019-08-13 16:41:06 -04:00
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
2021-01-25 23:01:49 -05:00
|
|
|
sys.store.rlock()
|
2020-04-09 04:04:08 -04:00
|
|
|
// If the user has a mapped policy or is a member of a group, we
|
|
|
|
// return that info. Otherwise we return error.
|
2021-05-27 13:15:02 -04:00
|
|
|
var groups []string
|
|
|
|
for _, v := range sys.iamUsersMap {
|
|
|
|
if v.ParentUser == name {
|
|
|
|
groups = v.Groups
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mappedPolicy, ok := sys.iamUserPolicyMap[name]
|
2021-01-25 23:01:49 -05:00
|
|
|
sys.store.runlock()
|
2021-05-27 13:15:02 -04:00
|
|
|
if !ok {
|
2020-04-09 04:04:08 -04:00
|
|
|
return u, errNoSuchUser
|
|
|
|
}
|
2019-09-09 19:12:29 -04:00
|
|
|
return madmin.UserInfo{
|
2020-05-20 14:33:35 -04:00
|
|
|
PolicyName: mappedPolicy.Policies,
|
2021-05-27 13:15:02 -04:00
|
|
|
MemberOf: groups,
|
2019-09-09 19:12:29 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
cred, found := sys.iamUsersMap[name]
|
2019-08-13 16:41:06 -04:00
|
|
|
if !found {
|
|
|
|
return u, errNoSuchUser
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
if cred.IsTemp() || cred.IsServiceAccount() {
|
2019-12-19 17:21:21 -05:00
|
|
|
return u, errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
return madmin.UserInfo{
|
2020-05-20 14:33:35 -04:00
|
|
|
PolicyName: sys.iamUserPolicyMap[name].Policies,
|
2019-11-12 06:16:25 -05:00
|
|
|
Status: func() madmin.AccountStatus {
|
2020-04-28 15:49:56 -04:00
|
|
|
if cred.IsValid() {
|
2019-11-12 06:16:25 -05:00
|
|
|
return madmin.AccountEnabled
|
|
|
|
}
|
|
|
|
return madmin.AccountDisabled
|
|
|
|
}(),
|
|
|
|
MemberOf: sys.iamUserGroupMemberships[name].ToSlice(),
|
2021-01-25 23:01:49 -05:00
|
|
|
}, nil
|
|
|
|
|
2019-08-13 16:41:06 -04:00
|
|
|
}
|
|
|
|
|
2018-10-16 17:55:23 -04:00
|
|
|
// SetUserStatus - sets current user status, supports disabled or enabled.
|
|
|
|
func (sys *IAMSys) SetUserStatus(accessKey string, status madmin.AccountStatus) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-16 17:55:23 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:55:23 -04:00
|
|
|
if status != madmin.AccountEnabled && status != madmin.AccountDisabled {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
2018-10-16 17:55:23 -04:00
|
|
|
|
|
|
|
cred, ok := sys.iamUsersMap[accessKey]
|
|
|
|
if !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
if cred.IsTemp() || cred.IsServiceAccount() {
|
2019-12-19 17:21:21 -05:00
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
uinfo := newUserIdentity(auth.Credentials{
|
|
|
|
AccessKey: accessKey,
|
2018-10-16 17:55:23 -04:00
|
|
|
SecretKey: cred.SecretKey,
|
2019-11-13 20:38:05 -05:00
|
|
|
Status: func() string {
|
|
|
|
if status == madmin.AccountEnabled {
|
2021-03-29 12:32:36 -04:00
|
|
|
return auth.AccountOn
|
2019-11-13 20:38:05 -05:00
|
|
|
}
|
2021-03-29 12:32:36 -04:00
|
|
|
return auth.AccountOff
|
2019-11-13 20:38:05 -05:00
|
|
|
}(),
|
2019-07-24 20:34:23 -04:00
|
|
|
})
|
2019-12-10 23:28:22 -05:00
|
|
|
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := sys.store.saveUserIdentity(context.Background(), accessKey, regUser, uinfo); err != nil {
|
2018-10-16 17:55:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
sys.iamUsersMap[accessKey] = uinfo.Credentials
|
2018-10-16 17:55:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
type newServiceAccountOpts struct {
|
|
|
|
sessionPolicy *iampolicy.Policy
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
|
|
|
}
|
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
// NewServiceAccount - create a new service account
|
2021-04-15 01:51:14 -04:00
|
|
|
func (sys *IAMSys) NewServiceAccount(ctx context.Context, parentUser string, groups []string, opts newServiceAccountOpts) (auth.Credentials, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2020-03-17 13:36:13 -04:00
|
|
|
return auth.Credentials{}, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return auth.Credentials{}, err
|
|
|
|
}
|
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 {
|
|
|
|
return auth.Credentials{}, err
|
|
|
|
}
|
2020-08-05 16:08:40 -04:00
|
|
|
if len(policyBuf) > 16*humanize.KiByte {
|
2020-04-14 14:28:56 -04:00
|
|
|
return auth.Credentials{}, fmt.Errorf("Session policy should not exceed 16 KiB characters")
|
2020-03-23 17:17:18 -04:00
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2021-05-05 19:04:50 -04:00
|
|
|
cr, found := sys.iamUsersMap[parentUser]
|
2021-05-13 19:21:06 -04:00
|
|
|
// Disallow service accounts to further create more service accounts.
|
|
|
|
if found && cr.IsServiceAccount() {
|
|
|
|
return auth.Credentials{}, errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err := sys.policyDBGet(parentUser, false)
|
|
|
|
if err != nil {
|
|
|
|
return auth.Credentials{}, err
|
|
|
|
}
|
2021-05-18 18:19:20 -04:00
|
|
|
for _, group := range groups {
|
|
|
|
gpolicies, err := sys.policyDBGet(group, true)
|
|
|
|
if err != nil && err != errNoSuchGroup {
|
|
|
|
return auth.Credentials{}, err
|
|
|
|
}
|
|
|
|
policies = append(policies, gpolicies...)
|
|
|
|
}
|
2021-05-13 19:21:06 -04:00
|
|
|
if len(policies) == 0 {
|
2021-05-05 19:04:50 -04:00
|
|
|
return auth.Credentials{}, errNoSuchUser
|
2020-04-28 15:49:56 -04:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
m[iampolicy.SessionPolicyName] = base64.StdEncoding.EncodeToString(policyBuf)
|
2020-03-23 17:17:18 -04:00
|
|
|
m[iamPolicyClaimNameSA()] = "embedded-policy"
|
|
|
|
} else {
|
|
|
|
m[iamPolicyClaimNameSA()] = "inherited-policy"
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
var (
|
|
|
|
cred auth.Credentials
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(opts.accessKey) > 0 {
|
|
|
|
cred, err = auth.CreateNewCredentialsWithMetadata(opts.accessKey, opts.secretKey, m, globalActiveCred.SecretKey)
|
|
|
|
} else {
|
|
|
|
cred, err = auth.GetNewCredentialsWithMetadata(m, globalActiveCred.SecretKey)
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return auth.Credentials{}, err
|
|
|
|
}
|
|
|
|
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)
|
2020-04-28 15:49:56 -04:00
|
|
|
|
2020-03-17 13:36:13 -04:00
|
|
|
u := newUserIdentity(cred)
|
|
|
|
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := sys.store.saveUserIdentity(context.Background(), u.Credentials.AccessKey, svcUser, u); err != nil {
|
2020-03-17 13:36:13 -04:00
|
|
|
return auth.Credentials{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.iamUsersMap[u.Credentials.AccessKey] = u.Credentials
|
|
|
|
|
|
|
|
return cred, nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
type updateServiceAccountOpts struct {
|
|
|
|
sessionPolicy *iampolicy.Policy
|
|
|
|
secretKey string
|
|
|
|
status string
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateServiceAccount - edit a service account
|
|
|
|
func (sys *IAMSys) UpdateServiceAccount(ctx context.Context, accessKey string, opts updateServiceAccountOpts) error {
|
|
|
|
if !sys.Initialized() {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
|
|
|
cr, ok := sys.iamUsersMap[accessKey]
|
|
|
|
if !ok || !cr.IsServiceAccount() {
|
|
|
|
return errNoSuchServiceAccount
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.secretKey != "" {
|
2021-05-07 12:13:30 -04:00
|
|
|
if !auth.IsSecretKeyValid(opts.secretKey) {
|
|
|
|
return auth.ErrInvalidSecretKeyLength
|
|
|
|
}
|
2021-04-15 01:51:14 -04:00
|
|
|
cr.SecretKey = opts.secretKey
|
|
|
|
}
|
|
|
|
|
2021-05-07 12:13:30 -04:00
|
|
|
switch opts.status {
|
|
|
|
// The caller did not ask to update status account, do nothing
|
|
|
|
case "":
|
|
|
|
// Update account status
|
|
|
|
case auth.AccountOn, auth.AccountOff:
|
2021-04-15 01:51:14 -04:00
|
|
|
cr.Status = opts.status
|
2021-05-07 12:13:30 -04:00
|
|
|
default:
|
|
|
|
return errors.New("unknown account status value")
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.sessionPolicy != nil {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
err := opts.sessionPolicy.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
policyBuf, err := json.Marshal(opts.sessionPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(policyBuf) > 16*humanize.KiByte {
|
|
|
|
return fmt.Errorf("Session policy should not exceed 16 KiB characters")
|
|
|
|
}
|
|
|
|
|
|
|
|
m[iampolicy.SessionPolicyName] = base64.StdEncoding.EncodeToString(policyBuf)
|
|
|
|
m[iamPolicyClaimNameSA()] = "embedded-policy"
|
|
|
|
m[parentClaim] = cr.ParentUser
|
|
|
|
cr.SessionToken, err = auth.JWTSignWithAccessKey(accessKey, m, globalActiveCred.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u := newUserIdentity(cr)
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := sys.store.saveUserIdentity(context.Background(), u.Credentials.AccessKey, svcUser, u); err != nil {
|
2021-04-15 01:51:14 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.iamUsersMap[u.Credentials.AccessKey] = u.Credentials
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
// ListServiceAccounts - lists all services 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
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:08:25 -05:00
|
|
|
<-sys.configLoaded
|
2020-06-11 17:11:30 -04:00
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
2020-04-24 15:10:09 -04:00
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
var serviceAccounts []auth.Credentials
|
|
|
|
for _, v := range sys.iamUsersMap {
|
2020-04-24 15:10:09 -04:00
|
|
|
if v.IsServiceAccount() && v.ParentUser == accessKey {
|
2021-04-15 01:51:14 -04:00
|
|
|
// Hide secret key & session key here
|
|
|
|
v.SecretKey = ""
|
|
|
|
v.SessionToken = ""
|
|
|
|
serviceAccounts = append(serviceAccounts, v)
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceAccounts, nil
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
// GetServiceAccount - gets information about a service account
|
|
|
|
func (sys *IAMSys) GetServiceAccount(ctx context.Context, accessKey string) (auth.Credentials, *iampolicy.Policy, error) {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2021-04-15 01:51:14 -04:00
|
|
|
return auth.Credentials{}, nil, errServerNotInitialized
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
|
|
|
sa, ok := sys.iamUsersMap[accessKey]
|
2021-04-15 01:51:14 -04:00
|
|
|
if !ok || !sa.IsServiceAccount() {
|
|
|
|
return auth.Credentials{}, nil, errNoSuchServiceAccount
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
2021-04-15 01:51:14 -04:00
|
|
|
|
|
|
|
var embeddedPolicy *iampolicy.Policy
|
|
|
|
|
|
|
|
jwtClaims, err := auth.ExtractClaims(sa.SessionToken, globalActiveCred.SecretKey)
|
|
|
|
if err == nil {
|
|
|
|
pt, ptok := jwtClaims.Lookup(iamPolicyClaimNameSA())
|
|
|
|
sp, spok := jwtClaims.Lookup(iampolicy.SessionPolicyName)
|
|
|
|
if ptok && spok && pt == "embedded-policy" {
|
2021-04-15 17:47:58 -04:00
|
|
|
policyBytes, err := base64.StdEncoding.DecodeString(sp)
|
2021-04-15 01:51:14 -04:00
|
|
|
if err == nil {
|
2021-04-15 17:47:58 -04:00
|
|
|
p, err := iampolicy.ParseConfig(bytes.NewReader(policyBytes))
|
|
|
|
if err == nil {
|
|
|
|
policy := iampolicy.Policy{}.Merge(*p)
|
|
|
|
embeddedPolicy = &policy
|
|
|
|
}
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide secret & session keys
|
|
|
|
sa.SecretKey = ""
|
|
|
|
sa.SessionToken = ""
|
|
|
|
|
|
|
|
return sa, embeddedPolicy, nil
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteServiceAccount - delete a service account
|
|
|
|
func (sys *IAMSys) DeleteServiceAccount(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
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
|
|
|
sa, ok := sys.iamUsersMap[accessKey]
|
|
|
|
if !ok || !sa.IsServiceAccount() {
|
2020-05-01 11:05:14 -04:00
|
|
|
return nil
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// It is ok to ignore deletion error on the mapped policy
|
2021-07-09 14:17:21 -04:00
|
|
|
err := sys.store.deleteUserIdentity(context.Background(), accessKey, svcUser)
|
2021-07-15 00:28:53 -04:00
|
|
|
if err != nil && err != errNoSuchUser {
|
2020-04-24 15:10:09 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(sys.iamUsersMap, accessKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (sys *IAMSys) CreateUser(accessKey string, uinfo madmin.UserInfo) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2018-10-09 17:00:01 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2021-05-05 19:36:39 -04:00
|
|
|
if !auth.IsAccessKeyValid(accessKey) {
|
|
|
|
return auth.ErrInvalidAccessKeyLength
|
|
|
|
}
|
|
|
|
|
|
|
|
if !auth.IsSecretKeyValid(uinfo.SecretKey) {
|
|
|
|
return auth.ErrInvalidSecretKeyLength
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2019-12-19 17:21:21 -05:00
|
|
|
cr, ok := sys.iamUsersMap[accessKey]
|
|
|
|
if cr.IsTemp() && ok {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
u := newUserIdentity(auth.Credentials{
|
|
|
|
AccessKey: accessKey,
|
|
|
|
SecretKey: uinfo.SecretKey,
|
2021-03-29 12:32:36 -04:00
|
|
|
Status: func() string {
|
|
|
|
if uinfo.Status == madmin.AccountEnabled {
|
|
|
|
return auth.AccountOn
|
|
|
|
}
|
|
|
|
return auth.AccountOff
|
|
|
|
}(),
|
2021-01-25 23:01:49 -05:00
|
|
|
})
|
|
|
|
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := sys.store.saveUserIdentity(context.Background(), accessKey, regUser, u); err != nil {
|
2018-10-16 15:48:19 -04:00
|
|
|
return err
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
2019-12-19 17:21:21 -05:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
sys.iamUsersMap[accessKey] = u.Credentials
|
2018-10-12 14:32:18 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// Set policy if specified.
|
|
|
|
if uinfo.PolicyName != "" {
|
2021-07-09 14:17:21 -04:00
|
|
|
return sys.policyDBSet(accessKey, uinfo.PolicyName, regUser, false)
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-29 16:18:46 -04:00
|
|
|
// SetUserSecretKey - sets user secret key
|
|
|
|
func (sys *IAMSys) SetUserSecretKey(accessKey string, secretKey string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-05-29 16:18:46 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2021-05-05 19:36:39 -04:00
|
|
|
if !auth.IsAccessKeyValid(accessKey) {
|
|
|
|
return auth.ErrInvalidAccessKeyLength
|
|
|
|
}
|
|
|
|
|
|
|
|
if !auth.IsSecretKeyValid(secretKey) {
|
|
|
|
return auth.ErrInvalidSecretKeyLength
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2019-05-29 16:18:46 -04:00
|
|
|
cred, ok := sys.iamUsersMap[accessKey]
|
|
|
|
if !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
cred.SecretKey = secretKey
|
|
|
|
u := newUserIdentity(cred)
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := sys.store.saveUserIdentity(context.Background(), accessKey, regUser, u); err != nil {
|
2019-05-29 16:18:46 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
sys.iamUsersMap[accessKey] = cred
|
2019-05-29 16:18:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
func (sys *IAMSys) loadUserFromStore(accessKey string) {
|
|
|
|
sys.store.lock()
|
|
|
|
// If user is already found proceed.
|
|
|
|
if _, found := sys.iamUsersMap[accessKey]; !found {
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.store.loadUser(context.Background(), accessKey, regUser, sys.iamUsersMap)
|
2021-01-25 23:01:49 -05:00
|
|
|
if _, found = sys.iamUsersMap[accessKey]; found {
|
|
|
|
// found user, load its mapped policies
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.store.loadMappedPolicy(context.Background(), accessKey, regUser, false, sys.iamUserPolicyMap)
|
2021-01-25 23:01:49 -05:00
|
|
|
} else {
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.store.loadUser(context.Background(), accessKey, svcUser, sys.iamUsersMap)
|
2021-01-25 23:01:49 -05:00
|
|
|
if svc, found := sys.iamUsersMap[accessKey]; found {
|
|
|
|
// Found service account, load its parent user and its mapped policies.
|
|
|
|
if sys.usersSysType == MinIOUsersSysType {
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.store.loadUser(context.Background(), svc.ParentUser, regUser, sys.iamUsersMap)
|
2021-01-25 23:01:49 -05:00
|
|
|
}
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.store.loadMappedPolicy(context.Background(), svc.ParentUser, regUser, false, sys.iamUserPolicyMap)
|
2020-06-11 17:11:30 -04:00
|
|
|
} else {
|
2021-01-25 23:01:49 -05:00
|
|
|
// None found fall back to STS users.
|
|
|
|
sys.store.loadUser(context.Background(), accessKey, stsUser, sys.iamUsersMap)
|
|
|
|
if _, found = sys.iamUsersMap[accessKey]; found {
|
|
|
|
// STS user found, load its mapped policy.
|
|
|
|
sys.store.loadMappedPolicy(context.Background(), accessKey, stsUser, false, sys.iamUserPolicyMap)
|
2020-06-11 17:11:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 23:01:49 -05:00
|
|
|
}
|
2020-10-19 12:54:40 -04:00
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
// Load associated policies if any.
|
|
|
|
for _, policy := range sys.iamUserPolicyMap[accessKey].toSlice() {
|
|
|
|
if _, found := sys.iamPolicyDocsMap[policy]; !found {
|
|
|
|
sys.store.loadPolicyDoc(context.Background(), policy, sys.iamPolicyDocsMap)
|
2020-06-11 17:11:30 -04:00
|
|
|
}
|
2021-01-25 23:01:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.buildUserGroupMemberships()
|
|
|
|
sys.store.unlock()
|
|
|
|
}
|
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) {
|
|
|
|
sys.store.lock()
|
|
|
|
parentUsersMap := make(map[string]auth.Credentials, len(sys.iamUsersMap))
|
|
|
|
for _, cred := range sys.iamUsersMap {
|
|
|
|
if cred.IsServiceAccount() || cred.IsTemp() {
|
|
|
|
userid, err := parseOpenIDParentUser(cred.ParentUser)
|
|
|
|
if err == errSkipFile {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parentUsersMap[userid] = cred
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sys.store.unlock()
|
|
|
|
|
|
|
|
expiredUsers := make([]auth.Credentials, 0, len(parentUsersMap))
|
|
|
|
for userid, cred := range parentUsersMap {
|
|
|
|
u, err := globalOpenIDConfig.LookupUser(userid)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(GlobalContext, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Disabled parentUser purge the entries locally
|
|
|
|
if !u.Enabled {
|
|
|
|
expiredUsers = append(expiredUsers, cred)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cred := range expiredUsers {
|
|
|
|
userType := regUser
|
|
|
|
if cred.IsServiceAccount() {
|
|
|
|
userType = svcUser
|
|
|
|
} else if cred.IsTemp() {
|
|
|
|
userType = stsUser
|
|
|
|
}
|
|
|
|
sys.store.deleteIAMConfig(ctx, getUserIdentityPath(cred.AccessKey, userType))
|
|
|
|
sys.store.deleteIAMConfig(ctx, getMappedPolicyPath(cred.AccessKey, userType, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.store.lock()
|
|
|
|
for _, cred := range expiredUsers {
|
|
|
|
delete(sys.iamUsersMap, cred.AccessKey)
|
|
|
|
delete(sys.iamUserPolicyMap, cred.AccessKey)
|
|
|
|
}
|
|
|
|
sys.store.unlock()
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
// GetUser - get user credentials
|
|
|
|
func (sys *IAMSys) GetUser(accessKey string) (cred auth.Credentials, ok bool) {
|
|
|
|
if !sys.Initialized() {
|
|
|
|
return cred, false
|
2020-06-11 17:11:30 -04:00
|
|
|
}
|
|
|
|
|
2021-03-02 20:08:25 -05:00
|
|
|
fallback := false
|
|
|
|
select {
|
|
|
|
case <-sys.configLoaded:
|
|
|
|
default:
|
2021-01-25 23:01:49 -05:00
|
|
|
sys.loadUserFromStore(accessKey)
|
2021-03-02 20:08:25 -05:00
|
|
|
fallback = true
|
2021-01-19 13:05:41 -05:00
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2021-01-19 13:05:41 -05:00
|
|
|
sys.store.rlock()
|
2018-10-09 17:00:01 -04:00
|
|
|
cred, ok = sys.iamUsersMap[accessKey]
|
2021-01-19 13:05:41 -05:00
|
|
|
if !ok && !fallback {
|
|
|
|
sys.store.runlock()
|
|
|
|
// accessKey not found, also
|
|
|
|
// IAM store is not in fallback mode
|
|
|
|
// we can try to reload again from
|
|
|
|
// the IAM store and see if credential
|
|
|
|
// exists now. If it doesn't proceed to
|
|
|
|
// fail.
|
2021-01-25 23:01:49 -05:00
|
|
|
sys.loadUserFromStore(accessKey)
|
2021-01-19 13:05:41 -05:00
|
|
|
|
|
|
|
sys.store.rlock()
|
|
|
|
cred, ok = sys.iamUsersMap[accessKey]
|
|
|
|
}
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
2020-04-28 15:49:56 -04:00
|
|
|
if ok && cred.IsValid() {
|
2021-04-29 16:01:42 -04:00
|
|
|
if cred.IsServiceAccount() || cred.IsTemp() {
|
2021-05-13 00:25:07 -04:00
|
|
|
policies, err := sys.policyDBGet(cred.ParentUser, false)
|
|
|
|
if err != nil {
|
|
|
|
// Reject if the policy map for user doesn't exist anymore.
|
|
|
|
logger.LogIf(context.Background(), fmt.Errorf("'%s' user does not have a policy present", cred.ParentUser))
|
|
|
|
return auth.Credentials{}, false
|
|
|
|
}
|
|
|
|
for _, group := range cred.Groups {
|
|
|
|
ps, err := sys.policyDBGet(group, true)
|
|
|
|
if err != nil {
|
|
|
|
// Reject if the policy map for group doesn't exist anymore.
|
|
|
|
logger.LogIf(context.Background(), fmt.Errorf("'%s' group does not have a policy present", group))
|
|
|
|
return auth.Credentials{}, false
|
|
|
|
}
|
|
|
|
policies = append(policies, ps...)
|
2021-05-04 14:58:19 -04:00
|
|
|
}
|
2021-06-24 15:00:06 -04:00
|
|
|
ok = len(policies) > 0 || globalPolicyOPA != nil
|
2020-04-28 15:49:56 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
return cred, ok && cred.IsValid()
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (sys *IAMSys) AddUsersToGroup(group string, members []string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-08-02 17:25:00 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
if group == "" {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
// Validate that all members exist.
|
|
|
|
for _, member := range members {
|
2019-12-19 17:21:21 -05:00
|
|
|
cr, ok := sys.iamUsersMap[member]
|
2019-08-02 17:25:00 -04:00
|
|
|
if !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
2019-12-19 17:21:21 -05:00
|
|
|
if cr.IsTemp() {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gi, ok := sys.iamGroupsMap[group]
|
|
|
|
if !ok {
|
|
|
|
// Set group as enabled by default when it doesn't
|
|
|
|
// exist.
|
|
|
|
gi = newGroupInfo(members)
|
|
|
|
} else {
|
|
|
|
mergedMembers := append(gi.Members, members...)
|
|
|
|
uniqMembers := set.CreateStringSet(mergedMembers...).ToSlice()
|
|
|
|
gi.Members = uniqMembers
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
if err := sys.store.saveGroupInfo(context.Background(), group, gi); err != nil {
|
2019-08-02 17:25:00 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.iamGroupsMap[group] = gi
|
|
|
|
|
|
|
|
// update user-group membership map
|
|
|
|
for _, member := range members {
|
|
|
|
gset := sys.iamUserGroupMemberships[member]
|
|
|
|
if gset == nil {
|
|
|
|
gset = set.CreateStringSet(group)
|
|
|
|
} else {
|
|
|
|
gset.Add(group)
|
|
|
|
}
|
|
|
|
sys.iamUserGroupMemberships[member] = gset
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUsersFromGroup - remove users from group. If no users are
|
|
|
|
// given, and the group is empty, deletes the group as well.
|
|
|
|
func (sys *IAMSys) RemoveUsersFromGroup(group string, members []string) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-08-02 17:25:00 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
if group == "" {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
// Validate that all members exist.
|
|
|
|
for _, member := range members {
|
2019-12-19 17:21:21 -05:00
|
|
|
cr, ok := sys.iamUsersMap[member]
|
2019-08-02 17:25:00 -04:00
|
|
|
if !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
2019-12-19 17:21:21 -05:00
|
|
|
if cr.IsTemp() {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
gi, ok := sys.iamGroupsMap[group]
|
|
|
|
if !ok {
|
|
|
|
return errNoSuchGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if attempting to delete a non-empty group.
|
|
|
|
if len(members) == 0 && len(gi.Members) != 0 {
|
|
|
|
return errGroupNotEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(members) == 0 {
|
|
|
|
// len(gi.Members) == 0 here.
|
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
// Remove the group from storage. First delete the
|
2020-04-16 19:22:34 -04:00
|
|
|
// mapped policy. No-mapped-policy case is ignored.
|
2021-07-09 14:17:21 -04:00
|
|
|
if err := sys.store.deleteMappedPolicy(context.Background(), group, regUser, true); err != nil && err != errNoSuchPolicy {
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
2020-10-19 12:54:40 -04:00
|
|
|
if err := sys.store.deleteGroupInfo(context.Background(), group); err != nil && err != errNoSuchGroup {
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete from server memory
|
|
|
|
delete(sys.iamGroupsMap, group)
|
|
|
|
delete(sys.iamGroupPolicyMap, group)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only removing members.
|
|
|
|
s := set.CreateStringSet(gi.Members...)
|
|
|
|
d := set.CreateStringSet(members...)
|
|
|
|
gi.Members = s.Difference(d).ToSlice()
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
err := sys.store.saveGroupInfo(context.Background(), group, gi)
|
2019-08-02 17:25:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sys.iamGroupsMap[group] = gi
|
|
|
|
|
|
|
|
// update user-group membership map
|
|
|
|
for _, member := range members {
|
|
|
|
gset := sys.iamUserGroupMemberships[member]
|
|
|
|
if gset == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gset.Remove(group)
|
|
|
|
sys.iamUserGroupMemberships[member] = gset
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGroupStatus - enable/disabled a group
|
|
|
|
func (sys *IAMSys) SetGroupStatus(group string, enabled bool) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-08-02 17:25:00 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return errIAMActionNotAllowed
|
|
|
|
}
|
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
if group == "" {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
gi, ok := sys.iamGroupsMap[group]
|
|
|
|
if !ok {
|
|
|
|
return errNoSuchGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
if enabled {
|
|
|
|
gi.Status = statusEnabled
|
|
|
|
} else {
|
|
|
|
gi.Status = statusDisabled
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
if err := sys.store.saveGroupInfo(context.Background(), group, gi); err != nil {
|
2019-08-02 17:25:00 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
sys.iamGroupsMap[group] = gi
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-08-13 16:41:06 -04:00
|
|
|
ps, err := sys.PolicyDBGet(group, true)
|
|
|
|
if err != nil {
|
|
|
|
return gd, err
|
|
|
|
}
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2021-03-06 12:27:06 -05:00
|
|
|
policy := strings.Join(ps, ",")
|
2019-08-13 16:41:06 -04:00
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
if sys.usersSysType != MinIOUsersSysType {
|
|
|
|
return madmin.GroupDesc{
|
|
|
|
Name: group,
|
|
|
|
Policy: policy,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
2019-09-26 14:23:13 -04:00
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
gi, ok := sys.iamGroupsMap[group]
|
|
|
|
if !ok {
|
|
|
|
return gd, errNoSuchGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
return madmin.GroupDesc{
|
|
|
|
Name: group,
|
|
|
|
Status: gi.Status,
|
|
|
|
Members: gi.Members,
|
|
|
|
Policy: policy,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
// ListGroups - lists groups.
|
|
|
|
func (sys *IAMSys) ListGroups() (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
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:08:25 -05:00
|
|
|
<-sys.configLoaded
|
2020-06-11 17:11:30 -04:00
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2020-11-02 20:52:13 -05:00
|
|
|
r = make([]string, 0, len(sys.iamGroupsMap))
|
2019-08-02 17:25:00 -04:00
|
|
|
for k := range sys.iamGroupsMap {
|
|
|
|
r = append(r, k)
|
|
|
|
}
|
2020-11-02 20:52:13 -05:00
|
|
|
|
2021-05-27 13:15:02 -04:00
|
|
|
if sys.usersSysType == LDAPUsersSysType {
|
|
|
|
for k := range sys.iamGroupPolicyMap {
|
|
|
|
r = append(r, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
return r, nil
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 04:04:08 -04:00
|
|
|
// PolicyDBSet - sets a policy for a user or group in the PolicyDB.
|
2019-08-02 17:25:00 -04:00
|
|
|
func (sys *IAMSys) PolicyDBSet(name, policy string, isGroup bool) error {
|
2020-11-08 00:03:06 -05:00
|
|
|
if !sys.Initialized() {
|
2019-07-24 20:34:23 -04:00
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.lock()
|
|
|
|
defer sys.store.unlock()
|
2019-07-24 20:34:23 -04:00
|
|
|
|
2021-03-20 05:37:52 -04:00
|
|
|
if sys.usersSysType == LDAPUsersSysType {
|
|
|
|
return sys.policyDBSet(name, policy, stsUser, isGroup)
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:17:21 -04:00
|
|
|
return sys.policyDBSet(name, policy, regUser, isGroup)
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2019-12-20 17:42:08 -05:00
|
|
|
// policyDBSet - sets a policy for user in the policy db. Assumes that caller
|
|
|
|
// has sys.Lock(). If policy == "", then policy mapping is removed.
|
2020-05-11 16:04:11 -04:00
|
|
|
func (sys *IAMSys) policyDBSet(name, policyName string, userType IAMUserType, isGroup bool) error {
|
2019-12-20 17:42:08 -05:00
|
|
|
if name == "" {
|
2019-07-24 20:34:23 -04:00
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2019-09-09 19:12:29 -04:00
|
|
|
|
|
|
|
if sys.usersSysType == MinIOUsersSysType {
|
|
|
|
if !isGroup {
|
|
|
|
if _, ok := sys.iamUsersMap[name]; !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := sys.iamGroupsMap[name]; !ok {
|
|
|
|
return errNoSuchGroup
|
|
|
|
}
|
2019-08-13 16:41:06 -04:00
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
|
2019-12-20 17:42:08 -05:00
|
|
|
// Handle policy mapping removal
|
2020-05-11 16:04:11 -04:00
|
|
|
if policyName == "" {
|
2021-04-16 21:18:55 -04:00
|
|
|
if sys.usersSysType == LDAPUsersSysType {
|
|
|
|
// Add a fallback removal towards previous content that may come back
|
|
|
|
// as a ghost user due to lack of delete, this change occurred
|
|
|
|
// introduced in PR #11840
|
2021-07-09 14:17:21 -04:00
|
|
|
sys.store.deleteMappedPolicy(context.Background(), name, regUser, false)
|
2021-04-16 21:18:55 -04:00
|
|
|
}
|
|
|
|
err := sys.store.deleteMappedPolicy(context.Background(), name, userType, isGroup)
|
|
|
|
if err != nil && err != errNoSuchPolicy {
|
2019-12-20 17:42:08 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !isGroup {
|
|
|
|
delete(sys.iamUserPolicyMap, name)
|
|
|
|
} else {
|
|
|
|
delete(sys.iamGroupPolicyMap, name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:33:35 -04:00
|
|
|
mp := newMappedPolicy(policyName)
|
|
|
|
for _, policy := range mp.toSlice() {
|
|
|
|
if _, found := sys.iamPolicyDocsMap[policy]; !found {
|
|
|
|
logger.LogIf(GlobalContext, fmt.Errorf("%w: (%s)", errNoSuchPolicy, policy))
|
2020-05-11 16:04:11 -04:00
|
|
|
return errNoSuchPolicy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 17:42:08 -05:00
|
|
|
// Handle policy mapping set/update
|
2020-10-19 12:54:40 -04:00
|
|
|
if err := sys.store.saveMappedPolicy(context.Background(), name, userType, isGroup, mp); err != nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-08-13 16:41:06 -04:00
|
|
|
if !isGroup {
|
|
|
|
sys.iamUserPolicyMap[name] = mp
|
|
|
|
} else {
|
|
|
|
sys.iamGroupPolicyMap[name] = mp
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (sys *IAMSys) PolicyDBGet(name string, isGroup bool, groups ...string) ([]string, error) {
|
2021-03-23 18:15:51 -04:00
|
|
|
if !sys.Initialized() {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
2021-03-25 03:38:15 -04:00
|
|
|
policies, err := sys.policyDBGet(name, isGroup)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-03-23 18:15:51 -04:00
|
|
|
}
|
|
|
|
|
2021-03-25 03:38:15 -04:00
|
|
|
if !isGroup {
|
|
|
|
for _, group := range groups {
|
|
|
|
ps, err := sys.policyDBGet(group, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
policies = append(policies, ps...)
|
|
|
|
}
|
2021-03-23 18:15:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return policies, nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 03:38:15 -04:00
|
|
|
// This call assumes that caller has the sys.RLock().
|
|
|
|
//
|
|
|
|
// If a group is passed, it returns policies associated with the group.
|
|
|
|
//
|
|
|
|
// If a user is passed, it returns policies of the user along with any groups
|
|
|
|
// that the server knows the user is a member of.
|
|
|
|
//
|
|
|
|
// In LDAP users mode, the server does not store any group membership
|
|
|
|
// information in IAM (i.e sys.iam*Map) - this info is stored only in the STS
|
|
|
|
// generated credentials. Thus we skip looking up group memberships, user map,
|
|
|
|
// and group map and check the appropriate policy maps directly.
|
2021-03-26 16:55:42 -04:00
|
|
|
func (sys *IAMSys) policyDBGet(name string, isGroup bool) (policies []string, err error) {
|
2019-08-02 17:25:00 -04:00
|
|
|
if isGroup {
|
2021-03-20 05:37:52 -04:00
|
|
|
if sys.usersSysType == MinIOUsersSysType {
|
|
|
|
g, ok := sys.iamGroupsMap[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errNoSuchGroup
|
|
|
|
}
|
2019-08-02 17:25:00 -04:00
|
|
|
|
2021-03-20 05:37:52 -04:00
|
|
|
// Group is disabled, so we return no policy - this
|
|
|
|
// ensures the request is denied.
|
|
|
|
if g.Status == statusDisabled {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-02-17 23:57:17 -05:00
|
|
|
}
|
|
|
|
|
2021-03-26 16:55:42 -04:00
|
|
|
return sys.iamGroupPolicyMap[name].toSlice(), nil
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
|
|
|
|
2021-05-13 19:21:06 -04:00
|
|
|
if name == globalActiveCred.AccessKey {
|
|
|
|
return []string{"consoleAdmin"}, nil
|
|
|
|
}
|
2021-05-04 14:58:19 -04:00
|
|
|
|
2021-05-13 19:21:06 -04:00
|
|
|
// When looking for a user's policies, we also check if the user
|
|
|
|
// and the groups they are member of are enabled.
|
|
|
|
var parentName string
|
|
|
|
u, ok := sys.iamUsersMap[name]
|
|
|
|
if ok {
|
2021-05-18 18:19:20 -04:00
|
|
|
if !u.IsValid() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-05-13 19:21:06 -04:00
|
|
|
parentName = u.ParentUser
|
2019-07-24 20:34:23 -04:00
|
|
|
}
|
|
|
|
|
2021-02-17 23:57:17 -05:00
|
|
|
mp, ok := sys.iamUserPolicyMap[name]
|
|
|
|
if !ok {
|
2021-05-13 19:21:06 -04:00
|
|
|
if parentName != "" {
|
|
|
|
mp = sys.iamUserPolicyMap[parentName]
|
2021-02-17 23:57:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// returned policy could be empty
|
2020-05-20 14:33:35 -04:00
|
|
|
policies = append(policies, mp.toSlice()...)
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
for _, group := range sys.iamUserGroupMemberships[name].ToSlice() {
|
2019-08-14 19:59:16 -04:00
|
|
|
// Skip missing or disabled groups
|
|
|
|
gi, ok := sys.iamGroupsMap[group]
|
|
|
|
if !ok || gi.Status == statusDisabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-26 16:55:42 -04:00
|
|
|
policies = append(policies, sys.iamGroupPolicyMap[group].toSlice()...)
|
2019-08-02 17:25:00 -04:00
|
|
|
}
|
2021-02-25 16:49:59 -05:00
|
|
|
|
2020-05-20 14:33:35 -04:00
|
|
|
return policies, nil
|
2019-07-24 20:34:23 -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
|
2021-07-11 20:39:52 -04:00
|
|
|
func (sys *IAMSys) IsAllowedServiceAccount(args iampolicy.Args, parentUser string) bool {
|
2020-03-17 13:36:13 -04:00
|
|
|
// Now check if we have a subject claim
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-25 16:49:59 -05:00
|
|
|
// Check policy for this service account.
|
2021-07-11 20:39:52 -04:00
|
|
|
svcPolicies, err := sys.PolicyDBGet(parentUser, false, args.Groups...)
|
2020-03-17 13:36:13 -04:00
|
|
|
if err != nil {
|
2021-03-26 16:55:42 -04:00
|
|
|
logger.LogIf(GlobalContext, err)
|
2020-03-17 13:36:13 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-25 16:49:59 -05:00
|
|
|
if len(svcPolicies) == 0 {
|
2020-03-17 13:36:13 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var availablePolicies []iampolicy.Policy
|
|
|
|
|
|
|
|
// Policies were found, evaluate all of them.
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.rlock()
|
2021-02-25 16:49:59 -05:00
|
|
|
for _, pname := range svcPolicies {
|
2020-03-17 13:36:13 -04:00
|
|
|
p, found := sys.iamPolicyDocsMap[pname]
|
|
|
|
if found {
|
|
|
|
availablePolicies = append(availablePolicies, p)
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.runlock()
|
2020-03-17 13:36:13 -04:00
|
|
|
|
|
|
|
if len(availablePolicies) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
combinedPolicy := availablePolicies[0]
|
|
|
|
for i := 1; i < len(availablePolicies); i++ {
|
|
|
|
combinedPolicy.Statements = append(combinedPolicy.Statements,
|
|
|
|
availablePolicies[i].Statements...)
|
|
|
|
}
|
|
|
|
|
2020-03-23 17:17:18 -04:00
|
|
|
parentArgs := args
|
2021-07-11 20:39:52 -04:00
|
|
|
parentArgs.AccountName = parentUser
|
|
|
|
// These are dynamic values set them appropriately.
|
|
|
|
parentArgs.ConditionValues["username"] = []string{parentUser}
|
|
|
|
parentArgs.ConditionValues["userid"] = []string{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
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
if saPolicyClaimStr == "inherited-policy" {
|
|
|
|
return combinedPolicy.IsAllowed(parentArgs)
|
2020-03-23 17:17:18 -04:00
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
|
|
|
// Now check if we have a sessionPolicy.
|
|
|
|
spolicy, ok := args.Claims[iampolicy.SessionPolicyName]
|
2020-04-14 14:28:56 -04:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
spolicyStr, ok := spolicy.(string)
|
|
|
|
if !ok {
|
|
|
|
// Sub policy if set, should be a string reject
|
|
|
|
// malformed/malicious requests.
|
|
|
|
return false
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
// Check if policy is parseable.
|
|
|
|
subPolicy, err := iampolicy.ParseConfig(bytes.NewReader([]byte(spolicyStr)))
|
|
|
|
if err != nil {
|
|
|
|
// Log any error in input session policy config.
|
2020-08-13 12:16:01 -04:00
|
|
|
logger.LogIf(GlobalContext, err)
|
2020-04-14 14:28:56 -04:00
|
|
|
return false
|
|
|
|
}
|
2020-03-17 13:36:13 -04:00
|
|
|
|
2021-07-11 20:39:52 -04:00
|
|
|
// This can only happen if policy was set but with an empty JSON.
|
|
|
|
if subPolicy.Version == "" && len(subPolicy.Statements) == 0 {
|
|
|
|
return combinedPolicy.IsAllowed(parentArgs)
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
if subPolicy.Version == "" {
|
|
|
|
return false
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
return combinedPolicy.IsAllowed(parentArgs) && subPolicy.IsAllowed(parentArgs)
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 14:33:35 -04:00
|
|
|
// IsAllowedLDAPSTS - checks for LDAP specific claims and values
|
2021-02-25 16:49:59 -05:00
|
|
|
func (sys *IAMSys) IsAllowedLDAPSTS(args iampolicy.Args, parentUser string) bool {
|
|
|
|
parentInClaimIface, ok := args.Claims[ldapUser]
|
|
|
|
if ok {
|
|
|
|
parentInClaim, ok := parentInClaimIface.(string)
|
|
|
|
if !ok {
|
|
|
|
// ldap parentInClaim name is not a string reject it.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentInClaim != parentUser {
|
|
|
|
// ldap claim has been modified maliciously reject it.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no ldap parentInClaim claim present reject it.
|
2020-05-20 14:33:35 -04:00
|
|
|
return false
|
|
|
|
}
|
2021-02-25 16:49:59 -05:00
|
|
|
|
|
|
|
// Check policy for this LDAP user.
|
2021-03-25 03:38:15 -04:00
|
|
|
ldapPolicies, err := sys.PolicyDBGet(parentUser, false, args.Groups...)
|
2021-02-25 16:49:59 -05:00
|
|
|
if err != nil {
|
2020-05-20 14:33:35 -04:00
|
|
|
return false
|
|
|
|
}
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2021-02-25 16:49:59 -05:00
|
|
|
if len(ldapPolicies) == 0 {
|
2020-05-20 14:33:35 -04:00
|
|
|
return false
|
|
|
|
}
|
2021-02-25 16:49:59 -05:00
|
|
|
|
|
|
|
var availablePolicies []iampolicy.Policy
|
|
|
|
|
|
|
|
// Policies were found, evaluate all of them.
|
|
|
|
sys.store.rlock()
|
|
|
|
for _, pname := range ldapPolicies {
|
|
|
|
p, found := sys.iamPolicyDocsMap[pname]
|
|
|
|
if found {
|
|
|
|
availablePolicies = append(availablePolicies, p)
|
2019-09-09 19:12:29 -04:00
|
|
|
}
|
2020-05-20 14:33:35 -04:00
|
|
|
}
|
2021-02-25 16:49:59 -05:00
|
|
|
sys.store.runlock()
|
|
|
|
|
|
|
|
if len(availablePolicies) == 0 {
|
2020-05-12 12:21:08 -04:00
|
|
|
return false
|
2019-09-09 19:12:29 -04:00
|
|
|
}
|
2021-02-25 16:49:59 -05:00
|
|
|
|
|
|
|
combinedPolicy := availablePolicies[0]
|
|
|
|
for i := 1; i < len(availablePolicies); i++ {
|
2020-05-20 14:33:35 -04:00
|
|
|
combinedPolicy.Statements =
|
|
|
|
append(combinedPolicy.Statements,
|
2021-02-25 16:49:59 -05:00
|
|
|
availablePolicies[i].Statements...)
|
2020-05-20 14:33:35 -04:00
|
|
|
}
|
2021-02-25 16:49:59 -05:00
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
hasSessionPolicy, isAllowedSP := isAllowedBySessionPolicy(args)
|
|
|
|
if hasSessionPolicy {
|
|
|
|
return isAllowedSP && combinedPolicy.IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:33:35 -04:00
|
|
|
return combinedPolicy.IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAllowedSTS is meant for STS based temporary credentials,
|
|
|
|
// which implements claims validation and verification other than
|
|
|
|
// applying policies.
|
2021-02-25 16:49:59 -05:00
|
|
|
func (sys *IAMSys) IsAllowedSTS(args iampolicy.Args, parentUser string) bool {
|
2020-05-20 14:33:35 -04:00
|
|
|
// If it is an LDAP request, check that user and group
|
|
|
|
// policies allow the request.
|
|
|
|
if sys.usersSysType == LDAPUsersSysType {
|
2021-02-25 16:49:59 -05:00
|
|
|
return sys.IsAllowedLDAPSTS(args, parentUser)
|
2020-05-20 14:33:35 -04:00
|
|
|
}
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2020-05-11 16:04:11 -04:00
|
|
|
policies, ok := args.GetPolicies(iamPolicyClaimNameOpenID())
|
2019-06-20 18:28:33 -04:00
|
|
|
if !ok {
|
2020-01-08 20:21:58 -05:00
|
|
|
// When claims are set, it should have a policy claim field.
|
2019-06-20 18:28:33 -04:00
|
|
|
return false
|
|
|
|
}
|
2020-01-08 20:21:58 -05:00
|
|
|
|
2020-05-11 16:04:11 -04:00
|
|
|
// When claims are set, it should have policies as claim.
|
|
|
|
if policies.IsEmpty() {
|
|
|
|
// No policy, no access!
|
2019-06-20 18:28:33 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-06-20 18:28:33 -04:00
|
|
|
// If policy is available for given user, check the policy.
|
2019-07-24 20:34:23 -04:00
|
|
|
mp, ok := sys.iamUserPolicyMap[args.AccountName]
|
2019-06-20 18:28:33 -04:00
|
|
|
if !ok {
|
2020-05-11 16:04:11 -04:00
|
|
|
// No policy set for the user that we can find, no access!
|
2019-06-20 18:28:33 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-11 16:04:11 -04:00
|
|
|
if !policies.Equals(mp.policySet()) {
|
2019-06-20 18:28:33 -04:00
|
|
|
// When claims has a policy, it should match the
|
|
|
|
// policy of args.AccountName which server remembers.
|
|
|
|
// if not reject such requests.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-11 16:04:11 -04:00
|
|
|
var availablePolicies []iampolicy.Policy
|
|
|
|
for pname := range policies {
|
|
|
|
p, found := sys.iamPolicyDocsMap[pname]
|
|
|
|
if !found {
|
2020-07-19 18:34:01 -04:00
|
|
|
// all policies presented in the claim should exist
|
|
|
|
logger.LogIf(GlobalContext, fmt.Errorf("expected policy (%s) missing from the JWT claim %s, rejecting the request", pname, iamPolicyClaimNameOpenID()))
|
2020-05-11 16:04:11 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
availablePolicies = append(availablePolicies, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
combinedPolicy := availablePolicies[0]
|
|
|
|
for i := 1; i < len(availablePolicies); i++ {
|
|
|
|
combinedPolicy.Statements = append(combinedPolicy.Statements,
|
|
|
|
availablePolicies[i].Statements...)
|
|
|
|
}
|
|
|
|
|
2021-07-11 20:39:52 -04:00
|
|
|
// These are dynamic values set them appropriately.
|
|
|
|
args.ConditionValues["username"] = []string{parentUser}
|
|
|
|
args.ConditionValues["userid"] = []string{parentUser}
|
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
// Now check if we have a sessionPolicy.
|
|
|
|
hasSessionPolicy, isAllowedSP := isAllowedBySessionPolicy(args)
|
|
|
|
if hasSessionPolicy {
|
|
|
|
return isAllowedSP && combinedPolicy.IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub policy not set, this is most common since subPolicy
|
|
|
|
// is optional, use the inherited policies.
|
|
|
|
return combinedPolicy.IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAllowedBySessionPolicy(args iampolicy.Args) (hasSessionPolicy bool, isAllowed bool) {
|
|
|
|
hasSessionPolicy = false
|
|
|
|
isAllowed = false
|
|
|
|
|
2019-06-20 18:28:33 -04:00
|
|
|
// Now check if we have a sessionPolicy.
|
|
|
|
spolicy, ok := args.Claims[iampolicy.SessionPolicyName]
|
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
|
|
|
policyBytes, err := base64.StdEncoding.DecodeString(spolicyStr)
|
|
|
|
if err != nil {
|
|
|
|
// Got a malformed base64 string
|
|
|
|
return
|
2019-06-20 18:28:33 -04:00
|
|
|
}
|
2021-07-15 18:27:34 -04:00
|
|
|
spolicyStr = string(policyBytes)
|
2019-06-20 18:28:33 -04:00
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
// Check if policy is parseable.
|
|
|
|
subPolicy, err := iampolicy.ParseConfig(bytes.NewReader([]byte(spolicyStr)))
|
|
|
|
if err != nil {
|
|
|
|
// Log any error in input session policy config.
|
|
|
|
logger.LogIf(GlobalContext, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policy without Version string value reject it.
|
|
|
|
if subPolicy.Version == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub policy is set and valid.
|
|
|
|
return hasSessionPolicy, subPolicy.IsAllowed(args)
|
2019-06-20 18:28:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
// GetCombinedPolicy returns a combined policy combining all policies
|
|
|
|
func (sys *IAMSys) GetCombinedPolicy(policies ...string) iampolicy.Policy {
|
|
|
|
// Policies were found, evaluate all of them.
|
|
|
|
sys.store.rlock()
|
|
|
|
defer sys.store.runlock()
|
|
|
|
|
|
|
|
var availablePolicies []iampolicy.Policy
|
|
|
|
for _, pname := range policies {
|
|
|
|
p, found := sys.iamPolicyDocsMap[pname]
|
|
|
|
if found {
|
|
|
|
availablePolicies = append(availablePolicies, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(availablePolicies) == 0 {
|
|
|
|
return iampolicy.Policy{}
|
|
|
|
}
|
|
|
|
|
|
|
|
combinedPolicy := availablePolicies[0]
|
|
|
|
for i := 1; i < len(availablePolicies); i++ {
|
|
|
|
combinedPolicy.Statements = append(combinedPolicy.Statements,
|
|
|
|
availablePolicies[i].Statements...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return combinedPolicy
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:28:33 -04:00
|
|
|
// IsAllowed - checks given policy args is allowed to continue the Rest API.
|
|
|
|
func (sys *IAMSys) IsAllowed(args iampolicy.Args) bool {
|
2018-10-15 15:44:03 -04:00
|
|
|
// If opa is configured, use OPA always.
|
|
|
|
if globalPolicyOPA != nil {
|
2019-07-27 23:03:25 -04:00
|
|
|
ok, err := globalPolicyOPA.IsAllowed(args)
|
|
|
|
if err != nil {
|
2020-08-13 12:16:01 -04:00
|
|
|
logger.LogIf(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
|
2021-03-26 16:55:42 -04:00
|
|
|
policies, err := sys.PolicyDBGet(args.AccountName, false, 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
|
|
|
}
|
|
|
|
|
2018-10-24 20:14:27 -04:00
|
|
|
// Set default canned policies only if not already overridden by users.
|
|
|
|
func setDefaultCannedPolicies(policies map[string]iampolicy.Policy) {
|
|
|
|
_, ok := policies["writeonly"]
|
|
|
|
if !ok {
|
|
|
|
policies["writeonly"] = iampolicy.WriteOnly
|
|
|
|
}
|
|
|
|
_, ok = policies["readonly"]
|
|
|
|
if !ok {
|
|
|
|
policies["readonly"] = iampolicy.ReadOnly
|
|
|
|
}
|
|
|
|
_, ok = policies["readwrite"]
|
|
|
|
if !ok {
|
|
|
|
policies["readwrite"] = iampolicy.ReadWrite
|
|
|
|
}
|
2020-02-04 20:58:38 -05:00
|
|
|
_, ok = policies["diagnostics"]
|
|
|
|
if !ok {
|
|
|
|
policies["diagnostics"] = iampolicy.AdminDiagnostics
|
|
|
|
}
|
2021-03-12 15:51:43 -05:00
|
|
|
_, ok = policies["consoleAdmin"]
|
|
|
|
if !ok {
|
|
|
|
policies["consoleAdmin"] = iampolicy.Admin
|
|
|
|
}
|
2018-10-24 20:14:27 -04:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:25:00 -04:00
|
|
|
// buildUserGroupMemberships - builds the memberships map. IMPORTANT:
|
|
|
|
// Assumes that sys.Lock is held by caller.
|
|
|
|
func (sys *IAMSys) buildUserGroupMemberships() {
|
|
|
|
for group, gi := range sys.iamGroupsMap {
|
|
|
|
sys.updateGroupMembershipsMap(group, &gi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateGroupMembershipsMap - updates the memberships map for a
|
|
|
|
// group. IMPORTANT: Assumes sys.Lock() is held by caller.
|
|
|
|
func (sys *IAMSys) updateGroupMembershipsMap(group string, gi *GroupInfo) {
|
|
|
|
if gi == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, member := range gi.Members {
|
|
|
|
v := sys.iamUserGroupMemberships[member]
|
|
|
|
if v == nil {
|
|
|
|
v = set.CreateStringSet(group)
|
|
|
|
} else {
|
|
|
|
v.Add(group)
|
|
|
|
}
|
|
|
|
sys.iamUserGroupMemberships[member] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeGroupFromMembershipsMap - removes the group from every member
|
|
|
|
// in the cache. IMPORTANT: Assumes sys.Lock() is held by caller.
|
|
|
|
func (sys *IAMSys) removeGroupFromMembershipsMap(group string) {
|
|
|
|
for member, groups := range sys.iamUserGroupMemberships {
|
|
|
|
if !groups.Contains(group) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groups.Remove(group)
|
|
|
|
sys.iamUserGroupMemberships[member] = groups
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 01:47:26 -04:00
|
|
|
// EnableLDAPSys - enable ldap system users type.
|
|
|
|
func (sys *IAMSys) EnableLDAPSys() {
|
|
|
|
sys.usersSysType = LDAPUsersSysType
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// NewIAMSys - creates new config system object.
|
|
|
|
func NewIAMSys() *IAMSys {
|
|
|
|
return &IAMSys{
|
2020-03-22 01:47:26 -04:00
|
|
|
usersSysType: MinIOUsersSysType,
|
2019-08-02 17:25:00 -04:00
|
|
|
iamUsersMap: make(map[string]auth.Credentials),
|
|
|
|
iamPolicyDocsMap: make(map[string]iampolicy.Policy),
|
|
|
|
iamUserPolicyMap: make(map[string]MappedPolicy),
|
2020-05-29 09:13:54 -04:00
|
|
|
iamGroupPolicyMap: make(map[string]MappedPolicy),
|
2019-08-02 17:25:00 -04:00
|
|
|
iamGroupsMap: make(map[string]GroupInfo),
|
|
|
|
iamUserGroupMemberships: make(map[string]set.StringSet),
|
2021-03-02 20:08:25 -05:00
|
|
|
configLoaded: make(chan struct{}),
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
}
|