2018-10-09 17:00:01 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
2018-10-09 17:00:01 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-06-20 18:28:33 -04:00
|
|
|
"bytes"
|
2018-10-09 17:00:01 -04:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2019-07-24 20:34:23 -04:00
|
|
|
"errors"
|
2018-10-09 17:00:01 -04:00
|
|
|
"path"
|
2018-10-12 14:32:18 -04:00
|
|
|
"strings"
|
2018-10-09 17:00:01 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-10-12 14:32:18 -04:00
|
|
|
etcd "github.com/coreos/etcd/clientv3"
|
2019-06-06 20:46:22 -04:00
|
|
|
"github.com/coreos/etcd/mvcc/mvccpb"
|
2019-05-29 19:35:12 -04:00
|
|
|
"github.com/minio/minio-go/v6/pkg/set"
|
2018-10-09 17:00:01 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
|
|
|
"github.com/minio/minio/pkg/auth"
|
2019-02-27 20:46:55 -05:00
|
|
|
iampolicy "github.com/minio/minio/pkg/iam/policy"
|
2018-10-09 17:00:01 -04:00
|
|
|
"github.com/minio/minio/pkg/madmin"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IAM configuration directory.
|
|
|
|
iamConfigPrefix = minioConfigPrefix + "/iam"
|
|
|
|
|
|
|
|
// IAM users directory.
|
|
|
|
iamConfigUsersPrefix = iamConfigPrefix + "/users/"
|
|
|
|
|
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.
|
|
|
|
iamConfigPolicyDBPrefix = iamConfigPrefix + "/policydb/"
|
|
|
|
iamConfigPolicyDBUsersPrefix = iamConfigPolicyDBPrefix + "users/"
|
|
|
|
iamConfigPolicyDBSTSUsersPrefix = iamConfigPolicyDBPrefix + "sts-users/"
|
|
|
|
|
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
|
|
|
|
|
|
|
// IAM format file
|
|
|
|
iamFormatFile = "format.json"
|
|
|
|
|
|
|
|
iamFormatVersion1 = 1
|
2018-10-09 17:00:01 -04:00
|
|
|
)
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
type iamFormat struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCurrentIAMFormat() iamFormat {
|
|
|
|
return iamFormat{Version: iamFormatVersion1}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIAMFormatFilePath() string {
|
|
|
|
return iamConfigPrefix + "/" + iamFormatFile
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserIdentityPath(user string, isSTS bool) string {
|
|
|
|
basePath := iamConfigUsersPrefix
|
|
|
|
if isSTS {
|
|
|
|
basePath = iamConfigSTSPrefix
|
|
|
|
}
|
|
|
|
return pathJoin(basePath, user, iamIdentityFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPolicyDocPath(name string) string {
|
|
|
|
return pathJoin(iamConfigPoliciesPrefix, name, iamPolicyFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMappedPolicyPath(name string, isSTS bool) string {
|
|
|
|
if isSTS {
|
|
|
|
return pathJoin(iamConfigPolicyDBSTSUsersPrefix, name+".json")
|
|
|
|
}
|
|
|
|
return pathJoin(iamConfigPolicyDBUsersPrefix, name+".json")
|
|
|
|
}
|
|
|
|
|
|
|
|
// MappedPolicy represents a policy name mapped to a user or group
|
|
|
|
type MappedPolicy struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
Policy string `json:"policy"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMappedPolicy(policy string) MappedPolicy {
|
|
|
|
return MappedPolicy{Version: 1, Policy: policy}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserIdentity represents a user's secret key and their status
|
|
|
|
type UserIdentity struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
Credentials auth.Credentials `json:"credentials"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUserIdentity(creds auth.Credentials) UserIdentity {
|
|
|
|
return UserIdentity{Version: 1, Credentials: creds}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadIAMConfigItem(objectAPI ObjectLayer, item interface{}, path string) error {
|
|
|
|
data, err := readConfig(context.Background(), objectAPI, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(data, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveIAMConfigItem(objectAPI ObjectLayer, item interface{}, path string) error {
|
|
|
|
data, err := json.Marshal(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return saveConfig(context.Background(), objectAPI, path, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper type for listIAMConfigItems
|
|
|
|
type itemOrErr struct {
|
|
|
|
Item string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lists files or dirs in the minioMetaBucket at the given path
|
|
|
|
// prefix. If dirs is true, only directories are listed, otherwise
|
|
|
|
// only objects are listed. All returned items have the pathPrefix
|
|
|
|
// removed from their names.
|
|
|
|
func listIAMConfigItems(objectAPI ObjectLayer, pathPrefix string, dirs bool,
|
|
|
|
doneCh <-chan struct{}) <-chan itemOrErr {
|
|
|
|
|
|
|
|
ch := make(chan itemOrErr)
|
|
|
|
dirList := func(lo ListObjectsInfo) []string {
|
|
|
|
return lo.Prefixes
|
|
|
|
}
|
|
|
|
filesList := func(lo ListObjectsInfo) (r []string) {
|
|
|
|
for _, o := range lo.Objects {
|
|
|
|
r = append(r, o.Name)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
marker := ""
|
|
|
|
for {
|
|
|
|
lo, err := objectAPI.ListObjects(context.Background(),
|
|
|
|
minioMetaBucket, pathPrefix, marker, "/", 1000)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case ch <- itemOrErr{Err: err}:
|
|
|
|
case <-doneCh:
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
marker = lo.NextMarker
|
|
|
|
lister := dirList(lo)
|
|
|
|
if !dirs {
|
|
|
|
lister = filesList(lo)
|
|
|
|
}
|
|
|
|
for _, itemPrefix := range lister {
|
|
|
|
item := strings.TrimPrefix(itemPrefix, pathPrefix)
|
|
|
|
item = strings.TrimSuffix(item, "/")
|
|
|
|
select {
|
|
|
|
case ch <- itemOrErr{Item: item}:
|
|
|
|
case <-doneCh:
|
|
|
|
close(ch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !lo.IsTruncated {
|
|
|
|
close(ch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadIAMConfigItemEtcd(ctx context.Context, item interface{}, path string) error {
|
|
|
|
pdata, err := readConfigEtcd(ctx, globalEtcdClient, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(pdata, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveIAMConfigItemEtcd(ctx context.Context, item interface{}, path string) error {
|
|
|
|
data, err := json.Marshal(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return saveConfigEtcd(context.Background(), globalEtcdClient, path, data)
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// IAMSys - config system.
|
|
|
|
type IAMSys struct {
|
|
|
|
sync.RWMutex
|
2019-07-24 20:34:23 -04:00
|
|
|
// map of usernames to credentials
|
|
|
|
iamUsersMap map[string]auth.Credentials
|
|
|
|
// map of policy names to policy definitions
|
|
|
|
iamPolicyDocsMap map[string]iampolicy.Policy
|
|
|
|
// map of usernames/temporary access keys to policy names
|
|
|
|
iamUserPolicyMap map[string]MappedPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPolicyDoc(objectAPI ObjectLayer, policy string, m map[string]iampolicy.Policy) error {
|
|
|
|
var p iampolicy.Policy
|
|
|
|
err := loadIAMConfigItem(objectAPI, &p, getPolicyDocPath(policy))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[policy] = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadPolicyDocs(objectAPI ObjectLayer, m map[string]iampolicy.Policy) error {
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
for item := range listIAMConfigItems(objectAPI, iamConfigPoliciesPrefix, true, doneCh) {
|
|
|
|
if item.Err != nil {
|
|
|
|
return item.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
policyName := item.Item
|
|
|
|
err := loadPolicyDoc(objectAPI, policyName, m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadUser(objectAPI ObjectLayer, user string, isSTS bool,
|
|
|
|
m map[string]auth.Credentials) error {
|
|
|
|
|
|
|
|
var u UserIdentity
|
|
|
|
err := loadIAMConfigItem(objectAPI, &u, getUserIdentityPath(user, isSTS))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Credentials.IsExpired() {
|
|
|
|
idFile := getUserIdentityPath(user, isSTS)
|
|
|
|
// Delete expired identity - ignoring errors here.
|
|
|
|
deleteConfig(context.Background(), objectAPI, idFile)
|
|
|
|
deleteConfig(context.Background(), objectAPI, getMappedPolicyPath(user, isSTS))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m[user] = u.Credentials
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadUsers(objectAPI ObjectLayer, isSTS bool, m map[string]auth.Credentials) error {
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
basePrefix := iamConfigUsersPrefix
|
|
|
|
if isSTS {
|
|
|
|
basePrefix = iamConfigSTSPrefix
|
|
|
|
}
|
|
|
|
for item := range listIAMConfigItems(objectAPI, basePrefix, true, doneCh) {
|
|
|
|
if item.Err != nil {
|
|
|
|
return item.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
userName := item.Item
|
|
|
|
err := loadUser(objectAPI, userName, isSTS, m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadMappedPolicy(objectAPI ObjectLayer, name string, isSTS bool,
|
|
|
|
m map[string]MappedPolicy) error {
|
|
|
|
|
|
|
|
var p MappedPolicy
|
|
|
|
err := loadIAMConfigItem(objectAPI, &p, getMappedPolicyPath(name, isSTS))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[name] = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadMappedPolicies(objectAPI ObjectLayer, isSTS bool, m map[string]MappedPolicy) error {
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
basePath := iamConfigPolicyDBUsersPrefix
|
|
|
|
if isSTS {
|
|
|
|
basePath = iamConfigPolicyDBSTSUsersPrefix
|
|
|
|
}
|
|
|
|
for item := range listIAMConfigItems(objectAPI, basePath, false, doneCh) {
|
|
|
|
if item.Err != nil {
|
|
|
|
return item.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
policyFile := item.Item
|
|
|
|
userOrGroupName := strings.TrimSuffix(policyFile, ".json")
|
|
|
|
err := loadMappedPolicy(objectAPI, userOrGroupName, isSTS, m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if objAPI == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
if globalEtcdClient == nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
return loadPolicyDoc(objAPI, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadUser - reloads a specific user from backend disks or etcd.
|
2019-07-24 20:34:23 -04:00
|
|
|
func (sys *IAMSys) LoadUser(objAPI ObjectLayer, accessKey string, isSTS bool) error {
|
2019-06-06 20:46:22 -04:00
|
|
|
if objAPI == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
if globalEtcdClient == nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
err := loadUser(objAPI, accessKey, isSTS, sys.iamUsersMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = loadMappedPolicy(objAPI, accessKey, isSTS, sys.iamUserPolicyMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
|
|
|
// When etcd is set, we use watch APIs so this code is not needed.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:39:21 -05:00
|
|
|
// Load - loads iam subsystem
|
2018-10-09 17:00:01 -04:00
|
|
|
func (sys *IAMSys) Load(objAPI ObjectLayer) error {
|
2019-04-26 09:18:50 -04:00
|
|
|
if globalEtcdClient != nil {
|
|
|
|
return sys.refreshEtcd()
|
|
|
|
}
|
2018-12-18 17:39:21 -05:00
|
|
|
return sys.refresh(objAPI)
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// sys.RLock is held by caller.
|
2019-06-06 20:46:22 -04:00
|
|
|
func (sys *IAMSys) reloadFromEvent(event *etcd.Event) {
|
|
|
|
eventCreate := event.IsModify() || event.IsCreate()
|
|
|
|
eventDelete := event.Type == etcd.EventTypeDelete
|
|
|
|
usersPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigUsersPrefix)
|
|
|
|
stsPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigSTSPrefix)
|
|
|
|
policyPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigPoliciesPrefix)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(),
|
|
|
|
defaultContextTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case eventCreate:
|
|
|
|
switch {
|
|
|
|
case usersPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigUsersPrefix))
|
2019-07-24 20:34:23 -04:00
|
|
|
loadEtcdUser(ctx, accessKey, false, sys.iamUsersMap)
|
2019-06-06 20:46:22 -04:00
|
|
|
case stsPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigSTSPrefix))
|
2019-07-24 20:34:23 -04:00
|
|
|
loadEtcdUser(ctx, accessKey, true, sys.iamUsersMap)
|
2019-06-06 20:46:22 -04:00
|
|
|
case policyPrefix:
|
|
|
|
policyName := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPoliciesPrefix))
|
2019-07-24 20:34:23 -04:00
|
|
|
loadEtcdPolicy(ctx, policyName, sys.iamPolicyDocsMap)
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
|
|
|
case eventDelete:
|
|
|
|
switch {
|
|
|
|
case usersPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigUsersPrefix))
|
|
|
|
delete(sys.iamUsersMap, accessKey)
|
|
|
|
case stsPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigSTSPrefix))
|
|
|
|
delete(sys.iamUsersMap, accessKey)
|
|
|
|
case policyPrefix:
|
|
|
|
policyName := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPoliciesPrefix))
|
2019-07-24 20:34:23 -04:00
|
|
|
delete(sys.iamPolicyDocsMap, policyName)
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch etcd entries for IAM
|
|
|
|
func (sys *IAMSys) watchIAMEtcd() {
|
|
|
|
watchEtcd := func() {
|
|
|
|
// Refresh IAMSys with etcd watch.
|
|
|
|
for {
|
|
|
|
watchCh := globalEtcdClient.Watch(context.Background(),
|
|
|
|
iamConfigPrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
select {
|
|
|
|
case <-GlobalServiceDoneCh:
|
|
|
|
return
|
|
|
|
case watchResp, ok := <-watchCh:
|
|
|
|
if !ok {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := watchResp.Err(); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
// log and retry.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, event := range watchResp.Events {
|
|
|
|
sys.Lock()
|
|
|
|
sys.reloadFromEvent(event)
|
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go watchEtcd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *IAMSys) watchIAMDisk(objAPI ObjectLayer) {
|
|
|
|
watchDisk := func() {
|
|
|
|
ticker := time.NewTicker(globalRefreshIAMInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-GlobalServiceDoneCh:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
sys.refresh(objAPI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Refresh IAMSys in background.
|
|
|
|
go watchDisk()
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// Migrate users directory in a single scan.
|
|
|
|
//
|
|
|
|
// 1. Migrate user policy from:
|
|
|
|
//
|
|
|
|
// `iamConfigUsersPrefix + "<username>/policy.json"`
|
|
|
|
//
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// `iamConfigPolicyDBUsersPrefix + "<username>.json"`.
|
|
|
|
//
|
|
|
|
// 2. Add versioning to the policy json file in the new
|
|
|
|
// location.
|
|
|
|
//
|
|
|
|
// 3. Migrate user identity json file to include version info.
|
|
|
|
func migrateUsersConfigToV1(objAPI ObjectLayer, isSTS bool) error {
|
|
|
|
basePrefix := iamConfigUsersPrefix
|
|
|
|
if isSTS {
|
|
|
|
basePrefix = iamConfigSTSPrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
for item := range listIAMConfigItems(objAPI, basePrefix, true, doneCh) {
|
|
|
|
if item.Err != nil {
|
|
|
|
return item.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := item.Item
|
|
|
|
|
|
|
|
{
|
|
|
|
// 1. check if there is policy file in old location.
|
|
|
|
oldPolicyPath := pathJoin(basePrefix, user, iamPolicyFile)
|
|
|
|
var policyName string
|
|
|
|
if err := loadIAMConfigItem(objAPI, &policyName, oldPolicyPath); err != nil {
|
|
|
|
switch err {
|
|
|
|
case errConfigNotFound:
|
|
|
|
// This case means it is already
|
|
|
|
// migrated or there is no policy on
|
|
|
|
// user.
|
|
|
|
default:
|
|
|
|
// File may be corrupt or network error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do on the policy file,
|
|
|
|
// so move on to check the id file.
|
|
|
|
goto next
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. copy policy file to new location.
|
|
|
|
mp := newMappedPolicy(policyName)
|
|
|
|
path := getMappedPolicyPath(user, isSTS)
|
|
|
|
if err := saveIAMConfigItem(objAPI, mp, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. delete policy file from old
|
|
|
|
// location. Ignore error.
|
|
|
|
deleteConfig(context.Background(), objAPI, oldPolicyPath)
|
|
|
|
}
|
|
|
|
next:
|
|
|
|
// 4. check if user identity has old format.
|
|
|
|
identityPath := pathJoin(basePrefix, user, iamIdentityFile)
|
|
|
|
var cred auth.Credentials
|
|
|
|
if err := loadIAMConfigItem(objAPI, &cred, identityPath); err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
// This should not happen.
|
|
|
|
default:
|
|
|
|
// File may be corrupt or network error
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the file is already in the new format,
|
|
|
|
// then the parsed auth.Credentials will have
|
|
|
|
// the zero value for the struct.
|
|
|
|
var zeroCred auth.Credentials
|
|
|
|
if cred == zeroCred {
|
|
|
|
// nothing to do
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a id file in old format. Copy value
|
|
|
|
// into new format and save it.
|
|
|
|
u := newUserIdentity(cred)
|
|
|
|
if err := saveIAMConfigItem(objAPI, u, identityPath); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to delete as identity file location
|
|
|
|
// has not changed.
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// migrateUsersConfigEtcd - same as migrateUsersConfig but on etcd.
|
|
|
|
func migrateUsersConfigEtcdToV1(isSTS bool) error {
|
|
|
|
basePrefix := iamConfigUsersPrefix
|
|
|
|
if isSTS {
|
|
|
|
basePrefix = iamConfigSTSPrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
defer cancel()
|
|
|
|
r, err := globalEtcdClient.Get(ctx, basePrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
users := etcdKvsToSet(basePrefix, r.Kvs)
|
|
|
|
for _, user := range users.ToSlice() {
|
|
|
|
{
|
|
|
|
// 1. check if there is a policy file in the old loc.
|
|
|
|
oldPolicyPath := pathJoin(basePrefix, user, iamPolicyFile)
|
|
|
|
var policyName string
|
|
|
|
err := loadIAMConfigItemEtcd(ctx, &policyName, oldPolicyPath)
|
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case errConfigNotFound:
|
|
|
|
// No mapped policy or already migrated.
|
|
|
|
default:
|
|
|
|
// corrupt data/read error, etc
|
|
|
|
}
|
|
|
|
goto next
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. copy policy to new loc.
|
|
|
|
mp := newMappedPolicy(policyName)
|
|
|
|
path := getMappedPolicyPath(user, isSTS)
|
|
|
|
if err := saveIAMConfigItemEtcd(ctx, mp, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. delete policy file in old loc.
|
|
|
|
deleteConfigEtcd(ctx, globalEtcdClient, oldPolicyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
next:
|
|
|
|
// 4. check if user identity has old format.
|
|
|
|
identityPath := pathJoin(basePrefix, user, iamIdentityFile)
|
|
|
|
var cred auth.Credentials
|
|
|
|
if err := loadIAMConfigItemEtcd(ctx, &cred, identityPath); err != nil {
|
|
|
|
switch err {
|
|
|
|
case errConfigNotFound:
|
|
|
|
// This case should not happen.
|
|
|
|
default:
|
|
|
|
// corrupt file or read error
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the file is already in the new format,
|
|
|
|
// then the parsed auth.Credentials will have
|
|
|
|
// the zero value for the struct.
|
|
|
|
var zeroCred auth.Credentials
|
|
|
|
if cred == zeroCred {
|
|
|
|
// nothing to do
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a id file in old format. Copy value
|
|
|
|
// into new format and save it.
|
|
|
|
u := newUserIdentity(cred)
|
|
|
|
if err := saveIAMConfigItemEtcd(ctx, u, identityPath); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to delete as identity file location
|
|
|
|
// has not changed.
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateToV1(objAPI ObjectLayer) error {
|
|
|
|
currentIAMFormat := getCurrentIAMFormat()
|
|
|
|
path := getIAMFormatFilePath()
|
|
|
|
|
|
|
|
if globalEtcdClient == nil {
|
|
|
|
var iamFmt iamFormat
|
|
|
|
if err := loadIAMConfigItem(objAPI, &iamFmt, path); err != nil {
|
|
|
|
switch err {
|
|
|
|
case errConfigNotFound:
|
|
|
|
// Need to migrate to V1.
|
|
|
|
default:
|
|
|
|
return errors.New("corrupt IAM format file")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if iamFmt.Version >= currentIAMFormat.Version {
|
|
|
|
// Already migrated to V1 or higher!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// This case should not happen
|
|
|
|
// (i.e. Version is 0 or negative.)
|
|
|
|
return errors.New("got an invalid IAM format version")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate long-term users
|
|
|
|
if err := migrateUsersConfigToV1(objAPI, false); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Migrate STS users
|
|
|
|
if err := migrateUsersConfigToV1(objAPI, true); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Save iam version file.
|
|
|
|
if err := saveIAMConfigItem(objAPI, currentIAMFormat, path); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var iamFmt iamFormat
|
|
|
|
if err := loadIAMConfigItemEtcd(context.Background(), &iamFmt, path); err != nil {
|
|
|
|
switch err {
|
|
|
|
case errConfigNotFound:
|
|
|
|
// Need to migrate to V1.
|
|
|
|
default:
|
|
|
|
return errors.New("corrupt IAM format file")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if iamFmt.Version >= currentIAMFormat.Version {
|
|
|
|
// Already migrated to V1 of higher!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// This case should not happen
|
|
|
|
// (i.e. Version is 0 or negative.)
|
|
|
|
return errors.New("got an invalid IAM format version")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate long-term users
|
|
|
|
if err := migrateUsersConfigEtcdToV1(false); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Migrate STS users
|
|
|
|
if err := migrateUsersConfigEtcdToV1(true); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Save iam version file.
|
|
|
|
if err := saveIAMConfigItemEtcd(context.Background(), currentIAMFormat, path); err != nil {
|
|
|
|
logger.LogIf(context.Background(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform IAM configuration migration.
|
|
|
|
func doIAMConfigMigration(objAPI ObjectLayer) error {
|
|
|
|
// Take IAM configuration migration lock
|
|
|
|
lockPath := iamConfigPrefix + "/migration.lock"
|
|
|
|
objLock := globalNSMutex.NewNSLock(context.Background(), minioMetaBucket, lockPath)
|
|
|
|
if err := objLock.GetLock(globalOperationTimeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer objLock.Unlock()
|
|
|
|
|
|
|
|
if err := migrateToV1(objAPI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Add future IAM migrations here.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// Init - initializes config system from iam.json
|
|
|
|
func (sys *IAMSys) Init(objAPI ObjectLayer) error {
|
|
|
|
if objAPI == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// Migrate IAM configuration
|
|
|
|
if err := doIAMConfigMigration(objAPI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-26 09:18:50 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-06-06 20:46:22 -04:00
|
|
|
defer sys.watchIAMEtcd()
|
2019-04-26 09:18:50 -04:00
|
|
|
} else {
|
2019-06-06 20:46:22 -04:00
|
|
|
defer sys.watchIAMDisk(objAPI)
|
2019-04-26 09:18:50 -04:00
|
|
|
}
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2018-10-12 14:32:18 -04:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
|
|
|
|
// Initializing IAM needs a retry mechanism for
|
|
|
|
// the following reasons:
|
|
|
|
// - Read quorum is lost just after the initialization
|
|
|
|
// of the object layer.
|
2019-02-13 07:59:36 -05:00
|
|
|
for range newRetryTimerSimple(doneCh) {
|
2019-04-26 09:18:50 -04:00
|
|
|
if globalEtcdClient != nil {
|
|
|
|
return sys.refreshEtcd()
|
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
// Load IAMSys once during boot.
|
|
|
|
if err := sys.refresh(objAPI); err != nil {
|
|
|
|
if err == errDiskNotFound ||
|
|
|
|
strings.Contains(err.Error(), InsufficientReadQuorum{}.Error()) ||
|
|
|
|
strings.Contains(err.Error(), InsufficientWriteQuorum{}.Error()) {
|
|
|
|
logger.Info("Waiting for IAM subsystem to be initialized..")
|
|
|
|
continue
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
return err
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
break
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
return nil
|
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 {
|
2018-10-09 17:00:01 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
if policyName == "" {
|
|
|
|
return errInvalidArgument
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
var err error
|
2019-07-24 20:34:23 -04:00
|
|
|
pFile := getPolicyDocPath(policyName)
|
2018-10-12 14:32:18 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-06-06 20:46:22 -04:00
|
|
|
err = deleteConfigEtcd(context.Background(), globalEtcdClient, pFile)
|
2018-10-12 14:32:18 -04:00
|
|
|
} else {
|
2019-06-06 20:46:22 -04:00
|
|
|
err = deleteConfig(context.Background(), objectAPI, pFile)
|
|
|
|
}
|
|
|
|
switch err.(type) {
|
|
|
|
case ObjectNotFound:
|
|
|
|
// Ignore error if policy is already deleted.
|
|
|
|
err = nil
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
delete(sys.iamPolicyDocsMap, policyName)
|
2018-10-16 15:48:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
// ListPolicies - lists all canned policies.
|
|
|
|
func (sys *IAMSys) ListPolicies() (map[string][]byte, error) {
|
2018-10-16 15:48:19 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return nil, errServerNotInitialized
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
var policyDocsMap = make(map[string][]byte)
|
2018-10-16 15:48:19 -04:00
|
|
|
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
for k, v := range sys.iamPolicyDocsMap {
|
2018-10-16 15:48:19 -04:00
|
|
|
data, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
policyDocsMap[k] = data
|
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 {
|
2018-10-09 17:00:01 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
if p.IsEmpty() || policyName == "" {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
path := getPolicyDocPath(policyName)
|
|
|
|
var err error
|
2018-10-12 14:32:18 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItemEtcd(context.Background(), p, path)
|
2018-10-12 14:32:18 -04:00
|
|
|
} else {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItem(objectAPI, p, path)
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-12 14:32:18 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
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 {
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2018-10-12 14:32:18 -04:00
|
|
|
var err error
|
2019-07-24 20:34:23 -04:00
|
|
|
mappingPath := getMappedPolicyPath(accessKey, false)
|
|
|
|
idPath := getUserIdentityPath(accessKey, false)
|
2018-10-12 14:32:18 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-06-06 20:46:22 -04:00
|
|
|
// It is okay to ignore errors when deleting policy.json for the user.
|
2019-07-24 20:34:23 -04:00
|
|
|
deleteConfigEtcd(context.Background(), globalEtcdClient, mappingPath)
|
|
|
|
err = deleteConfigEtcd(context.Background(), globalEtcdClient, idPath)
|
2018-10-12 14:32:18 -04:00
|
|
|
} else {
|
2019-06-06 20:46:22 -04:00
|
|
|
// It is okay to ignore errors when deleting policy.json for the user.
|
2019-07-24 20:34:23 -04:00
|
|
|
_ = deleteConfig(context.Background(), objectAPI, mappingPath)
|
|
|
|
err = deleteConfig(context.Background(), objectAPI, idPath)
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case ObjectNotFound:
|
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
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2018-10-09 17:00:01 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:08:59 -04:00
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
// 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 != "" {
|
2019-07-24 20:34:23 -04:00
|
|
|
p, ok := sys.iamPolicyDocsMap[policyName]
|
2018-10-29 14:08:59 -04:00
|
|
|
if !ok {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
if p.IsEmpty() {
|
2019-07-24 20:34:23 -04:00
|
|
|
delete(sys.iamUserPolicyMap, accessKey)
|
2018-10-29 14:08:59 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
mp := newMappedPolicy(policyName)
|
2018-10-29 14:08:59 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
mappingPath := getMappedPolicyPath(accessKey, true)
|
|
|
|
var err error
|
2018-10-29 14:08:59 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItemEtcd(context.Background(), mp, mappingPath)
|
2018-10-29 14:08:59 -04:00
|
|
|
} else {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItem(objectAPI, mp, mappingPath)
|
2018-10-29 14:08:59 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
sys.iamUserPolicyMap[accessKey] = mp
|
2018-10-29 14:08:59 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
idPath := getUserIdentityPath(accessKey, true)
|
|
|
|
u := newUserIdentity(cred)
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
var err error
|
2018-10-12 14:32:18 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItemEtcd(context.Background(), u, idPath)
|
2018-10-12 14:32:18 -04:00
|
|
|
} else {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItem(objectAPI, u, idPath)
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sys.iamUsersMap[accessKey] = cred
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-13 03:18:43 -04:00
|
|
|
// ListUsers - list all users.
|
|
|
|
func (sys *IAMSys) ListUsers() (map[string]madmin.UserInfo, error) {
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
var users = make(map[string]madmin.UserInfo)
|
|
|
|
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
|
|
|
for k, v := range sys.iamUsersMap {
|
|
|
|
users[k] = madmin.UserInfo{
|
2019-07-24 20:34:23 -04:00
|
|
|
PolicyName: sys.iamUserPolicyMap[k].Policy,
|
2018-10-16 15:48:19 -04:00
|
|
|
Status: madmin.AccountStatus(v.Status),
|
2018-10-13 03:18:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != madmin.AccountEnabled && status != madmin.AccountDisabled {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
cred, ok := sys.iamUsersMap[accessKey]
|
|
|
|
if !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
|
|
|
|
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-07-24 20:34:23 -04:00
|
|
|
Status: string(status),
|
|
|
|
})
|
|
|
|
idFile := getUserIdentityPath(accessKey, false)
|
|
|
|
var err error
|
2018-10-16 17:55:23 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItemEtcd(context.Background(), uinfo, idFile)
|
2018-10-16 17:55:23 -04:00
|
|
|
} else {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItem(objectAPI, uinfo, idFile)
|
2018-10-16 17:55:23 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// SetUser - set user credentials and policy.
|
2018-10-09 17:00:01 -04:00
|
|
|
func (sys *IAMSys) SetUser(accessKey string, uinfo madmin.UserInfo) error {
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
idFile := getUserIdentityPath(accessKey, false)
|
|
|
|
u := newUserIdentity(auth.Credentials{
|
|
|
|
AccessKey: accessKey,
|
|
|
|
SecretKey: uinfo.SecretKey,
|
|
|
|
Status: string(uinfo.Status),
|
|
|
|
})
|
|
|
|
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
var err error
|
2018-10-12 14:32:18 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItemEtcd(context.Background(), u, idFile)
|
2018-10-12 14:32:18 -04:00
|
|
|
} else {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItem(objectAPI, u, idFile)
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-12 14:32:18 -04: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 != "" {
|
|
|
|
return sys.policyDBSet(objectAPI, accessKey, uinfo.PolicyName, 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 {
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
cred, ok := sys.iamUsersMap[accessKey]
|
|
|
|
if !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
cred.SecretKey = secretKey
|
|
|
|
u := newUserIdentity(cred)
|
|
|
|
idFile := getUserIdentityPath(accessKey, false)
|
2019-05-29 16:18:46 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
var err error
|
2019-05-29 16:18:46 -04:00
|
|
|
if globalEtcdClient != nil {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItemEtcd(context.Background(), u, idFile)
|
2019-05-29 16:18:46 -04:00
|
|
|
} else {
|
2019-07-24 20:34:23 -04:00
|
|
|
err = saveIAMConfigItem(objectAPI, u, idFile)
|
2019-05-29 16:18:46 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
sys.iamUsersMap[accessKey] = cred
|
2019-05-29 16:18:46 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// GetUser - get user credentials
|
|
|
|
func (sys *IAMSys) GetUser(accessKey string) (cred auth.Credentials, ok bool) {
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
|
|
|
cred, ok = sys.iamUsersMap[accessKey]
|
|
|
|
return cred, ok && cred.IsValid()
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// PolicyDBSet - sets a policy for a user or group in the
|
|
|
|
// PolicyDB. This function applies only long-term users. For STS
|
|
|
|
// users, policy is set directly by called sys.policyDBSet().
|
|
|
|
func (sys *IAMSys) PolicyDBSet(name, policy string) error {
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
return sys.policyDBSet(objectAPI, name, policy, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// policyDBSet - sets a policy for user in the policy db. Assumes that
|
|
|
|
// caller has sys.Lock().
|
|
|
|
func (sys *IAMSys) policyDBSet(objectAPI ObjectLayer, name, policy string, isSTS bool) error {
|
|
|
|
if name == "" || policy == "" {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := sys.iamUsersMap[name]; !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := sys.iamPolicyDocsMap[policy]
|
|
|
|
if !ok {
|
|
|
|
return errNoSuchPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
mp := newMappedPolicy(policy)
|
|
|
|
_, ok = sys.iamUsersMap[name]
|
|
|
|
if !ok {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
mappingPath := getMappedPolicyPath(name, isSTS)
|
|
|
|
if globalEtcdClient != nil {
|
|
|
|
err = saveIAMConfigItemEtcd(context.Background(), mp, mappingPath)
|
|
|
|
} else {
|
|
|
|
err = saveIAMConfigItem(objectAPI, mp, mappingPath)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sys.iamUserPolicyMap[name] = mp
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PolicyDBGet - gets policy set on a user
|
|
|
|
func (sys *IAMSys) PolicyDBGet(name string) (string, error) {
|
|
|
|
if name == "" {
|
|
|
|
return "", errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil {
|
|
|
|
return "", errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
|
|
|
if _, ok := sys.iamUsersMap[name]; !ok {
|
|
|
|
return "", errNoSuchUser
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := sys.iamUserPolicyMap[name]
|
|
|
|
// returned policy could be empty
|
|
|
|
return policy.Policy, nil
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:28:33 -04:00
|
|
|
// IsAllowedSTS is meant for STS based temporary credentials,
|
|
|
|
// which implements claims validation and verification other than
|
|
|
|
// applying policies.
|
|
|
|
func (sys *IAMSys) IsAllowedSTS(args iampolicy.Args) bool {
|
|
|
|
pname, ok := args.Claims[iampolicy.PolicyName]
|
|
|
|
if !ok {
|
|
|
|
// When claims are set, it should have a "policy" field.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
pnameStr, ok := pname.(string)
|
|
|
|
if !ok {
|
|
|
|
// When claims has "policy" field, it should be string.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
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 {
|
|
|
|
// No policy available reject.
|
|
|
|
return false
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
name := mp.Policy
|
2019-06-20 18:28:33 -04:00
|
|
|
|
|
|
|
if pnameStr != name {
|
|
|
|
// When claims has a policy, it should match the
|
|
|
|
// policy of args.AccountName which server remembers.
|
|
|
|
// if not reject such requests.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check if we have a sessionPolicy.
|
|
|
|
spolicy, ok := args.Claims[iampolicy.SessionPolicyName]
|
|
|
|
if !ok {
|
|
|
|
// Sub policy not set, this is most common since subPolicy
|
|
|
|
// is optional, use the top level policy only.
|
2019-07-24 20:34:23 -04:00
|
|
|
p, ok := sys.iamPolicyDocsMap[pnameStr]
|
2019-06-20 18:28:33 -04:00
|
|
|
return ok && p.IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
spolicyStr, ok := spolicy.(string)
|
|
|
|
if !ok {
|
|
|
|
// Sub policy if set, should be a string reject
|
|
|
|
// malformed/malicious requests.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(context.Background(), err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policy without Version string value reject it.
|
|
|
|
if subPolicy.Version == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub policy is set and valid.
|
2019-07-24 20:34:23 -04:00
|
|
|
p, ok := sys.iamPolicyDocsMap[pnameStr]
|
2019-06-20 18:28:33 -04:00
|
|
|
return ok && p.IsAllowed(args) && subPolicy.IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return globalPolicyOPA.IsAllowed(args)
|
|
|
|
}
|
|
|
|
|
2019-06-20 18:28:33 -04:00
|
|
|
// With claims set, we should do STS related checks and validation.
|
|
|
|
if len(args.Claims) > 0 {
|
|
|
|
return sys.IsAllowedSTS(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// If policy is available for given user, check the policy.
|
2019-07-24 20:34:23 -04:00
|
|
|
if mp, found := sys.iamUserPolicyMap[args.AccountName]; found {
|
|
|
|
p, ok := sys.iamPolicyDocsMap[mp.Policy]
|
2018-10-16 15:48:19 -04:00
|
|
|
return ok && p.IsAllowed(args)
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// As policy is not available and OPA is not configured,
|
|
|
|
// return the owner value.
|
2018-10-09 17:00:01 -04:00
|
|
|
return args.IsOwner
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:36:45 -05:00
|
|
|
var defaultContextTimeout = 30 * time.Second
|
2018-10-12 14:32:18 -04:00
|
|
|
|
2019-06-06 20:46:22 -04:00
|
|
|
func etcdKvsToSet(prefix string, kvs []*mvccpb.KeyValue) set.StringSet {
|
2018-10-12 14:32:18 -04:00
|
|
|
users := set.NewStringSet()
|
2019-06-06 20:46:22 -04:00
|
|
|
for _, kv := range kvs {
|
2018-10-12 14:32:18 -04:00
|
|
|
// Extract user by stripping off the `prefix` value as suffix,
|
|
|
|
// then strip off the remaining basename to obtain the prefix
|
|
|
|
// value, usually in the following form.
|
|
|
|
//
|
|
|
|
// key := "config/iam/users/newuser/identity.json"
|
|
|
|
// prefix := "config/iam/users/"
|
|
|
|
// v := trim(trim(key, prefix), base(key)) == "newuser"
|
|
|
|
//
|
2018-10-24 20:40:06 -04:00
|
|
|
user := path.Clean(strings.TrimSuffix(strings.TrimPrefix(string(kv.Key), prefix), path.Base(string(kv.Key))))
|
2018-10-12 14:32:18 -04:00
|
|
|
if !users.Contains(user) {
|
|
|
|
users.Add(user)
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 20:46:22 -04:00
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
func etcdKvsToSetPolicyDB(prefix string, kvs []*mvccpb.KeyValue) set.StringSet {
|
|
|
|
items := set.NewStringSet()
|
|
|
|
for _, kv := range kvs {
|
|
|
|
// Extract user item by stripping off prefix and then
|
|
|
|
// stripping of ".json" suffix.
|
|
|
|
//
|
|
|
|
// key := "config/iam/policydb/users/myuser1.json"
|
|
|
|
// prefix := "config/iam/policydb/users/"
|
|
|
|
// v := trimSuffix(trimPrefix(key, prefix), ".json")
|
|
|
|
key := string(kv.Key)
|
|
|
|
item := path.Clean(strings.TrimSuffix(strings.TrimPrefix(key, prefix), ".json"))
|
|
|
|
items.Add(item)
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadEtcdMappedPolicy(ctx context.Context, name string, isSTS bool,
|
|
|
|
m map[string]MappedPolicy) error {
|
|
|
|
|
|
|
|
var p MappedPolicy
|
|
|
|
err := loadIAMConfigItemEtcd(ctx, &p, getMappedPolicyPath(name, isSTS))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[name] = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadEtcdMappedPolicies(isSTS bool, m map[string]MappedPolicy) error {
|
2019-06-06 20:46:22 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
defer cancel()
|
2019-07-24 20:34:23 -04:00
|
|
|
basePrefix := iamConfigPolicyDBUsersPrefix
|
|
|
|
if isSTS {
|
|
|
|
basePrefix = iamConfigPolicyDBSTSUsersPrefix
|
|
|
|
}
|
|
|
|
r, err := globalEtcdClient.Get(ctx, basePrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
2019-06-06 20:46:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
users := etcdKvsToSetPolicyDB(basePrefix, r.Kvs)
|
2018-10-12 14:32:18 -04:00
|
|
|
|
|
|
|
// Reload config and policies for all users.
|
|
|
|
for _, user := range users.ToSlice() {
|
2019-07-24 20:34:23 -04:00
|
|
|
if err = loadEtcdMappedPolicy(ctx, user, isSTS, m); err != nil {
|
2019-06-06 20:46:22 -04:00
|
|
|
return err
|
2018-10-16 15:48:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
func loadEtcdUser(ctx context.Context, user string, isSTS bool, m map[string]auth.Credentials) error {
|
|
|
|
var u UserIdentity
|
|
|
|
err := loadIAMConfigItemEtcd(ctx, &u, getUserIdentityPath(user, isSTS))
|
2019-06-06 20:46:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
|
|
|
|
if u.Credentials.IsExpired() {
|
|
|
|
idPath := getUserIdentityPath(user, isSTS)
|
|
|
|
// Delete expired identity.
|
|
|
|
deleteConfigEtcd(ctx, globalEtcdClient, idPath)
|
|
|
|
deleteConfigEtcd(ctx, globalEtcdClient, getMappedPolicyPath(user, isSTS))
|
|
|
|
return nil
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
|
|
|
|
m[user] = u.Credentials
|
2019-06-06 20:46:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
func loadEtcdUsers(isSTS bool, m map[string]auth.Credentials) error {
|
|
|
|
basePrefix := iamConfigUsersPrefix
|
|
|
|
if isSTS {
|
|
|
|
basePrefix = iamConfigSTSPrefix
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:48:19 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
defer cancel()
|
2019-07-24 20:34:23 -04:00
|
|
|
r, err := globalEtcdClient.Get(ctx, basePrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
2018-10-16 15:48:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
users := etcdKvsToSet(basePrefix, r.Kvs)
|
2018-10-16 15:48:19 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// Reload config and policies for all users.
|
|
|
|
for _, user := range users.ToSlice() {
|
|
|
|
if err = loadEtcdUser(ctx, user, isSTS, m); err != nil {
|
2018-10-16 15:48:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
func loadEtcdPolicy(ctx context.Context, policyName string, m map[string]iampolicy.Policy) error {
|
2019-06-06 20:46:22 -04:00
|
|
|
var p iampolicy.Policy
|
2019-07-24 20:34:23 -04:00
|
|
|
err := loadIAMConfigItemEtcd(ctx, &p, getPolicyDocPath(policyName))
|
|
|
|
if err != nil {
|
2019-06-06 20:46:22 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
m[policyName] = p
|
2018-10-12 14:32:18 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
func loadEtcdPolicies(policyDocsMap map[string]iampolicy.Policy) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultContextTimeout)
|
|
|
|
defer cancel()
|
|
|
|
r, err := globalEtcdClient.Get(ctx, iamConfigPoliciesPrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-06 20:46:22 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
policies := etcdKvsToSet(iamConfigPoliciesPrefix, r.Kvs)
|
2019-06-06 20:46:22 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
// Reload config and policies for all policys.
|
|
|
|
for _, policyName := range policies.ToSlice() {
|
|
|
|
err = loadEtcdPolicy(ctx, policyName, policyDocsMap)
|
2018-10-09 17:00:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 09:18:50 -04:00
|
|
|
func (sys *IAMSys) refreshEtcd() error {
|
|
|
|
iamUsersMap := make(map[string]auth.Credentials)
|
2019-07-24 20:34:23 -04:00
|
|
|
iamPolicyDocsMap := make(map[string]iampolicy.Policy)
|
|
|
|
iamUserPolicyMap := make(map[string]MappedPolicy)
|
2019-04-26 09:18:50 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
if err := loadEtcdPolicies(iamPolicyDocsMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := loadEtcdUsers(false, iamUsersMap); err != nil {
|
2019-04-26 09:18:50 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
// load STS temp users into the same map
|
|
|
|
if err := loadEtcdUsers(true, iamUsersMap); err != nil {
|
2019-04-26 09:18:50 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
if err := loadEtcdMappedPolicies(false, iamUserPolicyMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// load STS users policy mapping into the same map
|
|
|
|
if err := loadEtcdMappedPolicies(true, iamUserPolicyMap); err != nil {
|
2019-04-26 09:18:50 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets default canned policies, if none are set.
|
2019-07-24 20:34:23 -04:00
|
|
|
setDefaultCannedPolicies(iamPolicyDocsMap)
|
2019-04-26 09:18:50 -04:00
|
|
|
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
sys.iamUsersMap = iamUsersMap
|
2019-07-24 20:34:23 -04:00
|
|
|
sys.iamUserPolicyMap = iamUserPolicyMap
|
|
|
|
sys.iamPolicyDocsMap = iamPolicyDocsMap
|
2019-04-26 09:18:50 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
// Refresh IAMSys.
|
|
|
|
func (sys *IAMSys) refresh(objAPI ObjectLayer) error {
|
|
|
|
iamUsersMap := make(map[string]auth.Credentials)
|
2019-07-24 20:34:23 -04:00
|
|
|
iamPolicyDocsMap := make(map[string]iampolicy.Policy)
|
|
|
|
iamUserPolicyMap := make(map[string]MappedPolicy)
|
2018-10-09 17:00:01 -04:00
|
|
|
|
2019-07-24 20:34:23 -04:00
|
|
|
if err := loadPolicyDocs(objAPI, iamPolicyDocsMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := loadUsers(objAPI, false, iamUsersMap); err != nil {
|
2019-04-26 09:18:50 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
// load STS temp users into the same map
|
|
|
|
if err := loadUsers(objAPI, true, iamUsersMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := loadMappedPolicies(objAPI, false, iamUserPolicyMap); err != nil {
|
2019-04-26 09:18:50 -04:00
|
|
|
return err
|
|
|
|
}
|
2019-07-24 20:34:23 -04:00
|
|
|
// load STS policy mappings into the same map
|
|
|
|
if err := loadMappedPolicies(objAPI, true, iamUserPolicyMap); err != nil {
|
2019-04-26 09:18:50 -04:00
|
|
|
return err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2019-04-26 09:18:50 -04:00
|
|
|
// Sets default canned policies, if none are set.
|
2019-07-24 20:34:23 -04:00
|
|
|
setDefaultCannedPolicies(iamPolicyDocsMap)
|
2018-10-24 20:14:27 -04:00
|
|
|
|
2018-10-09 17:00:01 -04:00
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
sys.iamUsersMap = iamUsersMap
|
2019-07-24 20:34:23 -04:00
|
|
|
sys.iamPolicyDocsMap = iamPolicyDocsMap
|
|
|
|
sys.iamUserPolicyMap = iamUserPolicyMap
|
2018-10-09 17:00:01 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIAMSys - creates new config system object.
|
|
|
|
func NewIAMSys() *IAMSys {
|
|
|
|
return &IAMSys{
|
2019-07-24 20:34:23 -04:00
|
|
|
iamUsersMap: make(map[string]auth.Credentials),
|
|
|
|
iamPolicyDocsMap: make(map[string]iampolicy.Policy),
|
|
|
|
iamUserPolicyMap: make(map[string]MappedPolicy),
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
}
|