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

fix: sent integration labels and annotations to describers #1942

Merged
merged 1 commit into from
Nov 11, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ require (
github.com/lib/pq v1.10.9
github.com/nats-io/nats.go v1.36.0
github.com/open-policy-agent/opa v0.69.0
github.com/opengovern/og-util v1.1.0
github.com/opengovern/og-util v1.1.1
github.com/opengovern/plugin-aws v0.7.3
github.com/opengovern/plugin-gcp v0.0.0-20241014134959-2c0f222fc07b
github.com/opengovern/plugin-kubernetes-internal v0.18.12
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ github.com/opencontainers/runc v1.2.0 h1:qke7ZVCmJcKrJVY2iHJVC+0kql9uYdkusOPsQOO
github.com/opencontainers/runc v1.2.0/go.mod h1:/PXzF0h531HTMsYQnmxXkBD7YaGShm/2zcRB79dksUc=
github.com/opengovern/og-util v1.1.0 h1:bV2XKX8aIpJGC2CR4kh9CU/rJWPQed76gtVXM2zBDXM=
github.com/opengovern/og-util v1.1.0/go.mod h1:dyn8rhmxq59o1jnbgGfmcUvW7iB/eN6OxoTUUx6jEHA=
github.com/opengovern/og-util v1.1.1 h1:SXnPCNuhR9WxxupEa1hCtIOIh2Sy+MF80qVdpoxc58k=
github.com/opengovern/og-util v1.1.1/go.mod h1:dyn8rhmxq59o1jnbgGfmcUvW7iB/eN6OxoTUUx6jEHA=
github.com/opengovern/plugin-aws v0.7.3 h1:76hZOjulNlgn4uaq5lq1/pmGmgJqvX1ZQbgqcQn03gI=
github.com/opengovern/plugin-aws v0.7.3/go.mod h1:zfTMswfCyXZ0gD6SDCsmKg55LseXzeFzOH4jXn2QJVo=
github.com/opengovern/plugin-gcp v0.0.0-20241014134959-2c0f222fc07b h1:4xP98kDpOXUu6RcFJyZN63OeA2I26MLS+dEB9JWYQpY=
Expand Down
25 changes: 14 additions & 11 deletions pkg/describe/scheduler_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *Scheduler) RunDescribeResourceJobCycle(ctx context.Context, manuals boo
cred: credential,
}
wp.AddJob(func() (interface{}, error) {
err := s.enqueueCloudNativeDescribeJob(ctx, c.dc, c.cred.Secret)
err := s.enqueueCloudNativeDescribeJob(ctx, c.dc, c.cred.Secret, c.src)
if err != nil {
s.logger.Error("Failed to enqueueCloudNativeDescribeConnectionJob", zap.Error(err), zap.Uint("jobID", dc.ID))
DescribeResourceJobsCount.WithLabelValues("failure", "enqueue").Inc()
Expand Down Expand Up @@ -367,7 +367,8 @@ func newDescribeConnectionJob(a integrationapi.Integration, resourceType string,
}
}

func (s *Scheduler) enqueueCloudNativeDescribeJob(ctx context.Context, dc model.DescribeIntegrationJob, cipherText string) error {
func (s *Scheduler) enqueueCloudNativeDescribeJob(ctx context.Context, dc model.DescribeIntegrationJob, cipherText string,
integration *integrationapi.Integration) error {
ctx, span := otel.Tracer(opengovernanceTrace.JaegerTracerName).Start(ctx, opengovernanceTrace.GetCurrentFuncName())
defer span.End()

Expand Down Expand Up @@ -395,15 +396,17 @@ func (s *Scheduler) enqueueCloudNativeDescribeJob(ctx context.Context, dc model.
VaultConfig: s.conf.Vault,

DescribeJob: describe.DescribeJob{
JobID: dc.ID,
ResourceType: dc.ResourceType,
IntegrationID: dc.IntegrationID,
ProviderID: dc.ProviderID,
DescribedAt: dc.CreatedAt.UnixMilli(),
IntegrationType: dc.IntegrationType,
CipherText: cipherText,
TriggerType: dc.TriggerType,
RetryCounter: 0,
JobID: dc.ID,
ResourceType: dc.ResourceType,
IntegrationID: dc.IntegrationID,
ProviderID: dc.ProviderID,
DescribedAt: dc.CreatedAt.UnixMilli(),
IntegrationType: dc.IntegrationType,
CipherText: cipherText,
IntegrationLabels: integration.Labels,
IntegrationAnnotations: integration.Annotations,
TriggerType: dc.TriggerType,
RetryCounter: 0,
},
}

Expand Down
55 changes: 6 additions & 49 deletions services/integration/api/integrations/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,30 +201,6 @@ func (h API) DiscoverIntegrations(c echo.Context) error {

var integrationsAPI []models.Integration
for _, i := range integrations {
annotations, err := integration.GetAnnotations(jsonData)
if err != nil {
h.logger.Error("failed to get annotations", zap.Error(err))
}
annotationsJsonData, err := json.Marshal(annotations)
if err != nil {
return err
}
integrationAnnotationsJsonb := pgtype.JSONB{}
err = integrationAnnotationsJsonb.Set(annotationsJsonData)
i.Annotations = integrationAnnotationsJsonb

labels, err := integration.GetLabels(jsonData)
if err != nil {
h.logger.Error("failed to get labels", zap.Error(err))
}
labelsJsonData, err := json.Marshal(labels)
if err != nil {
return err
}
integrationLabelsJsonb := pgtype.JSONB{}
err = integrationLabelsJsonb.Set(labelsJsonData)
i.Labels = integrationLabelsJsonb

integrationAPI, err := i.ToApi()
if err != nil {
h.logger.Error("failed to create integration api", zap.Error(err))
Expand Down Expand Up @@ -316,34 +292,15 @@ func (h API) AddIntegrations(c echo.Context) error {

i.CredentialID = credentialID

annotations, err := integration.GetAnnotations(jsonData)
if err != nil {
h.logger.Error("failed to get annotations", zap.Error(err))
}
annotationsJsonData, err := json.Marshal(annotations)
if err != nil {
return err
}
integrationAnnotationsJsonb := pgtype.JSONB{}
err = integrationAnnotationsJsonb.Set(annotationsJsonData)
i.Annotations = integrationAnnotationsJsonb

labels, err := integration.GetLabels(jsonData)
if err != nil {
h.logger.Error("failed to get labels", zap.Error(err))
}
labelsJsonData, err := json.Marshal(labels)
if err != nil {
return err
}
integrationLabelsJsonb := pgtype.JSONB{}
err = integrationLabelsJsonb.Set(labelsJsonData)
i.Labels = integrationLabelsJsonb

healthcheckTime := time.Now()
i.LastCheck = &healthcheckTime

healthy, err := integration.HealthCheck(jsonData, i.ProviderID, labels)
iApi, err := i.ToApi()
if err != nil {
h.logger.Error("failed to create integration api", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to create integration api")
}
healthy, err := integration.HealthCheck(jsonData, i.ProviderID, iApi.Annotations)
if err != nil || !healthy {
h.logger.Info("integration is not healthy", zap.String("integration_id", i.IntegrationID.String()), zap.Error(err))
i.State = models2.IntegrationStateInactive
Expand Down
64 changes: 36 additions & 28 deletions services/integration/integration-type/aws-account/aws_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package aws_account

import (
"encoding/json"
"fmt"
"github.com/jackc/pgtype"
awsDescriberLocal "github.com/opengovern/opengovernance/services/integration/integration-type/aws-account/configs"
"github.com/opengovern/opengovernance/services/integration/integration-type/aws-account/discovery"
"github.com/opengovern/opengovernance/services/integration/integration-type/aws-account/healthcheck"
Expand All @@ -23,31 +25,19 @@ func (i *AwsCloudAccountIntegration) GetDescriberConfiguration() interfaces.Desc
}
}

func (i *AwsCloudAccountIntegration) GetAnnotations(jsonData []byte) (map[string]string, error) {
annotations := make(map[string]string)

return annotations, nil
}

func (i *AwsCloudAccountIntegration) GetLabels(jsonData []byte) (map[string]string, error) {
annotations := make(map[string]string)

return annotations, nil
}

func (i *AwsCloudAccountIntegration) HealthCheck(jsonData []byte, providerId string, labels map[string]string) (bool, error) {
func (i *AwsCloudAccountIntegration) HealthCheck(jsonData []byte, providerId string, labels map[string]string, annotations map[string]string) (bool, error) {
var credentials awsDescriberLocal.IntegrationCredentials
err := json.Unmarshal(jsonData, &credentials)
if err != nil {
return false, err
}

return healthcheck.AWSIntegrationHealthCheck(healthcheck.Config{
AWSAccessKeyID: credentials.AwsAccessKeyID,
AWSSecretAccessKey: credentials.AwsSecretAccessKey,
RoleToAssumeInMainAccount: credentials.RoleToAssumeInMainAccount,
CrossAccountRole: credentials.CrossAccountRoleName,
ExternalID: credentials.ExternalID,
return healthcheck.AWSIntegrationHealthCheck(healthcheck.AWSConfigInput{
AccessKeyID: credentials.AwsAccessKeyID,
SecretAccessKey: credentials.AwsSecretAccessKey,
RoleNameInPrimaryAccount: credentials.RoleToAssumeInMainAccount,
CrossAccountRoleARN: credentials.CrossAccountRoleName,
ExternalID: credentials.ExternalID,
}, providerId)
}

Expand All @@ -59,20 +49,38 @@ func (i *AwsCloudAccountIntegration) DiscoverIntegrations(jsonData []byte) ([]mo
}

var integrations []models.Integration
accounts, err := discovery.AWSIntegrationDiscovery(discovery.Config{
AWSAccessKeyID: credentials.AwsAccessKeyID,
AWSSecretAccessKey: credentials.AwsSecretAccessKey,
RoleToAssumeInMainAccount: credentials.RoleToAssumeInMainAccount,
CrossAccountRole: credentials.CrossAccountRoleName,
ExternalID: credentials.ExternalID,
accounts := discovery.AWSIntegrationDiscovery(discovery.Config{
AWSAccessKeyID: credentials.AwsAccessKeyID,
AWSSecretAccessKey: credentials.AwsSecretAccessKey,
RoleNameToAssumeInMainAccount: credentials.RoleToAssumeInMainAccount,
CrossAccountRoleName: credentials.CrossAccountRoleName,
ExternalID: credentials.ExternalID,
})
if err != nil {
return nil, err
}
for _, a := range accounts {
if a.Details.Error != "" {
return nil, fmt.Errorf(a.Details.Error)
}

labels := map[string]string{
"RoleNameInMainAccount": a.Labels.RoleNameInMainAccount,
"AccountType": a.Labels.AccountType,
"CrossAccountRoleARN": a.Labels.CrossAccountRoleARN,
"ExternalID": a.Labels.ExternalID,
}
labelsJsonData, err := json.Marshal(labels)
if err != nil {
return nil, err
}
integrationLabelsJsonb := pgtype.JSONB{}
err = integrationLabelsJsonb.Set(labelsJsonData)
if err != nil {
return nil, err
}

integrations = append(integrations, models.Integration{
ProviderID: a.AccountID,
Name: a.AccountName,
Labels: integrationLabelsJsonb,
})
}

Expand Down
Loading