Skip to content

Commit

Permalink
Merge pull request #8 from Recidiviz/dan/ssl-jdbc
Browse files Browse the repository at this point in the history
Serve HTTPS connections to enable JDBC connection
  • Loading branch information
ohaibbq committed Mar 5, 2024
2 parents 3745ece + b90f920 commit d1b9768
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -188,6 +188,27 @@ read_client = bigquery_storage.BigQueryReadClient(client_options=client_options)
result = client.query(sql).to_dataframe(bqstorage_client=read_client)
```

## How to connect from JDBC
Data exploration tools such as [JetBrains DataGrip](https://www.jetbrains.com/datagrip/) or [DBeaver](https://dbeaver.com/) are immensely valuable during development of projects using the emulator.

To connect the BigQuery Emulator to these tools from the JDBC, specify the following connection URL with your project ID:

```
jdbc:bigquery://https://localhost:9050;ProjectId=EMULATOR_PROJECT_ID;
```

The following options must also be specified:

|Option| Value |Description|
|--|-------------------------|--|
|`RootURL`| `https://localhost:9070` |This overwrites the URL used in the JDBC driver|
|`SSLTrustStore`|`/ssl/truststore.jks`| This must point to the JKS TrustStore that contains the SSL certificates for both the BigQuery Emulator, as well as `oauth2.googleapis.com`. A bundled truststore is located in the repository at `ssl/truststore.jks`. If using the Docker distribution, a volume can be created to access this data.|
|`SSLTrustStorePwd`|`test@123`|This is the password for the trust store. The default password for the bundled store is `test@123`.|

Known limitation: There is no documented method to bypass the JDBC OAuth flow, so a valid GCP OAuth credential is required to connect to the emulator.
See `Configuring Authentication` in [this document](https://storage.googleapis.com/simba-bq-release/jdbc/Simba%20Google%20BigQuery%20JDBC%20Connector%20Install%20and%20Configuration%20Guide_1.5.2.1005.pdf) for a list of valid authentication methods.
Exercise caution when using this feature with untrusted distributions of the BigQuery Emulator, as the OAuth Bearer token will be sent to the emulator server.

# Synopsis

If you use the Go language as a BigQuery client, you can launch the BigQuery emulator on the same process as the testing process.
Expand Down
5 changes: 4 additions & 1 deletion cmd/bigquery-emulator/main.go
Expand Up @@ -18,6 +18,7 @@ type option struct {
Project string `description:"specify the project name" long:"project"`
Dataset string `description:"specify the dataset name" long:"dataset"`
HTTPPort uint16 `description:"specify the http port number. this port used by bigquery api" long:"port" default:"9050"`
HTTPSPort uint16 `description:"specify the https port number. this port can be used by bigquery api or for JDBC driver connections" long:"https-port" default:"9070"`
GRPCPort uint16 `description:"specify the grpc port number. this port used by bigquery storage api" long:"grpc-port" default:"9060"`
LogLevel server.LogLevel `description:"specify the log level (debug/info/warn/error)" long:"log-level" default:"error"`
LogFormat server.LogFormat `description:"specify the log format (console/json)" long:"log-format" default:"console"`
Expand Down Expand Up @@ -126,10 +127,12 @@ func runServer(args []string, opt option) error {
done := make(chan error)
go func() {
httpAddr := fmt.Sprintf("0.0.0.0:%d", opt.HTTPPort)
httpsAddr := fmt.Sprintf("0.0.0.0:%d", opt.HTTPSPort)
grpcAddr := fmt.Sprintf("0.0.0.0:%d", opt.GRPCPort)
fmt.Fprintf(os.Stdout, "[bigquery-emulator] REST server listening at %s\n", httpAddr)
fmt.Fprintf(os.Stdout, "[bigquery-emulator] REST HTTPS server listening at %s\n", httpsAddr)
fmt.Fprintf(os.Stdout, "[bigquery-emulator] gRPC server listening at %s\n", grpcAddr)
done <- bqServer.Serve(ctx, httpAddr, grpcAddr)
done <- bqServer.Serve(ctx, httpAddr, httpsAddr, grpcAddr)
}()

select {
Expand Down
10 changes: 9 additions & 1 deletion server/server.go
Expand Up @@ -208,7 +208,7 @@ func (s *Server) Load(sources ...Source) error {
return nil
}

func (s *Server) Serve(ctx context.Context, httpAddr, grpcAddr string) error {
func (s *Server) Serve(ctx context.Context, httpAddr, httpsAddr, grpcAddr string) error {
httpServer := &http.Server{
Handler: s.Handler,
Addr: httpAddr,
Expand All @@ -217,6 +217,13 @@ func (s *Server) Serve(ctx context.Context, httpAddr, grpcAddr string) error {
}
s.httpServer = httpServer

httpsServer := &http.Server{
Handler: s.Handler,
Addr: httpsAddr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}

grpcServer := grpc.NewServer()
registerStorageServer(grpcServer, s)
s.grpcServer = grpcServer
Expand All @@ -233,6 +240,7 @@ func (s *Server) Serve(ctx context.Context, httpAddr, grpcAddr string) error {
var eg errgroup.Group
eg.Go(func() error { return grpcServer.Serve(grpcListener) })
eg.Go(func() error { return httpServer.Serve(httpListener) })
eg.Go(func() error { return httpsServer.ListenAndServeTLS("server.crt", "server.key") })
return eg.Wait()
}

Expand Down
31 changes: 31 additions & 0 deletions ssl/README.md
@@ -0,0 +1,31 @@
# SSL Configuration
## `server.crt`
This is the SSL certificate used by the HTTPS server.

```bash
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
```

## `server.key`
Server SSL Key

```bash
openssl genrsa -out server.key 204
```


## `truststore.jks`
This Truststore can be used for connecting to the BigQuery emulator via JDBC.
It contains the BigQuery Emulator server certificate and the `oauth2.googleapis.com` certificate.

```bash
keytool -import -alias bigquery-emulator-localhost \
-file server.crt -keystore truststore.jks -storepass "test@123"

keytool -import -alias oauth -file upload.video.google.com.cer \
-keystore truststore.jks -storepass "test@123"
```


## `upload.video.google.com.crt`
Public certificate for `oauth2.googleapis.com`
19 changes: 19 additions & 0 deletions ssl/server.crt
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDCTCCAfGgAwIBAgIUI3KcuYLsMbY8uAktUn7rAp5W/AAwDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI0MDMwNTE2MDE1NloXDTM0MDMw
MzE2MDE1NlowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEAwlAVNzms9VJeThXOTocyPvbQ6TAJNZPpq0C35obpxs8H
6tfvgGrkqwlH91b56dAK/OBm4k4VzHrbS3KDm6PFpe84T8R2bNLm6CXc6oftC7Ud
mOF3dNvaBw0M+NyjXXEGISKEWXkYkF2luo7q9MQLgQWU16zKG5ktfjvMuCL9988y
xjmSOjO5Ic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jHU3LbT7QRzlCT/AscRmpPG0Vxivp
9x+m78FfskTbTxOK0MFvukyzjcAnm6EDOxRpUeECgYAhXQOkTCF8iEeur/4GBkv/
eGl4cAF4bE/C43JpsMPFkGH6O/Q/0HKIoFPQKLQixMDxcBDXEtJIJM4dem8/WLfX
G9i2792n0bqo2wEyUFoD7+B+gzf7b0N8TgSNFqXdKFNS2/yt1QQEdvONnSxeXLiH
AOn6ZsVi0zeUPGGmMJ6KVw==
-----END PRIVATE KEY-----
Binary file added ssl/truststore.jks
Binary file not shown.
35 changes: 35 additions & 0 deletions ssl/upload.video.google.com.crt
@@ -0,0 +1,35 @@
-----BEGIN CERTIFICATE-----
MIIGETCCBPmgAwIBAgIRAJGz+SndLQSMEp5fHZIwmzMwDQYJKoZIhvcNAQELBQAw
RjELMAkGA1UEBhMCVVMxIjAgBgNVBAoTGUdvb2dsZSBUcnVzdCBTZXJ2aWNlcyBM
TEMxEzARBgNVBAMTCkdUUyBDQSAxQzMwHhcNMjQwMjA1MDgxOTE5WhcNMjQwNDI5
MDgxOTE4WjAiMSAwHgYDVQQDExd1cGxvYWQudmlkZW8uZ29vZ2xlLmNvbTBZMBMG
ByqGSM49AgEGCCqGSM49AwEHA0IABDAcQovXhW8dpwu0MvmE8X5qY1c+YpM6SMnm
AW9T83bHXtEMjErp6Tb1mPXIONensRnOFM/XBeKX9bDrUnX1qtujggPnMIID4zAO
BgNVHQ8BAf8EBAMCB4AwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDAYDVR0TAQH/BAIw
ADAdBgNVHQ4EFgQUn1hCQnptHBb/wohvDF53QccevFowHwYDVR0jBBgwFoAUinR/
r4XN7pXNPZzQ4kYU83E1HScwagYIKwYBBQUHAQEEXjBcMCcGCCsGAQUFBzABhhto
dHRwOi8vb2NzcC5wa2kuZ29vZy9ndHMxYzMwMQYIKwYBBQUHMAKGJWh0dHA6Ly9w
a2kuZ29vZy9yZXBvL2NlcnRzL2d0czFjMy5kZXIwggGYBgNVHREEggG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