2019-11-09 12:27:23 -05:00
|
|
|
/*
|
2020-04-07 22:30:59 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc.
|
2019-11-09 12:27:23 -05: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 (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-03-23 20:39:20 -04:00
|
|
|
"errors"
|
2019-11-09 12:27:23 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-02-09 12:53:07 -05:00
|
|
|
"sort"
|
2019-11-09 12:27:23 -05:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-02-09 12:53:07 -05:00
|
|
|
"github.com/minio/minio/cmd/config/dns"
|
2019-11-09 12:27:23 -05:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2019-11-19 05:03:18 -05:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2019-11-09 12:27:23 -05:00
|
|
|
iampolicy "github.com/minio/minio/pkg/iam/policy"
|
|
|
|
"github.com/minio/minio/pkg/madmin"
|
|
|
|
)
|
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
func validateAdminUsersReq(ctx context.Context, w http.ResponseWriter, r *http.Request, action iampolicy.AdminAction) (ObjectLayer, auth.Credentials) {
|
|
|
|
var cred auth.Credentials
|
|
|
|
var adminAPIErr APIErrorCode
|
|
|
|
|
2019-11-09 12:27:23 -05:00
|
|
|
// Get current object layer instance.
|
2020-10-09 12:59:52 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
2020-10-19 12:54:40 -04:00
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
2019-11-09 12:27:23 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
2019-11-19 05:03:18 -05:00
|
|
|
return nil, cred
|
2019-11-09 12:27:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate request signature.
|
2020-12-18 14:51:15 -05:00
|
|
|
cred, adminAPIErr = checkAdminRequestAuth(ctx, r, action, "")
|
2019-11-09 12:27:23 -05:00
|
|
|
if adminAPIErr != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(adminAPIErr), r.URL)
|
2019-11-19 05:03:18 -05:00
|
|
|
return nil, cred
|
2019-11-09 12:27:23 -05:00
|
|
|
}
|
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
return objectAPI, cred
|
2019-11-09 12:27:23 -05:00
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// RemoveUser - DELETE /minio/admin/v3/remove-user?accessKey=<access_key>
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) RemoveUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "RemoveUser")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.DeleteUserAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
accessKey := vars["accessKey"]
|
|
|
|
|
2021-02-25 16:49:59 -05:00
|
|
|
ok, _, err := globalIAMSys.IsTempUser(accessKey)
|
2019-12-19 17:21:21 -05:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, errIAMActionNotAllowed), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:27:23 -05:00
|
|
|
if err := globalIAMSys.DeleteUser(accessKey); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to delete user.
|
|
|
|
for _, nerr := range globalNotificationSys.DeleteUser(accessKey) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// ListUsers - GET /minio/admin/v3/list-users
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) ListUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "ListUsers")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2020-03-23 17:06:00 -04:00
|
|
|
objectAPI, cred := validateAdminUsersReq(ctx, w, r, iampolicy.ListUsersAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-23 17:06:00 -04:00
|
|
|
password := cred.SecretKey
|
|
|
|
|
2019-11-09 12:27:23 -05:00
|
|
|
allCredentials, err := globalIAMSys.ListUsers()
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(allCredentials)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
econfigData, err := madmin.EncryptData(password, data)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, econfigData)
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// GetUserInfo - GET /minio/admin/v3/user-info
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) GetUserInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "GetUserInfo")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
name := vars["accessKey"]
|
|
|
|
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
2019-11-09 12:27:23 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
cred, claims, owner, s3Err := validateAdminSignature(ctx, r, "")
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accessKey := cred.AccessKey
|
|
|
|
if cred.ParentUser != "" {
|
|
|
|
accessKey = cred.ParentUser
|
|
|
|
}
|
|
|
|
|
|
|
|
implicitPerm := name == accessKey
|
|
|
|
if !implicitPerm {
|
|
|
|
if !globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: accessKey,
|
2021-03-23 18:15:51 -04:00
|
|
|
Groups: cred.Groups,
|
2020-11-27 20:23:57 -05:00
|
|
|
Action: iampolicy.GetUserAdminAction,
|
|
|
|
ConditionValues: getConditionValues(r, "", accessKey, claims),
|
|
|
|
IsOwner: owner,
|
|
|
|
Claims: claims,
|
|
|
|
}) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAccessDenied), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 12:27:23 -05:00
|
|
|
|
|
|
|
userInfo, err := globalIAMSys.GetUserInfo(name)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(userInfo)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, data)
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// UpdateGroupMembers - PUT /minio/admin/v3/update-group-members
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) UpdateGroupMembers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "UpdateGroupMembers")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.AddUserToGroupAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var updReq madmin.GroupAddRemove
|
|
|
|
err = json.Unmarshal(data, &updReq)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if updReq.IsRemove {
|
|
|
|
err = globalIAMSys.RemoveUsersFromGroup(updReq.Group, updReq.Members)
|
|
|
|
} else {
|
|
|
|
err = globalIAMSys.AddUsersToGroup(updReq.Group, updReq.Members)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to load group.
|
|
|
|
for _, nerr := range globalNotificationSys.LoadGroup(updReq.Group) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// GetGroup - /minio/admin/v3/group?group=mygroup1
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) GetGroup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "GetGroup")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.GetGroupAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
group := vars["group"]
|
|
|
|
|
|
|
|
gdesc, err := globalIAMSys.GetGroupDescription(group)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(gdesc)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, body)
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// ListGroups - GET /minio/admin/v3/groups
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) ListGroups(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "ListGroups")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.ListGroupsAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
groups, err := globalIAMSys.ListGroups()
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(groups)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, body)
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// SetGroupStatus - PUT /minio/admin/v3/set-group-status?group=mygroup1&status=enabled
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) SetGroupStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "SetGroupStatus")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.EnableGroupAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
group := vars["group"]
|
|
|
|
status := vars["status"]
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if status == statusEnabled {
|
|
|
|
err = globalIAMSys.SetGroupStatus(group, true)
|
|
|
|
} else if status == statusDisabled {
|
|
|
|
err = globalIAMSys.SetGroupStatus(group, false)
|
|
|
|
} else {
|
|
|
|
err = errInvalidArgument
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to reload user.
|
|
|
|
for _, nerr := range globalNotificationSys.LoadGroup(group) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// SetUserStatus - PUT /minio/admin/v3/set-user-status?accessKey=<access_key>&status=[enabled|disabled]
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) SetUserStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "SetUserStatus")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.EnableUserAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
accessKey := vars["accessKey"]
|
|
|
|
status := vars["status"]
|
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
// This API is not allowed to lookup accessKey user status
|
2019-11-09 12:27:23 -05:00
|
|
|
if accessKey == globalActiveCred.AccessKey {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := globalIAMSys.SetUserStatus(accessKey, madmin.AccountStatus(status)); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to reload user.
|
|
|
|
for _, nerr := range globalNotificationSys.LoadUser(accessKey, false) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// AddUser - PUT /minio/admin/v3/add-user?accessKey=<access_key>
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) AddUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "AddUser")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
vars := mux.Vars(r)
|
2021-04-15 19:32:13 -04:00
|
|
|
accessKey := vars["accessKey"]
|
2020-11-27 20:23:57 -05:00
|
|
|
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
2019-11-09 12:27:23 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
cred, claims, owner, s3Err := validateAdminSignature(ctx, r, "")
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2019-11-09 12:27:23 -05:00
|
|
|
|
2020-12-19 16:24:21 -05:00
|
|
|
// Not allowed to add a user with same access key as root credential
|
|
|
|
if owner && accessKey == cred.AccessKey {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAddUserInvalidArgument), r.URL)
|
2020-11-27 20:23:57 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-19 16:24:21 -05:00
|
|
|
if (cred.IsTemp() || cred.IsServiceAccount()) && cred.ParentUser == accessKey {
|
|
|
|
// Incoming access key matches parent user then we should
|
|
|
|
// reject password change requests.
|
2019-11-09 12:27:23 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAddUserInvalidArgument), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
implicitPerm := accessKey == cred.AccessKey
|
|
|
|
if !implicitPerm {
|
2020-12-19 16:24:21 -05:00
|
|
|
parentUser := cred.ParentUser
|
|
|
|
if parentUser == "" {
|
|
|
|
parentUser = cred.AccessKey
|
|
|
|
}
|
2020-11-27 20:23:57 -05:00
|
|
|
if !globalIAMSys.IsAllowed(iampolicy.Args{
|
2020-12-19 16:24:21 -05:00
|
|
|
AccountName: parentUser,
|
2021-03-23 18:15:51 -04:00
|
|
|
Groups: cred.Groups,
|
2020-11-27 20:23:57 -05:00
|
|
|
Action: iampolicy.CreateUserAdminAction,
|
2020-12-19 16:24:21 -05:00
|
|
|
ConditionValues: getConditionValues(r, "", parentUser, claims),
|
2021-03-02 20:02:29 -05:00
|
|
|
IsOwner: owner,
|
2020-11-27 20:23:57 -05:00
|
|
|
Claims: claims,
|
|
|
|
}) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAccessDenied), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:35:50 -05:00
|
|
|
if implicitPerm && !globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: accessKey,
|
2021-03-23 18:15:51 -04:00
|
|
|
Groups: cred.Groups,
|
2021-03-02 18:35:50 -05:00
|
|
|
Action: iampolicy.CreateUserAdminAction,
|
|
|
|
ConditionValues: getConditionValues(r, "", accessKey, claims),
|
2021-03-02 20:02:29 -05:00
|
|
|
IsOwner: owner,
|
2021-03-02 18:35:50 -05:00
|
|
|
Claims: claims,
|
|
|
|
DenyOnly: true, // check if changing password is explicitly denied.
|
|
|
|
}) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAccessDenied), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:27:23 -05:00
|
|
|
if r.ContentLength > maxEConfigJSONSize || r.ContentLength == -1 {
|
|
|
|
// More than maxConfigSize bytes were available
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigTooLarge), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
password := cred.SecretKey
|
2019-11-09 12:27:23 -05:00
|
|
|
configBytes, err := madmin.DecryptData(password, io.LimitReader(r.Body, r.ContentLength))
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigBadJSON), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var uinfo madmin.UserInfo
|
|
|
|
if err = json.Unmarshal(configBytes, &uinfo); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigBadJSON), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:01:49 -05:00
|
|
|
if err = globalIAMSys.CreateUser(accessKey, uinfo); err != nil {
|
2019-11-09 12:27:23 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other Minio peers to reload user
|
|
|
|
for _, nerr := range globalNotificationSys.LoadUser(accessKey, false) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
// AddServiceAccount - PUT /minio/admin/v3/add-service-account
|
2020-03-17 13:36:13 -04:00
|
|
|
func (a adminAPIHandlers) AddServiceAccount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "AddServiceAccount")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2020-04-14 14:28:56 -04:00
|
|
|
// Get current object layer instance.
|
2020-10-09 12:59:52 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
2020-10-19 12:54:40 -04:00
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
2020-04-14 14:28:56 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
2020-03-17 13:36:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
cred, claims, owner, s3Err := validateAdminSignature(ctx, r, "")
|
2020-04-14 14:28:56 -04:00
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
2020-03-17 13:36:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
password := cred.SecretKey
|
2020-04-24 15:10:09 -04:00
|
|
|
reqBytes, err := madmin.DecryptData(password, io.LimitReader(r.Body, r.ContentLength))
|
2020-03-17 13:36:13 -04:00
|
|
|
if err != nil {
|
2020-04-14 14:28:56 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
2020-03-17 13:36:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var createReq madmin.AddServiceAccountReq
|
2020-04-24 15:10:09 -04:00
|
|
|
if err = json.Unmarshal(reqBytes, &createReq); err != nil {
|
2020-04-14 14:28:56 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
2020-03-17 13:36:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
// Disallow creating service accounts by root user.
|
|
|
|
if createReq.TargetUser == globalActiveCred.AccessKey {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminAccountNotEligible), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
targetUser string
|
|
|
|
targetGroups []string
|
|
|
|
)
|
|
|
|
|
|
|
|
targetUser = createReq.TargetUser
|
|
|
|
|
|
|
|
// Need permission if we are creating a service acccount
|
|
|
|
// for a user <> to the request sender
|
|
|
|
if targetUser != "" && targetUser != cred.AccessKey {
|
|
|
|
if !globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: cred.AccessKey,
|
|
|
|
Action: iampolicy.CreateServiceAccountAdminAction,
|
|
|
|
ConditionValues: getConditionValues(r, "", cred.AccessKey, claims),
|
|
|
|
IsOwner: owner,
|
|
|
|
Claims: claims,
|
|
|
|
}) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAccessDenied), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-04-28 15:49:56 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
if globalLDAPConfig.Enabled && targetUser != "" {
|
|
|
|
// If LDAP enabled, service accounts need
|
|
|
|
// to be created only for LDAP users.
|
|
|
|
var err error
|
|
|
|
_, targetGroups, err = globalLDAPConfig.LookupUserDN(targetUser)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if targetUser == "" {
|
|
|
|
targetUser = cred.AccessKey
|
|
|
|
}
|
|
|
|
if cred.ParentUser != "" {
|
|
|
|
targetUser = cred.ParentUser
|
|
|
|
}
|
|
|
|
targetGroups = cred.Groups
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := newServiceAccountOpts{sessionPolicy: createReq.Policy, accessKey: createReq.AccessKey, secretKey: createReq.SecretKey}
|
|
|
|
newCred, err := globalIAMSys.NewServiceAccount(ctx, targetUser, targetGroups, opts)
|
2020-03-17 13:36:13 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
// Notify all other Minio peers to reload user the service account
|
|
|
|
for _, nerr := range globalNotificationSys.LoadServiceAccount(newCred.AccessKey) {
|
2020-03-17 13:36:13 -04:00
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var createResp = madmin.AddServiceAccountResp{
|
2020-04-14 14:28:56 -04:00
|
|
|
Credentials: auth.Credentials{
|
2020-04-24 15:10:09 -04:00
|
|
|
AccessKey: newCred.AccessKey,
|
|
|
|
SecretKey: newCred.SecretKey,
|
2020-04-14 14:28:56 -04:00
|
|
|
},
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(createResp)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
encryptedData, err := madmin.EncryptData(password, data)
|
2020-03-17 13:36:13 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
writeSuccessResponseJSON(w, encryptedData)
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
// UpdateServiceAccount - POST /minio/admin/v3/update-service-account
|
|
|
|
func (a adminAPIHandlers) UpdateServiceAccount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "UpdateServiceAccount")
|
|
|
|
|
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
|
|
|
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cred, claims, owner, s3Err := validateAdminSignature(ctx, r, "")
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accessKey := mux.Vars(r)["accessKey"]
|
|
|
|
if accessKey == "" {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow editing service accounts by root user.
|
|
|
|
if owner {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminAccountNotEligible), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
svcAccount, _, err := globalIAMSys.GetServiceAccount(ctx, accessKey)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: cred.AccessKey,
|
|
|
|
Action: iampolicy.UpdateServiceAccountAdminAction,
|
|
|
|
ConditionValues: getConditionValues(r, "", cred.AccessKey, claims),
|
|
|
|
IsOwner: owner,
|
|
|
|
Claims: claims,
|
|
|
|
}) {
|
|
|
|
requestUser := cred.AccessKey
|
|
|
|
if cred.ParentUser != "" {
|
|
|
|
requestUser = cred.ParentUser
|
|
|
|
}
|
|
|
|
|
|
|
|
if requestUser != svcAccount.ParentUser {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAccessDenied), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
password := cred.SecretKey
|
|
|
|
reqBytes, err := madmin.DecryptData(password, io.LimitReader(r.Body, r.ContentLength))
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateReq madmin.UpdateServiceAccountReq
|
|
|
|
if err = json.Unmarshal(reqBytes, &updateReq); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := updateServiceAccountOpts{sessionPolicy: updateReq.NewPolicy, secretKey: updateReq.NewSecretKey, status: updateReq.NewStatus}
|
|
|
|
err = globalIAMSys.UpdateServiceAccount(ctx, accessKey, opts)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other Minio peers to reload user the service account
|
|
|
|
for _, nerr := range globalNotificationSys.LoadServiceAccount(accessKey) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessNoContent(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfoServiceAccount - GET /minio/admin/v3/info-service-account
|
|
|
|
func (a adminAPIHandlers) InfoServiceAccount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "InfoServiceAccount")
|
|
|
|
|
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
|
|
|
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI := newObjectLayerFn()
|
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cred, claims, owner, s3Err := validateAdminSignature(ctx, r, "")
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow creating service accounts by root user.
|
|
|
|
if owner {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminAccountNotEligible), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accessKey := mux.Vars(r)["accessKey"]
|
|
|
|
if accessKey == "" {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
svcAccount, policy, err := globalIAMSys.GetServiceAccount(ctx, accessKey)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: cred.AccessKey,
|
|
|
|
Action: iampolicy.ListServiceAccountsAdminAction,
|
|
|
|
ConditionValues: getConditionValues(r, "", cred.AccessKey, claims),
|
|
|
|
IsOwner: owner,
|
|
|
|
Claims: claims,
|
|
|
|
}) {
|
|
|
|
requestUser := cred.AccessKey
|
|
|
|
if cred.ParentUser != "" {
|
|
|
|
requestUser = cred.ParentUser
|
|
|
|
}
|
|
|
|
|
|
|
|
if requestUser != svcAccount.ParentUser {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAccessDenied), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var svcAccountPolicy iampolicy.Policy
|
|
|
|
|
|
|
|
impliedPolicy := policy == nil
|
|
|
|
|
|
|
|
// If policy is empty, check for policy of the parent user
|
|
|
|
if !impliedPolicy {
|
2021-04-15 17:47:58 -04:00
|
|
|
svcAccountPolicy = svcAccountPolicy.Merge(*policy)
|
2021-04-15 01:51:14 -04:00
|
|
|
} else {
|
|
|
|
policiesNames, err := globalIAMSys.PolicyDBGet(svcAccount.AccessKey, false)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2021-04-15 17:47:58 -04:00
|
|
|
svcAccountPolicy = svcAccountPolicy.Merge(globalIAMSys.GetCombinedPolicy(policiesNames...))
|
2021-04-15 01:51:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
policyJSON, err := json.Marshal(svcAccountPolicy)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var infoResp = madmin.InfoServiceAccountResp{
|
|
|
|
ParentUser: svcAccount.ParentUser,
|
|
|
|
AccountStatus: svcAccount.Status,
|
|
|
|
ImpliedPolicy: impliedPolicy,
|
|
|
|
Policy: string(policyJSON),
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(infoResp)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedData, err := madmin.EncryptData(cred.SecretKey, data)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, encryptedData)
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
// ListServiceAccounts - GET /minio/admin/v3/list-service-accounts
|
|
|
|
func (a adminAPIHandlers) ListServiceAccounts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "ListServiceAccounts")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
// Get current object layer instance.
|
2020-10-09 12:59:52 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
2020-10-19 12:54:40 -04:00
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
2020-04-24 15:10:09 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
cred, claims, owner, s3Err := validateAdminSignature(ctx, r, "")
|
2020-04-24 15:10:09 -04:00
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow creating service accounts by root user.
|
|
|
|
if owner {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminAccountNotEligible), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
var targetAccount string
|
|
|
|
|
|
|
|
user := r.URL.Query().Get("user")
|
|
|
|
if user != "" {
|
|
|
|
if !globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: cred.AccessKey,
|
|
|
|
Action: iampolicy.ListServiceAccountsAdminAction,
|
|
|
|
ConditionValues: getConditionValues(r, "", cred.AccessKey, claims),
|
|
|
|
IsOwner: owner,
|
|
|
|
Claims: claims,
|
|
|
|
}) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAccessDenied), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
targetAccount = user
|
|
|
|
} else {
|
|
|
|
targetAccount = cred.AccessKey
|
|
|
|
if cred.ParentUser != "" {
|
|
|
|
targetAccount = cred.ParentUser
|
|
|
|
}
|
2020-05-01 11:05:14 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
serviceAccounts, err := globalIAMSys.ListServiceAccounts(ctx, targetAccount)
|
2020-04-24 15:10:09 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
var serviceAccountsNames []string
|
|
|
|
|
|
|
|
for _, svc := range serviceAccounts {
|
|
|
|
serviceAccountsNames = append(serviceAccountsNames, svc.AccessKey)
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
var listResp = madmin.ListServiceAccountsResp{
|
2021-04-15 01:51:14 -04:00
|
|
|
Accounts: serviceAccountsNames,
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(listResp)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedData, err := madmin.EncryptData(cred.SecretKey, data)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, encryptedData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteServiceAccount - DELETE /minio/admin/v3/delete-service-account
|
|
|
|
func (a adminAPIHandlers) DeleteServiceAccount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "DeleteServiceAccount")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2020-04-24 15:10:09 -04:00
|
|
|
// Get current object layer instance.
|
2020-10-09 12:59:52 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
2020-10-19 12:54:40 -04:00
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
2020-04-24 15:10:09 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
cred, claims, owner, s3Err := validateAdminSignature(ctx, r, "")
|
2020-04-24 15:10:09 -04:00
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow creating service accounts by root user.
|
|
|
|
if owner {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminAccountNotEligible), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceAccount := mux.Vars(r)["accessKey"]
|
|
|
|
if serviceAccount == "" {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminInvalidArgument), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
svcAccount, _, err := globalIAMSys.GetServiceAccount(ctx, serviceAccount)
|
2020-04-24 15:10:09 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
adminPrivilege := globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: cred.AccessKey,
|
|
|
|
Action: iampolicy.RemoveServiceAccountAdminAction,
|
|
|
|
ConditionValues: getConditionValues(r, "", cred.AccessKey, claims),
|
|
|
|
IsOwner: owner,
|
|
|
|
Claims: claims,
|
|
|
|
})
|
2020-05-01 11:05:14 -04:00
|
|
|
|
2021-04-15 01:51:14 -04:00
|
|
|
if !adminPrivilege {
|
|
|
|
parentUser := cred.AccessKey
|
|
|
|
if cred.ParentUser != "" {
|
|
|
|
parentUser = cred.ParentUser
|
|
|
|
}
|
|
|
|
if parentUser != svcAccount.ParentUser {
|
|
|
|
// The service account belongs to another user but return not
|
|
|
|
// found error to mitigate brute force attacks. or the
|
|
|
|
// serviceAccount doesn't exist.
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminServiceAccountNotFound), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-04-24 15:10:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = globalIAMSys.DeleteServiceAccount(ctx, serviceAccount)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessNoContent(w)
|
2020-03-17 13:36:13 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 20:23:57 -05:00
|
|
|
// AccountInfoHandler returns usage
|
|
|
|
func (a adminAPIHandlers) AccountInfoHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "AccountInfo")
|
2020-05-15 21:16:45 -04:00
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-15 21:16:45 -04:00
|
|
|
|
|
|
|
// Get current object layer instance.
|
2020-10-09 12:59:52 -04:00
|
|
|
objectAPI := newObjectLayerFn()
|
2020-10-19 12:54:40 -04:00
|
|
|
if objectAPI == nil || globalNotificationSys == nil {
|
2020-05-15 21:16:45 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cred, claims, owner, s3Err := validateAdminSignature(ctx, r, "")
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set prefix value for "s3:prefix" policy conditionals.
|
|
|
|
r.Header.Set("prefix", "")
|
|
|
|
|
|
|
|
// Set delimiter value for "s3:delimiter" policy conditionals.
|
|
|
|
r.Header.Set("delimiter", SlashSeparator)
|
|
|
|
|
|
|
|
isAllowedAccess := func(bucketName string) (rd, wr bool) {
|
|
|
|
// Use the following trick to filter in place
|
|
|
|
// https://github.com/golang/go/wiki/SliceTricks#filter-in-place
|
|
|
|
if globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: cred.AccessKey,
|
2021-03-23 18:15:51 -04:00
|
|
|
Groups: cred.Groups,
|
2020-05-15 21:16:45 -04:00
|
|
|
Action: iampolicy.ListBucketAction,
|
|
|
|
BucketName: bucketName,
|
|
|
|
ConditionValues: getConditionValues(r, "", cred.AccessKey, claims),
|
|
|
|
IsOwner: owner,
|
|
|
|
ObjectName: "",
|
|
|
|
Claims: claims,
|
|
|
|
}) {
|
|
|
|
rd = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if globalIAMSys.IsAllowed(iampolicy.Args{
|
|
|
|
AccountName: cred.AccessKey,
|
2021-03-23 18:15:51 -04:00
|
|
|
Groups: cred.Groups,
|
2020-05-15 21:16:45 -04:00
|
|
|
Action: iampolicy.PutObjectAction,
|
|
|
|
BucketName: bucketName,
|
|
|
|
ConditionValues: getConditionValues(r, "", cred.AccessKey, claims),
|
|
|
|
IsOwner: owner,
|
|
|
|
ObjectName: "",
|
|
|
|
Claims: claims,
|
|
|
|
}) {
|
|
|
|
wr = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return rd, wr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the latest calculated data usage
|
|
|
|
dataUsageInfo, err := loadDataUsageFromBackend(ctx, objectAPI)
|
|
|
|
if err != nil {
|
2020-05-25 03:17:52 -04:00
|
|
|
// log the error, continue with the accounting response
|
2020-05-15 21:16:45 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
|
2021-02-09 12:53:07 -05:00
|
|
|
// If etcd, dns federation configured list buckets from etcd.
|
|
|
|
var buckets []BucketInfo
|
|
|
|
if globalDNSConfig != nil && globalBucketFederation {
|
|
|
|
dnsBuckets, err := globalDNSConfig.List()
|
|
|
|
if err != nil && !IsErrIgnored(err,
|
|
|
|
dns.ErrNoEntriesFound,
|
|
|
|
dns.ErrDomainMissing) {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, dnsRecords := range dnsBuckets {
|
|
|
|
buckets = append(buckets, BucketInfo{
|
|
|
|
Name: dnsRecords[0].Key,
|
|
|
|
Created: dnsRecords[0].CreationDate,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sort.Slice(buckets, func(i, j int) bool {
|
|
|
|
return buckets[i].Name < buckets[j].Name
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
buckets, err = objectAPI.ListBuckets(ctx)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 21:16:45 -04:00
|
|
|
accountName := cred.AccessKey
|
2021-03-23 20:39:20 -04:00
|
|
|
var policies []string
|
|
|
|
switch globalIAMSys.usersSysType {
|
|
|
|
case MinIOUsersSysType:
|
|
|
|
policies, err = globalIAMSys.PolicyDBGet(accountName, false)
|
|
|
|
case LDAPUsersSysType:
|
2021-03-25 03:38:15 -04:00
|
|
|
parentUser := accountName
|
|
|
|
if cred.ParentUser != "" {
|
|
|
|
parentUser = cred.ParentUser
|
|
|
|
}
|
|
|
|
policies, err = globalIAMSys.PolicyDBGet(parentUser, false, cred.Groups...)
|
2021-03-23 20:39:20 -04:00
|
|
|
default:
|
|
|
|
err = errors.New("should not happen!")
|
|
|
|
}
|
2020-11-27 20:23:57 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
acctInfo := madmin.AccountInfo{
|
2020-05-15 21:16:45 -04:00
|
|
|
AccountName: accountName,
|
2020-11-27 20:23:57 -05:00
|
|
|
Policy: globalIAMSys.GetCombinedPolicy(policies...),
|
2020-05-15 21:16:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
rd, wr := isAllowedAccess(bucket.Name)
|
|
|
|
if rd || wr {
|
|
|
|
var size uint64
|
|
|
|
// Fetch the data usage of the current bucket
|
2020-05-27 09:45:43 -04:00
|
|
|
if !dataUsageInfo.LastUpdate.IsZero() {
|
|
|
|
size = dataUsageInfo.BucketsUsage[bucket.Name].Size
|
2020-05-15 21:16:45 -04:00
|
|
|
}
|
2021-04-03 12:03:42 -04:00
|
|
|
acctInfo.Buckets = append(acctInfo.Buckets, madmin.BucketAccessInfo{
|
2020-05-15 21:16:45 -04:00
|
|
|
Name: bucket.Name,
|
|
|
|
Created: bucket.Created,
|
|
|
|
Size: size,
|
|
|
|
Access: madmin.AccountAccess{
|
|
|
|
Read: rd,
|
|
|
|
Write: wr,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usageInfoJSON, err := json.Marshal(acctInfo)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, usageInfoJSON)
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// InfoCannedPolicyV2 - GET /minio/admin/v2/info-canned-policy?name={policyName}
|
|
|
|
func (a adminAPIHandlers) InfoCannedPolicyV2(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "InfoCannedPolicyV2")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.GetPolicyAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := globalIAMSys.InfoPolicy(mux.Vars(r)["name"])
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(policy)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(data)
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfoCannedPolicy - GET /minio/admin/v3/info-canned-policy?name={policyName}
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) InfoCannedPolicy(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "InfoCannedPolicy")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.GetPolicyAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
policy, err := globalIAMSys.InfoPolicy(mux.Vars(r)["name"])
|
2019-11-09 12:27:23 -05:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-27 13:24:04 -04:00
|
|
|
if err = json.NewEncoder(w).Encode(policy); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-04-07 22:30:59 -04:00
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListCannedPoliciesV2 - GET /minio/admin/v2/list-canned-policies
|
|
|
|
func (a adminAPIHandlers) ListCannedPoliciesV2(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "ListCannedPoliciesV2")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.ListUserPoliciesAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err := globalIAMSys.ListPolicies()
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
policyMap := make(map[string][]byte, len(policies))
|
|
|
|
for k, p := range policies {
|
|
|
|
var err error
|
|
|
|
policyMap[k], err = json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = json.NewEncoder(w).Encode(policyMap); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:27:23 -05:00
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// ListCannedPolicies - GET /minio/admin/v3/list-canned-policies
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) ListCannedPolicies(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "ListCannedPolicies")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.ListUserPoliciesAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err := globalIAMSys.ListPolicies()
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:16:40 -04:00
|
|
|
var newPolicies = make(map[string]iampolicy.Policy)
|
|
|
|
for name, p := range policies {
|
|
|
|
_, err = json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newPolicies[name] = p
|
|
|
|
}
|
|
|
|
if err = json.NewEncoder(w).Encode(newPolicies); err != nil {
|
2019-11-09 12:27:23 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// RemoveCannedPolicy - DELETE /minio/admin/v3/remove-canned-policy?name=<policy_name>
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) RemoveCannedPolicy(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "RemoveCannedPolicy")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.DeletePolicyAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
policyName := vars["name"]
|
|
|
|
|
|
|
|
if err := globalIAMSys.DeletePolicy(policyName); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to delete policy
|
|
|
|
for _, nerr := range globalNotificationSys.DeletePolicy(policyName) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// AddCannedPolicy - PUT /minio/admin/v3/add-canned-policy?name=<policy_name>
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) AddCannedPolicy(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "AddCannedPolicy")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.CreatePolicyAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
policyName := vars["name"]
|
|
|
|
|
|
|
|
// Error out if Content-Length is missing.
|
|
|
|
if r.ContentLength <= 0 {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrMissingContentLength), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error out if Content-Length is beyond allowed size.
|
|
|
|
if r.ContentLength > maxBucketPolicySize {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrEntityTooLarge), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
iamPolicy, err := iampolicy.ParseConfig(io.LimitReader(r.Body, r.ContentLength))
|
|
|
|
if err != nil {
|
2020-01-03 14:28:52 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
2019-11-09 12:27:23 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version in policy must not be empty
|
|
|
|
if iamPolicy.Version == "" {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrMalformedPolicy), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = globalIAMSys.SetPolicy(policyName, *iamPolicy); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to reload policy
|
|
|
|
for _, nerr := range globalNotificationSys.LoadPolicy(policyName) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:30:59 -04:00
|
|
|
// SetPolicyForUserOrGroup - PUT /minio/admin/v3/set-policy?policy=xxx&user-or-group=?[&is-group]
|
2019-11-09 12:27:23 -05:00
|
|
|
func (a adminAPIHandlers) SetPolicyForUserOrGroup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "SetPolicyForUserOrGroup")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-05-11 13:34:08 -04:00
|
|
|
|
2019-11-19 05:03:18 -05:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.AttachPolicyAdminAction)
|
2019-11-09 12:27:23 -05:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
policyName := vars["policyName"]
|
|
|
|
entityName := vars["userOrGroup"]
|
|
|
|
isGroup := vars["isGroup"] == "true"
|
|
|
|
|
2019-12-19 17:21:21 -05:00
|
|
|
if !isGroup {
|
2021-02-25 16:49:59 -05:00
|
|
|
ok, _, err := globalIAMSys.IsTempUser(entityName)
|
2020-04-09 04:04:08 -04:00
|
|
|
if err != nil && err != errNoSuchUser {
|
2019-12-19 17:21:21 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, errIAMActionNotAllowed), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:27:23 -05:00
|
|
|
if err := globalIAMSys.PolicyDBSet(entityName, policyName, isGroup); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify all other MinIO peers to reload policy
|
|
|
|
for _, nerr := range globalNotificationSys.LoadPolicyMapping(entityName, isGroup) {
|
|
|
|
if nerr.Err != nil {
|
|
|
|
logger.GetReqInfo(ctx).SetTags("peerAddress", nerr.Host.String())
|
|
|
|
logger.LogIf(ctx, nerr.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|