2019-08-09 00:10:04 +02:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* 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-11-01 23:53:16 +01:00
|
|
|
"bytes"
|
2019-08-09 00:10:04 +02:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-03-17 18:36:13 +01:00
|
|
|
jwtgo "github.com/dgrijalva/jwt-go"
|
2020-07-14 18:38:05 +02:00
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
2019-08-09 00:10:04 +02:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
|
|
|
"github.com/minio/minio/pkg/auth"
|
|
|
|
iampolicy "github.com/minio/minio/pkg/iam/policy"
|
2019-11-01 23:53:16 +01:00
|
|
|
"github.com/minio/minio/pkg/madmin"
|
2020-07-08 04:04:29 +02:00
|
|
|
etcd "go.etcd.io/etcd/v3/clientv3"
|
|
|
|
"go.etcd.io/etcd/v3/mvcc/mvccpb"
|
2019-08-09 00:10:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var defaultContextTimeout = 30 * time.Second
|
|
|
|
|
|
|
|
func etcdKvsToSet(prefix string, kvs []*mvccpb.KeyValue) set.StringSet {
|
|
|
|
users := set.NewStringSet()
|
|
|
|
for _, kv := range kvs {
|
|
|
|
// 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"
|
|
|
|
//
|
|
|
|
user := path.Clean(strings.TrimSuffix(strings.TrimPrefix(string(kv.Key), prefix), path.Base(string(kv.Key))))
|
|
|
|
users.Add(user)
|
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// IAMEtcdStore implements IAMStorageAPI
|
|
|
|
type IAMEtcdStore struct {
|
|
|
|
sync.RWMutex
|
2020-04-07 23:26:39 +02:00
|
|
|
|
2019-08-09 00:10:04 +02:00
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
client *etcd.Client
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func newIAMEtcdStore(ctx context.Context) *IAMEtcdStore {
|
|
|
|
return &IAMEtcdStore{client: globalEtcdClient, ctx: ctx}
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) lock() {
|
2019-08-09 00:10:04 +02:00
|
|
|
ies.Lock()
|
2020-04-07 23:26:39 +02:00
|
|
|
}
|
2019-08-09 00:10:04 +02:00
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) unlock() {
|
|
|
|
ies.Unlock()
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) rlock() {
|
|
|
|
ies.RLock()
|
|
|
|
}
|
2019-08-09 00:10:04 +02:00
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) runlock() {
|
|
|
|
ies.RUnlock()
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) saveIAMConfig(item interface{}, path string) error {
|
|
|
|
data, err := json.Marshal(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-01 23:53:16 +01:00
|
|
|
if globalConfigEncrypted {
|
|
|
|
data, err = madmin.EncryptData(globalActiveCred.String(), data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 23:26:39 +02:00
|
|
|
return saveKeyEtcd(ies.ctx, ies.client, path, data)
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) loadIAMConfig(item interface{}, path string) error {
|
2020-04-07 23:26:39 +02:00
|
|
|
pdata, err := readKeyEtcd(ies.ctx, ies.client, path)
|
2019-08-09 00:10:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-30 08:04:39 +01:00
|
|
|
|
2019-11-01 23:53:16 +01:00
|
|
|
if globalConfigEncrypted {
|
|
|
|
pdata, err = madmin.DecryptData(globalActiveCred.String(), bytes.NewReader(pdata))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 00:10:04 +02:00
|
|
|
return json.Unmarshal(pdata, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) deleteIAMConfig(path string) error {
|
2020-04-07 23:26:39 +02:00
|
|
|
return deleteKeyEtcd(ies.ctx, ies.client, path)
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) migrateUsersConfigToV1(ctx context.Context, isSTS bool) error {
|
2019-08-09 00:10:04 +02:00
|
|
|
basePrefix := iamConfigUsersPrefix
|
|
|
|
if isSTS {
|
|
|
|
basePrefix = iamConfigSTSPrefix
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-09 00:10:04 +02:00
|
|
|
defer cancel()
|
|
|
|
r, err := ies.client.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 := ies.loadIAMConfig(&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)
|
2020-03-17 18:36:13 +01:00
|
|
|
userType := regularUser
|
|
|
|
if isSTS {
|
|
|
|
userType = stsUser
|
|
|
|
}
|
|
|
|
path := getMappedPolicyPath(user, userType, false)
|
2019-08-09 00:10:04 +02:00
|
|
|
if err := ies.saveIAMConfig(mp, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. delete policy file in old loc.
|
|
|
|
deleteKeyEtcd(ctx, ies.client, oldPolicyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
next:
|
|
|
|
// 4. check if user identity has old format.
|
|
|
|
identityPath := pathJoin(basePrefix, user, iamIdentityFile)
|
|
|
|
var cred auth.Credentials
|
|
|
|
if err := ies.loadIAMConfig(&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
|
2020-05-20 20:33:35 +02:00
|
|
|
if cred.Equal(zeroCred) {
|
2019-08-09 00:10:04 +02:00
|
|
|
// nothing to do
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found a id file in old format. Copy value
|
|
|
|
// into new format and save it.
|
|
|
|
cred.AccessKey = user
|
|
|
|
u := newUserIdentity(cred)
|
|
|
|
if err := ies.saveIAMConfig(u, identityPath); err != nil {
|
2020-04-07 23:26:39 +02:00
|
|
|
logger.LogIf(ctx, err)
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to delete as identity file location
|
|
|
|
// has not changed.
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) migrateToV1(ctx context.Context) error {
|
2019-08-09 00:10:04 +02:00
|
|
|
var iamFmt iamFormat
|
|
|
|
path := getIAMFormatFilePath()
|
|
|
|
if err := ies.loadIAMConfig(&iamFmt, path); err != nil {
|
|
|
|
switch err {
|
|
|
|
case errConfigNotFound:
|
|
|
|
// Need to migrate to V1.
|
|
|
|
default:
|
2019-08-30 19:41:02 +02:00
|
|
|
return err
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if iamFmt.Version >= iamFormatVersion1 {
|
|
|
|
// 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
|
2020-04-07 23:26:39 +02:00
|
|
|
if err := ies.migrateUsersConfigToV1(ctx, false); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Migrate STS users
|
2020-04-07 23:26:39 +02:00
|
|
|
if err := ies.migrateUsersConfigToV1(ctx, true); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Save iam version file.
|
|
|
|
if err := ies.saveIAMConfig(newIAMFormatVersion1(), path); err != nil {
|
2020-04-07 23:26:39 +02:00
|
|
|
logger.LogIf(ctx, err)
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be called under config migration lock
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) migrateBackendFormat(ctx context.Context) error {
|
|
|
|
return ies.migrateToV1(ctx)
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) loadPolicyDoc(policy string, m map[string]iampolicy.Policy) error {
|
|
|
|
var p iampolicy.Policy
|
|
|
|
err := ies.loadIAMConfig(&p, getPolicyDocPath(policy))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[policy] = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) loadPolicyDocs(ctx context.Context, m map[string]iampolicy.Policy) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-09 00:10:04 +02:00
|
|
|
defer cancel()
|
|
|
|
r, err := ies.client.Get(ctx, iamConfigPoliciesPrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
policies := etcdKvsToSet(iamConfigPoliciesPrefix, r.Kvs)
|
|
|
|
|
|
|
|
// Reload config and policies for all policys.
|
|
|
|
for _, policyName := range policies.ToSlice() {
|
|
|
|
err = ies.loadPolicyDoc(policyName, m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:36:13 +01:00
|
|
|
func (ies *IAMEtcdStore) loadUser(user string, userType IAMUserType, m map[string]auth.Credentials) error {
|
2019-08-09 00:10:04 +02:00
|
|
|
var u UserIdentity
|
2020-03-17 18:36:13 +01:00
|
|
|
err := ies.loadIAMConfig(&u, getUserIdentityPath(user, userType))
|
2019-08-09 00:10:04 +02:00
|
|
|
if err != nil {
|
2019-10-30 03:50:26 +01:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Credentials.IsExpired() {
|
|
|
|
// Delete expired identity.
|
2020-04-07 23:26:39 +02:00
|
|
|
deleteKeyEtcd(ies.ctx, ies.client, getUserIdentityPath(user, userType))
|
|
|
|
deleteKeyEtcd(ies.ctx, ies.client, getMappedPolicyPath(user, userType, false))
|
2019-08-09 00:10:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:36:13 +01:00
|
|
|
// If this is a service account, rotate the session key if we are changing the server creds
|
|
|
|
if globalOldCred.IsValid() && u.Credentials.IsServiceAccount() {
|
|
|
|
if !globalOldCred.Equal(globalActiveCred) {
|
|
|
|
m := jwtgo.MapClaims{}
|
|
|
|
stsTokenCallback := func(t *jwtgo.Token) (interface{}, error) {
|
|
|
|
return []byte(globalOldCred.SecretKey), nil
|
|
|
|
}
|
|
|
|
if _, err := jwtgo.ParseWithClaims(u.Credentials.SessionToken, m, stsTokenCallback); err == nil {
|
|
|
|
jwt := jwtgo.NewWithClaims(jwtgo.SigningMethodHS512, jwtgo.MapClaims(m))
|
|
|
|
if token, err := jwt.SignedString([]byte(globalActiveCred.SecretKey)); err == nil {
|
|
|
|
u.Credentials.SessionToken = token
|
|
|
|
err := ies.saveIAMConfig(&u, getUserIdentityPath(user, userType))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 00:10:04 +02:00
|
|
|
if u.Credentials.AccessKey == "" {
|
|
|
|
u.Credentials.AccessKey = user
|
|
|
|
}
|
|
|
|
m[user] = u.Credentials
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) loadUsers(ctx context.Context, userType IAMUserType, m map[string]auth.Credentials) error {
|
2020-03-17 18:36:13 +01:00
|
|
|
var basePrefix string
|
|
|
|
switch userType {
|
|
|
|
case srvAccUser:
|
|
|
|
basePrefix = iamConfigServiceAccountsPrefix
|
|
|
|
case stsUser:
|
2019-08-09 00:10:04 +02:00
|
|
|
basePrefix = iamConfigSTSPrefix
|
2020-03-17 18:36:13 +01:00
|
|
|
default:
|
|
|
|
basePrefix = iamConfigUsersPrefix
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-09 00:10:04 +02:00
|
|
|
defer cancel()
|
|
|
|
r, err := ies.client.Get(ctx, basePrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
users := etcdKvsToSet(basePrefix, r.Kvs)
|
|
|
|
|
|
|
|
// Reload config for all users.
|
|
|
|
for _, user := range users.ToSlice() {
|
2020-03-17 18:36:13 +01:00
|
|
|
if err = ies.loadUser(user, userType, m); err != nil {
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) loadGroup(group string, m map[string]GroupInfo) error {
|
|
|
|
var gi GroupInfo
|
|
|
|
err := ies.loadIAMConfig(&gi, getGroupInfoPath(group))
|
|
|
|
if err != nil {
|
2019-10-30 03:50:26 +01:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchGroup
|
|
|
|
}
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[group] = gi
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) loadGroups(ctx context.Context, m map[string]GroupInfo) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-09 00:10:04 +02:00
|
|
|
defer cancel()
|
|
|
|
r, err := ies.client.Get(ctx, iamConfigGroupsPrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := etcdKvsToSet(iamConfigGroupsPrefix, r.Kvs)
|
|
|
|
|
|
|
|
// Reload config for all groups.
|
|
|
|
for _, group := range groups.ToSlice() {
|
|
|
|
if err = ies.loadGroup(group, m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:36:13 +01:00
|
|
|
func (ies *IAMEtcdStore) loadMappedPolicy(name string, userType IAMUserType, isGroup bool, m map[string]MappedPolicy) error {
|
2019-08-09 00:10:04 +02:00
|
|
|
var p MappedPolicy
|
2020-03-17 18:36:13 +01:00
|
|
|
err := ies.loadIAMConfig(&p, getMappedPolicyPath(name, userType, isGroup))
|
2019-08-09 00:10:04 +02:00
|
|
|
if err != nil {
|
2019-10-30 03:50:26 +01:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchPolicy
|
|
|
|
}
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[name] = p
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) loadMappedPolicies(ctx context.Context, userType IAMUserType, isGroup bool, m map[string]MappedPolicy) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-09 00:10:04 +02:00
|
|
|
defer cancel()
|
|
|
|
var basePrefix string
|
2020-03-17 18:36:13 +01:00
|
|
|
if isGroup {
|
2019-08-09 00:10:04 +02:00
|
|
|
basePrefix = iamConfigPolicyDBGroupsPrefix
|
2020-03-17 18:36:13 +01:00
|
|
|
} else {
|
|
|
|
switch userType {
|
|
|
|
case srvAccUser:
|
|
|
|
basePrefix = iamConfigPolicyDBServiceAccountsPrefix
|
|
|
|
case stsUser:
|
|
|
|
basePrefix = iamConfigPolicyDBSTSUsersPrefix
|
|
|
|
default:
|
|
|
|
basePrefix = iamConfigPolicyDBUsersPrefix
|
|
|
|
}
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
r, err := ies.client.Get(ctx, basePrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
users := etcdKvsToSetPolicyDB(basePrefix, r.Kvs)
|
|
|
|
|
|
|
|
// Reload config and policies for all users.
|
|
|
|
for _, user := range users.ToSlice() {
|
2020-03-17 18:36:13 +01:00
|
|
|
if err = ies.loadMappedPolicy(user, userType, isGroup, m); err != nil {
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) loadAll(ctx context.Context, sys *IAMSys) error {
|
2019-08-09 00:10:04 +02:00
|
|
|
iamUsersMap := make(map[string]auth.Credentials)
|
|
|
|
iamGroupsMap := make(map[string]GroupInfo)
|
|
|
|
iamUserPolicyMap := make(map[string]MappedPolicy)
|
|
|
|
iamGroupPolicyMap := make(map[string]MappedPolicy)
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
ies.rlock()
|
2020-06-11 23:11:30 +02:00
|
|
|
isMinIOUsersSys := sys.usersSysType == MinIOUsersSysType
|
2020-04-07 23:26:39 +02:00
|
|
|
ies.runlock()
|
2019-09-10 01:12:29 +02:00
|
|
|
|
2020-06-11 23:11:30 +02:00
|
|
|
ies.lock()
|
|
|
|
if err := ies.loadPolicyDocs(ctx, sys.iamPolicyDocsMap); err != nil {
|
|
|
|
ies.unlock()
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-06-11 23:11:30 +02:00
|
|
|
// Sets default canned policies, if none are set.
|
|
|
|
setDefaultCannedPolicies(sys.iamPolicyDocsMap)
|
2019-09-10 01:12:29 +02:00
|
|
|
|
2020-06-11 23:11:30 +02:00
|
|
|
ies.unlock()
|
2019-08-09 00:10:04 +02:00
|
|
|
|
2019-09-10 01:12:29 +02:00
|
|
|
if isMinIOUsersSys {
|
2020-04-07 23:26:39 +02:00
|
|
|
if err := ies.loadUsers(ctx, regularUser, iamUsersMap); err != nil {
|
2020-03-17 18:36:13 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-04-07 23:26:39 +02:00
|
|
|
if err := ies.loadGroups(ctx, iamGroupsMap); err != nil {
|
2019-09-10 01:12:29 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
2019-09-10 01:12:29 +02:00
|
|
|
|
2020-06-11 23:11:30 +02:00
|
|
|
// load polices mapped to users
|
|
|
|
if err := ies.loadMappedPolicies(ctx, regularUser, false, iamUserPolicyMap); err != nil {
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-06-11 23:11:30 +02:00
|
|
|
|
2019-08-09 00:10:04 +02:00
|
|
|
// load policies mapped to groups
|
2020-04-07 23:26:39 +02:00
|
|
|
if err := ies.loadMappedPolicies(ctx, regularUser, true, iamGroupPolicyMap); err != nil {
|
2019-08-09 00:10:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-11 23:11:30 +02:00
|
|
|
if err := ies.loadUsers(ctx, srvAccUser, iamUsersMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// load STS temp users
|
|
|
|
if err := ies.loadUsers(ctx, stsUser, iamUsersMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// load STS policy mappings
|
|
|
|
if err := ies.loadMappedPolicies(ctx, stsUser, false, iamUserPolicyMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
ies.lock()
|
|
|
|
defer ies.Unlock()
|
2019-08-09 00:10:04 +02:00
|
|
|
|
2020-05-27 21:38:44 +02:00
|
|
|
// 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)
|
|
|
|
// Deleting on the etcd is taken care of in the next cycle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range iamGroupPolicyMap {
|
|
|
|
sys.iamGroupPolicyMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range iamGroupsMap {
|
|
|
|
sys.iamGroupsMap[k] = v
|
|
|
|
}
|
|
|
|
|
2019-08-09 00:10:04 +02:00
|
|
|
sys.buildUserGroupMemberships()
|
2020-06-11 23:11:30 +02:00
|
|
|
sys.storeFallback = false
|
2019-08-09 00:10:04 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) savePolicyDoc(policyName string, p iampolicy.Policy) error {
|
|
|
|
return ies.saveIAMConfig(&p, getPolicyDocPath(policyName))
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:36:13 +01:00
|
|
|
func (ies *IAMEtcdStore) saveMappedPolicy(name string, userType IAMUserType, isGroup bool, mp MappedPolicy) error {
|
|
|
|
return ies.saveIAMConfig(mp, getMappedPolicyPath(name, userType, isGroup))
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:36:13 +01:00
|
|
|
func (ies *IAMEtcdStore) saveUserIdentity(name string, userType IAMUserType, u UserIdentity) error {
|
|
|
|
return ies.saveIAMConfig(u, getUserIdentityPath(name, userType))
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) saveGroupInfo(name string, gi GroupInfo) error {
|
|
|
|
return ies.saveIAMConfig(gi, getGroupInfoPath(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) deletePolicyDoc(name string) error {
|
2019-10-30 03:50:26 +01:00
|
|
|
err := ies.deleteIAMConfig(getPolicyDocPath(name))
|
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = errNoSuchPolicy
|
|
|
|
}
|
|
|
|
return err
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:36:13 +01:00
|
|
|
func (ies *IAMEtcdStore) deleteMappedPolicy(name string, userType IAMUserType, isGroup bool) error {
|
|
|
|
err := ies.deleteIAMConfig(getMappedPolicyPath(name, userType, isGroup))
|
2019-10-30 03:50:26 +01:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = errNoSuchPolicy
|
|
|
|
}
|
|
|
|
return err
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:36:13 +01:00
|
|
|
func (ies *IAMEtcdStore) deleteUserIdentity(name string, userType IAMUserType) error {
|
|
|
|
err := ies.deleteIAMConfig(getUserIdentityPath(name, userType))
|
2019-10-30 03:50:26 +01:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = errNoSuchUser
|
|
|
|
}
|
|
|
|
return err
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) deleteGroupInfo(name string) error {
|
2019-10-30 03:50:26 +01:00
|
|
|
err := ies.deleteIAMConfig(getGroupInfoPath(name))
|
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = errNoSuchGroup
|
|
|
|
}
|
|
|
|
return err
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 23:26:39 +02:00
|
|
|
func (ies *IAMEtcdStore) watch(ctx context.Context, sys *IAMSys) {
|
2020-04-09 04:00:39 +02:00
|
|
|
for {
|
|
|
|
outerLoop:
|
|
|
|
// Refresh IAMSys with etcd watch.
|
|
|
|
watchCh := ies.client.Watch(ctx,
|
|
|
|
iamConfigPrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
|
2019-08-09 00:10:04 +02:00
|
|
|
for {
|
2020-04-09 04:00:39 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case watchResp, ok := <-watchCh:
|
|
|
|
if !ok {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
// Upon an error on watch channel
|
|
|
|
// re-init the watch channel.
|
|
|
|
goto outerLoop
|
|
|
|
}
|
|
|
|
if err := watchResp.Err(); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
// log and retry.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
// Upon an error on watch channel
|
|
|
|
// re-init the watch channel.
|
|
|
|
goto outerLoop
|
|
|
|
}
|
|
|
|
for _, event := range watchResp.Events {
|
|
|
|
ies.lock()
|
|
|
|
ies.reloadFromEvent(sys, event)
|
|
|
|
ies.unlock()
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sys.RLock is held by caller.
|
|
|
|
func (ies *IAMEtcdStore) reloadFromEvent(sys *IAMSys, event *etcd.Event) {
|
|
|
|
eventCreate := event.IsModify() || event.IsCreate()
|
|
|
|
eventDelete := event.Type == etcd.EventTypeDelete
|
|
|
|
usersPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigUsersPrefix)
|
|
|
|
groupsPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigGroupsPrefix)
|
|
|
|
stsPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigSTSPrefix)
|
|
|
|
policyPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigPoliciesPrefix)
|
|
|
|
policyDBUsersPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigPolicyDBUsersPrefix)
|
|
|
|
policyDBSTSUsersPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigPolicyDBSTSUsersPrefix)
|
2019-08-13 22:41:06 +02:00
|
|
|
policyDBGroupsPrefix := strings.HasPrefix(string(event.Kv.Key), iamConfigPolicyDBGroupsPrefix)
|
2019-08-09 00:10:04 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case eventCreate:
|
|
|
|
switch {
|
|
|
|
case usersPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigUsersPrefix))
|
2020-03-17 18:36:13 +01:00
|
|
|
ies.loadUser(accessKey, regularUser, sys.iamUsersMap)
|
2019-08-09 00:10:04 +02:00
|
|
|
case stsPrefix:
|
|
|
|
accessKey := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigSTSPrefix))
|
2020-03-17 18:36:13 +01:00
|
|
|
ies.loadUser(accessKey, stsUser, sys.iamUsersMap)
|
2019-08-09 00:10:04 +02:00
|
|
|
case groupsPrefix:
|
|
|
|
group := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigGroupsPrefix))
|
|
|
|
ies.loadGroup(group, sys.iamGroupsMap)
|
|
|
|
gi := sys.iamGroupsMap[group]
|
|
|
|
sys.removeGroupFromMembershipsMap(group)
|
|
|
|
sys.updateGroupMembershipsMap(group, &gi)
|
|
|
|
case policyPrefix:
|
|
|
|
policyName := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPoliciesPrefix))
|
|
|
|
ies.loadPolicyDoc(policyName, sys.iamPolicyDocsMap)
|
|
|
|
case policyDBUsersPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPolicyDBUsersPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
2020-03-17 18:36:13 +01:00
|
|
|
ies.loadMappedPolicy(user, regularUser, false, sys.iamUserPolicyMap)
|
2019-08-09 00:10:04 +02:00
|
|
|
case policyDBSTSUsersPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPolicyDBSTSUsersPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
2020-03-17 18:36:13 +01:00
|
|
|
ies.loadMappedPolicy(user, stsUser, false, sys.iamUserPolicyMap)
|
2019-08-13 22:41:06 +02:00
|
|
|
case policyDBGroupsPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPolicyDBGroupsPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
2020-03-17 18:36:13 +01:00
|
|
|
ies.loadMappedPolicy(user, regularUser, true, sys.iamGroupPolicyMap)
|
2019-08-09 00:10:04 +02: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 groupsPrefix:
|
|
|
|
group := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigGroupsPrefix))
|
|
|
|
sys.removeGroupFromMembershipsMap(group)
|
|
|
|
delete(sys.iamGroupsMap, group)
|
|
|
|
delete(sys.iamGroupPolicyMap, group)
|
|
|
|
case policyPrefix:
|
|
|
|
policyName := path.Dir(strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPoliciesPrefix))
|
|
|
|
delete(sys.iamPolicyDocsMap, policyName)
|
|
|
|
case policyDBUsersPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPolicyDBUsersPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
|
|
|
delete(sys.iamUserPolicyMap, user)
|
|
|
|
case policyDBSTSUsersPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPolicyDBSTSUsersPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
|
|
|
delete(sys.iamUserPolicyMap, user)
|
2019-08-13 22:41:06 +02:00
|
|
|
case policyDBGroupsPrefix:
|
|
|
|
policyMapFile := strings.TrimPrefix(string(event.Kv.Key),
|
|
|
|
iamConfigPolicyDBGroupsPrefix)
|
|
|
|
user := strings.TrimSuffix(policyMapFile, ".json")
|
|
|
|
delete(sys.iamGroupPolicyMap, user)
|
2019-08-09 00:10:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|