Skip to content

Commit

Permalink
Remove trailing slashes from S3 endpoint (#5987)
Browse files Browse the repository at this point in the history
* Remove trailing slashes from S3 endpoint

* Test for the restic arg cleaning

* Reorder imports

* Apply Pavan's suggestion
  • Loading branch information
Hakan Memisoglu authored and Ilya Kislenko committed Jul 15, 2019
1 parent 2f21fcb commit b00b779
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
Expand Down Expand Up @@ -131,6 +132,10 @@ func resticS3Args(profile *param.Profile, repository string) []string {
if profile.Location.Endpoint != "" {
s3Endpoint = profile.Location.Endpoint
}
if strings.HasSuffix(s3Endpoint, "/") {
log.Debugln("Removing trailing slashes from the endpoint")
s3Endpoint = strings.TrimRight(s3Endpoint, "/")
}
return []string{
fmt.Sprintf("export %s=%s\n", location.AWSAccessKeyID, profile.Credential.KeyPair.ID),
fmt.Sprintf("export %s=%s\n", location.AWSSecretAccessKey, profile.Credential.KeyPair.Secret),
Expand Down
87 changes: 87 additions & 0 deletions pkg/restic/restic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"testing"

. "gopkg.in/check.v1"

"github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/param"
)

type ResticDataSuite struct{}
Expand Down Expand Up @@ -43,3 +46,87 @@ func (s *ResticDataSuite) TestGetSnapshotID(c *C) {
c.Check(id, Equals, tc.expected, Commentf("Failed for log: %s", tc.log))
}
}

func (s *ResticDataSuite) TestResticArgs(c *C) {
for _, tc := range []struct {
profile *param.Profile
repo string
password string
expected []string
}{
{
profile: &param.Profile{
Location: v1alpha1.Location{
Type: v1alpha1.LocationTypeS3Compliant,
Endpoint: "endpoint",
},
Credential: param.Credential{
Type: param.CredentialTypeKeyPair,
KeyPair: &param.KeyPair{
ID: "id",
Secret: "secret",
},
},
},
repo: "repo",
password: "my-secret",
expected: []string{
"export AWS_ACCESS_KEY_ID=id\n",
"export AWS_SECRET_ACCESS_KEY=secret\n",
"export RESTIC_REPOSITORY=s3:endpoint/repo\n",
"export RESTIC_PASSWORD=my-secret\n",
"restic",
},
},
{
profile: &param.Profile{
Location: v1alpha1.Location{
Type: v1alpha1.LocationTypeS3Compliant,
Endpoint: "endpoint/", // Remove trailing slash
},
Credential: param.Credential{
Type: param.CredentialTypeKeyPair,
KeyPair: &param.KeyPair{
ID: "id",
Secret: "secret",
},
},
},
repo: "repo",
password: "my-secret",
expected: []string{
"export AWS_ACCESS_KEY_ID=id\n",
"export AWS_SECRET_ACCESS_KEY=secret\n",
"export RESTIC_REPOSITORY=s3:endpoint/repo\n",
"export RESTIC_PASSWORD=my-secret\n",
"restic",
},
},
{
profile: &param.Profile{
Location: v1alpha1.Location{
Type: v1alpha1.LocationTypeS3Compliant,
Endpoint: "endpoint/////////", // Also remove all of the trailing slashes
},
Credential: param.Credential{
Type: param.CredentialTypeKeyPair,
KeyPair: &param.KeyPair{
ID: "id",
Secret: "secret",
},
},
},
repo: "repo",
password: "my-secret",
expected: []string{
"export AWS_ACCESS_KEY_ID=id\n",
"export AWS_SECRET_ACCESS_KEY=secret\n",
"export RESTIC_REPOSITORY=s3:endpoint/repo\n",
"export RESTIC_PASSWORD=my-secret\n",
"restic",
},
},
} {
c.Assert(resticArgs(tc.profile, tc.repo, tc.password), DeepEquals, tc.expected)
}
}

0 comments on commit b00b779

Please sign in to comment.