From 34cf976364a84424a7303fb98e9d11952fa95fb2 Mon Sep 17 00:00:00 2001 From: Mohamed Labouardy Date: Fri, 14 Apr 2023 15:43:59 +0200 Subject: [PATCH 1/2] fix: conflict --- providers/gcp/gateway/gateways.go | 107 ++++++++++++++++++++++++++++++ providers/gcp/gcp.go | 2 + 2 files changed, 109 insertions(+) create mode 100644 providers/gcp/gateway/gateways.go diff --git a/providers/gcp/gateway/gateways.go b/providers/gcp/gateway/gateways.go new file mode 100644 index 000000000..eaf2e0d38 --- /dev/null +++ b/providers/gcp/gateway/gateways.go @@ -0,0 +1,107 @@ +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 { + fmt.Printf("%+v\n", apiGateway) + + layout := "2006-01-02T15:04:05.999999999Z" + parsedCreatedTime, err := time.Parse(layout, 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 +} diff --git a/providers/gcp/gcp.go b/providers/gcp/gcp.go index f57a2d61c..f12421013 100644 --- a/providers/gcp/gcp.go +++ b/providers/gcp/gcp.go @@ -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" @@ -31,6 +32,7 @@ func listOfSupportedServices() []providers.FetchDataFunction { redis.Instances, container.Clusters, kms.Keys, + gateway.ApiGateways, } } From add89c6736a23511d080e10342742709966ccbb8 Mon Sep 17 00:00:00 2001 From: Mohamed Labouardy Date: Fri, 14 Apr 2023 15:42:49 +0200 Subject: [PATCH 2/2] fix: use rfc layout vs hardcoded --- providers/gcp/gateway/gateways.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/providers/gcp/gateway/gateways.go b/providers/gcp/gateway/gateways.go index eaf2e0d38..d14461bc1 100644 --- a/providers/gcp/gateway/gateways.go +++ b/providers/gcp/gateway/gateways.go @@ -44,10 +44,7 @@ RegionsLoop: } for _, apiGateway := range apiGateways.Gateways { - fmt.Printf("%+v\n", apiGateway) - - layout := "2006-01-02T15:04:05.999999999Z" - parsedCreatedTime, err := time.Parse(layout, apiGateway.CreateTime) + 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