Skip to content

Commit

Permalink
[BUGFIX] - HTTP Authentication for Private Repositories
Browse files Browse the repository at this point in the history
No matter what i try the goexec for git config refuses to work; something isn't getting parsed correctly in the exec. For the life of me I can't workout what
  • Loading branch information
gambol99 committed May 17, 2023
1 parent 6d3bbaf commit 9d8d8b0
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 20 deletions.
54 changes: 34 additions & 20 deletions cmd/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ import (
"github.com/spf13/cobra"

"github.com/appvia/terranetes-controller/pkg/utils"
"github.com/appvia/terranetes-controller/pkg/utils/template"
"github.com/appvia/terranetes-controller/pkg/version"
)

func init() {
log.SetFormatter(&log.TextFormatter{})
}

var gitConfig = `
[url "{{ .Source }}"]
insteadOf = {{ .Destination }}
`

func main() {
var source, destination string
var timeout time.Duration
Expand Down Expand Up @@ -113,31 +119,39 @@ func Run(ctx context.Context, source, destination string, timeout time.Duration,
}

case os.Getenv("GIT_USERNAME") != "" && os.Getenv("GIT_PASSWORD") != "":
filename := path.Join("${HOME}", ".git", "config")

found, err := utils.FileExists(os.ExpandEnv(filename))
data, err := template.New(gitConfig, map[string]string{
"Source": fmt.Sprintf("%s://%s:%s@%s%s",
uri.Scheme,
os.Getenv("GIT_USERNAME"),
os.Getenv("GIT_PASSWORD"),
uri.Hostname(), uri.Path,
),
"Destination": source,
})
if err != nil {
return err
return fmt.Errorf("failed to create git config template: %w", err)
}
if found {
log.WithField("path", filename).Warn("git configuration file already found, skipping")

break
}
filename := os.ExpandEnv(
path.Join("${HOME}", utils.GetEnv("GIT_CONFIG", ".gitconfig")),
)

// @step: update the gitconfig to overload the URL
// git config --global url."https://token:$GIT_TOKEN@github.com/example/".insteadOf "ssh://git@github.com/example/"
args := []string{
"config", "--global",
fmt.Sprintf("url.\"https://%s:%s@%s/%s\".insteadOf \"%s\"",
os.Getenv("GIT_USERNAME"),
os.Getenv("GIT_PASSWORD"),
uri.Hostname(), uri.Path, source),
}
if err := exec.Command("git", args...).Run(); err != nil {
return fmt.Errorf("failed tp update the git configuration: %w", err)
wr, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0760)
if err != nil {
return fmt.Errorf("failed to open git config file: %w", err)
}

err = func() error {
if _, err := wr.Write(data); err != nil {
return fmt.Errorf("failed to write git config file: %w", err)
}
defer wr.Close()

return nil
}()
if err != nil {
return err
}
}

// @step: retrieve the working directory
Expand All @@ -152,8 +166,8 @@ func Run(ctx context.Context, source, destination string, timeout time.Duration,
}

log.WithFields(log.Fields{
"source": source,
"dest": destination,
"source": source,
}).Info("downloading the assets")

// @step: create a temporary directory
Expand Down
3 changes: 3 additions & 0 deletions pkg/assets/job.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ spec:
- --command=/bin/cp /run/config/* /data
- --command=/bin/cp /bin/step /run/bin/step
- --command=/bin/source --dest=/data --source={{ .Configuration.Module }}
env:
- name: HOME
value: /data
envFrom:
{{- if .Secrets.Config }}
- secretRef:
Expand Down
29 changes: 29 additions & 0 deletions pkg/utils/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2023 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package utils

import "os"

// GetEnv returns the value of an environment variable or a default value if not set
func GetEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}

return fallback
}
34 changes: 34 additions & 0 deletions pkg/utils/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2023 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package utils

import (
"os"
"testing"

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

func TestGetEnvDefault(t *testing.T) {
assert.Equal(t, "default", GetEnv("TEST_ENV", "default"))
}

func TestGetEnv(t *testing.T) {
os.Setenv("TEST_ENV", "test")
assert.Equal(t, "test", GetEnv("TEST_ENV", "default"))
}

0 comments on commit 9d8d8b0

Please sign in to comment.