Skip to content

Commit

Permalink
rebase current main and implement revoke
Browse files Browse the repository at this point in the history
  • Loading branch information
hirosassa committed Apr 6, 2022
1 parent 63073a8 commit 5fff1d8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
19 changes: 11 additions & 8 deletions bqrole/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func PermitDataset(role bq.AccessRole, project string, users, datasets []string,
return nil
}

func RevokeDataset(role bq.AccessRole, project string, users, datasets []string) error {
func RevokeDataset(role bq.AccessRole, project string, users, datasets []string, yes bool) error {
ctx := context.Background()
client, err := bq.NewClient(ctx, project)
if err != nil {
Expand All @@ -103,14 +103,17 @@ func RevokeDataset(role bq.AccessRole, project string, users, datasets []string)
fmt.Printf("role: %s\n", role)
fmt.Printf("datasets: %s\n", datasets)
fmt.Printf("users: %s\n", users)
fmt.Printf("Are you sure? [y/n]")

reader := bufio.NewReader(os.Stdin)
res, err := reader.ReadString('\n')
if !yes {
fmt.Printf("Are you sure? [y/n]")

if err != nil || strings.TrimSpace(res) != "y" {
fmt.Println("Abort.")
return nil
reader := bufio.NewReader(os.Stdin)
res, err := reader.ReadString('\n')

if err != nil || strings.TrimSpace(res) != "y" {
fmt.Println("Abort.")
return nil
}
}

defer client.Close()
Expand Down Expand Up @@ -207,7 +210,7 @@ func revokeDatasetPermission(ctx context.Context, client *bq.Client, role bq.Acc
return nil
}

func hasBQRole(p ProjectPolicy, user string, role string) bool {
func hasBQRole(p *ProjectPolicy, user string, role string) bool {
for _, b := range p.Bindings {
if b.Role == role {
for _, m := range b.Members {
Expand Down
17 changes: 10 additions & 7 deletions bqrole/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func PermitProject(role, project string, users []string, yes bool) error {
return nil
}

func RevokeProject(role, project string, users []string) error {
func RevokeProject(role, project string, users []string, yes bool) error {
ctx := context.Background()
client, err := bq.NewClient(ctx, project)
if err != nil {
Expand All @@ -78,14 +78,17 @@ func RevokeProject(role, project string, users []string) error {
fmt.Printf("project_id: %s\n", project)
fmt.Printf("role: %s\n", role)
fmt.Printf("users: %s\n", users)
fmt.Printf("If you proceeds, PROJECT-WIDE permission will be added. Are you sure? [y/n]")

reader := bufio.NewReader(os.Stdin)
res, err := reader.ReadString('\n')
if !yes {
fmt.Printf("If you proceeds, PROJECT-WIDE permission will be added. Are you sure? [y/n]")

if err != nil || strings.TrimSpace(res) != "y" {
fmt.Println("Abort.")
return nil
reader := bufio.NewReader(os.Stdin)
res, err := reader.ReadString('\n')

if err != nil || strings.TrimSpace(res) != "y" {
fmt.Println("Abort.")
return nil
}
}

defer client.Close()
Expand Down
23 changes: 21 additions & 2 deletions cmd/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ bqiam revoke project READER -p bq-project-id -u user1@email.com
},
}

cmd.PersistentFlags().BoolP("yes", "y", false, "Automatic yes to prompts")
cmd.AddCommand(
newRevokeDatasetCmd(),
newRevokeProjectCmd(),
)

return cmd
Expand All @@ -69,6 +71,9 @@ bqiam project READER -p bq-project-id -u user1@email.com -u user2@email.com`,

cmd.Flags().StringSliceP("users", "u", []string{}, "Specify user email(s)")

_ = registerProjectsCompletions(cmd)
_ = registerUsersCompletions(cmd)

return cmd
}

Expand All @@ -92,7 +97,12 @@ func runRevokeProjectCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to parse users flag: %s", err)
}

err = bqrole.RevokeProject(role, project, users)
yes, err := cmd.Flags().GetBool("yes")
if err != nil {
return fmt.Errorf("failed to parse yes flag: %s", err)
}

err = bqrole.RevokeProject(role, project, users, yes)
if err != nil {
return fmt.Errorf("failed to revoke: %s", err)
}
Expand Down Expand Up @@ -120,6 +130,10 @@ bqiam dataset READER -p bq-project-id -u user1@email.com -u user2@email.com -d d
cmd.Flags().StringSliceP("users", "u", []string{}, "Specify user email(s)")
cmd.Flags().StringSliceP("datasets", "d", []string{}, "Specify dataset(s)")

_ = registerProjectsCompletions(cmd)
_ = registerDatasetsCompletions(cmd)
_ = registerUsersCompletions(cmd)

return cmd
}

Expand Down Expand Up @@ -148,7 +162,12 @@ func runRevokeDatasetCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to parse datasets flag: %s", err)
}

err = bqrole.RevokeDataset(role, project, users, datasets)
yes, err := cmd.Flags().GetBool("yes")
if err != nil {
return fmt.Errorf("failed to parse yes flag: %s", err)
}

err = bqrole.RevokeDataset(role, project, users, datasets, yes)
if err != nil {
return fmt.Errorf("failed to revoke: %s", err)
}
Expand Down

0 comments on commit 5fff1d8

Please sign in to comment.