From 861bdc26f83207c24fca4619132f6c2bac6bb240 Mon Sep 17 00:00:00 2001 From: Uwe Hoffmann <534011+uwedeportivo@users.noreply.github.com> Date: Mon, 19 Oct 2020 18:23:46 -0700 Subject: [PATCH 1/2] validate command: ability to create access token for first admin user --- cmd/src/validate.go | 81 +++++++++++++++++++++++++++++++++++++----- src-conf_2.json | 1 + validate_script_2.json | 8 +++++ 3 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 src-conf_2.json create mode 100644 validate_script_2.json diff --git a/cmd/src/validate.go b/cmd/src/validate.go index 68429a92b7..9fa2181da8 100644 --- a/cmd/src/validate.go +++ b/cmd/src/validate.go @@ -22,9 +22,10 @@ import ( type validationSpec struct { FirstAdmin struct { - Email string - Username string - Password string + Email string + Username string + Password string + CreateAccessToken bool } WaitRepoCloned struct { Repo string @@ -68,9 +69,9 @@ Please visit https://docs.sourcegraph.com/admin/validation for documentation of var ( contextFlag = flagSet.String("context", "", `Comma-separated list of key=value pairs to add to the script execution context`) secretsFlag = flagSet.String("secrets", "", "Path to a file containing key=value lines. The key value pairs will be added to the script context") - apiFlags = api.NewFlags(flagSet) + apiFlags = api.NewFlags(flagSet) ) - + handler := func(args []string) error { flagSet.Parse(args) @@ -117,8 +118,8 @@ Please visit https://docs.sourcegraph.com/admin/validation for documentation of } commands = append(commands, &command{ - flagSet: flagSet, - handler: handler, + flagSet: flagSet, + handler: handler, usageFunc: usageFunc, }) } @@ -174,6 +175,14 @@ func (vd *validator) validate(script []byte, scriptContext map[string]string, is if err != nil { return err } + + if vspec.FirstAdmin.CreateAccessToken { + token, err := vd.createAccessToken(vspec.FirstAdmin.Username) + if err != nil { + return err + } + fmt.Println(token) + } } if vspec.ExternalService.DisplayName != "" { @@ -303,7 +312,7 @@ func (vd *validator) listClonedRepos(fs []string) ([]string, error) { var resp struct { Repositories struct { Nodes []struct { - Name string `json:"name"` + Name string `json:"name"` MirrorInfo struct { Cloned bool `json:"cloned"` } `json:"mirrorInfo"` @@ -312,7 +321,7 @@ func (vd *validator) listClonedRepos(fs []string) ([]string, error) { } err := vd.graphQL(vdListRepos, map[string]interface{}{ - "names": fs, + "names": fs, }, &resp) names := make([]string, 0, len(resp.Repositories.Nodes)) @@ -341,6 +350,60 @@ func (vd *validator) waitRepoCloned(repoName string, sleepSeconds int, maxTries return false, nil } +const vdUserQuery = ` +query User($username: String) { + user(username: $username) { + id + } +}` + +func (vd *validator) userID(username string) (string, error) { + var resp struct { + User struct { + ID string `json:"id"` + } `json:"user"` + } + + err := vd.graphQL(vdUserQuery, map[string]interface{}{ + "username": username, + }, &resp) + + return resp.User.ID, err +} + +const vdCreateAccessTokenMutation = ` +mutation CreateAccessToken($user: ID!, $scopes: [String!]!, $note: String!) { + createAccessToken( + user:$user, + scopes:$scopes, + note: $note + ) + { + token + } +}` + +func (vd *validator) createAccessToken(username string) (string, error) { + userID, err := vd.userID(username) + if err != nil { + return "", err + } + + var resp struct { + CreateAccessToken struct { + Token string `json:"token"` + } `json:"createAccessToken"` + } + + err = vd.graphQL(vdCreateAccessTokenMutation, map[string]interface{}{ + "user": userID, + "scopes": []string{"user:all", "site-admin:sudo"}, + "note": "src_cli_validate", + }, &resp) + + return resp.CreateAccessToken.Token, err +} + // SiteAdminInit initializes the instance with given admin account. // It returns an authenticated client as the admin for doing e2e testing. func (vd *validator) siteAdminInit(baseURL, email, username, password string) (*vdClient, error) { diff --git a/src-conf_2.json b/src-conf_2.json new file mode 100644 index 0000000000..555eec2d0f --- /dev/null +++ b/src-conf_2.json @@ -0,0 +1 @@ +{"endpoint": "http://localhost:3080"} diff --git a/validate_script_2.json b/validate_script_2.json new file mode 100644 index 0000000000..6571757ec2 --- /dev/null +++ b/validate_script_2.json @@ -0,0 +1,8 @@ +{ + "firstAdmin": { + "email": "uwe@sourcegraph.com", + "username": "uwe", + "password": "testtesttest", + "createAccessToken": true + } +} From 78f958a495cfab8cfd2cc000a573bbfcda5ac24b Mon Sep 17 00:00:00 2001 From: Uwe Hoffmann <534011+uwedeportivo@users.noreply.github.com> Date: Mon, 19 Oct 2020 19:36:46 -0700 Subject: [PATCH 2/2] revert accidental test file additions --- src-conf_2.json | 1 - validate_script_2.json | 8 -------- 2 files changed, 9 deletions(-) delete mode 100644 src-conf_2.json delete mode 100644 validate_script_2.json diff --git a/src-conf_2.json b/src-conf_2.json deleted file mode 100644 index 555eec2d0f..0000000000 --- a/src-conf_2.json +++ /dev/null @@ -1 +0,0 @@ -{"endpoint": "http://localhost:3080"} diff --git a/validate_script_2.json b/validate_script_2.json deleted file mode 100644 index 6571757ec2..0000000000 --- a/validate_script_2.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "firstAdmin": { - "email": "uwe@sourcegraph.com", - "username": "uwe", - "password": "testtesttest", - "createAccessToken": true - } -}