mirror of
https://github.com/versity/versitygw.git
synced 2026-09-19 22:44:28 +00:00
test: fix test w/curl error, bucket creation/location tests
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type CreateBucketCommandXML struct {
|
||||
XMLName xml.Name `xml:"CreateBucketConfiguration"`
|
||||
XMLNamespace string `xml:"xmlns,attr"`
|
||||
LocationConstraint string `xml:"LocationConstraint"`
|
||||
}
|
||||
|
||||
type CreateBucketCommand struct {
|
||||
*S3Command
|
||||
Config *CreateBucketCommandXML
|
||||
}
|
||||
|
||||
func NewCreateBucketCommand(s3Command *S3Command, locationConstraint string, constraintSet bool) (*CreateBucketCommand, error) {
|
||||
if s3Command.BucketName == "" {
|
||||
return nil, errors.New("CreateBucket must have bucket name")
|
||||
}
|
||||
s3Command.Method = "PUT"
|
||||
s3Command.Query = ""
|
||||
var config *CreateBucketCommandXML = nil
|
||||
if constraintSet {
|
||||
config = &CreateBucketCommandXML{
|
||||
XMLNamespace: "http://s3.amazonaws.com/doc/2006-03-01/",
|
||||
LocationConstraint: locationConstraint,
|
||||
}
|
||||
}
|
||||
command := &CreateBucketCommand{
|
||||
S3Command: s3Command,
|
||||
Config: config,
|
||||
}
|
||||
if constraintSet {
|
||||
xmlData, err := xml.Marshal(command.Config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error marshalling XML: %w", err)
|
||||
}
|
||||
command.Payload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + string(xmlData)
|
||||
}
|
||||
return command, nil
|
||||
}
|
||||
@@ -38,11 +38,15 @@ func GetBase64ChecksumLength(checksumType string) (int64, error) {
|
||||
|
||||
func (p *Payload) GetDataSize() (int64, error) {
|
||||
if !p.dataSizeCalculated {
|
||||
dataSize, err := p.dataSource.SourceDataByteSize()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("error getting payload data size: %w", err)
|
||||
if p.dataSource != nil {
|
||||
dataSize, err := p.dataSource.SourceDataByteSize()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("error getting payload data size: %w", err)
|
||||
}
|
||||
p.dataSize = dataSize
|
||||
} else {
|
||||
p.dataSize = 0
|
||||
}
|
||||
p.dataSize = dataSize
|
||||
p.dataSizeCalculated = true
|
||||
}
|
||||
return p.dataSize, nil
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
logger "github.com/versity/versitygw/tests/rest_scripts/logger"
|
||||
"os"
|
||||
@@ -112,9 +111,6 @@ type S3Command struct {
|
||||
}
|
||||
|
||||
func (s *S3Command) OpenSSLCommand() error {
|
||||
if s.FilePath == "" {
|
||||
return errors.New("for openssl command, filePath must be set")
|
||||
}
|
||||
if err := s.prepareForBuild(); err != nil {
|
||||
return fmt.Errorf("error preparing for command building: %w", err)
|
||||
}
|
||||
@@ -162,9 +158,6 @@ func (s *S3Command) prepareForBuild() error {
|
||||
}
|
||||
|
||||
func (s *S3Command) preparePayload() error {
|
||||
if s.PayloadFile != "" && s.Payload != "" {
|
||||
return fmt.Errorf("cannot have both payload and payloadFile parameters set")
|
||||
}
|
||||
if s.PayloadFile != "" {
|
||||
s.dataSource = NewFileDataSource(s.PayloadFile)
|
||||
} else if s.Payload != "" {
|
||||
@@ -198,6 +191,8 @@ func (s *S3Command) initializeOpenSSLPayloadAndGetContentLength() error {
|
||||
streamingUnsignedPayloadTrailerImpl := NewStreamingUnsignedPayloadWithTrailer(s.dataSource, int64(s.ChunkSize), s.ChecksumType)
|
||||
streamingUnsignedPayloadTrailerImpl.OmitTrailerOrKey(s.OmitPayloadTrailer, s.OmitPayloadTrailerKey)
|
||||
s.payloadOpenSSL = streamingUnsignedPayloadTrailerImpl
|
||||
case UnsignedPayload, "":
|
||||
s.payloadOpenSSL = NewWholePayload(s.dataSource)
|
||||
default:
|
||||
return fmt.Errorf("unsupported OpenSSL payload type: '%s'", s.PayloadType)
|
||||
}
|
||||
@@ -407,6 +402,9 @@ func (s *S3Command) buildOpenSSLCommand() error {
|
||||
if _, err = file.Write(openSSLCommandBytes); err != nil {
|
||||
return fmt.Errorf("error writing to file: %w", err)
|
||||
}
|
||||
if _, err := file.Write([]byte{'\r', '\n', '\r', '\n'}); err != nil {
|
||||
return fmt.Errorf("error writing to file: %w", err)
|
||||
}
|
||||
if s.PayloadFile != "" || s.Payload != "" {
|
||||
if err = s.writeOpenSSLPayload(file); err != nil {
|
||||
return fmt.Errorf("error writing openssl payload: %w", err)
|
||||
@@ -416,9 +414,6 @@ func (s *S3Command) buildOpenSSLCommand() error {
|
||||
}
|
||||
|
||||
func (s *S3Command) writeOpenSSLPayload(file *os.File) error {
|
||||
if _, err := file.Write([]byte{'\r', '\n', '\r', '\n'}); err != nil {
|
||||
return fmt.Errorf("error writing to file: %w", err)
|
||||
}
|
||||
if awsPayload, ok := s.payloadOpenSSL.(*PayloadStreamingAWS4HMACSHA256); ok {
|
||||
awsPayload.AddInitialSignatureAndSigningKey(s.signature, s.signingKey)
|
||||
}
|
||||
|
||||
@@ -30,6 +30,9 @@ func (w *WholePayload) GetContentLength() (int64, error) {
|
||||
}
|
||||
|
||||
func (w *WholePayload) WritePayload(filePath string) error {
|
||||
if w.dataSource == nil {
|
||||
return nil
|
||||
}
|
||||
sourceFile, err := w.dataSource.GetReader()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating tee reader: %w", err)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
package config
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
CreateBucket = "createBucket"
|
||||
PutBucketTagging = "putBucketTagging"
|
||||
PutObject = "putObject"
|
||||
)
|
||||
@@ -57,6 +58,9 @@ var omitPayloadTrailer *bool
|
||||
var omitPayloadTrailerKey *bool
|
||||
var omitContentLength *bool
|
||||
|
||||
var locationConstraint *string
|
||||
var locationConstraintSet bool = false
|
||||
|
||||
type restParams map[string]string
|
||||
|
||||
func (r *restParams) String() string {
|
||||
@@ -90,6 +94,10 @@ func main() {
|
||||
log.Fatalf("Error checking flags: %v", err)
|
||||
}
|
||||
|
||||
if err := validateConfig(); err != nil {
|
||||
log.Fatalf("Error validating config: %v", err)
|
||||
}
|
||||
|
||||
baseCommand := &command.S3Command{
|
||||
Method: *method,
|
||||
Url: *url,
|
||||
@@ -137,6 +145,10 @@ func getS3CommandType(baseCommand *command.S3Command) (command.S3CommandConverte
|
||||
var s3Command command.S3CommandConverter
|
||||
var err error
|
||||
switch *commandType {
|
||||
case CreateBucket:
|
||||
if s3Command, err = command.NewCreateBucketCommand(baseCommand, *locationConstraint, locationConstraintSet); err != nil {
|
||||
return nil, fmt.Errorf("error setting up CreateBucket command: %v", err)
|
||||
}
|
||||
case PutBucketTagging:
|
||||
fields := command.PutBucketTaggingFields{
|
||||
TagCount: *tagCount,
|
||||
@@ -148,7 +160,7 @@ func getS3CommandType(baseCommand *command.S3Command) (command.S3CommandConverte
|
||||
}
|
||||
case PutObject:
|
||||
if s3Command, err = command.NewPutObjectCommand(baseCommand); err != nil {
|
||||
return nil, fmt.Errorf("error setting up PutBucketTagging command: %v", err)
|
||||
return nil, fmt.Errorf("error setting up PutObject command: %v", err)
|
||||
}
|
||||
default:
|
||||
s3Command = baseCommand
|
||||
@@ -211,6 +223,7 @@ func checkFlags() error {
|
||||
omitContentLength = flag.Bool("omitContentLength", false, "Omit content length parameter")
|
||||
flag.Var(&tagKeys, "tagKey", "Tag key (can add multiple)")
|
||||
flag.Var(&tagValues, "tagValue", "Tag value (can add multiple)")
|
||||
locationConstraint = flag.String("locationConstraint", "", "Location constraint for bucket creation")
|
||||
// Parse the flags
|
||||
flag.Parse()
|
||||
|
||||
@@ -218,13 +231,35 @@ func checkFlags() error {
|
||||
if f.Name == "customHostParam" {
|
||||
customHostParamSet = true
|
||||
}
|
||||
if f.Name == "locationConstraint" {
|
||||
locationConstraintSet = true
|
||||
}
|
||||
})
|
||||
|
||||
if flag.Lookup("awsAccessKeyId").Value.String() == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateConfig() error {
|
||||
if *awsAccessKeyId == "" {
|
||||
return fmt.Errorf("the 'awsAccessKeyId' value must be set")
|
||||
}
|
||||
if flag.Lookup("awsSecretAccessKey").Value.String() == "" {
|
||||
if *awsSecretAccessKey == "" {
|
||||
return fmt.Errorf("the 'awsSecretAccessKey' value must be set")
|
||||
}
|
||||
if *payloadFile != "" && *payload != "" {
|
||||
return fmt.Errorf("cannot have both payload and payloadFile parameters set")
|
||||
}
|
||||
if *client == command.OPENSSL {
|
||||
if *filePath == "" {
|
||||
return fmt.Errorf("for OpenSSL commands, file path must be set")
|
||||
}
|
||||
} else {
|
||||
if *chunkSize != 0 || *omitPayloadTrailerKey || *omitPayloadTrailer {
|
||||
return fmt.Errorf("use of one or more params only suppored for OpenSSL commands")
|
||||
}
|
||||
}
|
||||
if *client == command.CURL && *filePath != "" {
|
||||
return fmt.Errorf("writing to file not currently supported for curl commands")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user