Skip to content

Commit

Permalink
feat: support codecommit URL translation (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ga-paul-t committed Sep 6, 2021
1 parent 948b296 commit 45e9f21
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 6 deletions.
20 changes: 20 additions & 0 deletions .uplift.yml
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# Copyright (c) 2021 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# in the Software without restriction, including without limitation the rights
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

firstVersion: v1.0.0
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Copyright (c) 2021 Gemba Advantage
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# in the Software without restriction, including without limitation the rights
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

BINDIR := $(CURDIR)/bin
BINNAME ?= codecommit-sign
BINVERSION := ''
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ scoop install codecommit-sign

## Quick Start

Retreive the HTTPS clone URL to your chosen CodeCommit repository, either through the AWS Console or the AWS CLI. And then sign it:
Retreive (_or construct_) the clone URL to your chosen CodeCommit repository and then sign it. Depending on your chosen authentication mechanism, you may need to provide an AWS named profile through the optional `--profile` flag.

### HTTPS

```sh
codecommit-sign https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/<REPOSITORY_NAME>
codecommit-sign https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/repository
```

Depending on your chosen authentication mechanism, you can provide an AWS named profile:
### GRC

```sh
codecommit-sign https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/<REPOSITORY_NAME> --profile <AWS_PROFILE>
codecommit-sign codecommit::eu-west-1://repository
```
29 changes: 27 additions & 2 deletions cmd/codecommitsign/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,31 @@ import (
"context"
"fmt"
"io"
"strings"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/gembaadvantage/codecommit-sign/pkg/awsv4"
"github.com/gembaadvantage/codecommit-sign/pkg/translate"
"github.com/spf13/cobra"
)

const (
desc = ``
exs = ``
desc = `Generate an AWS authenticated V4 signed CodeCommit URL that can be used to fetch
and push changes to a CodeCommit repository within an AWS account.
Both HTTPS and (git-remote-codecommit) GRC URL formats are supported. If a GRC
URL is provided, it will automatically be translated into a compatible HTTPS
URL`

exs = `Sign a CodeCommit HTTPS URL:
$ codecommit-sign https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/repository
https://<USERNAME>:<PASSWORD>@git-codecommit.eu-west-1.amazonaws.com/v1/repos/repository
Sign a CodeCommit GRC URL:
$ codecommit-sign codecommit::eu-west-1://repository
https://<USERNAME>:<PASSWORD>@git-codecommit.eu-west-1.amazonaws.com/v1/repos/repository`
)

type signOptions struct {
Expand Down Expand Up @@ -85,6 +101,15 @@ func (o signOptions) Run(out io.Writer) error {
}

signer := awsv4.NewSigner(creds)
// Detect if a GRC URL has been provided and translate
if strings.HasPrefix(o.CloneURL, "codecommit::") {
var terr error
o.CloneURL, terr = translate.FromGrc(o.CloneURL)
if terr != nil {
return terr
}
}

surl, err := signer.Sign(o.CloneURL)
if err != nil {
return err
Expand Down
66 changes: 66 additions & 0 deletions pkg/translate/grc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright (c) 2021 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package translate

import (
"errors"
"fmt"
"regexp"
"strings"
)

var (
urlRgx = regexp.MustCompile(`^https://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`)
grcRgx = regexp.MustCompile(`^codecommit::(.+)://(.+)$`)
)

// ToGrc translates a CodeCommit HTTPS URL to a compatible CodeCommit (git-remote-codecommit)
// GRC based URL that can be used to fetch and push changes to a CodeCommit repository
func ToGrc(url string) (string, error) {
m := urlRgx.FindStringSubmatch(url)
if len(m) < 3 {
return "", errors.New("malformed codecommit HTTPS URL")
}

region := m[1]
repo := m[len(m)-1]

return fmt.Sprintf("codecommit::%s://%s", region, repo), nil
}

// FromGrc translates a CodeCommit (git-remote-codecommit) GRC URL to a compatible HTTPS URL
// that can be used to fetch and push changes to a CodeCommit repository
func FromGrc(url string) (string, error) {
m := grcRgx.FindStringSubmatch(url)
if len(m) < 3 {
return "", errors.New("malformed codecommit grc URL")
}

region := m[1]
repo := m[len(m)-1]
if strings.Contains(repo, "@") {
repo = strings.Split(repo, "@")[1]
}

return fmt.Sprintf("https://git-codecommit.%s.amazonaws.com/v1/repos/%s", region, repo), nil
}
116 changes: 116 additions & 0 deletions pkg/translate/grc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright (c) 2021 Gemba Advantage
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package translate

import (
"testing"
)

func TestToGrc(t *testing.T) {
tests := []struct {
name string
url string
expected string
err string
}{
{
name: "Valid",
url: "https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/repository",
expected: "codecommit::eu-west-1://repository",
},
{
name: "NoRegion",
url: "https://git-codecommit..amazonaws.com/v1/repos/repository",
expected: "",
err: "malformed codecommit HTTPS URL",
},
{
name: "NoRepositoryName",
url: "https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/",
expected: "",
err: "malformed codecommit HTTPS URL",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := ToGrc(tt.url)

if err != nil {
if err.Error() != tt.err {
t.Errorf("expected %s but received %s\n", tt.err, err)
}
}

if actual != tt.expected {
t.Errorf("expected %s but received %s\n", tt.expected, actual)
}
})
}
}

func TestFromGrc(t *testing.T) {
tests := []struct {
name string
url string
expected string
err string
}{
{
name: "NoNamedProfile",
url: "codecommit::eu-west-1://repository",
expected: "https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/repository",
},
{
name: "IgnoresNamedProfile",
url: "codecommit::eu-west-1://profile@repository",
expected: "https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/repository",
},
{
name: "NoRegion",
url: "codecommit::://repository",
expected: "",
err: "malformed codecommit grc URL",
},
{
name: "NoRepositoryName",
url: "codecommit::eu-west-1://",
expected: "",
err: "malformed codecommit grc URL",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, err := FromGrc(tt.url)

if err != nil {
if err.Error() != tt.err {
t.Errorf("expected %s but received %s\n", tt.err, err)
}
}

if actual != tt.expected {
t.Errorf("expected %s but received %s\n", tt.expected, actual)
}
})
}
}

0 comments on commit 45e9f21

Please sign in to comment.