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/>.
|
2019-08-08 18:10:04 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-07-17 20:41:29 -04:00
|
|
|
"unicode/utf8"
|
2019-08-08 18:10:04 -04:00
|
|
|
|
2021-05-11 05:02:32 -04:00
|
|
|
jsoniter "github.com/json-iterator/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/config"
|
|
|
|
"github.com/minio/minio/internal/kms"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
2021-05-28 13:31:42 -04:00
|
|
|
"go.etcd.io/etcd/api/v3/mvccpb"
|
|
|
|
etcd "go.etcd.io/etcd/client/v3"
|
2019-08-08 18:10:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var defaultContextTimeout = 30 * time.Second
|
|
|
|
|
|
|
|
func etcdKvsToSet(prefix string, kvs []*mvccpb.KeyValue) set.StringSet {
|
|
|
|
users := set.NewStringSet()
|
|
|
|
for _, kv := range kvs {
|
2021-03-05 11:36:16 -05:00
|
|
|
user := extractPathPrefixAndSuffix(string(kv.Key), prefix, path.Base(string(kv.Key)))
|
2019-08-08 18:10:04 -04:00
|
|
|
users.Add(user)
|
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:36:16 -05:00
|
|
|
// Extract path string by stripping off the `prefix` value and the suffix,
|
|
|
|
// value, usually in the following form.
|
|
|
|
// s := "config/iam/users/foo/config.json"
|
|
|
|
// prefix := "config/iam/users/"
|
|
|
|
// suffix := "config.json"
|
|
|
|
// result is foo
|
|
|
|
func extractPathPrefixAndSuffix(s string, prefix string, suffix string) string {
|
2021-05-24 12:28:19 -04:00
|
|
|
return pathClean(strings.TrimSuffix(strings.TrimPrefix(s, prefix), suffix))
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IAMEtcdStore implements IAMStorageAPI
|
|
|
|
type IAMEtcdStore struct {
|
|
|
|
sync.RWMutex
|
2020-04-07 17:26:39 -04:00
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
*iamCache
|
|
|
|
|
|
|
|
usersSysType UsersSysType
|
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
client *etcd.Client
|
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
func newIAMEtcdStore(client *etcd.Client, usersSysType UsersSysType) *IAMEtcdStore {
|
2021-11-08 15:55:27 -05:00
|
|
|
return &IAMEtcdStore{
|
|
|
|
iamCache: newIamCache(),
|
|
|
|
client: client,
|
|
|
|
usersSysType: usersSysType,
|
|
|
|
}
|
2021-11-03 22:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) rlock() *iamCache {
|
|
|
|
ies.RLock()
|
|
|
|
return ies.iamCache
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
func (ies *IAMEtcdStore) runlock() {
|
|
|
|
ies.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ies *IAMEtcdStore) lock() *iamCache {
|
2019-08-08 18:10:04 -04:00
|
|
|
ies.Lock()
|
2021-11-03 22:47:49 -04:00
|
|
|
return ies.iamCache
|
2020-04-07 17:26:39 -04:00
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
func (ies *IAMEtcdStore) unlock() {
|
|
|
|
ies.Unlock()
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2021-11-03 22:47:49 -04:00
|
|
|
func (ies *IAMEtcdStore) getUsersSysType() UsersSysType {
|
|
|
|
return ies.usersSysType
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
func (ies *IAMEtcdStore) saveIAMConfig(ctx context.Context, item interface{}, itemPath string, opts ...options) error {
|
2019-08-08 18:10:04 -04:00
|
|
|
data, err := json.Marshal(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
if GlobalKMS != nil {
|
|
|
|
data, err = config.EncryptBytes(GlobalKMS, data, kms.Context{
|
|
|
|
minioMetaBucket: path.Join(minioMetaBucket, itemPath),
|
|
|
|
})
|
2019-11-01 18:53:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
return saveKeyEtcd(ctx, ies.client, itemPath, data, opts...)
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:03:39 -05:00
|
|
|
func decryptData(data []byte, itemPath string) ([]byte, error) {
|
2021-03-05 11:36:16 -05:00
|
|
|
var err error
|
2021-05-18 18:19:20 -04:00
|
|
|
if !utf8.Valid(data) && GlobalKMS != nil {
|
|
|
|
data, err = config.DecryptBytes(GlobalKMS, data, kms.Context{
|
|
|
|
minioMetaBucket: path.Join(minioMetaBucket, itemPath),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-06-04 14:15:13 -04:00
|
|
|
// This fallback is needed because of a bug, in kms.Context{}
|
|
|
|
// construction during migration.
|
|
|
|
data, err = config.DecryptBytes(GlobalKMS, data, kms.Context{
|
|
|
|
minioMetaBucket: itemPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-12-11 12:03:39 -05:00
|
|
|
return nil, err
|
2021-06-04 14:15:13 -04:00
|
|
|
}
|
2019-11-01 18:53:16 -04:00
|
|
|
}
|
|
|
|
}
|
2021-12-11 12:03:39 -05:00
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIAMConfig(item interface{}, data []byte, itemPath string) error {
|
|
|
|
data, err := decryptData(data, itemPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-02 12:15:06 -05:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2021-04-22 11:45:30 -04:00
|
|
|
return json.Unmarshal(data, item)
|
2021-03-05 11:36:16 -05:00
|
|
|
}
|
2019-11-01 18:53:16 -04:00
|
|
|
|
2021-03-05 11:36:16 -05:00
|
|
|
func (ies *IAMEtcdStore) loadIAMConfig(ctx context.Context, item interface{}, path string) error {
|
2021-04-22 11:45:30 -04:00
|
|
|
data, err := readKeyEtcd(ctx, ies.client, path)
|
2021-03-05 11:36:16 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
return getIAMConfig(item, data, path)
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:03:39 -05:00
|
|
|
func (ies *IAMEtcdStore) loadIAMConfigBytes(ctx context.Context, path string) ([]byte, error) {
|
|
|
|
data, err := readKeyEtcd(ctx, ies.client, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return decryptData(data, path)
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
func (ies *IAMEtcdStore) deleteIAMConfig(ctx context.Context, path string) error {
|
|
|
|
return deleteKeyEtcd(ctx, ies.client, path)
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2021-12-11 12:03:39 -05:00
|
|
|
func (ies *IAMEtcdStore) loadPolicyDoc(ctx context.Context, policy string, m map[string]PolicyDoc) error {
|
|
|
|
data, err := ies.loadIAMConfigBytes(ctx, getPolicyDocPath(policy))
|
2019-08-08 18:10:04 -04:00
|
|
|
if err != nil {
|
2021-01-25 23:01:49 -05:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchPolicy
|
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
2021-12-11 12:03:39 -05:00
|
|
|
|
|
|
|
var p PolicyDoc
|
|
|
|
err = p.parseJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-08 18:10:04 -04:00
|
|
|
m[policy] = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:03:39 -05:00
|
|
|
func (ies *IAMEtcdStore) getPolicyDocKV(ctx context.Context, kvs *mvccpb.KeyValue, m map[string]PolicyDoc) error {
|
|
|
|
data, err := decryptData(kvs.Value, string(kvs.Key))
|
2021-03-05 11:36:16 -05:00
|
|
|
if err != nil {
|
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchPolicy
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 12:03:39 -05:00
|
|
|
|
|
|
|
var p PolicyDoc
|
|
|
|
err = p.parseJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:36:16 -05:00
|
|
|
policy := extractPathPrefixAndSuffix(string(kvs.Key), iamConfigPoliciesPrefix, path.Base(string(kvs.Key)))
|
|
|
|
m[policy] = p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:03:39 -05:00
|
|
|
func (ies *IAMEtcdStore) loadPolicyDocs(ctx context.Context, m map[string]PolicyDoc) error {
|
2020-04-07 17:26:39 -04:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-08 18:10:04 -04:00
|
|
|
defer cancel()
|
2021-03-05 11:36:16 -05:00
|
|
|
// Retrieve all keys and values to avoid too many calls to etcd in case of
|
|
|
|
// a large number of policies
|
|
|
|
r, err := ies.client.Get(ctx, iamConfigPoliciesPrefix, etcd.WithPrefix())
|
2019-08-08 18:10:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:36:16 -05:00
|
|
|
// Parse all values to construct the policies data model.
|
|
|
|
for _, kvs := range r.Kvs {
|
2021-11-03 22:47:49 -04:00
|
|
|
if err = ies.getPolicyDocKV(ctx, kvs, m); err != nil && err != errNoSuchPolicy {
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
func (ies *IAMEtcdStore) getUserKV(ctx context.Context, userkv *mvccpb.KeyValue, userType IAMUserType, m map[string]UserIdentity, basePrefix string) error {
|
2019-08-08 18:10:04 -04:00
|
|
|
var u UserIdentity
|
2021-04-22 11:45:30 -04:00
|
|
|
err := getIAMConfig(&u, userkv.Value, string(userkv.Key))
|
2019-08-08 18:10:04 -04:00
|
|
|
if err != nil {
|
2019-10-29 22:50:26 -04:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
2021-03-05 11:36:16 -05:00
|
|
|
user := extractPathPrefixAndSuffix(string(userkv.Key), basePrefix, path.Base(string(userkv.Key)))
|
|
|
|
return ies.addUser(ctx, user, userType, u, m)
|
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
func (ies *IAMEtcdStore) addUser(ctx context.Context, user string, userType IAMUserType, u UserIdentity, m map[string]UserIdentity) error {
|
2019-08-08 18:10:04 -04:00
|
|
|
if u.Credentials.IsExpired() {
|
|
|
|
// Delete expired identity.
|
2020-10-19 12:54:40 -04:00
|
|
|
deleteKeyEtcd(ctx, ies.client, getUserIdentityPath(user, userType))
|
|
|
|
deleteKeyEtcd(ctx, ies.client, getMappedPolicyPath(user, userType, false))
|
2019-08-08 18:10:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if u.Credentials.AccessKey == "" {
|
|
|
|
u.Credentials.AccessKey = user
|
|
|
|
}
|
2022-07-01 16:19:13 -04:00
|
|
|
m[user] = u
|
2019-08-08 18:10:04 -04:00
|
|
|
return nil
|
2021-03-05 11:36:16 -05:00
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
func (ies *IAMEtcdStore) loadUser(ctx context.Context, user string, userType IAMUserType, m map[string]UserIdentity) error {
|
2021-03-05 11:36:16 -05:00
|
|
|
var u UserIdentity
|
|
|
|
err := ies.loadIAMConfig(ctx, &u, getUserIdentityPath(user, userType))
|
|
|
|
if err != nil {
|
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchUser
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ies.addUser(ctx, user, userType, u, m)
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2022-07-01 16:19:13 -04:00
|
|
|
func (ies *IAMEtcdStore) loadUsers(ctx context.Context, userType IAMUserType, m map[string]UserIdentity) error {
|
2020-03-17 13:36:13 -04:00
|
|
|
var basePrefix string
|
|
|
|
switch userType {
|
2021-07-09 14:17:21 -04:00
|
|
|
case svcUser:
|
2020-03-17 13:36:13 -04:00
|
|
|
basePrefix = iamConfigServiceAccountsPrefix
|
|
|
|
case stsUser:
|
2019-08-08 18:10:04 -04:00
|
|
|
basePrefix = iamConfigSTSPrefix
|
2020-03-17 13:36:13 -04:00
|
|
|
default:
|
|
|
|
basePrefix = iamConfigUsersPrefix
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
cctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-08 18:10:04 -04:00
|
|
|
defer cancel()
|
2020-10-19 12:54:40 -04:00
|
|
|
|
2021-03-05 11:36:16 -05:00
|
|
|
// Retrieve all keys and values to avoid too many calls to etcd in case of
|
|
|
|
// a large number of users
|
|
|
|
r, err := ies.client.Get(cctx, basePrefix, etcd.WithPrefix())
|
2019-08-08 18:10:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:36:16 -05:00
|
|
|
// Parse all users values to create the proper data model
|
|
|
|
for _, userKv := range r.Kvs {
|
2021-11-03 22:47:49 -04:00
|
|
|
if err = ies.getUserKV(ctx, userKv, userType, m, basePrefix); err != nil && err != errNoSuchUser {
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
func (ies *IAMEtcdStore) loadGroup(ctx context.Context, group string, m map[string]GroupInfo) error {
|
2019-08-08 18:10:04 -04:00
|
|
|
var gi GroupInfo
|
2020-10-19 12:54:40 -04:00
|
|
|
err := ies.loadIAMConfig(ctx, &gi, getGroupInfoPath(group))
|
2019-08-08 18:10:04 -04:00
|
|
|
if err != nil {
|
2019-10-29 22:50:26 -04:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchGroup
|
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[group] = gi
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
func (ies *IAMEtcdStore) loadGroups(ctx context.Context, m map[string]GroupInfo) error {
|
2020-10-19 12:54:40 -04:00
|
|
|
cctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-08 18:10:04 -04:00
|
|
|
defer cancel()
|
2020-10-19 12:54:40 -04:00
|
|
|
|
|
|
|
r, err := ies.client.Get(cctx, iamConfigGroupsPrefix, etcd.WithPrefix(), etcd.WithKeysOnly())
|
2019-08-08 18:10:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := etcdKvsToSet(iamConfigGroupsPrefix, r.Kvs)
|
|
|
|
|
|
|
|
// Reload config for all groups.
|
|
|
|
for _, group := range groups.ToSlice() {
|
2021-01-25 23:01:49 -05:00
|
|
|
if err = ies.loadGroup(ctx, group, m); err != nil && err != errNoSuchGroup {
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
func (ies *IAMEtcdStore) loadMappedPolicy(ctx context.Context, name string, userType IAMUserType, isGroup bool, m map[string]MappedPolicy) error {
|
2019-08-08 18:10:04 -04:00
|
|
|
var p MappedPolicy
|
2020-10-19 12:54:40 -04:00
|
|
|
err := ies.loadIAMConfig(ctx, &p, getMappedPolicyPath(name, userType, isGroup))
|
2019-08-08 18:10:04 -04:00
|
|
|
if err != nil {
|
2019-10-29 22:50:26 -04:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchPolicy
|
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m[name] = p
|
|
|
|
return nil
|
2021-03-05 11:36:16 -05:00
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
|
2021-03-05 11:36:16 -05:00
|
|
|
func getMappedPolicy(ctx context.Context, kv *mvccpb.KeyValue, userType IAMUserType, isGroup bool, m map[string]MappedPolicy, basePrefix string) error {
|
|
|
|
var p MappedPolicy
|
2021-04-22 11:45:30 -04:00
|
|
|
err := getIAMConfig(&p, kv.Value, string(kv.Key))
|
2021-03-05 11:36:16 -05:00
|
|
|
if err != nil {
|
|
|
|
if err == errConfigNotFound {
|
|
|
|
return errNoSuchPolicy
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := extractPathPrefixAndSuffix(string(kv.Key), basePrefix, ".json")
|
|
|
|
m[name] = p
|
|
|
|
return nil
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:39 -04:00
|
|
|
func (ies *IAMEtcdStore) loadMappedPolicies(ctx context.Context, userType IAMUserType, isGroup bool, m map[string]MappedPolicy) error {
|
2020-10-19 12:54:40 -04:00
|
|
|
cctx, cancel := context.WithTimeout(ctx, defaultContextTimeout)
|
2019-08-08 18:10:04 -04:00
|
|
|
defer cancel()
|
|
|
|
var basePrefix string
|
2020-03-17 13:36:13 -04:00
|
|
|
if isGroup {
|
2019-08-08 18:10:04 -04:00
|
|
|
basePrefix = iamConfigPolicyDBGroupsPrefix
|
2020-03-17 13:36:13 -04:00
|
|
|
} else {
|
|
|
|
switch userType {
|
2021-07-09 14:17:21 -04:00
|
|
|
case svcUser:
|
2020-03-17 13:36:13 -04:00
|
|
|
basePrefix = iamConfigPolicyDBServiceAccountsPrefix
|
|
|
|
case stsUser:
|
|
|
|
basePrefix = iamConfigPolicyDBSTSUsersPrefix
|
|
|
|
default:
|
|
|
|
basePrefix = iamConfigPolicyDBUsersPrefix
|
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
2021-03-05 11:36:16 -05:00
|
|
|
// Retrieve all keys and values to avoid too many calls to etcd in case of
|
|
|
|
// a large number of policy mappings
|
|
|
|
r, err := ies.client.Get(cctx, basePrefix, etcd.WithPrefix())
|
2019-08-08 18:10:04 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:36:16 -05:00
|
|
|
// Parse all policies mapping to create the proper data model
|
|
|
|
for _, kv := range r.Kvs {
|
|
|
|
if err = getMappedPolicy(ctx, kv, userType, isGroup, m, basePrefix); err != nil && err != errNoSuchPolicy {
|
2019-08-08 18:10:04 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:03:39 -05:00
|
|
|
func (ies *IAMEtcdStore) savePolicyDoc(ctx context.Context, policyName string, p PolicyDoc) error {
|
2020-10-19 12:54:40 -04:00
|
|
|
return ies.saveIAMConfig(ctx, &p, getPolicyDocPath(policyName))
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:06:05 -05:00
|
|
|
func (ies *IAMEtcdStore) saveMappedPolicy(ctx context.Context, name string, userType IAMUserType, isGroup bool, mp MappedPolicy, opts ...options) error {
|
|
|
|
return ies.saveIAMConfig(ctx, mp, getMappedPolicyPath(name, userType, isGroup), opts...)
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:06:05 -05:00
|
|
|
func (ies *IAMEtcdStore) saveUserIdentity(ctx context.Context, name string, userType IAMUserType, u UserIdentity, opts ...options) error {
|
|
|
|
return ies.saveIAMConfig(ctx, u, getUserIdentityPath(name, userType), opts...)
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
func (ies *IAMEtcdStore) saveGroupInfo(ctx context.Context, name string, gi GroupInfo) error {
|
|
|
|
return ies.saveIAMConfig(ctx, gi, getGroupInfoPath(name))
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
func (ies *IAMEtcdStore) deletePolicyDoc(ctx context.Context, name string) error {
|
|
|
|
err := ies.deleteIAMConfig(ctx, getPolicyDocPath(name))
|
2019-10-29 22:50:26 -04:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = errNoSuchPolicy
|
|
|
|
}
|
|
|
|
return err
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
func (ies *IAMEtcdStore) deleteMappedPolicy(ctx context.Context, name string, userType IAMUserType, isGroup bool) error {
|
|
|
|
err := ies.deleteIAMConfig(ctx, getMappedPolicyPath(name, userType, isGroup))
|
2019-10-29 22:50:26 -04:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = errNoSuchPolicy
|
|
|
|
}
|
|
|
|
return err
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
func (ies *IAMEtcdStore) deleteUserIdentity(ctx context.Context, name string, userType IAMUserType) error {
|
|
|
|
err := ies.deleteIAMConfig(ctx, getUserIdentityPath(name, userType))
|
2019-10-29 22:50:26 -04:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = errNoSuchUser
|
|
|
|
}
|
|
|
|
return err
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 12:54:40 -04:00
|
|
|
func (ies *IAMEtcdStore) deleteGroupInfo(ctx context.Context, name string) error {
|
|
|
|
err := ies.deleteIAMConfig(ctx, getGroupInfoPath(name))
|
2019-10-29 22:50:26 -04:00
|
|
|
if err == errConfigNotFound {
|
|
|
|
err = errNoSuchGroup
|
|
|
|
}
|
|
|
|
return err
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
|
|
|
|
2021-10-18 14:21:57 -04:00
|
|
|
func (ies *IAMEtcdStore) watch(ctx context.Context, keyPath string) <-chan iamWatchEvent {
|
|
|
|
ch := make(chan iamWatchEvent)
|
2020-04-08 22:00:39 -04:00
|
|
|
|
2021-10-18 14:21:57 -04:00
|
|
|
// go routine to read events from the etcd watch channel and send them
|
|
|
|
// down `ch`
|
|
|
|
go func() {
|
2019-08-08 18:10:04 -04:00
|
|
|
for {
|
2021-10-18 14:21:57 -04:00
|
|
|
outerLoop:
|
|
|
|
watchCh := ies.client.Watch(ctx,
|
|
|
|
keyPath, etcd.WithPrefix(), etcd.WithKeysOnly())
|
|
|
|
|
|
|
|
for {
|
|
|
|
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 {
|
|
|
|
isCreateEvent := event.IsModify() || event.IsCreate()
|
|
|
|
isDeleteEvent := event.Type == etcd.EventTypeDelete
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isCreateEvent:
|
|
|
|
ch <- iamWatchEvent{
|
|
|
|
isCreated: true,
|
|
|
|
keyPath: string(event.Kv.Key),
|
|
|
|
}
|
|
|
|
case isDeleteEvent:
|
|
|
|
ch <- iamWatchEvent{
|
|
|
|
isCreated: false,
|
|
|
|
keyPath: string(event.Kv.Key),
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 12:54:40 -04:00
|
|
|
|
2021-06-01 11:37:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|
2021-10-18 14:21:57 -04:00
|
|
|
}()
|
|
|
|
return ch
|
2019-08-08 18:10:04 -04:00
|
|
|
}
|