-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: added tailscale and fly integrations #2455
Open
ArshiaBP
wants to merge
1
commit into
main
Choose a base branch
from
feat_tailscale_fly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
services/integration/integration-type/fly-account/configs/credentials.go
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,5 @@ | ||
package configs | ||
|
||
type IntegrationCredentials struct { | ||
Token string `json:"token"` | ||
} |
6 changes: 6 additions & 0 deletions
6
services/integration/integration-type/fly-account/configs/deployment.go
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,6 @@ | ||
package configs | ||
|
||
const ( | ||
DescriberDeploymentName = "og-describer-fly" | ||
DescriberRunCommand = "/og-describer-fly" | ||
) |
7 changes: 7 additions & 0 deletions
7
services/integration/integration-type/fly-account/configs/general.go
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,7 @@ | ||
package configs | ||
|
||
import "github.com/opengovern/og-util/pkg/integration" | ||
|
||
const ( | ||
IntegrationTypeFlyAccount = integration.Type("fly_account") // example: AWS_ACCOUNT, AZURE_SUBSCRIPTION | ||
) |
9 changes: 9 additions & 0 deletions
9
services/integration/integration-type/fly-account/configs/nats.go
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,9 @@ | ||
package configs | ||
|
||
const ( | ||
StreamName = "og_describer_fly" | ||
JobQueueTopic = "og_describer_fly_job_queue" | ||
ConsumerGroup = "describer-fly" | ||
JobQueueTopicManuals = "og_describer_fly_manuals_job_queue" | ||
ConsumerGroupManuals = "describer-fly-manuals" | ||
) |
15 changes: 15 additions & 0 deletions
15
services/integration/integration-type/fly-account/configs/resource_types_list.go
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,15 @@ | ||
package configs | ||
|
||
var TablesToResourceTypes = map[string]string{ | ||
"fly_app": "Fly/App", | ||
"fly_machine": "Fly/Machine", | ||
"fly_volume": "Fly/Volume", | ||
"fly_secret": "Fly/Secret", | ||
} | ||
|
||
var ResourceTypesList = []string{ | ||
"Fly/App", | ||
"Fly/Machine", | ||
"Fly/Volume", | ||
"Fly/Secret", | ||
} |
59 changes: 59 additions & 0 deletions
59
services/integration/integration-type/fly-account/discovery/fly_integration_discovery.go
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,59 @@ | ||
package discovery | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// Config represents the JSON input configuration | ||
type Config struct { | ||
Token string `json:"token"` | ||
} | ||
|
||
type App struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Status string `json:"status"` | ||
} | ||
|
||
type Response struct { | ||
Apps []App `json:"apps"` | ||
} | ||
|
||
// Discover retrieves fly user info | ||
func Discover(token string) ([]App, error) { | ||
var response Response | ||
|
||
url := "https://api.machines.dev/v1/apps?org_slug=personal" | ||
|
||
client := http.DefaultClient | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("request execution failed: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil { | ||
return nil, fmt.Errorf("failed to decode response: %w", err) | ||
} | ||
|
||
return response.Apps, nil | ||
} | ||
|
||
func FlyIntegrationDiscovery(cfg Config) ([]App, error) { | ||
// Check for the token | ||
if cfg.Token == "" { | ||
return nil, errors.New("token must be configured") | ||
} | ||
|
||
return Discover(cfg.Token) | ||
} |
104 changes: 104 additions & 0 deletions
104
services/integration/integration-type/fly-account/fly_account.go
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,104 @@ | ||
package doppler_account | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/jackc/pgtype" | ||
flyDescriberLocal "github.com/opengovern/opencomply/services/integration/integration-type/fly-account/configs" | ||
"github.com/opengovern/opencomply/services/integration/integration-type/fly-account/discovery" | ||
"github.com/opengovern/opencomply/services/integration/integration-type/fly-account/healthcheck" | ||
"github.com/opengovern/opencomply/services/integration/integration-type/interfaces" | ||
"github.com/opengovern/opencomply/services/integration/models" | ||
) | ||
|
||
type FlyAccountIntegration struct{} | ||
|
||
func (i *FlyAccountIntegration) GetConfiguration() interfaces.IntegrationConfiguration { | ||
return interfaces.IntegrationConfiguration{ | ||
NatsScheduledJobsTopic: flyDescriberLocal.JobQueueTopic, | ||
NatsManualJobsTopic: flyDescriberLocal.JobQueueTopicManuals, | ||
NatsStreamName: flyDescriberLocal.StreamName, | ||
NatsConsumerGroup: flyDescriberLocal.ConsumerGroup, | ||
NatsConsumerGroupManuals: flyDescriberLocal.ConsumerGroupManuals, | ||
|
||
SteampipePluginName: "fly", | ||
|
||
UISpecFileName: "fly-account.json", | ||
|
||
DescriberDeploymentName: flyDescriberLocal.DescriberDeploymentName, | ||
DescriberRunCommand: flyDescriberLocal.DescriberRunCommand, | ||
} | ||
} | ||
|
||
func (i *FlyAccountIntegration) HealthCheck(jsonData []byte, providerId string, labels map[string]string, annotations map[string]string) (bool, error) { | ||
var credentials flyDescriberLocal.IntegrationCredentials | ||
err := json.Unmarshal(jsonData, &credentials) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
var appName string | ||
if v, ok := labels["AppName"]; ok { | ||
appName = v | ||
} | ||
isHealthy, err := healthcheck.FlyIntegrationHealthcheck(healthcheck.Config{ | ||
Token: credentials.Token, | ||
AppName: appName, | ||
}) | ||
return isHealthy, err | ||
} | ||
|
||
func (i *FlyAccountIntegration) DiscoverIntegrations(jsonData []byte) ([]models.Integration, error) { | ||
var credentials flyDescriberLocal.IntegrationCredentials | ||
err := json.Unmarshal(jsonData, &credentials) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var integrations []models.Integration | ||
apps, err := discovery.FlyIntegrationDiscovery(discovery.Config{ | ||
Token: credentials.Token, | ||
}) | ||
for _, app := range apps { | ||
labels := map[string]string{ | ||
"AppName": app.Name, | ||
"Status": app.Status, | ||
} | ||
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: app.ID, | ||
Name: app.Name, | ||
Labels: integrationLabelsJsonb, | ||
}) | ||
} | ||
return integrations, nil | ||
} | ||
|
||
func (i *FlyAccountIntegration) GetResourceTypesByLabels(labels map[string]string) (map[string]*interfaces.ResourceTypeConfiguration, error) { | ||
resourceTypesMap := make(map[string]*interfaces.ResourceTypeConfiguration) | ||
for _, resourceType := range flyDescriberLocal.ResourceTypesList { | ||
resourceTypesMap[resourceType] = nil | ||
} | ||
return resourceTypesMap, nil | ||
} | ||
|
||
func (i *FlyAccountIntegration) GetResourceTypeFromTableName(tableName string) string { | ||
if v, ok := flyDescriberLocal.TablesToResourceTypes[tableName]; ok { | ||
return v | ||
} | ||
return "" | ||
} | ||
|
||
func (i *FlyAccountIntegration) GetTablesByLabels(map[string]string) ([]string, error) { | ||
var tables []string | ||
for t, _ := range flyDescriberLocal.TablesToResourceTypes { | ||
tables = append(tables, t) | ||
} | ||
return tables, nil | ||
} |
65 changes: 65 additions & 0 deletions
65
services/integration/integration-type/fly-account/healthcheck/fly_integration_healthcheck.go
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,65 @@ | ||
package healthcheck | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// Config represents the JSON input configuration | ||
type Config struct { | ||
Token string `json:"token"` | ||
AppName string `json:"app_name"` | ||
} | ||
|
||
type App struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Status string `json:"status"` | ||
} | ||
|
||
// IsHealthy checks if the JWT has read access to all required resources | ||
func IsHealthy(token, appName string) error { | ||
var app App | ||
|
||
url := fmt.Sprintf("https://api.machines.dev/v1/apps/%s", appName) | ||
|
||
client := http.DefaultClient | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("request execution failed: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if err = json.NewDecoder(resp.Body).Decode(&app); err != nil { | ||
return fmt.Errorf("failed to decode response: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func FlyIntegrationHealthcheck(cfg Config) (bool, error) { | ||
// Check for the token | ||
if cfg.Token == "" { | ||
return false, errors.New("token must be configured") | ||
} | ||
|
||
if cfg.AppName == "" { | ||
return false, errors.New("app name must be configured") | ||
} | ||
|
||
err := IsHealthy(cfg.Token, cfg.AppName) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} |
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
5 changes: 5 additions & 0 deletions
5
services/integration/integration-type/tailscale-account/configs/credentials.go
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,5 @@ | ||
package configs | ||
|
||
type IntegrationCredentials struct { | ||
Token string `json:"token"` | ||
} |
6 changes: 6 additions & 0 deletions
6
services/integration/integration-type/tailscale-account/configs/deployment.go
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,6 @@ | ||
package configs | ||
|
||
const ( | ||
DescriberDeploymentName = "og-describer-tailscale" | ||
DescriberRunCommand = "/og-describer-tailscale" | ||
) |
7 changes: 7 additions & 0 deletions
7
services/integration/integration-type/tailscale-account/configs/general.go
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,7 @@ | ||
package configs | ||
|
||
import "github.com/opengovern/og-util/pkg/integration" | ||
|
||
const ( | ||
IntegrationTypeTailScaleAccount = integration.Type("tailscale_account") // example: AWS_ACCOUNT, AZURE_SUBSCRIPTION | ||
) |
9 changes: 9 additions & 0 deletions
9
services/integration/integration-type/tailscale-account/configs/nats.go
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,9 @@ | ||
package configs | ||
|
||
const ( | ||
StreamName = "og_describer_tailscale" | ||
JobQueueTopic = "og_describer_tailscale_job_queue" | ||
ConsumerGroup = "describer-tailscale" | ||
JobQueueTopicManuals = "og_describer_tailscale_manuals_job_queue" | ||
ConsumerGroupManuals = "describer-tailscale-manuals" | ||
) |
29 changes: 29 additions & 0 deletions
29
services/integration/integration-type/tailscale-account/configs/resource_types_list.go
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,29 @@ | ||
package configs | ||
|
||
var TablesToResourceTypes = map[string]string{ | ||
"tailscale_device": "TailScale/Device", | ||
"tailscale_user": "TailScale/User", | ||
"tailscale_contact": "TailScale/Contact", | ||
"tailscale_device_invite": "TailScale/Device/Invite", | ||
"tailscale_device_posture": "TailScale/Device/Posture", | ||
"tailscale_user_invite": "TailScale/User/Invite", | ||
"tailscale_key": "TailScale/Key", | ||
"tailscale_policy": "TailScale/Policy", | ||
"tailscale_tailnet_setting": "TailScale/TailnetSetting", | ||
"tailscale_webhook": "TailScale/Webhook", | ||
"tailscale_dns": "TailScale/DNS", | ||
} | ||
|
||
var ResourceTypesList = []string{ | ||
"TailScale/Device", | ||
"TailScale/User", | ||
"TailScale/Contact", | ||
"TailScale/Device/Invite", | ||
"TailScale/Device/Posture", | ||
"TailScale/User/Invite", | ||
"TailScale/Key", | ||
"TailScale/Policy", | ||
"TailScale/TailnetSetting", | ||
"TailScale/Webhook", | ||
"TailScale/DNS", | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
package name is wrong