-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: remove create object & bucket approval #225
Changes from 7 commits
6e08609
7cb2c86
fd9a23c
a4f5966
16cb540
7b6c1c0
fb653d4
dc98349
8068347
191a9ab
24c5e95
29a4932
06dde86
4ce7454
7cc0777
08d95d6
99a44ad
31c3420
2679dcb
c8e9704
923837b
d21756b
7d74a14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"fmt" | ||
"io" | ||
"math" | ||
"math/rand" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
|
@@ -32,7 +33,6 @@ import ( | |
// IBucketClient interface defines functions related to bucket. | ||
// The concept of "bucket" is the same as the concept of a bucket in AWS S3 storage. | ||
type IBucketClient interface { | ||
GetCreateBucketApproval(ctx context.Context, createBucketMsg *storageTypes.MsgCreateBucket) (*storageTypes.MsgCreateBucket, error) | ||
CreateBucket(ctx context.Context, bucketName string, primaryAddr string, opts types.CreateBucketOptions) (string, error) | ||
DeleteBucket(ctx context.Context, bucketName string, opt types.DeleteBucketOption) (string, error) | ||
UpdateBucketVisibility(ctx context.Context, bucketName string, visibility storageTypes.VisibilityType, opt types.UpdateVisibilityOption) (string, error) | ||
|
@@ -57,66 +57,6 @@ type IBucketClient interface { | |
ListBucketsByPaymentAccount(ctx context.Context, paymentAccount string, opts types.ListBucketsByPaymentAccountOptions) (types.ListBucketsByPaymentAccountResult, error) | ||
} | ||
|
||
// GetCreateBucketApproval - Send create bucket approval request to SP and returns the signature info for the approval of preCreating resources. | ||
// | ||
// - ctx: Context variables for the current API call. | ||
// | ||
// - createBucketMsg: The msg of create bucket which defined by the greenfield chain. | ||
// | ||
// - ret1: The msg of create bucket which contain the approval signature from the storage provider | ||
// | ||
// - ret2: Return error when get approval failed, otherwise return nil. | ||
func (c *Client) GetCreateBucketApproval(ctx context.Context, createBucketMsg *storageTypes.MsgCreateBucket) (*storageTypes.MsgCreateBucket, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need to remove those old "getApproval" methods because we need make sure the change is backward compatible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an exported method. Clients code might have already used it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should not delete this part for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
unsignedBytes := createBucketMsg.GetSignBytes() | ||
|
||
// set the action type | ||
urlVal := make(url.Values) | ||
urlVal["action"] = []string{types.CreateBucketAction} | ||
|
||
reqMeta := requestMeta{ | ||
urlValues: urlVal, | ||
urlRelPath: "get-approval", | ||
contentSHA256: types.EmptyStringSHA256, | ||
txnMsg: hex.EncodeToString(unsignedBytes), | ||
} | ||
|
||
sendOpt := sendOptions{ | ||
method: http.MethodGet, | ||
adminInfo: AdminAPIInfo{ | ||
isAdminAPI: true, | ||
adminVersion: types.AdminV1Version, | ||
}, | ||
} | ||
|
||
primarySPAddr := createBucketMsg.GetPrimarySpAddress() | ||
endpoint, err := c.getSPUrlByAddr(primarySPAddr) | ||
if err != nil { | ||
log.Error().Msg(fmt.Sprintf("route endpoint by addr: %s failed, err: %s", primarySPAddr, err.Error())) | ||
return nil, err | ||
} | ||
|
||
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// fetch primary signed msg from sp response | ||
signedRawMsg := resp.Header.Get(types.HTTPHeaderSignedMsg) | ||
if signedRawMsg == "" { | ||
return nil, errors.New("fail to fetch pre createBucket signature") | ||
} | ||
|
||
signedMsgBytes, err := hex.DecodeString(signedRawMsg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var signedMsg storageTypes.MsgCreateBucket | ||
storageTypes.ModuleCdc.MustUnmarshalJSON(signedMsgBytes, &signedMsg) | ||
|
||
return &signedMsg, nil | ||
} | ||
|
||
// CreateBucket - Create a new bucket in greenfield. | ||
// | ||
// This API sends a request to the storage provider to get approval for creating bucket and sends the createBucket transaction to the Greenfield. | ||
|
@@ -159,17 +99,30 @@ func (c *Client) CreateBucket(ctx context.Context, bucketName string, primaryAdd | |
if err != nil { | ||
return "", err | ||
} | ||
signedMsg, err := c.GetCreateBucketApproval(ctx, createBucketMsg) | ||
|
||
accAddress, err := sdk.AccAddressFromHexUnsafe(primaryAddr) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
sp, err := c.GetStorageProviderInfo(ctx, accAddress) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
families, err := c.QuerySpAvailableGlobalVirtualGroupFamilies(ctx, sp.Id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'd better use the optimal API to get one VGF here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
createBucketMsg.PrimarySpApproval.GlobalVirtualGroupFamilyId = families[rand.Intn(len(families))] | ||
|
||
// set the default txn broadcast mode as block mode | ||
if opts.TxOpts == nil { | ||
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC | ||
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode} | ||
} | ||
msgs := []sdk.Msg{signedMsg} | ||
msgs := []sdk.Msg{createBucketMsg} | ||
|
||
if opts.Tags != nil { | ||
// Set tag | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"encoding/xml" | ||
"errors" | ||
"fmt" | ||
|
@@ -34,7 +33,6 @@ import ( | |
// IObjectClient interface defines functions related to object operations. | ||
// The concept of "object" is the same as the concept of the object in AWS S3 storage. | ||
type IObjectClient interface { | ||
GetCreateObjectApproval(ctx context.Context, createObjectMsg *storageTypes.MsgCreateObject) (*storageTypes.MsgCreateObject, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to delete this API for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
CreateObject(ctx context.Context, bucketName, objectName string, reader io.Reader, opts types.CreateObjectOptions) (string, error) | ||
UpdateObjectContent(ctx context.Context, bucketName, objectName string, reader io.Reader, opts types.UpdateObjectOptions) (string, error) | ||
PutObject(ctx context.Context, bucketName, objectName string, objectSize int64, reader io.Reader, opts types.PutObjectOptions) error | ||
|
@@ -116,6 +114,19 @@ func (c *Client) CreateObject(ctx context.Context, bucketName, objectName string | |
return "", err | ||
} | ||
|
||
// This code block checks for unsupported or potentially risky formats in object names. | ||
// The checks are essential for ensuring the security and compatibility of the object names within the system. | ||
// 1. ".." in object names: Checked to prevent path traversal attacks which might access directories outside the intended scope. | ||
// 2. Object name being "/": The root directory should not be used as an object name due to potential security risks and ambiguity. | ||
// 3. "\\" in object names: Backslashes are checked because they are often not supported in UNIX-like file systems and can cause issues in path parsing. | ||
// 4. SQL Injection patterns in object names: Ensures that the object name does not contain patterns that could be used for SQL injection attacks, maintaining the integrity of the database. | ||
if strings.Contains(objectName, "..") || | ||
objectName == "/" || | ||
strings.Contains(objectName, "\\") || | ||
utils.IsSQLInjection(objectName) { | ||
return "", fmt.Errorf("fail to check object name:%s", objectName) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you wrap these format check codes into a single function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also move the object name rules into the CheckObjectName function, not comment here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CheckObjectName already has the same comments, so i just delete it |
||
|
||
// compute hash root of payload | ||
expectCheckSums, size, redundancyType, err := c.ComputeHashRoots(reader, opts.IsSerialComputeMode) | ||
if err != nil { | ||
|
@@ -144,17 +155,12 @@ func (c *Client) CreateObject(ctx context.Context, bucketName, objectName string | |
return "", err | ||
} | ||
|
||
signedCreateObjectMsg, err := c.GetCreateObjectApproval(ctx, createObjectMsg) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// set the default txn broadcast mode as block mode | ||
if opts.TxOpts == nil { | ||
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC | ||
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode} | ||
} | ||
msgs := []sdk.Msg{signedCreateObjectMsg} | ||
msgs := []sdk.Msg{createObjectMsg} | ||
|
||
if opts.Tags != nil { | ||
// Set tag | ||
|
@@ -1031,59 +1037,6 @@ func (c *Client) ListObjects(ctx context.Context, bucketName string, opts types. | |
return listObjectsResult, nil | ||
} | ||
|
||
// GetCreateObjectApproval returns the signature info for the approval of preCreating resources | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // Deprecated: GetCreateObjectApproval returns the signature info for the approval of preCreating resources No need to delete this API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean change the comment to other than delete this line: // Deprecated: GetCreateObjectApproval returns the signature info for the approval of preCreating resources There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the same to GetCreateBucketApproval API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
func (c *Client) GetCreateObjectApproval(ctx context.Context, createObjectMsg *storageTypes.MsgCreateObject) (*storageTypes.MsgCreateObject, error) { | ||
unsignedBytes := createObjectMsg.GetSignBytes() | ||
|
||
// set the action type | ||
urlValues := url.Values{ | ||
"action": {types.CreateObjectAction}, | ||
} | ||
|
||
reqMeta := requestMeta{ | ||
urlValues: urlValues, | ||
urlRelPath: "get-approval", | ||
contentSHA256: types.EmptyStringSHA256, | ||
txnMsg: hex.EncodeToString(unsignedBytes), | ||
} | ||
|
||
sendOpt := sendOptions{ | ||
method: http.MethodGet, | ||
adminInfo: AdminAPIInfo{ | ||
isAdminAPI: true, | ||
adminVersion: types.AdminV1Version, | ||
}, | ||
} | ||
|
||
bucketName := createObjectMsg.BucketName | ||
endpoint, err := c.getSPUrlByBucket(bucketName) | ||
if err != nil { | ||
log.Error().Msg(fmt.Sprintf("route endpoint by bucket: %s failed, err: %s", bucketName, err.Error())) | ||
return nil, err | ||
} | ||
|
||
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// fetch primary signed msg from sp response | ||
signedRawMsg := resp.Header.Get(types.HTTPHeaderSignedMsg) | ||
if signedRawMsg == "" { | ||
return nil, errors.New("fail to fetch pre createObject signature") | ||
} | ||
|
||
signedMsgBytes, err := hex.DecodeString(signedRawMsg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var signedMsg storageTypes.MsgCreateObject | ||
storageTypes.ModuleCdc.MustUnmarshalJSON(signedMsgBytes, &signedMsg) | ||
|
||
return &signedMsg, nil | ||
} | ||
|
||
// CreateFolder send create empty object txn to greenfield chain | ||
func (c *Client) CreateFolder(ctx context.Context, bucketName, objectName string, opts types.CreateObjectOptions) (string, error) { | ||
if !strings.HasSuffix(objectName, "/") { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it better to clarify what kind of nonce here the SDK need to get?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As SP off-chain-auth also did have a nonce concept, it would better to explain what nonce is it .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's just redundant function for local test, i will delete it