forked from TrueCloudLab/frostfs-s3-gw
[#367] Add check of AccessBox attributes
Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
This commit is contained in:
parent
5315f7b733
commit
e22ff52165
22 changed files with 157 additions and 110 deletions
|
@ -6,29 +6,27 @@ import (
|
|||
"time"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
||||
)
|
||||
|
||||
// keyWrapper is wrapper for context keys.
|
||||
type keyWrapper string
|
||||
|
||||
// authHeaders is a wrapper for authentication headers of a request.
|
||||
var authHeadersKey = keyWrapper("__context_auth_headers_key")
|
||||
|
||||
// boxData is an ID used to store accessbox.Box in a context.
|
||||
var boxDataKey = keyWrapper("__context_box_key")
|
||||
|
||||
// clientTime is an ID used to store client time.Time in a context.
|
||||
var clientTimeKey = keyWrapper("__context_client_time")
|
||||
// boxKey is an ID used to store Box in a context.
|
||||
var boxKey = keyWrapper("__context_box_key")
|
||||
|
||||
// GetBoxData extracts accessbox.Box from context.
|
||||
func GetBoxData(ctx context.Context) (*accessbox.Box, error) {
|
||||
var box *accessbox.Box
|
||||
data, ok := ctx.Value(boxDataKey).(*accessbox.Box)
|
||||
data, ok := ctx.Value(boxKey).(*Box)
|
||||
if !ok || data == nil {
|
||||
return nil, fmt.Errorf("couldn't get box from context")
|
||||
}
|
||||
|
||||
if data.AccessBox == nil {
|
||||
return nil, fmt.Errorf("couldn't get box data from context")
|
||||
}
|
||||
|
||||
box = data
|
||||
box := data.AccessBox
|
||||
if box.Gate == nil {
|
||||
box.Gate = &accessbox.GateData{}
|
||||
}
|
||||
|
@ -37,35 +35,39 @@ func GetBoxData(ctx context.Context) (*accessbox.Box, error) {
|
|||
|
||||
// GetAuthHeaders extracts auth.AuthHeader from context.
|
||||
func GetAuthHeaders(ctx context.Context) (*AuthHeader, error) {
|
||||
authHeaders, ok := ctx.Value(authHeadersKey).(*AuthHeader)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("couldn't get auth headers from context")
|
||||
data, ok := ctx.Value(boxKey).(*Box)
|
||||
if !ok || data == nil {
|
||||
return nil, fmt.Errorf("couldn't get box from context")
|
||||
}
|
||||
|
||||
return authHeaders, nil
|
||||
return data.AuthHeaders, nil
|
||||
}
|
||||
|
||||
// GetClientTime extracts time.Time from context.
|
||||
func GetClientTime(ctx context.Context) (time.Time, error) {
|
||||
clientTime, ok := ctx.Value(clientTimeKey).(time.Time)
|
||||
if !ok {
|
||||
data, ok := ctx.Value(boxKey).(*Box)
|
||||
if !ok || data == nil {
|
||||
return time.Time{}, fmt.Errorf("couldn't get box from context")
|
||||
}
|
||||
|
||||
if data.ClientTime.IsZero() {
|
||||
return time.Time{}, fmt.Errorf("couldn't get client time from context")
|
||||
}
|
||||
|
||||
return clientTime, nil
|
||||
return data.ClientTime, nil
|
||||
}
|
||||
|
||||
// SetBoxData sets accessbox.Box in the context.
|
||||
func SetBoxData(ctx context.Context, box *accessbox.Box) context.Context {
|
||||
return context.WithValue(ctx, boxDataKey, box)
|
||||
// GetAccessBoxAttrs extracts []object.Attribute from context.
|
||||
func GetAccessBoxAttrs(ctx context.Context) ([]object.Attribute, error) {
|
||||
data, ok := ctx.Value(boxKey).(*Box)
|
||||
if !ok || data == nil {
|
||||
return nil, fmt.Errorf("couldn't get box from context")
|
||||
}
|
||||
|
||||
return data.Attributes, nil
|
||||
}
|
||||
|
||||
// SetAuthHeaders sets auth.AuthHeader in the context.
|
||||
func SetAuthHeaders(ctx context.Context, header *AuthHeader) context.Context {
|
||||
return context.WithValue(ctx, authHeadersKey, header)
|
||||
}
|
||||
|
||||
// SetClientTime sets time.Time in the context.
|
||||
func SetClientTime(ctx context.Context, newTime time.Time) context.Context {
|
||||
return context.WithValue(ctx, clientTimeKey, newTime)
|
||||
// SetBox sets Box in the context.
|
||||
func SetBox(ctx context.Context, box *Box) context.Context {
|
||||
return context.WithValue(ctx, boxKey, box)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue