@ -41,19 +41,19 @@ import (
"github.com/fluxcd/flux2/internal/utils"
)
type S ourceGitFlags struct {
GitURL string
GitBranch string
Gi tT ag string
GitSemver string
GitUsername string
GitPassword string
GitKeyAlgorithm flags . PublicKeyAlgorithm
Git RSABits flags . RSAKeyBits
Git ECDSACurve flags . ECDSACurve
GitSecretRef string
G itImplementation flags . GitImplementation
type s ourceGitFlags struct {
url string
branch string
tag string
semver string
username string
password string
caFile string
keyAlgorithm flags . PublicKeyAlgorithm
key RSABits flags . RSAKeyBits
key ECDSACurve flags . ECDSACurve
secretRef string
g itImplementation flags . GitImplementation
}
var createSourceGitCmd = & cobra . Command {
@ -100,29 +100,30 @@ For private Git repositories, the basic authentication credentials are stored in
RunE : createSourceGitCmdRun ,
}
var source Args = N ewSourceGitFlags( )
var source GitArgs = n ewSourceGitFlags( )
func init ( ) {
createSourceGitCmd . Flags ( ) . StringVar ( & sourceArgs . GitURL , "url" , "" , "git address, e.g. ssh://git@host/org/repository" )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceArgs . GitBranch , "branch" , "master" , "git branch" )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceArgs . GitTag , "tag" , "" , "git tag" )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceArgs . GitSemver , "tag-semver" , "" , "git tag semver range" )
createSourceGitCmd . Flags ( ) . StringVarP ( & sourceArgs . GitUsername , "username" , "u" , "" , "basic authentication username" )
createSourceGitCmd . Flags ( ) . StringVarP ( & sourceArgs . GitPassword , "password" , "p" , "" , "basic authentication password" )
createSourceGitCmd . Flags ( ) . Var ( & sourceArgs . GitKeyAlgorithm , "ssh-key-algorithm" , sourceArgs . GitKeyAlgorithm . Description ( ) )
createSourceGitCmd . Flags ( ) . Var ( & sourceArgs . GitRSABits , "ssh-rsa-bits" , sourceArgs . GitRSABits . Description ( ) )
createSourceGitCmd . Flags ( ) . Var ( & sourceArgs . GitECDSACurve , "ssh-ecdsa-curve" , sourceArgs . GitECDSACurve . Description ( ) )
createSourceGitCmd . Flags ( ) . StringVarP ( & sourceArgs . GitSecretRef , "secret-ref" , "" , "" , "the name of an existing secret containing SSH or basic credentials" )
createSourceGitCmd . Flags ( ) . Var ( & sourceArgs . GitImplementation , "git-implementation" , sourceArgs . GitImplementation . Description ( ) )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceGitArgs . url , "url" , "" , "git address, e.g. ssh://git@host/org/repository" )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceGitArgs . branch , "branch" , "master" , "git branch" )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceGitArgs . tag , "tag" , "" , "git tag" )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceGitArgs . semver , "tag-semver" , "" , "git tag semver range" )
createSourceGitCmd . Flags ( ) . StringVarP ( & sourceGitArgs . username , "username" , "u" , "" , "basic authentication username" )
createSourceGitCmd . Flags ( ) . StringVarP ( & sourceGitArgs . password , "password" , "p" , "" , "basic authentication password" )
createSourceGitCmd . Flags ( ) . Var ( & sourceGitArgs . keyAlgorithm , "ssh-key-algorithm" , sourceGitArgs . keyAlgorithm . Description ( ) )
createSourceGitCmd . Flags ( ) . Var ( & sourceGitArgs . keyRSABits , "ssh-rsa-bits" , sourceGitArgs . keyRSABits . Description ( ) )
createSourceGitCmd . Flags ( ) . Var ( & sourceGitArgs . keyECDSACurve , "ssh-ecdsa-curve" , sourceGitArgs . keyECDSACurve . Description ( ) )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceGitArgs . secretRef , "secret-ref" , "" , "the name of an existing secret containing SSH or basic credentials" )
createSourceGitCmd . Flags ( ) . Var ( & sourceGitArgs . gitImplementation , "git-implementation" , sourceGitArgs . gitImplementation . Description ( ) )
createSourceGitCmd . Flags ( ) . StringVar ( & sourceGitArgs . caFile , "ca-file" , "" , "path to TLS CA file used for validating self-signed certificates, requires libgit2" )
createSourceCmd . AddCommand ( createSourceGitCmd )
}
func NewSourceGitFlags( ) S ourceGitFlags {
return S ourceGitFlags{
GitKeyAlgorithm: "rsa" ,
GitRSABits: 2048 ,
GitECDSACurve: flags . ECDSACurve { Curve : elliptic . P384 ( ) } ,
func newSourceGitFlags( ) s ourceGitFlags {
return s ourceGitFlags{
keyAlgorithm: "rsa" ,
keyRSABits: 2048 ,
keyECDSACurve: flags . ECDSACurve { Curve : elliptic . P384 ( ) } ,
}
}
@ -132,17 +133,21 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
}
name := args [ 0 ]
if source Args. GitURL == "" {
if source GitArgs. url == "" {
return fmt . Errorf ( "url is required" )
}
if sourceGitArgs . gitImplementation . String ( ) != sourcev1 . LibGit2Implementation && sourceGitArgs . caFile != "" {
return fmt . Errorf ( "specifing a CA file requires --git-implementation=%s" , sourcev1 . LibGit2Implementation )
}
tmpDir , err := ioutil . TempDir ( "" , name )
if err != nil {
return err
}
defer os . RemoveAll ( tmpDir )
u , err := url . Parse ( source Args. GitURL )
u , err := url . Parse ( source GitArgs. url )
if err != nil {
return fmt . Errorf ( "git URL parse failed: %w" , err )
}
@ -159,7 +164,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
Labels : sourceLabels ,
} ,
Spec : sourcev1 . GitRepositorySpec {
URL : source Args. GitURL ,
URL : source GitArgs. url ,
Interval : metav1 . Duration {
Duration : createArgs . interval ,
} ,
@ -167,22 +172,22 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} ,
}
if source Args. G itImplementation != "" {
gitRepository . Spec . GitImplementation = source Args. G itImplementation. String ( )
if source GitArgs. g itImplementation != "" {
gitRepository . Spec . GitImplementation = source GitArgs. g itImplementation. String ( )
}
if source Args. GitS emver != "" {
gitRepository . Spec . Reference . SemVer = source Args. GitS emver
} else if source Args. Gi tT ag != "" {
gitRepository . Spec . Reference . Tag = source Args. Gi tT ag
if source GitArgs. s emver != "" {
gitRepository . Spec . Reference . SemVer = source GitArgs. s emver
} else if source Git Args. tag != "" {
gitRepository . Spec . Reference . Tag = source Git Args. tag
} else {
gitRepository . Spec . Reference . Branch = source Args. GitB ranch
gitRepository . Spec . Reference . Branch = source GitArgs. b ranch
}
if createArgs . export {
if source Args. GitS ecretRef != "" {
if source GitArgs. s ecretRef != "" {
gitRepository . Spec . SecretRef = & meta . LocalObjectReference {
Name : source Args. GitS ecretRef,
Name : source GitArgs. s ecretRef,
}
}
return exportGit ( gitRepository )
@ -198,11 +203,11 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
withAuth := false
// TODO(hidde): move all auth prep to separate func?
if source Args. GitS ecretRef != "" {
if source GitArgs. s ecretRef != "" {
withAuth = true
} else if u . Scheme == "ssh" {
logger . Generatef ( "generating deploy key pair" )
pair , err := generateKeyPair ( ctx , source Args. GitKeyAlgorithm , sourceArgs . GitRSABits , sourceArgs . Git ECDSACurve)
pair , err := generateKeyPair ( ctx , source GitArgs. keyAlgorithm , sourceGitArgs . keyRSABits , sourceGitArgs . key ECDSACurve)
if err != nil {
return err
}
@ -240,7 +245,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
return err
}
withAuth = true
} else if source Args. GitUsername != "" && sourceArgs . GitP assword != "" {
} else if source GitArgs. username != "" && sourceGitArgs . p assword != "" {
logger . Actionf ( "applying secret with basic auth credentials" )
secret := corev1 . Secret {
ObjectMeta : metav1 . ObjectMeta {
@ -249,10 +254,19 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
Labels : sourceLabels ,
} ,
StringData : map [ string ] string {
"username" : source Args. GitU sername,
"password" : source Args. GitP assword,
"username" : source GitArgs. u sername,
"password" : source GitArgs. p assword,
} ,
}
if sourceGitArgs . caFile != "" {
ca , err := ioutil . ReadFile ( sourceGitArgs . caFile )
if err != nil {
return fmt . Errorf ( "failed to read CA file '%s': %w" , sourceGitArgs . caFile , err )
}
secret . StringData [ "caFile" ] = string ( ca )
}
if err := upsertSecret ( ctx , kubeClient , secret ) ; err != nil {
return err
}
@ -267,8 +281,8 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
if withAuth {
secretName := name
if source Args. GitS ecretRef != "" {
secretName = source Args. GitS ecretRef
if source GitArgs. s ecretRef != "" {
secretName = source GitArgs. s ecretRef
}
gitRepository . Spec . SecretRef = & meta . LocalObjectReference {
Name : secretName ,