seaweedfs/weed/iamapi/iamapi_management_handlers.go

507 lines
15 KiB
Go
Raw Normal View History

2020-12-09 20:11:49 +08:00
package iamapi
import (
"crypto/sha1"
"encoding/json"
2020-12-10 03:03:14 +08:00
"fmt"
"math/rand"
2020-12-09 20:11:49 +08:00
"net/http"
"net/url"
2021-04-11 02:57:45 +08:00
"reflect"
"strings"
2021-04-06 16:43:08 +08:00
"sync"
2020-12-10 03:03:14 +08:00
"time"
2020-12-09 20:11:49 +08:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
"github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
"github.com/chrislusf/seaweedfs/weed/s3api/s3err"
2020-12-09 20:11:49 +08:00
"github.com/aws/aws-sdk-go/service/iam"
)
const (
2021-04-11 02:57:45 +08:00
charsetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
charset = charsetUpper + "abcdefghijklmnopqrstuvwxyz/"
policyDocumentVersion = "2012-10-17"
StatementActionAdmin = "*"
StatementActionWrite = "Put*"
StatementActionRead = "Get*"
StatementActionList = "List*"
StatementActionTagging = "Tagging*"
2020-12-09 20:11:49 +08:00
)
var (
seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
policyDocuments = map[string]*PolicyDocument{}
2021-04-11 02:57:45 +08:00
policyLock = sync.RWMutex{}
)
2020-12-10 03:03:14 +08:00
2021-04-11 02:57:45 +08:00
func MapToStatementAction(action string) string {
switch action {
case StatementActionAdmin:
return s3_constants.ACTION_ADMIN
case StatementActionWrite:
return s3_constants.ACTION_WRITE
case StatementActionRead:
return s3_constants.ACTION_READ
case StatementActionList:
return s3_constants.ACTION_LIST
case StatementActionTagging:
return s3_constants.ACTION_TAGGING
default:
return ""
}
}
func MapToIdentitiesAction(action string) string {
switch action {
case s3_constants.ACTION_ADMIN:
return StatementActionAdmin
case s3_constants.ACTION_WRITE:
return StatementActionWrite
case s3_constants.ACTION_READ:
return StatementActionRead
case s3_constants.ACTION_LIST:
return StatementActionList
case s3_constants.ACTION_TAGGING:
return StatementActionTagging
default:
return ""
}
}
2021-04-06 16:43:08 +08:00
type Statement struct {
Effect string `json:"Effect"`
Action []string `json:"Action"`
Resource []string `json:"Resource"`
}
2021-04-11 02:57:45 +08:00
type Policies struct {
Policies map[string]PolicyDocument `json:"policies"`
}
type PolicyDocument struct {
2021-04-06 16:43:08 +08:00
Version string `json:"Version"`
Statement []*Statement `json:"Statement"`
2020-12-10 03:03:14 +08:00
}
2021-04-11 02:57:45 +08:00
func (p PolicyDocument) String() string {
b, _ := json.Marshal(p)
return string(b)
}
func Hash(s *string) string {
h := sha1.New()
h.Write([]byte(*s))
return fmt.Sprintf("%x", h.Sum(nil))
}
2020-12-10 03:03:14 +08:00
func StringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func (iama *IamApiServer) ListUsers(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp ListUsersResponse) {
2020-12-09 20:11:49 +08:00
for _, ident := range s3cfg.Identities {
resp.ListUsersResult.Users = append(resp.ListUsersResult.Users, &iam.User{UserName: &ident.Name})
}
return resp
}
2020-12-10 03:03:14 +08:00
func (iama *IamApiServer) ListAccessKeys(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp ListAccessKeysResponse) {
status := iam.StatusTypeActive
2021-04-28 20:15:22 +08:00
userName := values.Get("UserName")
2020-12-10 03:03:14 +08:00
for _, ident := range s3cfg.Identities {
2021-04-28 20:15:22 +08:00
if userName != "" && userName != ident.Name {
continue
}
2020-12-10 03:03:14 +08:00
for _, cred := range ident.Credentials {
resp.ListAccessKeysResult.AccessKeyMetadata = append(resp.ListAccessKeysResult.AccessKeyMetadata,
&iam.AccessKeyMetadata{UserName: &ident.Name, AccessKeyId: &cred.AccessKey, Status: &status},
)
}
}
return resp
}
func (iama *IamApiServer) CreateUser(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp CreateUserResponse) {
userName := values.Get("UserName")
resp.CreateUserResult.User.UserName = &userName
s3cfg.Identities = append(s3cfg.Identities, &iam_pb.Identity{Name: userName})
return resp
}
2021-03-29 15:01:44 +08:00
func (iama *IamApiServer) DeleteUser(s3cfg *iam_pb.S3ApiConfiguration, userName string) (resp DeleteUserResponse, err error) {
for i, ident := range s3cfg.Identities {
if userName == ident.Name {
2021-04-11 02:57:45 +08:00
s3cfg.Identities = append(s3cfg.Identities[:i], s3cfg.Identities[i+1:]...)
2021-03-29 15:01:44 +08:00
return resp, nil
}
}
return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
}
func (iama *IamApiServer) GetUser(s3cfg *iam_pb.S3ApiConfiguration, userName string) (resp GetUserResponse, err error) {
for _, ident := range s3cfg.Identities {
if userName == ident.Name {
resp.GetUserResult.User = iam.User{UserName: &ident.Name}
return resp, nil
}
}
return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
}
func (iama *IamApiServer) UpdateUser(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp UpdateUserResponse, err error) {
userName := values.Get("UserName")
newUserName := values.Get("NewUserName")
if newUserName != "" {
for _, ident := range s3cfg.Identities {
if userName == ident.Name {
ident.Name = newUserName
return resp, nil
}
}
} else {
return resp, nil
}
return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
}
func GetPolicyDocument(policy *string) (policyDocument PolicyDocument, err error) {
if err = json.Unmarshal([]byte(*policy), &policyDocument); err != nil {
return PolicyDocument{}, err
}
return policyDocument, err
}
func (iama *IamApiServer) CreatePolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp CreatePolicyResponse, err error) {
policyName := values.Get("PolicyName")
policyDocumentString := values.Get("PolicyDocument")
policyDocument, err := GetPolicyDocument(&policyDocumentString)
if err != nil {
return CreatePolicyResponse{}, err
}
policyId := Hash(&policyDocumentString)
arn := fmt.Sprintf("arn:aws:iam:::policy/%s", policyName)
resp.CreatePolicyResult.Policy.PolicyName = &policyName
resp.CreatePolicyResult.Policy.Arn = &arn
resp.CreatePolicyResult.Policy.PolicyId = &policyId
2021-04-11 02:57:45 +08:00
policies := Policies{}
policyLock.Lock()
defer policyLock.Unlock()
if err = iama.s3ApiConfig.GetPolicies(&policies); err != nil {
return resp, err
}
policies.Policies[policyName] = policyDocument
if err = iama.s3ApiConfig.PutPolicies(&policies); err != nil {
return resp, err
}
return resp, nil
}
2022-07-11 16:38:41 +08:00
// https://docs.aws.amazon.com/IAM/latest/APIReference/API_PutUserPolicy.html
func (iama *IamApiServer) PutUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp PutUserPolicyResponse, err error) {
userName := values.Get("UserName")
policyName := values.Get("PolicyName")
policyDocumentString := values.Get("PolicyDocument")
policyDocument, err := GetPolicyDocument(&policyDocumentString)
if err != nil {
return PutUserPolicyResponse{}, err
}
policyDocuments[policyName] = &policyDocument
actions := GetActions(&policyDocument)
for _, ident := range s3cfg.Identities {
2022-07-11 16:38:41 +08:00
if userName != ident.Name {
continue
}
ident.Actions = actions
2022-07-12 11:25:36 +08:00
return resp, nil
}
2022-07-12 11:25:36 +08:00
return resp, fmt.Errorf("%s: the user with name %s cannot be found", iam.ErrCodeNoSuchEntityException, userName)
}
2021-04-11 02:57:45 +08:00
func (iama *IamApiServer) GetUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp GetUserPolicyResponse, err error) {
userName := values.Get("UserName")
policyName := values.Get("PolicyName")
for _, ident := range s3cfg.Identities {
if userName != ident.Name {
continue
}
resp.GetUserPolicyResult.UserName = userName
resp.GetUserPolicyResult.PolicyName = policyName
if len(ident.Actions) == 0 {
return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
}
policyDocument := PolicyDocument{Version: policyDocumentVersion}
statements := make(map[string][]string)
for _, action := range ident.Actions {
// parse "Read:EXAMPLE-BUCKET"
act := strings.Split(action, ":")
resource := "*"
if len(act) == 2 {
resource = fmt.Sprintf("arn:aws:s3:::%s/*", act[1])
}
statements[resource] = append(statements[resource],
fmt.Sprintf("s3:%s", MapToIdentitiesAction(act[0])),
)
}
for resource, actions := range statements {
isEqAction := false
for i, statement := range policyDocument.Statement {
if reflect.DeepEqual(statement.Action, actions) {
policyDocument.Statement[i].Resource = append(
policyDocument.Statement[i].Resource, resource)
isEqAction = true
break
}
}
if isEqAction {
continue
}
policyDocumentStatement := Statement{
Effect: "Allow",
Action: actions,
}
policyDocumentStatement.Resource = append(policyDocumentStatement.Resource, resource)
policyDocument.Statement = append(policyDocument.Statement, &policyDocumentStatement)
}
resp.GetUserPolicyResult.PolicyDocument = policyDocument.String()
return resp, nil
}
return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
}
2021-04-08 14:16:36 +08:00
func (iama *IamApiServer) DeleteUserPolicy(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp PutUserPolicyResponse, err error) {
userName := values.Get("UserName")
for i, ident := range s3cfg.Identities {
if ident.Name == userName {
s3cfg.Identities = append(s3cfg.Identities[:i], s3cfg.Identities[i+1:]...)
return resp, nil
}
}
return resp, fmt.Errorf(iam.ErrCodeNoSuchEntityException)
}
func GetActions(policy *PolicyDocument) (actions []string) {
for _, statement := range policy.Statement {
if statement.Effect != "Allow" {
continue
}
for _, resource := range statement.Resource {
// Parse "arn:aws:s3:::my-bucket/shared/*"
res := strings.Split(resource, ":")
2020-12-10 20:03:55 +08:00
if len(res) != 6 || res[0] != "arn" || res[1] != "aws" || res[2] != "s3" {
2020-12-11 00:03:25 +08:00
glog.Infof("not match resource: %s", res)
continue
}
for _, action := range statement.Action {
// Parse "s3:Get*"
act := strings.Split(action, ":")
if len(act) != 2 || act[0] != "s3" {
2020-12-10 20:03:55 +08:00
glog.Infof("not match action: %s", act)
continue
}
2021-04-11 02:57:45 +08:00
statementAction := MapToStatementAction(act[1])
if res[5] == "*" {
2021-04-11 02:57:45 +08:00
actions = append(actions, statementAction)
continue
}
// Parse my-bucket/shared/*
path := strings.Split(res[5], "/")
if len(path) != 2 || path[1] != "*" {
2020-12-10 20:03:55 +08:00
glog.Infof("not match bucket: %s", path)
continue
}
2021-04-11 02:57:45 +08:00
actions = append(actions, fmt.Sprintf("%s:%s", statementAction, path[0]))
}
}
}
return actions
}
2020-12-10 03:03:14 +08:00
func (iama *IamApiServer) CreateAccessKey(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp CreateAccessKeyResponse) {
userName := values.Get("UserName")
status := iam.StatusTypeActive
accessKeyId := StringWithCharset(21, charsetUpper)
secretAccessKey := StringWithCharset(42, charset)
resp.CreateAccessKeyResult.AccessKey.AccessKeyId = &accessKeyId
resp.CreateAccessKeyResult.AccessKey.SecretAccessKey = &secretAccessKey
resp.CreateAccessKeyResult.AccessKey.UserName = &userName
resp.CreateAccessKeyResult.AccessKey.Status = &status
changed := false
for _, ident := range s3cfg.Identities {
if userName == ident.Name {
ident.Credentials = append(ident.Credentials,
&iam_pb.Credential{AccessKey: accessKeyId, SecretKey: secretAccessKey})
changed = true
break
}
}
if !changed {
s3cfg.Identities = append(s3cfg.Identities,
2022-07-20 17:43:50 +08:00
&iam_pb.Identity{
Name: userName,
2020-12-10 03:03:14 +08:00
Credentials: []*iam_pb.Credential{
{
AccessKey: accessKeyId,
SecretKey: secretAccessKey,
},
},
},
)
}
return resp
}
func (iama *IamApiServer) DeleteAccessKey(s3cfg *iam_pb.S3ApiConfiguration, values url.Values) (resp DeleteAccessKeyResponse) {
userName := values.Get("UserName")
accessKeyId := values.Get("AccessKeyId")
for _, ident := range s3cfg.Identities {
if userName == ident.Name {
for i, cred := range ident.Credentials {
if cred.AccessKey == accessKeyId {
ident.Credentials = append(ident.Credentials[:i], ident.Credentials[i+1:]...)
break
}
}
break
}
}
return resp
2020-12-09 20:11:49 +08:00
}
2022-04-20 10:12:33 +08:00
// handleImplicitUsername adds username who signs the request to values if 'username' is not specified
// According to https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/create-access-key.html/
// "If you do not specify a user name, IAM determines the user name implicitly based on the Amazon Web
// Services access key ID signing the request."
func handleImplicitUsername(r *http.Request, values url.Values) {
2022-04-22 22:47:11 +08:00
if len(r.Header["Authorization"]) == 0 || values.Get("UserName") != "" {
return
2022-04-20 10:12:33 +08:00
}
// get username who signs the request. For a typical Authorization:
2022-04-22 22:47:11 +08:00
// "AWS4-HMAC-SHA256 Credential=197FSAQ7HHTA48X64O3A/20220420/test1/iam/aws4_request, SignedHeaders=content-type;
// host;x-amz-date, Signature=6757dc6b3d7534d67e17842760310e99ee695408497f6edc4fdb84770c252dc8",
2022-04-22 22:47:11 +08:00
// the "test1" will be extracted as the username
glog.V(4).Infof("Authorization field: %v", r.Header["Authorization"][0])
2022-04-22 22:47:11 +08:00
s := strings.Split(r.Header["Authorization"][0], "Credential=")
if len(s) < 2 {
return
}
s = strings.Split(s[1], ",")
if len(s) < 2 {
return
}
s = strings.Split(s[0], "/")
if len(s) < 5 {
return
}
userName := s[2]
values.Set("UserName", userName)
2022-04-20 10:12:33 +08:00
}
2020-12-09 20:11:49 +08:00
func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
2021-11-01 09:05:34 +08:00
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
2020-12-09 20:11:49 +08:00
return
}
values := r.PostForm
s3cfg := &iam_pb.S3ApiConfiguration{}
2021-04-06 16:43:08 +08:00
if err := iama.s3ApiConfig.GetS3ApiConfiguration(s3cfg); err != nil {
2021-11-01 09:05:34 +08:00
s3err.WriteErrorResponse(w, r, s3err.ErrInternalError)
2020-12-09 20:11:49 +08:00
return
}
2021-04-06 16:43:08 +08:00
glog.V(4).Infof("DoActions: %+v", values)
2020-12-09 20:11:49 +08:00
var response interface{}
2021-03-29 15:01:44 +08:00
var err error
2020-12-10 20:03:55 +08:00
changed := true
2020-12-09 20:11:49 +08:00
switch r.Form.Get("Action") {
case "ListUsers":
response = iama.ListUsers(s3cfg, values)
2020-12-10 20:03:55 +08:00
changed = false
2020-12-09 20:11:49 +08:00
case "ListAccessKeys":
2022-04-20 10:12:33 +08:00
handleImplicitUsername(r, values)
2020-12-10 03:03:14 +08:00
response = iama.ListAccessKeys(s3cfg, values)
2020-12-10 20:03:55 +08:00
changed = false
2020-12-10 03:03:14 +08:00
case "CreateUser":
response = iama.CreateUser(s3cfg, values)
2021-03-29 15:01:44 +08:00
case "GetUser":
userName := values.Get("UserName")
response, err = iama.GetUser(s3cfg, userName)
if err != nil {
2021-11-01 09:02:08 +08:00
writeIamErrorResponse(w, r, err, "user", userName, nil)
2021-03-29 15:01:44 +08:00
return
}
2021-04-11 02:57:45 +08:00
changed = false
case "UpdateUser":
response, err = iama.UpdateUser(s3cfg, values)
if err != nil {
glog.Errorf("UpdateUser: %+v", err)
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
return
}
2020-12-10 03:03:14 +08:00
case "DeleteUser":
2021-03-29 15:01:44 +08:00
userName := values.Get("UserName")
response, err = iama.DeleteUser(s3cfg, userName)
if err != nil {
2021-11-01 09:02:08 +08:00
writeIamErrorResponse(w, r, err, "user", userName, nil)
2021-03-29 15:01:44 +08:00
return
}
2020-12-10 03:03:14 +08:00
case "CreateAccessKey":
2022-04-20 10:12:33 +08:00
handleImplicitUsername(r, values)
2020-12-10 03:03:14 +08:00
response = iama.CreateAccessKey(s3cfg, values)
case "DeleteAccessKey":
2022-04-20 10:12:33 +08:00
handleImplicitUsername(r, values)
2020-12-10 03:03:14 +08:00
response = iama.DeleteAccessKey(s3cfg, values)
case "CreatePolicy":
response, err = iama.CreatePolicy(s3cfg, values)
if err != nil {
2021-04-06 16:43:08 +08:00
glog.Errorf("CreatePolicy: %+v", err)
2021-11-01 09:05:34 +08:00
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
return
}
case "PutUserPolicy":
response, err = iama.PutUserPolicy(s3cfg, values)
if err != nil {
2021-04-06 16:43:08 +08:00
glog.Errorf("PutUserPolicy: %+v", err)
2021-11-01 09:05:34 +08:00
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest)
return
}
2021-04-11 02:57:45 +08:00
case "GetUserPolicy":
response, err = iama.GetUserPolicy(s3cfg, values)
if err != nil {
2021-11-01 09:02:08 +08:00
writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
2021-04-11 02:57:45 +08:00
return
}
changed = false
2021-04-08 14:16:36 +08:00
case "DeleteUserPolicy":
if response, err = iama.DeleteUserPolicy(s3cfg, values); err != nil {
2021-11-01 09:02:08 +08:00
writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
2022-04-19 14:16:32 +08:00
return
2021-04-08 14:16:36 +08:00
}
2020-12-09 20:11:49 +08:00
default:
2021-04-08 14:16:36 +08:00
errNotImplemented := s3err.GetAPIError(s3err.ErrNotImplemented)
errorResponse := ErrorResponse{}
errorResponse.Error.Code = &errNotImplemented.Code
errorResponse.Error.Message = &errNotImplemented.Description
2021-11-01 09:02:08 +08:00
s3err.WriteXMLResponse(w, r, errNotImplemented.HTTPStatusCode, errorResponse)
2020-12-09 20:11:49 +08:00
return
}
2020-12-10 20:03:55 +08:00
if changed {
2021-04-06 16:43:08 +08:00
err := iama.s3ApiConfig.PutS3ApiConfiguration(s3cfg)
2020-12-11 00:03:25 +08:00
if err != nil {
2021-11-01 09:02:08 +08:00
writeIamErrorResponse(w, r, fmt.Errorf(iam.ErrCodeServiceFailureException), "", "", err)
2020-12-10 20:03:55 +08:00
return
}
}
2021-11-01 09:02:08 +08:00
s3err.WriteXMLResponse(w, r, http.StatusOK, response)
2020-12-09 20:11:49 +08:00
}