Last active
May 12, 2022 14:18
-
-
Save salrashid123/7e3c52292eef0310ef34a398439dccb5 to your computer and use it in GitHub Desktop.
Cloud Identity SearchTransitive Groups in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
see https://blog.salrashid.dev/articles/2022/search_group_membership/ | |
*/ | |
import ( | |
"fmt" | |
"log" | |
"golang.org/x/oauth2/google" | |
"google.golang.org/api/cloudidentity/v1" | |
"google.golang.org/api/option" | |
) | |
func main() { | |
// ctx := context.Background() | |
// serviceAccountFile := "/home/srashid/gcp_misc/certs/google_apps_svc_dwd.json" | |
// serviceAccountJSON, err := ioutil.ReadFile(serviceAccountFile) | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
// config, err := google.JWTConfigFromJSON(serviceAccountJSON, cloudidentity.CloudPlatformScope, cloudidentity.CloudIdentityGroupsReadonlyScope) | |
// if err != nil { | |
// log.Fatal(err) | |
// } | |
// ts := config.TokenSource(ctx) | |
ts, err := google.DefaultTokenSource(ctx) | |
if err != nil { | |
log.Fatal(err) | |
} | |
cisvc, err := cloudidentity.NewService(ctx, option.WithTokenSource(ts)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
//member_key_namespace | |
err = cisvc.Groups.Memberships.SearchTransitiveGroups("groups/-").Query("member_key_id=='[email protected]' && 'cloudidentity.googleapis.com/groups.discussion_forum' in labels").Pages(ctx, func(g *cloudidentity.SearchTransitiveGroupsResponse) error { | |
//err = cisvc.Groups.Memberships.SearchTransitiveGroups("groups/-").Query("member_key_id=='[email protected]' && 'cloudidentity.googleapis.com/groups.discussion_forum' in labels").Pages(ctx, func(g *cloudidentity.SearchTransitiveGroupsResponse) error { | |
for _, m := range g.Memberships { | |
fmt.Printf("%s (%s)\n", m.GroupKey.Id, m.DisplayName) | |
} | |
return nil | |
}) | |
if err != nil { | |
fmt.Printf("%v", err) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment