From a115f3830af361d777a6964bbd84b34e58be708c Mon Sep 17 00:00:00 2001 From: Mustafa KIRIMLI Date: Thu, 11 Apr 2019 10:09:52 +0400 Subject: [PATCH] configure git to access to http(s) based repos behind CloudFlare Access --- cmd/cloudflared/access/cmd.go | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/cmd/cloudflared/access/cmd.go b/cmd/cloudflared/access/cmd.go index 9753c719..5693119e 100644 --- a/cmd/cloudflared/access/cmd.go +++ b/cmd/cloudflared/access/cmd.go @@ -72,6 +72,14 @@ func Commands() []*cli.Command { ArgsUsage: "allow-request will allow the curl request to continue even if the jwt is not present.", SkipFlagParsing: true, }, + { + Name: "gitConfig", + Action: gitConfig, + Usage: "gitConfig ", + Description: `The gitConfig subcommand configures your git to injects the JWT into a cf-access-token + header when using git to reach an application behind Access.`, + SkipFlagParsing: true, + }, { Name: "token", Action: generateToken, @@ -182,6 +190,41 @@ func curl(c *cli.Context) error { return shell.Run("curl", cmdArgs...) } +/* gitConfig modifies your ~/.gitconfig file to +pass Access JWT along in request +*/ +func gitConfig(c *cli.Context) error { + raven.SetDSN(sentryDSN) + logger := log.CreateLogger() + args := c.Args() + if args.Len() < 1 { + logger.Error("Please provide the access app and command you wish to run.") + return errors.New("incorrect args") + } + + cmdArgs := args.Slice() + appURL, err := getAppURL(cmdArgs) + if err != nil { + return err + } + tok, err := token.GetTokenIfExists(appURL) + if err != nil || tok == "" { + tok, err = token.FetchToken(appURL) + if err != nil { + logger.Error("Failed to refresh token: ", err) + return err + } + } + + gitArgs := []string{ + "config", + "--global", + fmt.Sprintf("http.https://%s.extraheader", appURL.Hostname()), + fmt.Sprintf("cf-access-token:%s", tok), + } + return shell.Run("git", gitArgs...) +} + // token dumps provided token to stdout func generateToken(c *cli.Context) error { raven.SetDSN(sentryDSN)