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

Add settings for controlling CORS and changing reverse proxy target #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ use_repo(
"com_github_machinebox_graphql",
"com_github_mattn_go_sqlite3",
"com_github_pkg_errors",
"com_github_rs_cors",
"com_github_stretchr_testify",
"com_github_vektah_gqlparser_v2",
"io_entgo_contrib",
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ Currently this is used to download the build profile.

## Using the Application

Go to http://localhost:8081.
This will go through the reverse proxy that the backend runs.
Go to <http://localhost:3000> or <http://localhost:8081> (which goes through a reverse proxy in the go backend).

> **_NOTE:_** Even though the frontend is available directly at http://localhost:3000, APIs don't work on that port, so you will
not be able to see any data if you access the application in this way.
> **_NOTE:_** Should the frontend be served on a different url, remember to
> update `frontendProxyUrl` and/or `allowedOrigins` in the configuration.

The home page of the application will appear as follows:

Expand Down Expand Up @@ -119,7 +118,7 @@ in the portal configuration file. Despite having the name "browser", it is not p

## Using GraphiQL To Explore the GraphQL API

The GraphiQL explorer is available via http://localhost:8081/graphiql.
The GraphiQL explorer is available via <http://localhost:8081/graphiql>.

## Generated Code

Expand Down
1 change: 1 addition & 0 deletions cmd/bb_portal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ go_library(
"@com_github_improbable_eng_grpc_web//go/grpcweb",
"@com_github_jackc_pgx_v5//stdlib",
"@com_github_mattn_go_sqlite3//:go-sqlite3",
"@com_github_rs_cors//:cors",
"@io_entgo_contrib//entgql",
"@io_entgo_ent//dialect",
"@io_entgo_ent//dialect/sql",
Expand Down
16 changes: 12 additions & 4 deletions cmd/bb_portal/grpcweb_proxy_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,29 @@ import (
)

func registerAndStartServer(
configuration *bb_portal.GrpcWebProxyConfiguration,
globalConfig *bb_portal.ApplicationConfiguration,
proxyConfig *bb_portal.GrpcWebProxyConfiguration,
siblingsGroup program.Group,
grpcClientFactory bb_grpc.ClientFactory,
metricsName string,
registerServer func(*go_grpc.Server, go_grpc.ClientConnInterface),
) {
grpcClient, err := grpcClientFactory.NewClientFromConfiguration(configuration.Client)
grpcClient, err := grpcClientFactory.NewClientFromConfiguration(proxyConfig.Client)
if err != nil {
log.Fatalf("Could not connect to gRPC server: %v", err)
}

options := []grpcweb.Option{
grpcweb.WithOriginFunc(func(origin string) bool { return slices.Contains(configuration.AllowedOrigins, origin) }),
grpcweb.WithOriginFunc(func(origin string) bool {
return slices.Contains(globalConfig.AllowedOrigins, origin) || slices.Contains(globalConfig.AllowedOrigins, "*")
}),
}

grpcServer := go_grpc.NewServer()
registerServer(grpcServer, grpcClient)
grpcWebServer := grpcweb.WrapServer(grpcServer, options...)
bb_http.NewServersFromConfigurationAndServe(
configuration.HttpServers,
proxyConfig.HttpServers,
bb_http.NewMetricsHandler(grpcWebServer, metricsName),
siblingsGroup,
)
Expand Down Expand Up @@ -76,6 +79,7 @@ func StartGrpcWebProxyServer(

if configuration.BuildQueueStateProxy != nil {
registerAndStartServer(
configuration,
configuration.BuildQueueStateProxy,
siblingsGroup,
grpcClientFactory,
Expand All @@ -91,6 +95,7 @@ func StartGrpcWebProxyServer(

if configuration.ActionCacheProxy != nil {
registerAndStartServer(
configuration,
configuration.ActionCacheProxy,
siblingsGroup,
grpcClientFactory,
Expand All @@ -106,6 +111,7 @@ func StartGrpcWebProxyServer(

if configuration.ContentAddressableStorageProxy != nil {
registerAndStartServer(
configuration,
configuration.ContentAddressableStorageProxy,
siblingsGroup,
grpcClientFactory,
Expand All @@ -121,6 +127,7 @@ func StartGrpcWebProxyServer(

if configuration.InitialSizeClassCacheProxy != nil {
registerAndStartServer(
configuration,
configuration.InitialSizeClassCacheProxy,
siblingsGroup,
grpcClientFactory,
Expand All @@ -136,6 +143,7 @@ func StartGrpcWebProxyServer(

if configuration.FileSystemAccessCacheProxy != nil {
registerAndStartServer(
configuration,
configuration.FileSystemAccessCacheProxy,
siblingsGroup,
grpcClientFactory,
Expand Down
35 changes: 26 additions & 9 deletions cmd/bb_portal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"database/sql"
"flag"
"log"
"log/slog"
"net/http"
"net/http/httputil"
"net/url"
"os"
"slices"
"time"

_ "net/http/pprof"
Expand All @@ -22,6 +24,7 @@ import (
"github.com/gorilla/mux"
_ "github.com/jackc/pgx/v5/stdlib"
_ "github.com/mattn/go-sqlite3"
"github.com/rs/cors"
build "google.golang.org/genproto/googleapis/devtools/build/v1"
go_grpc "google.golang.org/grpc"

Expand Down Expand Up @@ -121,10 +124,19 @@ func main() {
serveFileService := servefiles.NewFileServerServiceFromConfiguration(dependenciesGroup, &configuration, grpcClientFactory)

router := mux.NewRouter()
newPortalService(blobArchiver, dbClient, serveFileService, router)
c := cors.New(
cors.Options{
AllowOriginFunc: func(origin string) bool {
return slices.Contains(configuration.AllowedOrigins, origin) || slices.Contains(configuration.AllowedOrigins, "*")
},
AllowedMethods: []string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
AllowedHeaders: []string{"Authorization", "Content-Type"},
},
)
newPortalService(&configuration, blobArchiver, dbClient, serveFileService, router)
bb_http.NewServersFromConfigurationAndServe(
configuration.HttpServers,
bb_http.NewMetricsHandler(router, "PortalUI"),
bb_http.NewMetricsHandler(c.Handler(router), "PortalUI"),
siblingsGroup,
)

Expand Down Expand Up @@ -202,7 +214,7 @@ func fatal(msg string, args ...any) {
os.Exit(1)
}

func newPortalService(archiver processing.BlobMultiArchiver, dbClient *ent.Client, serveFilesService *servefiles.FileServerService, router *mux.Router) {
func newPortalService(configuration *bb_portal.ApplicationConfiguration, archiver processing.BlobMultiArchiver, dbClient *ent.Client, serveFilesService *servefiles.FileServerService, router *mux.Router) {
srv := handler.NewDefaultServer(graphql.NewSchema(dbClient))
srv.Use(entgql.Transactioner{TxOpener: dbClient})

Expand All @@ -214,13 +226,18 @@ func newPortalService(archiver processing.BlobMultiArchiver, dbClient *ent.Clien
router.HandleFunc("/api/servefile/{instanceName:(?:.*?/)?}blobs/{digestFunction}/command/{hash}-{sizeBytes}/", serveFilesService.HandleCommand).Methods("GET")
router.HandleFunc("/api/servefile/{instanceName:(?:.*?/)?}blobs/{digestFunction}/directory/{hash}-{sizeBytes}/", serveFilesService.HandleDirectory).Methods("GET")
}
router.PathPrefix("/").Handler(frontendServer())
if configuration.FrontendProxyUrl != "" {
router.PathPrefix("/").Handler(frontendServer(configuration.FrontendProxyUrl))
}
}

func frontendServer() http.Handler {
targetURL := &url.URL{
Scheme: "http",
Host: "localhost:3000",
func frontendServer(proxyURL string) http.Handler {
remote, err := url.Parse(proxyURL)
if err != nil {
log.Fatalf("Could not parse proxy URL: %v", err)
}
return httputil.NewSingleHostReverseProxy(targetURL)

log.Println("Proxying frontend to", remote)

return httputil.NewSingleHostReverseProxy(remote)
}
8 changes: 3 additions & 5 deletions config/portal.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// - A Buildbarn frontend, accessible at localhost:8980

{
frontendProxyUrl: 'http://localhost:3000',
allowedOrigins: ['http://localhost:3000', 'http://localhost:8081'],

serveFilesCasConfiguration: {
grpc: { address: 'localhost:8980' },
},
Expand Down Expand Up @@ -44,7 +47,6 @@
client: {
address: 'localhost:8984',
},
allowedOrigins: ['http://localhost:8081'],
httpServers: [{
listenAddresses: [':9433'],
authenticationPolicy: {
Expand All @@ -66,7 +68,6 @@
client: {
address: 'localhost:8980',
},
allowedOrigins: ['http://localhost:8081'],
httpServers: [{
listenAddresses: [':9434'],
authenticationPolicy: {
Expand All @@ -88,7 +89,6 @@
client: {
address: 'localhost:8980',
},
allowedOrigins: ['http://localhost:8081'],
httpServers: [{
listenAddresses: [':9435'],
authenticationPolicy: {
Expand All @@ -110,7 +110,6 @@
client: {
address: 'localhost:8980',
},
allowedOrigins: ['http://localhost:8081'],
httpServers: [{
listenAddresses: [':9436'],
authenticationPolicy: {
Expand All @@ -131,7 +130,6 @@
client: {
address: 'localhost:8980',
},
allowedOrigins: ['http://localhost:8081'],
httpServers: [{
listenAddresses: [':9437'],
authenticationPolicy: {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import React from 'react';
import { Divider, Space, Typography } from 'antd';
import { env } from 'next-runtime-env';
import styles from './page.module.css';
import Content from '@/components/Content';
import Uploader from '@/components/Uploader';
import { env } from 'next-runtime-env';

const bazelrcLines = `build --bes_backend=${env('NEXT_PUBLIC_BES_GRPC_BACKEND_URL')}\nbuild --bes_results_url=${env('NEXT_PUBLIC_BES_BACKEND_URL')}/bazel-invocations/`;

Expand Down Expand Up @@ -33,7 +33,7 @@ export default function Home() {
flag to analyze
</Typography.Text>
}
action="/api/v1/bep/upload"
action={`${env("NEXT_PUBLIC_BES_BACKEND_URL")}/api/v1/bep/upload`}
/>
<Divider />
<Typography.Text>
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/machinebox/graphql v0.2.2
github.com/mattn/go-sqlite3 v1.14.22
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.11.1
github.com/stretchr/testify v1.9.0
github.com/vektah/gqlparser/v2 v2.5.17
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
Expand Down Expand Up @@ -107,7 +108,6 @@ require (
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/sercand/kuberesolver/v5 v5.1.1 // indirect
github.com/sosodev/duration v1.3.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.9 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,9 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand Down
Loading