github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/s3/s3_plugin.go (about)

     1  package s3
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  
     7  	"github.com/evergreen-ci/evergreen/plugin"
     8  	"github.com/evergreen-ci/evergreen/util"
     9  	"github.com/goamz/goamz/s3"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func init() {
    14  	plugin.Publish(&S3Plugin{})
    15  }
    16  
    17  // S3Plugin handles uploading and downloading from Amazon's S3 service
    18  type S3Plugin struct {
    19  }
    20  
    21  const (
    22  	S3GetCmd     = "get"
    23  	S3PutCmd     = "put"
    24  	S3PluginName = "s3"
    25  )
    26  
    27  var (
    28  	// Regular expression for validating S3 bucket names
    29  	BucketNameRegex = regexp.MustCompile(`^[A-Za-z0-9_\-.]+$`)
    30  )
    31  
    32  // Name returns the name of the plugin. Fulfills Plugin interface.
    33  func (self *S3Plugin) Name() string {
    34  	return S3PluginName
    35  }
    36  
    37  // NewCommand returns commands of the given name.
    38  // Fulfills Plugin interface.
    39  func (self *S3Plugin) NewCommand(cmdName string) (plugin.Command, error) {
    40  	if cmdName == S3PutCmd {
    41  		return &S3PutCommand{}, nil
    42  	}
    43  	if cmdName == S3GetCmd {
    44  		return &S3GetCommand{}, nil
    45  	}
    46  	return nil, errors.Errorf("No such command: %v", cmdName)
    47  }
    48  
    49  func validateS3BucketName(bucket string) error {
    50  	// if it's an expandable string, we can't expand yet since we don't have
    51  	// access to the task config expansions. So, we defer till during runtime
    52  	// to do the validation
    53  	if plugin.IsExpandable(bucket) {
    54  		return nil
    55  	}
    56  	if len(bucket) < 3 {
    57  		return errors.New("must be at least 3 characters")
    58  	}
    59  	if len(bucket) > 63 {
    60  		return errors.New("must be no more than 63 characters")
    61  	}
    62  	if strings.HasPrefix(bucket, ".") || strings.HasPrefix(bucket, "-") {
    63  		return errors.New("must not begin with a period or hyphen")
    64  	}
    65  	if strings.HasSuffix(bucket, ".") || strings.HasSuffix(bucket, "-") {
    66  		return errors.New("must not end with a period or hyphen")
    67  	}
    68  	if strings.Contains(bucket, "..") {
    69  		return errors.New("must not have two consecutive periods")
    70  	}
    71  	if !BucketNameRegex.MatchString(bucket) {
    72  		return errors.New("must contain only combinations of uppercase/lowercase " +
    73  			"letters, numbers, hyphens, underscores and periods")
    74  	}
    75  	return nil
    76  }
    77  
    78  func validS3Permissions(perm string) bool {
    79  	return util.SliceContains(
    80  		[]s3.ACL{
    81  			s3.Private,
    82  			s3.PublicRead,
    83  			s3.PublicReadWrite,
    84  			s3.AuthenticatedRead,
    85  			s3.BucketOwnerRead,
    86  			s3.BucketOwnerFull,
    87  		},
    88  		s3.ACL(perm),
    89  	)
    90  }