diff --git a/components/public-api-server/go.mod b/components/public-api-server/go.mod index 0059125d365d8d..9675a770b35fae 100644 --- a/components/public-api-server/go.mod +++ b/components/public-api-server/go.mod @@ -24,6 +24,7 @@ require ( github.com/stripe/stripe-go/v72 v72.122.0 google.golang.org/grpc v1.49.0 google.golang.org/protobuf v1.28.1 + gorm.io/gorm v1.24.1 ) require ( @@ -35,11 +36,14 @@ require ( github.com/cyphar/filepath-securejoin v0.2.3 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/felixge/httpsnoop v1.0.1 // indirect + github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect @@ -56,6 +60,7 @@ require ( golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + gorm.io/driver/mysql v1.4.4 // indirect ) replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway diff --git a/components/public-api-server/go.sum b/components/public-api-server/go.sum index 19737363ee1870..ee343cf5cbd9fb 100644 --- a/components/public-api-server/go.sum +++ b/components/public-api-server/go.sum @@ -87,6 +87,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= @@ -168,6 +170,11 @@ github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb/go.mod h1:N github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -569,6 +576,11 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ= +gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM= +gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.24.1 h1:CgvzRniUdG67hBAzsxDGOAuq4Te1osVMYsa1eQbd4fs= +gorm.io/gorm v1.24.1/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/components/public-api-server/pkg/apiv1/tokens.go b/components/public-api-server/pkg/apiv1/tokens.go index e32fd2760737f8..cb24ccbc762445 100644 --- a/components/public-api-server/pkg/apiv1/tokens.go +++ b/components/public-api-server/pkg/apiv1/tokens.go @@ -19,19 +19,21 @@ import ( "github.com/gitpod-io/gitpod/public-api-server/pkg/auth" "github.com/gitpod-io/gitpod/public-api-server/pkg/proxy" "github.com/google/uuid" + "gorm.io/gorm" ) -func NewTokensService(connPool proxy.ServerConnectionPool, expClient experiments.Client) *TokensService { +func NewTokensService(connPool proxy.ServerConnectionPool, expClient experiments.Client, dbConn *gorm.DB) *TokensService { return &TokensService{ connectionPool: connPool, expClient: expClient, + dbConn: dbConn, } } type TokensService struct { connectionPool proxy.ServerConnectionPool - - expClient experiments.Client + expClient experiments.Client + dbConn *gorm.DB v1connect.UnimplementedTokensServiceHandler } diff --git a/components/public-api-server/pkg/apiv1/tokens_test.go b/components/public-api-server/pkg/apiv1/tokens_test.go index 4351fd724279f4..616a39aedcfee7 100644 --- a/components/public-api-server/pkg/apiv1/tokens_test.go +++ b/components/public-api-server/pkg/apiv1/tokens_test.go @@ -286,7 +286,7 @@ func setupTokensService(t *testing.T, expClient experiments.Client) (*protocol.M serverMock := protocol.NewMockAPIInterface(ctrl) - svc := NewTokensService(&FakeServerConnPool{api: serverMock}, expClient) + svc := NewTokensService(&FakeServerConnPool{api: serverMock}, expClient, nil) _, handler := v1connect.NewTokensServiceHandler(svc, connect.WithInterceptors(auth.NewServerInterceptor())) diff --git a/components/public-api-server/pkg/server/integration_test.go b/components/public-api-server/pkg/server/integration_test.go index 151432d0d4adf9..48f83fe9a0ba41 100644 --- a/components/public-api-server/pkg/server/integration_test.go +++ b/components/public-api-server/pkg/server/integration_test.go @@ -31,7 +31,7 @@ func TestPublicAPIServer_v1_WorkspaceService(t *testing.T) { connPool := proxy.ServerConnectionPool(&proxy.NoConnectionPool{ServerAPI: gitpodAPI}) - require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient())) + require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient(), nil)) baseserver.StartServerForTests(t, srv) workspaceClient := v1connect.NewWorkspacesServiceClient(http.DefaultClient, srv.HTTPAddress(), connect.WithInterceptors(auth.NewClientInterceptor("some-token"))) @@ -60,7 +60,7 @@ func TestConnectWorkspaceService_RequiresAuth(t *testing.T) { connPool := proxy.ServerConnectionPool(&proxy.NoConnectionPool{ServerAPI: gitpodAPI}) - require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient())) + require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient(), nil)) baseserver.StartServerForTests(t, srv) diff --git a/components/public-api-server/pkg/server/server.go b/components/public-api-server/pkg/server/server.go index 211833859290cc..a09099079a6791 100644 --- a/components/public-api-server/pkg/server/server.go +++ b/components/public-api-server/pkg/server/server.go @@ -6,14 +6,17 @@ package server import ( "fmt" + "net" "net/http" "net/url" "os" "strings" "github.com/bufbuild/connect-go" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/common-go/experiments" "github.com/gitpod-io/gitpod/common-go/log" + "gorm.io/gorm" "github.com/gitpod-io/gitpod/components/public-api/go/config" "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect" @@ -41,6 +44,16 @@ func Start(logger *logrus.Entry, version string, cfg *config.Configuration) erro return fmt.Errorf("failed to setup connection pool: %w", err) } + dbConn, err := common_db.Connect(common_db.ConnectionParams{ + User: os.Getenv("DB_USERNAME"), + Password: os.Getenv("DB_PASSWORD"), + Host: net.JoinHostPort(os.Getenv("DB_HOST"), os.Getenv("DB_PORT")), + Database: "gitpod", + }) + if err != nil { + return fmt.Errorf("failed to establish database connection: %w", err) + } + expClient := experiments.NewClient() srv, err := baseserver.New("public_api_server", @@ -73,7 +86,7 @@ func Start(logger *logrus.Entry, version string, cfg *config.Configuration) erro srv.HTTPMux().Handle("/stripe/invoices/webhook", handlers.ContentTypeHandler(stripeWebhookHandler, "application/json")) - if registerErr := register(srv, connPool, expClient); registerErr != nil { + if registerErr := register(srv, connPool, expClient, dbConn); registerErr != nil { return fmt.Errorf("failed to register services: %w", registerErr) } @@ -84,7 +97,7 @@ func Start(logger *logrus.Entry, version string, cfg *config.Configuration) erro return nil } -func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expClient experiments.Client) error { +func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expClient experiments.Client, dbConn *gorm.DB) error { proxy.RegisterMetrics(srv.MetricsRegistry()) connectMetrics := NewConnectMetrics() @@ -107,7 +120,7 @@ func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expCl teamsRoute, teamsServiceHandler := v1connect.NewTeamsServiceHandler(apiv1.NewTeamsService(connPool), handlerOptions...) srv.HTTPMux().Handle(teamsRoute, teamsServiceHandler) - tokensRoute, tokensServiceHandler := v1connect.NewTokensServiceHandler(apiv1.NewTokensService(connPool, expClient), handlerOptions...) + tokensRoute, tokensServiceHandler := v1connect.NewTokensServiceHandler(apiv1.NewTokensService(connPool, expClient, dbConn), handlerOptions...) srv.HTTPMux().Handle(tokensRoute, tokensServiceHandler) userRoute, userServiceHandler := v1connect.NewUserServiceHandler(apiv1.NewUserService(connPool), handlerOptions...)