forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cubefs#507 from wenjia322/cors
Feature: support CORS access control
- Loading branch information
Showing
12 changed files
with
341 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package objectnode | ||
|
||
// https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html | ||
|
||
import ( | ||
"encoding/xml" | ||
|
||
"github.com/chubaofs/chubaofs/util/errors" | ||
) | ||
|
||
var methodsRequest = []string{"GET", "PUT", "HEAD", "POST", "DELETE", "*"} | ||
|
||
type CORSConfiguration struct { | ||
XMLName xml.Name `xml:"CORSConfiguration" json:"xml_name"` | ||
CORSRule []*CORSRule `xml:"CORSRule" json:"cors_rule"` | ||
} | ||
|
||
type CORSRule struct { | ||
AllowedHeader []string `xml:"AllowedHeader" json:"allowed_header"` | ||
AllowedMethod []string `xml:"AllowedMethod" json:"allowed_method"` | ||
AllowedOrigin []string `xml:"AllowedOrigin" json:"allowed_origin"` | ||
ExposeHeader []string `xml:"ExposeHeader" json:"expose_header"` | ||
MaxAgeSeconds uint16 `xml:"MaxAgeSeconds" json:"max_age_seconds"` | ||
} | ||
|
||
func (rule *CORSRule) match(origin, method string, headers []string) bool { | ||
// todo if "*" are used in some text | ||
if !contains(rule.AllowedOrigin, "*") && !contains(rule.AllowedOrigin, origin) { | ||
return false | ||
} | ||
if !contains(rule.AllowedMethod, "*") && !contains(rule.AllowedMethod, method) { | ||
return false | ||
} | ||
if contains(rule.AllowedHeader, "*") { | ||
return true | ||
} | ||
for _, header := range headers { | ||
if !contains(rule.AllowedHeader, header) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (corsConfig *CORSConfiguration) validate() bool { | ||
if len(corsConfig.CORSRule) > 100 { | ||
return false | ||
} | ||
for _, rule := range corsConfig.CORSRule { | ||
for _, method := range rule.AllowedMethod { | ||
if !contains(methodsRequest, method) { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func parseCorsConfig(bytes []byte) (corsConfig *CORSConfiguration, err error) { | ||
corsConfig = &CORSConfiguration{} | ||
if err = xml.Unmarshal(bytes, corsConfig); err != nil { | ||
return | ||
} | ||
if ok := corsConfig.validate(); !ok { | ||
return nil, errors.New("invalid cors configuration") | ||
} | ||
return | ||
} | ||
|
||
func storeBucketCors(bytes []byte, vol *Volume) (err error) { | ||
if err = vol.store.Put(vol.name, bucketRootPath, XAttrKeyOSSCORS, bytes); err != nil { | ||
return | ||
} | ||
return nil | ||
} | ||
|
||
func deleteBucketCors(vol *Volume) (err error) { | ||
if err = vol.store.Delete(vol.name, bucketRootPath, XAttrKeyOSSCORS); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package objectnode | ||
|
||
// https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/EnableCorsUsingREST.html | ||
|
||
import ( | ||
"encoding/json" | ||
"encoding/xml" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/chubaofs/chubaofs/util/log" | ||
) | ||
|
||
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketCors.html | ||
func (o *ObjectNode) getBucketCorsHandler(w http.ResponseWriter, r *http.Request) { | ||
|
||
var err error | ||
var param = ParseRequestParam(r) | ||
if param.Bucket() == "" { | ||
_ = NoSuchBucket.ServeResponse(w, r) | ||
return | ||
} | ||
|
||
var vol *Volume | ||
if vol, err = o.vm.Volume(param.Bucket()); err != nil { | ||
_ = NoSuchBucket.ServeResponse(w, r) | ||
return | ||
} | ||
|
||
var output = CORSConfiguration{} | ||
|
||
cors := vol.loadCors() | ||
if cors != nil { | ||
output.CORSRule = cors.CORSRule | ||
} | ||
var corsData []byte | ||
if corsData, err = xml.Marshal(output); err != nil { | ||
_ = InternalErrorCode(err).ServeResponse(w, r) | ||
return | ||
} | ||
|
||
_, _ = w.Write(corsData) | ||
return | ||
} | ||
|
||
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketCors.html | ||
func (o *ObjectNode) putBucketCorsHandler(w http.ResponseWriter, r *http.Request) { | ||
log.LogInfof("Put bucket cors") | ||
|
||
var err error | ||
var param = ParseRequestParam(r) | ||
if param.Bucket() == "" { | ||
_ = NoSuchBucket.ServeResponse(w, r) | ||
return | ||
} | ||
var vol *Volume | ||
if vol, err = o.vm.Volume(param.Bucket()); err != nil { | ||
_ = NoSuchBucket.ServeResponse(w, r) | ||
return | ||
} | ||
|
||
var bytes []byte | ||
if bytes, err = ioutil.ReadAll(r.Body); err != nil && err != io.EOF { | ||
_ = InternalErrorCode(err).ServeResponse(w, r) | ||
return | ||
} | ||
|
||
var corsConfig *CORSConfiguration | ||
if corsConfig, err = parseCorsConfig(bytes); err != nil { | ||
_ = InvalidArgument.ServeResponse(w, r) | ||
return | ||
} | ||
if corsConfig == nil { | ||
_ = InvalidArgument.ServeResponse(w, r) | ||
return | ||
} | ||
|
||
var newBytes []byte | ||
if newBytes, err = json.Marshal(corsConfig); err != nil { | ||
_ = InternalErrorCode(err).ServeResponse(w, r) | ||
return | ||
} | ||
if err = storeBucketCors(newBytes, vol); err != nil { | ||
_ = InternalErrorCode(err).ServeResponse(w, r) | ||
return | ||
} | ||
vol.storeCors(corsConfig) | ||
|
||
return | ||
} | ||
|
||
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketCors.html | ||
func (o *ObjectNode) deleteBucketCorsHandler(w http.ResponseWriter, r *http.Request) { | ||
log.LogInfof("Delete bucket cors") | ||
|
||
var err error | ||
var param = ParseRequestParam(r) | ||
if param.Bucket() == "" { | ||
_ = NoSuchBucket.ServeResponse(w, r) | ||
return | ||
} | ||
var vol *Volume | ||
if vol, err = o.vm.Volume(param.Bucket()); err != nil { | ||
_ = NoSuchBucket.ServeResponse(w, r) | ||
return | ||
} | ||
|
||
if err = deleteBucketCors(vol); err != nil { | ||
_ = InternalErrorCode(err).ServeResponse(w, r) | ||
return | ||
} | ||
vol.storeCors(nil) | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
return | ||
} | ||
|
||
// Option object | ||
// Reference: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTOPTIONSobject.html | ||
func (o *ObjectNode) optionsObjectHandler(w http.ResponseWriter, r *http.Request) { | ||
log.LogInfof("optionsObjectHandler: OPTIONS object, requestID(%v) remote(%v)", GetRequestID(r), r.RemoteAddr) | ||
// Already done in methods 'corsMiddleware'. | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.