Skip to content

Commit 0295613

Browse files
authored
The repository connections required ssh-agent (#90)
There is an issue in previous release where the application would ignore any private key file path and simply rely on the ssh-agent to authentiate to a private key repository. This has now been fixed and the application will now use a private key file specified in the configuration or passed via the command line. If there is no private key file specified, the application will fallback to using the ssh-agent. If the ssh-agent is not running, the application will generate an error. fixes #69
1 parent 7ef3cdf commit 0295613

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

docs/configuration-reference.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@ messages to any desired timezone.
142142

143143
The default value for `timestamp_timezone` is `utc`
144144

145+
### Git settings
146+
147+
Whenw working with git repositories, there are some global settings that should
148+
be configured. This section provides a configuration settings for setting
149+
global git options.
150+
151+
#### `name`
152+
153+
Configures the name to use when making commits using git. The name configured
154+
here will be displayed in the commit message.
155+
156+
The default value for `name` is `null`
157+
158+
#### `email`
159+
160+
Configures the email address to use when making git commits. The email address
161+
will be used in commit messages.
162+
163+
The default value for `email` is `null`
164+
165+
#### `user`
166+
167+
Sets the default user to use when connecting to a git repository over SSH.
168+
Most git server implementations require this value to be `git` which is the
169+
default; however it can be changed here if needed.
170+
171+
The default value for `user` is `git`.
172+
145173
### Profiles
146174

147175
Profiles provide server specific configuration settings for working with

internal/runners/repository.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ func (r Repository) Clone() (string, error) {
7979
logger.Trace()
8080

8181
repo := repositories.Repository{
82-
Url: r.Url,
82+
Url: r.Url,
83+
User: "git",
8384
}
8485
if r.PrivateKeyFile != "" {
85-
pk, err := utils.ReadStringFromFile(r.PrivateKeyFile)
86+
pk, err := utils.ReadFromFile(r.PrivateKeyFile)
8687
if err != nil {
8788
return "", err
8889
}

pkg/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const (
3737

3838
defaultGitName = ""
3939
defaultGitEmail = ""
40+
defaultGitUser = "git"
4041
)
4142

4243
type Config struct {
@@ -69,6 +70,7 @@ type Config struct {
6970
// Git settings
7071
GitName string `json:"git_name"`
7172
GitEmail string `json:"git_email"`
73+
GitUser string `json:"git_user"`
7274
}
7375

7476
func NewConfig(defaults map[string]interface{}, envBinding map[string]string, appWorkingDir, sysConfigPath, fileName string) *Config {
@@ -200,6 +202,7 @@ func (ac *Config) populateFields() {
200202

201203
ac.GitName = viper.GetString("git.name")
202204
ac.GitEmail = viper.GetString("git.email")
205+
ac.GitUser = viper.GetString("git.user")
203206
}
204207

205208
var defaultValues = map[string]interface{}{
@@ -221,6 +224,7 @@ var defaultValues = map[string]interface{}{
221224

222225
"git.name": defaultGitName,
223226
"git.email": defaultGitEmail,
227+
"git.user": defaultGitUser,
224228
}
225229

226230
var defaultEnvVarBindings = map[string]string{
@@ -242,6 +246,7 @@ var defaultEnvVarBindings = map[string]string{
242246

243247
"git.name": "IPCTL_GIT_NAME",
244248
"git.email": "IPCTL_GIT_EMAIL",
249+
"git.user": "IPCTL_GIT_USER",
245250
}
246251

247252
// getConfigFileFromFlag reads in the file passed in using the --config flag on the cli

pkg/repositories/repository.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import (
1111

1212
"github.com/go-git/go-git/v5"
1313
"github.com/go-git/go-git/v5/plumbing"
14+
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
1415
"github.com/itential/ipctl/pkg/logger"
1516
giturls "github.com/whilp/git-urls"
17+
"golang.org/x/crypto/ssh"
1618
)
1719

1820
type Repository struct {
1921
Url string
2022
Reference string
21-
PrivateKey string
23+
User string
24+
PrivateKey []byte
2225
}
2326

2427
func (r Repository) Clone() (string, error) {
@@ -36,7 +39,23 @@ func (r Repository) Clone() (string, error) {
3639

3740
cloneOptions := &git.CloneOptions{
3841
URL: r.Url,
39-
//Auth: r.PrivateKey,
42+
}
43+
44+
if r.PrivateKey != nil {
45+
logger.Debug("setting up auth using private key")
46+
47+
signer, err := ssh.ParsePrivateKey(r.PrivateKey)
48+
if err != nil {
49+
return "", err
50+
}
51+
52+
cloneOptions.Auth = &gitssh.PublicKeys{
53+
User: r.User,
54+
Signer: signer,
55+
HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
56+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
57+
},
58+
}
4059
}
4160

4261
// Git will default to main/master if no repo is specified

0 commit comments

Comments
 (0)