Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Support for API Gateways in GCP #715

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
104 changes: 104 additions & 0 deletions providers/gcp/gateway/gateways.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package gateway

import (
"context"
"fmt"
"time"

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

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

regions, err := listGCPRegions(client.GCPClient.Credentials.ProjectID, option.WithCredentials(client.GCPClient.Credentials))
if err != nil {
logrus.WithError(err).Errorf("failed to list zones to fetch api gateways")
return resources, err
}

apiGatewayService, err := apigateway.NewService(ctx, option.WithCredentials(client.GCPClient.Credentials))
if err != nil {
logrus.WithError(err).Errorf("failed to create API Gateway service")
return resources, err
}

RegionsLoop:
for _, regionName := range regions {
apiGateways, err := apiGatewayService.Projects.Locations.Gateways.List(
"projects/" + client.GCPClient.Credentials.ProjectID + "/locations/" + regionName,
).Do()
if err != nil {
if err.Error() == "googleapi: Error 403: Location "+regionName+" is not found or access is unauthorized., forbidden" {
continue RegionsLoop
} else {
logrus.WithError(err).Errorf("failed to list API Gateways")
return resources, err

}
}

for _, apiGateway := range apiGateways.Gateways {
parsedCreatedTime, err := time.Parse(time.RFC3339Nano, apiGateway.CreateTime)
if err != nil {
logrus.WithError(err).Errorf("failed to parse create time for API Gateways")
return resources, err
}

resources = append(resources, models.Resource{
Provider: "GCP",
Account: client.Name,
Service: "API Gateways",
ResourceId: apiGateway.Name,
Name: apiGateway.DisplayName,
CreatedAt: parsedCreatedTime,
Region: regionName,
Metadata: map[string]string{
"API Config": apiGateway.ApiConfig,
"Default Hostname": apiGateway.DefaultHostname,
"State": apiGateway.State,
},
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://console.cloud.google.com/api-gateway/gateway/%s/location/%s?project=%s", apiGateway.DisplayName, regionName, client.GCPClient.Credentials.ProjectID),
})

}

}

logrus.WithFields(logrus.Fields{
"provider": "GCP",
"account": client.Name,
"service": "API Gateway",
"resources": len(resources),
}).Info("Fetched resources")

return resources, nil
}

func listGCPRegions(projectId string, creds option.ClientOption) ([]string, error) {
var regions []string

ctx := context.Background()
computeService, err := compute.NewService(ctx, creds)
if err != nil {
logrus.WithError(err).Errorf("failed to create new service for fetching GCP regions for api gateway")
return nil, err
}

regionList, err := computeService.Regions.List(projectId).Do()
if err != nil {
logrus.WithError(err).Errorf("failed to list regions for fetching GCP regions for api gateway")
return nil, err
}

for _, region := range regionList.Items {
regions = append(regions, region.Name)
}
return regions, nil
}
2 changes: 2 additions & 0 deletions providers/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
certficate "github.com/tailwarden/komiser/providers/gcp/certificate"
"github.com/tailwarden/komiser/providers/gcp/compute"
"github.com/tailwarden/komiser/providers/gcp/container"
"github.com/tailwarden/komiser/providers/gcp/gateway"
"github.com/tailwarden/komiser/providers/gcp/iam"
"github.com/tailwarden/komiser/providers/gcp/kms"
"github.com/tailwarden/komiser/providers/gcp/redis"
Expand All @@ -31,6 +32,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
redis.Instances,
container.Clusters,
kms.Keys,
gateway.ApiGateways,
}
}

Expand Down