Skip to content

Commit ab83b66

Browse files
committed
add configure command
1 parent d8abd35 commit ab83b66

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

cmd/git-remote-https+iap/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const (
2121
var (
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

4453
func 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+
77118
func 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.

internal/git/git.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ func ConfigGetURLMatch(key, url string) string {
3333
return strings.TrimSpace(string(stdout.Bytes()))
3434
}
3535

36+
func SetGlobalConfig(url, section, key, value string) {
37+
x := fmt.Sprintf("%s.%s.%s", section, url, key)
38+
args := []string{"config", "--global", x, value}
39+
cmd := exec.Command(GitBinary, args...)
40+
41+
if err := cmd.Run(); err != nil {
42+
log.Fatal().Msgf("SetGlobalConfig - could not set config '%s': %s", x, err)
43+
}
44+
}
45+
3646
func PassThruRemoteHTTPSHelper(remote, url string) {
3747
u, err := _url.Parse(url)
3848
if err != nil {

0 commit comments

Comments
 (0)