Skip to content

Commit

Permalink
(feat) Engine CreateClients helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Oct 31, 2024
1 parent 4e2a033 commit df01975
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package discovery

import (
"fmt"
"net/http"
"os"
"runtime"
"time"

"github.com/getsentry/sentry-go"
"github.com/google/uuid"
"github.com/overmindtech/sdp-go"
"github.com/overmindtech/sdp-go/auth"
"github.com/overmindtech/sdp-go/sdpconnect"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"golang.org/x/oauth2"
)

func AddEngineFlags(command *cobra.Command) {
Expand All @@ -22,7 +30,6 @@ func AddEngineFlags(command *cobra.Command) {
cobra.CheckErr(viper.BindEnv("api-key", "OVM_API_KEY", "API_KEY"))
command.PersistentFlags().String("source-access-token", "", "The access token to use to authenticate the source for managed sources")
cobra.CheckErr(viper.BindEnv("source-access-token", "SOURCE_ACCESS_TOKEN"))
// SOURCE_TOKEN_TYPE
command.PersistentFlags().String("source-token-type", "", "The type of token to use to authenticate the source for managed sources")
cobra.CheckErr(viper.BindEnv("source-token-type", "SOURCE_TOKEN_TYPE"))
command.PersistentFlags().Int("max-parallel", 0, "The maximum number of parallel executions")
Expand Down Expand Up @@ -88,21 +95,57 @@ func EngineConfigFromViper(engineType, version string) (*EngineConfig, error) {
}

// MapFromEngineConfig Returns the config as a map
func MapFromEngineConfig(c *EngineConfig) map[string]any {
func MapFromEngineConfig(ec *EngineConfig) map[string]any {

var apiKeyClientSecret string

if c.ApiKey != "" {
if ec.ApiKey != "" {
apiKeyClientSecret = "[REDACTED]"
}

var sourceAccessToken string
if ec.SourceAccessToken != "" {
sourceAccessToken = "[REDACTED]"
}

return map[string]interface{}{
"engine-type": c.EngineType,
"version": c.Version,
"source-name": c.SourceName,
"source-uuid": c.SourceUUID,
"app": c.App,
"engine-type": ec.EngineType,
"version": ec.Version,
"source-name": ec.SourceName,
"source-uuid": ec.SourceUUID,
"source-access-token": sourceAccessToken,
"source-token-type": ec.SourceTokenType,
"managed-source": ec.OvermindManagedSource,
"app": ec.App,
"api-key": apiKeyClientSecret,
"max-parallel-executions": c.MaxParallelExecutions,
"max-parallel-executions": ec.MaxParallelExecutions,
}
}

func (ec *EngineConfig) CreateClients(oi sdp.OvermindInstance) (auth.TokenClient, *HeartbeatOptions, error) {
if ec.ApiKey != "" {
tokenClient, err := auth.NewAPIKeyClient(oi.ApiUrl.String(), ec.ApiKey)
if err != nil {
sentry.CaptureException(err)

log.WithError(err).Fatal("Could not create API key client")
}

tokenSource := auth.NewAPIKeyTokenSource(ec.ApiKey, oi.ApiUrl.String())
transport := oauth2.Transport{
Source: tokenSource,
Base: http.DefaultTransport,
}
authenticatedClient := http.Client{
Transport: otelhttp.NewTransport(&transport),
}
heartbeatOptions := HeartbeatOptions{
ManagementClient: sdpconnect.NewManagementServiceClient(
&authenticatedClient,
oi.ApiUrl.String(),
),
Frequency: time.Second * 30,
}
return tokenClient, &heartbeatOptions, nil
}
return nil, nil, fmt.Errorf("No clients created")
}

0 comments on commit df01975

Please sign in to comment.