@@ -21,6 +21,9 @@ const (
2121var (
2222 version string
2323
24+ // only used in configureCmd
25+ repoURL , helperID , helperSecret , clientID string
26+
2427 rootCmd = & cobra.Command {
2528 Use : fmt .Sprintf ("%s remote url" , BinaryName ),
2629 Short : "git-remote-helper that handles authentication for GCP Identity Aware Proxy" ,
@@ -39,12 +42,28 @@ var (
3942 Short : "Install protocol in Git config" ,
4043 Run : installGitProtocol ,
4144 }
45+
46+ configureCmd = & cobra.Command {
47+ Use : "configure" ,
48+ Short : "Configure IAP for a given repository" ,
49+ Run : configureIAP ,
50+ }
4251)
4352
4453func init () {
4554 rootCmd .AddCommand (versionCmd )
4655 rootCmd .AddCommand (installProtocolCmd )
4756
57+ configureCmd .Flags ().StringVar (& repoURL , "repoURL" , "" , "URL of the git repository to configure (required)" )
58+ configureCmd .MarkFlagRequired ("repoURL" )
59+ configureCmd .Flags ().StringVar (& helperID , "helperID" , "" , "OAuth Client ID for the helper (required)" )
60+ configureCmd .MarkFlagRequired ("helperID" )
61+ configureCmd .Flags ().StringVar (& helperSecret , "helperSecret" , "" , "OAuth Client Secret for the helper (required)" )
62+ configureCmd .MarkFlagRequired ("helperSecret" )
63+ configureCmd .Flags ().StringVar (& clientID , "clientID" , "" , "OAuth Client ID of the IAP instance (required)" )
64+ configureCmd .MarkFlagRequired ("clientID" )
65+ rootCmd .AddCommand (configureCmd )
66+
4867 // set default log level
4968 zerolog .SetGlobalLevel (zerolog .DebugLevel )
5069}
@@ -74,6 +93,28 @@ func installGitProtocol(cmd *cobra.Command, args []string) {
7493 log .Info ().Msgf ("%s protocol configured in git!" , p )
7594}
7695
96+ func configureIAP (cmd * cobra.Command , args []string ) {
97+ repo , err := _url .Parse (repoURL )
98+ https := fmt .Sprintf ("https://%s" , repo .Host )
99+ if err != nil {
100+ log .Error ().Msgf ("Could not convert %s in https://: %s" , https , err )
101+ }
102+
103+ log .Info ().Msgf ("Configure IAP for %s" , https )
104+ git .SetGlobalConfig (https , "iap" , "helperID" , helperID )
105+ git .SetGlobalConfig (https , "iap" , "helperSecret" , helperSecret )
106+ git .SetGlobalConfig (https , "iap" , "clientID" , clientID )
107+
108+ // let users manipulate standard 'https://' urls
109+ httpsIAP := fmt .Sprintf ("https+iap://%s" , repo .Host )
110+ git .SetGlobalConfig (httpsIAP , "url" , "insteadOf" , https )
111+
112+ // set cookie path
113+ domainSlug := strings .ReplaceAll (repo .Host , "." , "-" )
114+ cookiePath := fmt .Sprintf ("~/.config/gcp-iap/%s.cookie" , domainSlug )
115+ git .SetGlobalConfig (https , "http" , "cookieFile" , cookiePath )
116+ }
117+
77118func handleIAPAuthCookieFor (url string ) {
78119 // All our work will be based on the basedomain of the provided URL
79120 // as IAP would be setup for the whole domain.
0 commit comments