Created
April 1, 2020 13:30
-
-
Save salrashid123/43dfed22880ac91a079818da7ba549c0 to your computer and use it in GitHub Desktop.
Gsuites domain wide delegation/impersonation
This file contains 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 | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"context" | |
"cloud.google.com/go/storage" | |
"golang.org/x/oauth2/google" | |
admin "google.golang.org/api/admin/directory/v1" | |
driveactivity "google.golang.org/api/driveactivity/v2" | |
"google.golang.org/api/iterator" | |
"google.golang.org/api/option" | |
) | |
func main() { | |
serviceAccountFile := "/path/to/your/svc.json" | |
serviceAccountJSON, err := ioutil.ReadFile(serviceAccountFile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
config, err := google.JWTConfigFromJSON(serviceAccountJSON, | |
admin.AdminDirectoryUserScope, driveactivity.DriveActivityReadonlyScope, "https://www.googleapis.com/auth/cloud-platform", | |
) | |
config.Subject = "[email protected]" | |
ctx := context.Background() | |
storageClient, err := storage.NewClient(ctx, option.WithTokenSource(config.TokenSource(ctx))) | |
if err != nil { | |
log.Fatalf("%v", err) | |
} | |
bucketName := "somebucketName" | |
it := storageClient.Bucket(bucketName).Objects(ctx, nil) | |
for { | |
attrs, err := it.Next() | |
if err == iterator.Done { | |
break | |
} | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println(attrs.Name) | |
} | |
srv, err := admin.New(config.Client(context.Background())) | |
if err != nil { | |
log.Fatal(err) | |
} | |
usersReport, err := srv.Users.List().Customer("yourcxnumber").MaxResults(10).OrderBy("email").Do() | |
if err != nil { | |
log.Fatal(err) | |
} | |
if len(usersReport.Users) == 0 { | |
fmt.Print("No users found.\n") | |
} else { | |
fmt.Print("Users:\n") | |
for _, u := range usersReport.Users { | |
fmt.Printf("%s (%s)\n", u.PrimaryEmail, u.Name.FullName) | |
} | |
} | |
// ************************************** | |
//https://developers.google.com/admin-sdk/reports/v1/appendix/activity/drive | |
dsrv, err := driveactivity.New(config.Client(context.Background())) | |
if err != nil { | |
log.Fatalf("Unable to retrieve driveactivity Client %v", err) | |
} | |
q := driveactivity.QueryDriveActivityRequest{PageSize: 10} | |
r, err := dsrv.Activity.Query(&q).Do() | |
if err != nil { | |
log.Fatalf("Unable to retrieve list of activities. %v", err) | |
} | |
fmt.Println("Recent Activity:") | |
if len(r.Activities) > 0 { | |
for _, a := range r.Activities { | |
log.Printf("%v, \n", a) | |
} | |
} else { | |
fmt.Print("No activity.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment