Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.41.1 - Unreleased

- Groups: use the documented single-label membership query in listings and backups, preserving native pagination and escaping member identities correctly. (#1059) — thanks @hengshan.
- Docs: reject explicitly empty or whitespace-only `--batch` values before input or authentication, preventing accidental immediate edits when a batch variable is empty. (#1156)
- Docs: return a nonzero exit status when `batch end --continue-on-error` retains failed requests, while preserving its result summary and successfully applied edits. (#1154)
- Sheets: submit atomic structural request arrays with `sheets batch-request`, preserving explicit JSON values, previewing exact requests, and requiring explicit grants in restricted command policies. (#1108) — thanks @gurgeous.
Expand Down
15 changes: 14 additions & 1 deletion docs/workspace-admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,20 @@ gog --account admin@example.com admin orgunits delete Eng --force

## Groups

Group commands share the same Admin SDK setup:
To list your direct and indirect Cloud Identity memberships:

```bash
gog --account admin@example.com groups list --all --json
```

This requires eligible Workspace/Cloud Identity access, the Groups read-only
scope, and permission to view the memberships. See Google's
[transitive-membership requirements](https://docs.cloud.google.com/identity/docs/how-to/query-memberships).
The command uses Google's documented query for all direct and indirect group
memberships, excluding Cloud Search identity-mapped groups.
`--page` continues from Google's opaque page token, and `--all` follows every page.

The `admin groups` commands use the Admin SDK setup:

```bash
gog --account admin@example.com admin groups list --domain example.com
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const (
groupRoleMember = "MEMBER"

groupLabelDiscussionForum = "cloudidentity.googleapis.com/groups.discussion_forum"
groupLabelDynamic = "cloudidentity.googleapis.com/groups.dynamic"
groupReadonlyScope = "https://www.googleapis.com/auth/cloud-identity.groups.readonly"

groupsWorkspaceRequiredMessage = "Cloud Identity Groups require a Google Workspace/Cloud Identity account; consumer accounts (gmail.com/googlemail.com) are not supported."
Expand Down Expand Up @@ -207,12 +206,13 @@ func wrapCloudIdentityError(err error, account string) error {
}

func searchTransitiveGroupsQuery(memberKeyID string) string {
memberKeyID = strings.ReplaceAll(strings.TrimSpace(memberKeyID), "'", "\\'")
memberKeyID = strings.ReplaceAll(strings.TrimSpace(memberKeyID), "\\", "\\\\")
memberKeyID = strings.ReplaceAll(memberKeyID, "'", "\\'")
// Follow Google's documented query for direct and indirect group memberships.
return fmt.Sprintf(
"member_key_id == '%s' && ('%s' in labels || '%s' in labels)",
"member_key_id == '%s' && '%s' in labels",
memberKeyID,
groupLabelDiscussionForum,
groupLabelDynamic,
)
}

Expand Down
130 changes: 130 additions & 0 deletions internal/cmd/groups_query_regression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package cmd

import (
"context"
"encoding/json"
"net/http"
"strconv"
"strings"
"testing"

"google.golang.org/api/cloudidentity/v1"
)

const groupsQueryTestCursor = "opaque+page/2==?"

func supportedGroupsQueryService(t *testing.T, pageSize int64) (*cloudidentity.Service, <-chan string) {
t.Helper()
requests := make(chan string, 4)
svc := newCloudIdentityTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || !strings.HasSuffix(r.URL.Path, "/groups/-/memberships:searchTransitiveGroups") {
http.NotFound(w, r)
return
}
const query = "member_key_id == 'person@example.com' && 'cloudidentity.googleapis.com/groups.discussion_forum' in labels"
w.Header().Set("Content-Type", "application/json")
if r.URL.Query().Get("query") != query {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{
"code": http.StatusBadRequest, "message": "unsupported group label query", "status": "INVALID_ARGUMENT",
}})
return
}
if got := r.URL.Query().Get("pageSize"); got != strconv.FormatInt(pageSize, 10) {
t.Errorf("pageSize = %q, want %d", got, pageSize)
}
cursor := r.URL.Query().Get("pageToken")
requests <- cursor
switch cursor {
case "":
_ = json.NewEncoder(w).Encode(map[string]any{
"memberships": []map[string]any{{
"groupKey": map[string]any{"id": "regular@example.com"}, "displayName": "Regular", "relationType": "DIRECT",
}},
"nextPageToken": groupsQueryTestCursor,
})
case groupsQueryTestCursor:
_ = json.NewEncoder(w).Encode(map[string]any{
"memberships": []map[string]any{{
"groupKey": map[string]any{"id": "dynamic@example.com"}, "displayName": "Dynamic", "relationType": "INDIRECT",
}},
})
default:
http.Error(w, "unexpected page token", http.StatusBadRequest)
}
}))
return svc, requests
}

func assertGroupsQueryRequests(t *testing.T, requests <-chan string, want []string) {
t.Helper()
for _, expected := range want {
select {
case got := <-requests:
if got != expected {
t.Fatalf("page token = %q, want %q", got, expected)
}
default:
t.Fatalf("missing request for page token %q", expected)
}
}
select {
case got := <-requests:
t.Fatalf("unexpected extra request for page token %q", got)
default:
}
}

func TestGroupsListSupportedQueryPagination(t *testing.T) {
for _, tc := range []struct {
name string
args []string
wantNames []string
wantPage string
wantCalls []string
}{
{name: "first page", wantNames: []string{"regular@example.com"}, wantPage: groupsQueryTestCursor, wantCalls: []string{""}},
{name: "all pages", args: []string{"--all"}, wantNames: []string{"regular@example.com", "dynamic@example.com"}, wantCalls: []string{"", groupsQueryTestCursor}},
{name: "cursor", args: []string{"--page", groupsQueryTestCursor}, wantNames: []string{"dynamic@example.com"}, wantCalls: []string{groupsQueryTestCursor}},
{name: "all from cursor", args: []string{"--all", "--page", groupsQueryTestCursor}, wantNames: []string{"dynamic@example.com"}, wantCalls: []string{groupsQueryTestCursor}},
} {
t.Run(tc.name, func(t *testing.T) {
args := append([]string{"--json", "--account", "person@example.com", "groups", "list", "--max", "1"}, tc.args...)
svc, requests := supportedGroupsQueryService(t, 1)
result := executeWithCloudIdentityTestService(t, args, svc)
if result.err != nil {
t.Fatalf("list groups: %v; stderr=%q", result.err, result.stderr)
}
var output struct {
Groups []struct {
GroupName string `json:"groupName"`
} `json:"groups"`
NextPageToken string `json:"nextPageToken"`
}
if err := json.Unmarshal([]byte(result.stdout), &output); err != nil {
t.Fatal(err)
}
if len(output.Groups) != len(tc.wantNames) || output.NextPageToken != tc.wantPage {
t.Fatalf("unexpected output: %s", result.stdout)
}
for i, name := range tc.wantNames {
if output.Groups[i].GroupName != name {
t.Fatalf("group %d = %q, want %q", i, output.Groups[i].GroupName, name)
}
}
assertGroupsQueryRequests(t, requests, tc.wantCalls)
})
}
}

func TestBackupGroupsSupportedQuery(t *testing.T) {
svc, requests := supportedGroupsQueryService(t, 1000)
groups, err := fetchBackupCloudIdentityGroups(context.Background(), svc, "person@example.com")
if err != nil {
t.Fatalf("fetch backup groups: %v", err)
}
if len(groups) != 2 || groups[0].GroupKey.Id != "dynamic@example.com" || groups[1].GroupKey.Id != "regular@example.com" {
t.Fatalf("unexpected groups: %#v", groups)
}
assertGroupsQueryRequests(t, requests, []string{"", groupsQueryTestCursor})
}
29 changes: 13 additions & 16 deletions internal/cmd/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,18 @@ func TestTruncate(t *testing.T) {
}

func TestSearchTransitiveGroupsQuery(t *testing.T) {
got := searchTransitiveGroupsQuery("person@example.com")
if !strings.Contains(got, "member_key_id == 'person@example.com'") {
t.Fatalf("missing member_key_id clause: %q", got)
}
if !strings.Contains(got, "'"+groupLabelDiscussionForum+"' in labels") {
t.Fatalf("missing discussion label clause: %q", got)
}
if !strings.Contains(got, "'"+groupLabelDynamic+"' in labels") {
t.Fatalf("missing dynamic label clause: %q", got)
}
}

func TestSearchTransitiveGroupsQuery_EscapesSingleQuote(t *testing.T) {
got := searchTransitiveGroupsQuery("o'connor@example.com")
if !strings.Contains(got, "member_key_id == 'o\\'connor@example.com'") {
t.Fatalf("expected escaped single quote: %q", got)
for _, tc := range []struct{ input, escaped string }{
{input: "person@example.com", escaped: "person@example.com"},
{input: " \tPerson@Example.com\n", escaped: "Person@Example.com"},
{input: "o'connor@example.com", escaped: `o\'connor@example.com`},
{input: `o\'connor@example.com`, escaped: `o\\\'connor@example.com`},
} {
t.Run(tc.input, func(t *testing.T) {
got := searchTransitiveGroupsQuery(tc.input)
want := "member_key_id == '" + tc.escaped + "' && 'cloudidentity.googleapis.com/groups.discussion_forum' in labels"
if got != want {
t.Fatalf("query = %q, want %q", got, want)
}
})
}
}
Loading