|
| 1 | +/* |
| 2 | +Copyright (c) 2021 Gemba Advantage |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "io" |
| 28 | + "os" |
| 29 | + "os/exec" |
| 30 | + "regexp" |
| 31 | + "strings" |
| 32 | + |
| 33 | + "github.com/aws/aws-sdk-go-v2/config" |
| 34 | + "github.com/gembaadvantage/codecommit-sign/pkg/awsv4" |
| 35 | + "github.com/gembaadvantage/codecommit-sign/pkg/translate" |
| 36 | + "github.com/spf13/cobra" |
| 37 | +) |
| 38 | + |
| 39 | +var ( |
| 40 | + grcRgx = regexp.MustCompile(`^codecommit::(.+)://(.+)$`) |
| 41 | +) |
| 42 | + |
| 43 | +type gitOptions struct { |
| 44 | + Cmd string |
| 45 | + RemoteURL string |
| 46 | +} |
| 47 | + |
| 48 | +func newRootCmd(out io.Writer, args []string) *cobra.Command { |
| 49 | + opts := gitOptions{} |
| 50 | + |
| 51 | + cmd := &cobra.Command{ |
| 52 | + Use: "git-remote-codecommit", |
| 53 | + Short: "TODO", |
| 54 | + Args: cobra.ExactArgs(2), |
| 55 | + SilenceUsage: true, |
| 56 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 57 | + opts.Cmd = args[0] |
| 58 | + |
| 59 | + // Prefix protocol, as it will have been stipped off by the git client |
| 60 | + opts.RemoteURL = "codecommit::" + args[1] |
| 61 | + |
| 62 | + return opts.Run(out) |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + cmd.AddCommand(newVersionCmd(out)) |
| 67 | + return cmd |
| 68 | +} |
| 69 | + |
| 70 | +func (o gitOptions) Run(out io.Writer) error { |
| 71 | + // Load named profile if one has been provided |
| 72 | + opts := []func(*config.LoadOptions) error{} |
| 73 | + if profile := identifyProfile(o.RemoteURL); profile != "" { |
| 74 | + opts = append(opts, config.WithSharedConfigProfile(profile)) |
| 75 | + } |
| 76 | + |
| 77 | + cfg, err := config.LoadDefaultConfig(context.TODO(), opts...) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + creds, err := cfg.Credentials.Retrieve(context.TODO()) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + // Translate and then sign the RemoteURL |
| 88 | + grcURL, err := translate.FromGrc(o.RemoteURL) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + signer := awsv4.NewSigner(creds) |
| 94 | + url, err := signer.Sign(grcURL) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + cmd := exec.Command("git", "remote-http", o.Cmd, url) |
| 100 | + cmd.Stdin = os.Stdin |
| 101 | + cmd.Stderr = os.Stderr |
| 102 | + cmd.Stdout = os.Stdout |
| 103 | + return cmd.Run() |
| 104 | +} |
| 105 | + |
| 106 | +func identifyProfile(url string) string { |
| 107 | + m := grcRgx.FindStringSubmatch(url) |
| 108 | + if len(m) < 3 { |
| 109 | + return "" |
| 110 | + } |
| 111 | + |
| 112 | + // A GRC url will contain an optional profile with a trailing @ |
| 113 | + profile := m[len(m)-1] |
| 114 | + if strings.Contains(profile, "@") { |
| 115 | + return strings.Split(profile, "@")[0] |
| 116 | + } |
| 117 | + |
| 118 | + return "" |
| 119 | +} |
0 commit comments