-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Listing all the predefined roles.
- Loading branch information
Showing
10 changed files
with
972 additions
and
717 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package rootcmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"go.einride.tech/iam/cmd/iamctl/internal/connection" | ||
"google.golang.org/genproto/googleapis/iam/admin/v1" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
) | ||
|
||
var listRolesCommand = &cobra.Command{ | ||
Use: "list-roles", | ||
Short: "List IAM roles", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
viperCfg := viper.New() | ||
if err := viperCfg.BindPFlags(cmd.Flags()); err != nil { | ||
return err | ||
} | ||
if err := viperCfg.BindPFlags(cmd.PersistentFlags()); err != nil { | ||
return err | ||
} | ||
var flags listRolesFlags | ||
if err := viperCfg.Unmarshal(&flags); err != nil { | ||
return err | ||
} | ||
conn, err := flags.Connect(cmd.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
_ = conn.Close() | ||
}() | ||
client := admin.NewIAMClient(conn) | ||
return runListRolesCommand(cmd.Context(), client, &flags) | ||
}, | ||
} | ||
|
||
type listRolesFlags struct { | ||
connection.Flags `mapstructure:",squash"` | ||
Full bool `mapstructure:"full"` | ||
} | ||
|
||
func init() { | ||
listRolesCommand.Flags().Bool("full", false, "list full roles") | ||
} | ||
|
||
func runListRolesCommand( | ||
ctx context.Context, | ||
client admin.IAMClient, | ||
flags *listRolesFlags, | ||
) error { | ||
var nextPageToken string | ||
var view admin.RoleView | ||
if flags.Full { | ||
view = admin.RoleView_FULL | ||
} | ||
for { | ||
response, err := client.ListRoles(ctx, &admin.ListRolesRequest{ | ||
PageToken: nextPageToken, | ||
View: view, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
for _, role := range response.Roles { | ||
fmt.Println(protojson.Format(role)) | ||
} | ||
nextPageToken = response.NextPageToken | ||
if nextPageToken == "" { | ||
break | ||
} | ||
} | ||
return nil | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package iammixin provides utilities for registering gRPC servers with IAM mixins. | ||
package iammixin |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package iammixin | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/genproto/googleapis/iam/admin/v1" | ||
"google.golang.org/genproto/googleapis/iam/v1" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// Server is an interface for servers that implement the essential IAM mixins. | ||
type Server interface { | ||
iam.IAMPolicyServer | ||
ListRoles(context.Context, *admin.ListRolesRequest) (*admin.ListRolesResponse, error) | ||
} | ||
|
||
// Register the IAM mixin server with the provided gRPC server. | ||
func Register(server *grpc.Server, serverImpl Server) { | ||
iam.RegisterIAMPolicyServer(server, serverImpl) | ||
admin.RegisterIAMServer(server, &adminAdapter{server: serverImpl}) | ||
} | ||
|
||
// adminAdapter provides unimplemented methods for the non-essential IAM admin mixins. | ||
type adminAdapter struct { | ||
admin.UnimplementedIAMServer | ||
server Server | ||
} | ||
|
||
// ListRoles implements admin.IAMServer. | ||
func (a *adminAdapter) ListRoles( | ||
ctx context.Context, | ||
request *admin.ListRolesRequest, | ||
) (*admin.ListRolesResponse, error) { | ||
return a.server.ListRoles(ctx, request) | ||
} | ||
|
||
// SetIamPolicy implements admin.IAMServer. | ||
func (a *adminAdapter) SetIamPolicy( | ||
ctx context.Context, | ||
request *iam.SetIamPolicyRequest, | ||
) (*iam.Policy, error) { | ||
return a.server.SetIamPolicy(ctx, request) | ||
} | ||
|
||
// GetIamPolicy implements admin.IAMServer. | ||
func (a *adminAdapter) GetIamPolicy( | ||
ctx context.Context, | ||
request *iam.GetIamPolicyRequest, | ||
) (*iam.Policy, error) { | ||
return a.server.GetIamPolicy(ctx, request) | ||
} | ||
|
||
// TestIamPermissions implements admin.IAMServer. | ||
func (a *adminAdapter) TestIamPermissions( | ||
ctx context.Context, | ||
request *iam.TestIamPermissionsRequest, | ||
) (*iam.TestIamPermissionsResponse, error) { | ||
return a.server.TestIamPermissions(ctx, request) | ||
} |
Oops, something went wrong.