Skip to content

Commit

Permalink
check s3URL scheme upon AWS ObjectStore Init()
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Parfitt <[email protected]>
  • Loading branch information
bookshelfdave committed Jul 24, 2018
1 parent b0af81e commit 06d6665
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/cloudprovider/aws/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package aws

import (
"context"
"net/url"

"github.com/pkg/errors"

Expand Down Expand Up @@ -51,3 +52,16 @@ func GetBucketRegion(bucket string) (string, error) {

return "", errors.New("unable to determine bucket's region")
}

// IsValidS3URLScheme returns true if the scheme is http:// or https://
// and the url parses correctly, otherwise, return false
func IsValidS3URLScheme(s3URL string) bool {
u, err := url.Parse(s3URL)
if err != nil {
return false
}
if u.Scheme != "http" && u.Scheme != "https" {
return false
}
return true
}
29 changes: 29 additions & 0 deletions pkg/cloudprovider/aws/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2018 the Heptio Ark contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package aws

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestS3URL(t *testing.T) {
assert.True(t, IsValidS3URLScheme("http://foo"))
assert.True(t, IsValidS3URLScheme("https://foo"))
assert.False(t, IsValidS3URLScheme("httpd://foo"))
assert.False(t, IsValidS3URLScheme(""))
}
4 changes: 4 additions & 0 deletions pkg/cloudprovider/aws/object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (o *objectStore) Init(config map[string]string) error {
WithS3ForcePathStyle(s3ForcePathStyle)

if s3URL != "" {
if !IsValidS3URLScheme(s3URL) {
return errors.Errorf("Invalid s3Url: %s", s3URL)
}

awsConfig = awsConfig.WithEndpointResolver(
endpoints.ResolverFunc(func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
if service == endpoints.S3ServiceID {
Expand Down

0 comments on commit 06d6665

Please sign in to comment.