Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method to retrieve roles and service accounts #52

Merged
merged 4 commits into from
Feb 7, 2025
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
64 changes: 64 additions & 0 deletions cloudsupport/v1/gkesupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
container "cloud.google.com/go/container/apiv1"
"github.com/kubescape/k8s-interface/k8sinterface"
"golang.org/x/oauth2/google"
"google.golang.org/api/iam/v1"
"google.golang.org/api/option"
containerpb "google.golang.org/genproto/googleapis/container/v1"
)

Expand All @@ -18,6 +20,7 @@ type IGKESupport interface {
GetProject(cluster string) (string, error)
GetRegion(cluster string) (string, error)
GetContextName(cluster string) string
GetIAMMappings(project string) (map[string]string, map[string]string, error)
}
type GKESupport struct {
}
Expand Down Expand Up @@ -111,3 +114,64 @@ func (gkeSupport *GKESupport) GetContextName(cluster string) string {
}
return parsedName[3]
}

// GetIAMMappings returns iam-roles and service accounts
func (gkeSupport *GKESupport) GetIAMMappings(project string) (map[string]string, map[string]string, error) {
ctx := context.Background()
client, err := google.DefaultClient(ctx, iam.CloudPlatformScope)
if err != nil {
return nil, nil, fmt.Errorf("failed to create Google Cloud client: %w", err)
}
if err != nil {
return nil, nil, fmt.Errorf("failed to create Google Cloud client: %w", err)
}

iamService, err := iam.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return nil, nil, fmt.Errorf("failed to create IAM service client: %w", err)
}

roleMappings := make(map[string]string)
saMappings := make(map[string]string)

roleIterator, err := iamService.Projects.Roles.List("projects/" + project).Do()
if err != nil {
return nil, nil, fmt.Errorf("failed to retrieve roles: %w", err)
}
for {
for _, role := range roleIterator.Roles {
roleMappings[role.Name] = role.Title
}

if roleIterator.NextPageToken == "" {
break
}

roleIterator, err = iamService.Projects.Roles.List("projects/" + project).PageToken(roleIterator.NextPageToken).Do()
if err != nil {
return nil, nil, fmt.Errorf("failed to retrieve roles: %w", err)
}
}

saIterator, err := iamService.Projects.ServiceAccounts.List("projects/" + project).Do()
if err != nil {
return nil, nil, fmt.Errorf("failed to retrieve service accounts: %w", err)
}
for {

for _, sa := range saIterator.Accounts {
saMappings[sa.Name] = sa.Name
}

if saIterator.NextPageToken == "" {
break
}

saIterator, err = iamService.Projects.ServiceAccounts.List("projects/" + project).PageToken(saIterator.NextPageToken).Do()
if err != nil {
return nil, nil, fmt.Errorf("failed to retrieve service accounts: %w", err)
}
}

return roleMappings, saMappings, nil
}
4 changes: 4 additions & 0 deletions cloudsupport/v1/gkesupportmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ func (gkeSupportM *GKESupportMock) GetContextName(cluster string) string {
}
return parsedName[3]
}

func (gkeSupportM *GKESupportMock) GetIAMMappings(project string) (map[string]string, map[string]string, error) {
return nil, nil, nil
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cilium/cilium v1.16.6 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
Expand Down Expand Up @@ -92,6 +93,7 @@ require (
go.mongodb.org/mongo-driver v1.17.1 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.44.0 // indirect
go.opentelemetry.io/otel v1.33.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.41.0 // indirect
Expand Down Expand Up @@ -169,7 +171,7 @@ require (
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect
google.golang.org/api v0.171.0 // indirect
google.golang.org/api v0.171.0
google.golang.org/grpc v1.69.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
Loading