feat(sg): add command to generate a dotcom user gateway access token (#63125)

We can now generate gateway access tokens from sg instead of having to
manually wrangle a script to do it every time. This will help with
making Cody Gateway easier to run locally.

## Test plan
Tested locally.

---------

Co-authored-by: William Bezuidenhout <william.bezuidenhout@sourcegraph.com>
This commit is contained in:
Jan Hartman 2024-06-07 12:40:51 +02:00 committed by GitHub
parent 18bdafac78
commit aa615bc37f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 2 deletions

View File

@ -15,6 +15,7 @@ go_library(
"sg_backport.go",
"sg_bazel.go",
"sg_cloud.go",
"sg_cody_gateway.go",
"sg_db.go",
"sg_deploy.go",
"sg_doctor.go",
@ -82,6 +83,7 @@ go_library(
"//dev/sg/root",
"//dev/sg/sams",
"//dev/team",
"//internal/accesstoken",
"//internal/database",
"//internal/database/basestore",
"//internal/database/connections/live",

View File

@ -9,9 +9,8 @@ import (
"time"
hashstructure "github.com/mitchellh/hashstructure/v2"
"github.com/urfave/cli/v2"
"github.com/sourcegraph/log"
"github.com/urfave/cli/v2"
"github.com/sourcegraph/sourcegraph/dev/sg/ci"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/analytics"
@ -312,6 +311,7 @@ var sg = &cli.App{
release.Command,
updateCommand,
versionCommand,
codyGatewayCommand,
},
ExitErrHandler: func(cmd *cli.Context, err error) {
interrupt.Wait()

47
dev/sg/sg_cody_gateway.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"os"
"strings"
"github.com/urfave/cli/v2"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/category"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/std"
"github.com/sourcegraph/sourcegraph/internal/accesstoken"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
var codyGatewayCommand = &cli.Command{
Name: "cody-gateway",
Usage: "set of commands that are helpful for working with Cody Gateway locally",
Category: category.Util,
Subcommands: []*cli.Command{{
Name: "gen-token",
Usage: "generate a new token for use with Cody Gateway",
UsageText: "sg cody-gateway gen-token [sg token]",
Description: "generates a token with the given `sg token` - which should be prefixed with `sgp_`",
Action: genGatewayAccessTokenExec,
}},
}
func genGatewayAccessTokenExec(c *cli.Context) error {
out := std.NewOutput(os.Stderr, false)
if c.NArg() == 0 {
out.WriteWarningf("The first argument should be a Sourcegraph private access token - see --help for more information")
return errors.New("missing private access token argument")
}
out.WriteNoticef("Generating new gateway access token ...")
privateToken := c.Args().Get(0)
if !strings.HasPrefix(privateToken, "sgp_") {
out.WriteWarningf("Token must be prefixed with \"sgp_\"")
return errors.New("invalid token: not prefixed with sgp_")
}
accessToken, err := accesstoken.GenerateDotcomUserGatewayAccessToken(privateToken)
if err != nil {
return errors.Newf("failed to generate gateway access token: %s", err)
}
std.Out.Write(accessToken)
return nil
}