feat(ssh): add possibilty to use multiple SSH configurations
This commit is contained in:
parent
dd6eeb3041
commit
b27c1f9e89
|
@ -6,9 +6,9 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
mapper "github.com/datahearth/config-mapper/internal"
|
||||
"github.com/datahearth/config-mapper/internal/configuration"
|
||||
"github.com/datahearth/config-mapper/internal/git"
|
||||
mapper "gitea.antoine-langlois.net/datahearth/config-mapper/internal"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/git"
|
||||
"github.com/fatih/color"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
|
4
go.mod
4
go.mod
|
@ -1,4 +1,4 @@
|
|||
module github.com/datahearth/config-mapper
|
||||
module gitea.antoine-langlois.net/datahearth/config-mapper
|
||||
|
||||
go 1.17
|
||||
|
||||
|
@ -6,6 +6,7 @@ require (
|
|||
github.com/fatih/color v1.13.0
|
||||
github.com/gernest/wow v0.1.0
|
||||
github.com/go-git/go-git/v5 v5.4.2
|
||||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/spf13/cobra v1.3.0
|
||||
github.com/spf13/viper v1.10.1
|
||||
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
|
||||
|
@ -28,7 +29,6 @@ require (
|
|||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.4.3 // indirect
|
||||
github.com/pelletier/go-toml v1.9.4 // indirect
|
||||
github.com/sergi/go-diff v1.1.0 // indirect
|
||||
github.com/spf13/afero v1.6.0 // indirect
|
||||
|
|
3
go.sum
3
go.sum
|
@ -313,8 +313,9 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
|
|||
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
|
||||
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
|
||||
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
|
|
|
@ -22,7 +22,7 @@ type Git struct {
|
|||
Name string `mapstructure:"name" yaml:"name"`
|
||||
Email string `mapstructure:"email" yaml:"email"`
|
||||
BasicAuth BasicAuth `mapstructure:"basic-auth" yaml:"basic-auth"`
|
||||
SSH Ssh `mapstructure:"ssh" yaml:"ssh"`
|
||||
SSH interface{} `mapstructure:"ssh" yaml:"ssh"`
|
||||
}
|
||||
|
||||
type BasicAuth struct {
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
osUser "os/user"
|
||||
"strings"
|
||||
|
@ -156,7 +155,7 @@ func getUriContent(uri string) (string, string, error) {
|
|||
|
||||
func createPubKeyAuth(key string) (ssh.AuthMethod, error) {
|
||||
var signer ssh.Signer
|
||||
privateKey, err := ioutil.ReadFile(key)
|
||||
privateKey, err := os.ReadFile(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -2,16 +2,18 @@ package git
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/datahearth/config-mapper/internal/configuration"
|
||||
"github.com/datahearth/config-mapper/internal/misc"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/misc"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -48,25 +50,45 @@ func NewRepository(config configuration.Git, repoPath string) (RepositoryActions
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if config.SSH.Passphrase != "" && config.SSH.PrivateKey != "" {
|
||||
privateKey, err := misc.AbsolutePath(config.SSH.PrivateKey)
|
||||
if err != nil {
|
||||
switch sshConfig := config.SSH.(type) {
|
||||
case map[string]interface{}:
|
||||
var outConfig configuration.Ssh
|
||||
if err := mapstructure.Decode(sshConfig, &outConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(privateKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
auth, err = ssh.NewPublicKeysFromFile("git", privateKey, config.SSH.Passphrase)
|
||||
auth, err = getSSHAuthMethod(outConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
auth = &http.BasicAuth{
|
||||
Username: config.BasicAuth.Username,
|
||||
Password: config.BasicAuth.Password,
|
||||
case []interface{}:
|
||||
for i, c := range sshConfig {
|
||||
if _, ok := c.(map[interface{}]interface{}); !ok {
|
||||
fmt.Printf("invalid format for configuration n°%d", i)
|
||||
continue
|
||||
}
|
||||
|
||||
var outConfig configuration.Ssh
|
||||
if err := mapstructure.Decode(c, &outConfig); err != nil {
|
||||
fmt.Printf("failed to decode ssh configuration n°%d: %v\n", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
auth, err = getSSHAuthMethod(outConfig)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create SSH authentication method for configuration n°%d: %v\n", i, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if auth == nil {
|
||||
auth = &http.BasicAuth{
|
||||
Username: config.BasicAuth.Username,
|
||||
Password: config.BasicAuth.Password,
|
||||
}
|
||||
}
|
||||
default:
|
||||
return nil, errors.New("git ssh configuration canno't be unmarshaled. Please, pass a valid configuration")
|
||||
}
|
||||
|
||||
repo := &Repository{
|
||||
|
@ -170,3 +192,25 @@ func (r *Repository) GetAuthor() *object.Signature {
|
|||
When: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func getSSHAuthMethod(config configuration.Ssh) (transport.AuthMethod, error) {
|
||||
if config.Passphrase == "" && config.PrivateKey == "" {
|
||||
return nil, errors.New("passphrase and private are empty")
|
||||
}
|
||||
|
||||
privateKey, err := misc.AbsolutePath(config.PrivateKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(privateKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
auth, err := ssh.NewPublicKeysFromFile("git", privateKey, config.Passphrase)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return auth, nil
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/datahearth/config-mapper/internal/misc"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/misc"
|
||||
)
|
||||
|
||||
type Index struct {
|
||||
|
|
|
@ -7,9 +7,9 @@ import (
|
|||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/datahearth/config-mapper/internal/configuration"
|
||||
"github.com/datahearth/config-mapper/internal/git"
|
||||
"github.com/datahearth/config-mapper/internal/misc"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/git"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/misc"
|
||||
"github.com/fatih/color"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
@ -57,7 +57,7 @@ func (e *Items) Action(action string) {
|
|||
continue
|
||||
}
|
||||
if storagePath == "" && systemPath == "" {
|
||||
fmt.Printf("⛔ Skipping %s\n", src)
|
||||
fmt.Println("⛔ Skipping")
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/datahearth/config-mapper/internal/configuration"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration"
|
||||
)
|
||||
|
||||
func AbsolutePath(p string) (string, error) {
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/datahearth/config-mapper/internal/configuration"
|
||||
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration"
|
||||
"github.com/gernest/wow"
|
||||
"github.com/gernest/wow/spin"
|
||||
"github.com/spf13/viper"
|
||||
|
|
2
main.go
2
main.go
|
@ -1,6 +1,6 @@
|
|||
package main
|
||||
|
||||
import "github.com/datahearth/config-mapper/cmd"
|
||||
import "gitea.antoine-langlois.net/datahearth/config-mapper/cmd"
|
||||
|
||||
func main() {
|
||||
cmd.Execute()
|
||||
|
|
|
@ -113,8 +113,8 @@ if __name__ == "__main__":
|
|||
api_token: str
|
||||
if len(sys.argv) > 1:
|
||||
api_token = sys.argv.pop()
|
||||
elif os.getenv("GIT_CFG_MAPPER_TOKEN"):
|
||||
api_token = os.getenv("GIT_CFG_MAPPER_TOKEN")
|
||||
elif "GIT_CFG_MAPPER_TOKEN" in os.environ:
|
||||
api_token = os.environ["GIT_CFG_MAPPER_TOKEN"]
|
||||
else:
|
||||
log("no gitea api token found in CLI params nor in ENV", LogLevel.ERROR)
|
||||
sys.exit(1)
|
||||
|
|
Reference in New Issue