Skip to content

Commit

Permalink
Merge pull request #712 from ShubhamPalriwala/feature/671-support-gcp…
Browse files Browse the repository at this point in the history
…-custom-iam-roles

Add Support for Custom IAM roles in GCP
  • Loading branch information
mlabouardy authored Apr 11, 2023
2 parents 29fb8b3 + ac5a937 commit 9fc9093
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions providers/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
storage.Buckets,
bigquery.BigQueryTables,
certficate.Certificates,
iam.IamRoles,
iam.ServiceAccounts,
sql.SqlInstances,
}
Expand Down
58 changes: 58 additions & 0 deletions providers/gcp/iam/iam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package iam

import (
"context"
"fmt"
"strings"
"time"

"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
"google.golang.org/api/iam/v1"
"google.golang.org/api/option"
)

func IamRoles(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)

iamService, err := iam.NewService(ctx, option.WithCredentials(client.GCPClient.Credentials))
if err != nil {
logrus.WithError(err).Errorf("failed to create IAM roles service")
return resources, err
}

roles, err := iamService.Projects.Roles.List(
"projects/" + client.GCPClient.Credentials.ProjectID,
).Do()
if err != nil {
logrus.WithError(err).Errorf("failed to list IAM roles")
return resources, err
}

for _, role := range roles.Roles {
targetForUrl := strings.Replace(role.Name, "/", "<", -1)

resources = append(resources, models.Resource{
Provider: "GCP",
Account: client.Name,
Service: "IAM Roles",
ResourceId: role.Name,
Name: role.Title,
Metadata: map[string]string{
"Description": role.Description,
},
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://console.cloud.google.com/iam-admin/roles/details/%s?project=%s", targetForUrl, client.GCPClient.Credentials.ProjectID),
})
}
logrus.WithFields(logrus.Fields{
"provider": "GCP",
"account": client.Name,
"service": "IAM Custom Roles",
"resources": len(resources),
}).Info("Fetched resources")

return resources, nil

}

0 comments on commit 9fc9093

Please sign in to comment.