github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/cmd/scopessynchronizer/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "net/http" 6 "net/url" 7 "os" 8 9 httptransport "github.com/go-openapi/runtime/client" 10 "github.com/kyma-incubator/compass/components/director/internal/domain/auth" 11 "github.com/kyma-incubator/compass/components/director/internal/domain/oauth20" 12 "github.com/kyma-incubator/compass/components/director/internal/domain/systemauth" 13 scopes "github.com/kyma-incubator/compass/components/director/internal/scopes_sync" 14 "github.com/kyma-incubator/compass/components/director/internal/uid" 15 configprovider "github.com/kyma-incubator/compass/components/director/pkg/config" 16 "github.com/kyma-incubator/compass/components/director/pkg/correlation" 17 httputil "github.com/kyma-incubator/compass/components/director/pkg/http" 18 "github.com/kyma-incubator/compass/components/director/pkg/log" 19 "github.com/kyma-incubator/compass/components/director/pkg/persistence" 20 "github.com/kyma-incubator/compass/components/director/pkg/signal" 21 "github.com/ory/hydra-client-go/client" 22 "github.com/vrischmann/envconfig" 23 ) 24 25 const envPrefix = "APP" 26 27 type config struct { 28 Database persistence.DatabaseConfig 29 ConfigurationFile string 30 OAuth20 oauth20.Config 31 } 32 33 func main() { 34 ctx, cancel := context.WithCancel(context.Background()) 35 defer cancel() 36 37 uidSvc := uid.NewService() 38 correlationID := uidSvc.Generate() 39 ctx = withCorrelationID(ctx, correlationID) 40 41 term := make(chan os.Signal) 42 signal.HandleInterrupts(ctx, cancel, term) 43 44 cfg := config{} 45 err := envconfig.InitWithPrefix(&cfg, envPrefix) 46 exitOnError(ctx, err, "Error while loading app config") 47 48 oAuth20HTTPClient := &http.Client{ 49 Transport: httputil.NewCorrelationIDTransport(httputil.NewServiceAccountTokenTransport(httputil.NewHTTPTransportWrapper(http.DefaultTransport.(*http.Transport)))), 50 Timeout: cfg.OAuth20.HTTPClientTimeout, 51 } 52 adminURL, err := url.Parse(cfg.OAuth20.URL) 53 exitOnError(ctx, err, "Error while parsing OAuth client endpoint") 54 55 transport := httptransport.NewWithClient(adminURL.Host, adminURL.Path, []string{adminURL.Scheme}, oAuth20HTTPClient) 56 hydra := client.New(transport, nil) 57 58 cfgProvider := configProvider(ctx, cfg) 59 oAuth20Svc := oauth20.NewService(cfgProvider, uidSvc, cfg.OAuth20.PublicAccessTokenEndpoint, hydra.Admin) 60 61 transact, closeFunc, err := persistence.Configure(ctx, cfg.Database) 62 exitOnError(ctx, err, "Error while establishing the connection to the database") 63 defer func() { 64 err := closeFunc() 65 exitOnError(ctx, err, "Error while closing the connection to the database") 66 }() 67 68 authConverter := auth.NewConverter() 69 systemAuthConverter := systemauth.NewConverter(authConverter) 70 syncService := scopes.NewService(oAuth20Svc, transact, systemauth.NewRepository(systemAuthConverter)) 71 err = syncService.SynchronizeClientScopes(ctx) 72 exitOnError(ctx, err, "Error while updating client scopes") 73 } 74 75 func exitOnError(ctx context.Context, err error, context string) { 76 if err != nil { 77 log.C(ctx).WithError(err).Errorf("%s: %v", context, err) 78 os.Exit(1) 79 } 80 } 81 82 func withCorrelationID(ctx context.Context, id string) context.Context { 83 correlationIDKey := correlation.RequestIDHeaderKey 84 return correlation.SaveCorrelationIDHeaderToContext(ctx, &correlationIDKey, &id) 85 } 86 87 func configProvider(ctx context.Context, cfg config) *configprovider.Provider { 88 provider := configprovider.NewProvider(cfg.ConfigurationFile) 89 exitOnError(ctx, provider.Load(), "Error on loading configuration file") 90 91 return provider 92 }