-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add builtin netrc helper #82
Open
6543
wants to merge
12
commits into
woodpecker-ci:main
Choose a base branch
from
6543-forks:build_in_netrc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bda5f22
draft
6543 d4dbc40
parsin into own func
6543 42852ff
start with tests
6543 4b262e6
use build-in netrc helper on windows by default
6543 5a608d1
Apply suggestions from code review
6543 a622645
Update plugin.go
6543 cf6bfde
Merge branch 'main' into build_in_netrc
6543 012b85e
Merge branch 'main' into build_in_netrc
6543 80f68be
Merge branch 'main' into build_in_netrc
6543 4c86c72
Merge branch 'main' into build_in_netrc
6543 091794d
Merge branch 'main' into build_in_netrc
6543 04e3d4b
Merge branch 'main' into build_in_netrc
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/woodpecker-ci/plugin-git/netrc" | ||
) | ||
|
||
var netrcCommand = &cli.Command{ | ||
Name: "netrc", | ||
Usage: "built-in credentials helper to read netrc", | ||
Flags: []cli.Flag{&cli.StringFlag{ | ||
Name: "home", | ||
Usage: "Change home directory", | ||
EnvVars: []string{"PLUGIN_HOME"}, | ||
}}, | ||
Action: netrcGet, | ||
} | ||
|
||
func netrcGet(c *cli.Context) error { | ||
if c.Args().Len() == 0 { | ||
curExec, err := os.Executable() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("built-in credentials helper to read netrc\n"+ | ||
"exec \"git config --global credential.helper '%s netrc'\" to use it\n", curExec) | ||
return nil | ||
} | ||
|
||
// set custom home | ||
if c.IsSet("home") { | ||
os.Setenv("HOME", c.String("home")) | ||
} | ||
|
||
// implement custom git credentials helper | ||
// https://git-scm.com/docs/gitcredentials | ||
switch c.Args().First() { | ||
case "get": | ||
netRC, err := netrc.Read() | ||
if err != nil { | ||
return err | ||
} | ||
if netRC != nil { | ||
fmt.Printf("username=%s\n", netRC.Login) | ||
fmt.Printf("password=%s\n", netRC.Password) | ||
fmt.Println("quit=true") | ||
} | ||
case "store": | ||
// TODO: netrc.Save() | ||
case "erase": | ||
_, err := netrc.Delete() | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("got unknown helper arg '%s'", c.Args().First()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func setNetRCHelper() *exec.Cmd { | ||
curExec, err := os.Executable() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
credHelper := fmt.Sprintf("%s netrc", curExec) | ||
|
||
return appendEnv(exec.Command("git", "config", "--global", "credential.helper", credHelper), defaultEnvVars...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package netrc | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
type NetRC struct { | ||
Machine, | ||
Login, | ||
Password string | ||
} | ||
|
||
func getFilePath() (string, error) { | ||
// get home | ||
homeDir := getHomeDir() | ||
|
||
// calc netrc file path | ||
file := filepath.Join(homeDir, ".netrc") | ||
if runtime.GOOS == "windows" { | ||
file = filepath.Join(homeDir, "_netrc") | ||
} | ||
|
||
stats, err := os.Stat(file) | ||
if err != nil { | ||
return "", nil | ||
} | ||
if !stats.Mode().IsRegular() { | ||
return "", fmt.Errorf("'%s' exist but is a %s", file, stats.Mode().Type().String()) | ||
} | ||
return file, nil | ||
} | ||
|
||
// Delete delete the netrc if file exist | ||
func Delete() (bool, error) { | ||
file, err := getFilePath() | ||
if err != nil || file == "" { | ||
return false, err | ||
} | ||
|
||
return true, os.Remove(file) | ||
} | ||
|
||
// Save save a netrc | ||
func Save(n *NetRC) error { | ||
if n == nil { | ||
return nil | ||
} | ||
|
||
// get home | ||
homeDir := getHomeDir() | ||
|
||
// calc netrc file path | ||
file := filepath.Join(homeDir, ".netrc") | ||
if runtime.GOOS == "windows" { | ||
file = filepath.Join(homeDir, "_netrc") | ||
} | ||
|
||
content := fmt.Sprintf(` | ||
machine %s | ||
login %s | ||
password %s | ||
`, | ||
n.Machine, | ||
n.Login, | ||
n.Password, | ||
) | ||
|
||
return os.WriteFile(file, []byte(content), 0o600) | ||
} | ||
|
||
// Read return netrc if file or env var exist | ||
func Read() (*NetRC, error) { | ||
// try to read from env var | ||
netRC := &NetRC{ | ||
Machine: os.Getenv("CI_NETRC_MACHINE"), | ||
Login: os.Getenv("CI_NETRC_USERNAME"), | ||
Password: os.Getenv("CI_NETRC_PASSWORD"), | ||
} | ||
// if we get at least user and password from env we can return | ||
if netRC.Login != "" && netRC.Password != "" { | ||
return netRC, nil | ||
} | ||
|
||
file, err := getFilePath() | ||
if err != nil || file == "" { | ||
return nil, err | ||
} | ||
raw, err := os.ReadFile(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while reading file '%s': %w", file, err) | ||
} | ||
|
||
return parseNetRC(string(raw)) | ||
} | ||
|
||
func getHomeDir() string { | ||
if homeDir := os.Getenv("HOME"); homeDir != "" { | ||
return homeDir | ||
} | ||
if homeDir, _ := os.UserHomeDir(); homeDir != "" { | ||
return homeDir | ||
} | ||
pwd, _ := os.Getwd() | ||
return pwd | ||
} | ||
|
||
func parseNetRC(raw string) (*NetRC, error) { | ||
netRC := &NetRC{} | ||
for _, v := range strings.Split(raw, "\n") { | ||
v = strings.TrimSpace(v) | ||
if strings.HasPrefix(v, "machine") { | ||
netRC.Machine = strings.TrimSpace(strings.TrimPrefix(v, "machine")) | ||
} | ||
if strings.HasPrefix(v, "login") { | ||
netRC.Login = strings.TrimSpace(strings.TrimPrefix(v, "login")) | ||
} | ||
if strings.HasPrefix(v, "password") { | ||
netRC.Password = strings.TrimSpace(strings.TrimPrefix(v, "password")) | ||
} | ||
} | ||
|
||
if netRC.Login == "" && netRC.Password == "" { | ||
return nil, fmt.Errorf("parsing netrc failed, got empty result") | ||
} | ||
|
||
return netRC, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package netrc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseNetRC(t *testing.T) { | ||
n, err := parseNetRC(``) | ||
assert.Error(t, err) | ||
assert.Nil(t, n) | ||
|
||
n, err = parseNetRC(`machine example.org`) | ||
assert.Error(t, err) | ||
assert.Nil(t, n) | ||
|
||
n, err = parseNetRC(` | ||
machine test.com | ||
password someTestPWD | ||
|
||
login awesomeUser | ||
`) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, &NetRC{Machine: "test.com", Login: "awesomeUser", Password: "someTestPWD"}, n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this already added because you have the
app.Flags = globalFlags
statement which adds the flags globally?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to test ... but as it's mostly used only internally I dont think we do need it anyway ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested this, working as I expected.
You can try with this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That means just apply this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@6543 @qwerty287 What is the status here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as per lable > wip till i personally do dedicate time to it or it gets priorised at work ...