-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #712 from ShubhamPalriwala/feature/671-support-gcp…
…-custom-iam-roles Add Support for Custom IAM roles in GCP
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 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
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 | ||
|
||
} |