check s3URL scheme upon AWS ObjectStore Init()

Signed-off-by: Dave Parfitt <diparfitt@gmail.com>
pull/697/head
Dave Parfitt 2018-07-23 16:08:23 -04:00
parent b0af81e780
commit 06d6665abb
3 changed files with 47 additions and 0 deletions

View File

@ -18,6 +18,7 @@ package aws
import (
"context"
"net/url"
"github.com/pkg/errors"
@ -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
}

View File

@ -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(""))
}

View File

@ -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 {