|
| 1 | +<p align="center"> |
| 2 | +<img src="mascot/banner.png" alt="db-auth-gateway" title="db-auth-gateway" /> |
| 3 | +</p> |
| 4 | + |
1 | 5 | # db-auth-gateway |
2 | | -An authentication proxy for Google Cloud managed databases |
| 6 | + |
| 7 | +An authentication proxy for Google Cloud managed databases. Based on the ideas |
| 8 | +of [cloudsql-proxy](https://github.com/GoogleCloudPlatform/cloudsql-proxy) but |
| 9 | +intended to be run as a standalone network accessible service rather than a |
| 10 | +sidecar. |
| 11 | + |
| 12 | +We've been using `cloudsql-proxy` for several years now to power our |
| 13 | +[db-operator](https://github.com/kloeckner-i/db-operator) project. It has been |
| 14 | +for the most part reliable but key differences between how we deploy it and |
| 15 | +Google's reference architecture have led to production issues. |
| 16 | + |
| 17 | +We developed `db-auth-gateway` to address these issues and add a variety of wish |
| 18 | +list features such as improved observability, and testing. |
| 19 | + |
| 20 | +## Features |
| 21 | + |
| 22 | +* Connection draining during shutdown to support zero downtime deployments and |
| 23 | +load balancing. |
| 24 | +* Prometheus metrics support for improved observability. |
| 25 | +* Full testsuite including realistic Google service mocks. |
| 26 | +* Simplified modern code base. |
| 27 | + |
| 28 | +## Quickstart |
| 29 | + |
| 30 | +Use `docker-compose` to start a local PostgreSQL instance, and Google API mock: |
| 31 | + |
| 32 | +```shell script |
| 33 | +make start_mock |
| 34 | +``` |
| 35 | + |
| 36 | +Then you can then run `db-auth-gateway` locally with: |
| 37 | + |
| 38 | +```shell script |
| 39 | +db-auth-gateway --api-endpoint=http://localhost:8080 --credential-file=DISABLED \ |
| 40 | +--instance=my-project:my-region:my-database |
| 41 | +``` |
| 42 | + |
| 43 | +`db-auth-gateway` will listen on port 5432 (by default) for SQL connections. |
| 44 | + |
| 45 | +```shell script |
| 46 | +PGPASSWORD=mysecretpassword psql -h localhost -p 5432 -d postgres postgres |
| 47 | +``` |
| 48 | + |
| 49 | +### Flags |
| 50 | + |
| 51 | +`db-auth-gateway` has a variety of command line flags for configuring its behavior: |
| 52 | + |
| 53 | +| Flag | Default | Description | |
| 54 | +|:---|:---:|:---| |
| 55 | +| --credential-file | | JSON file containing the Google Cloud credentials | |
| 56 | +| --instance | | Fully qualified database instance to connect to (project:region:name) | |
| 57 | +| --listen | :5432 | Address and port to listen on | |
| 58 | +| --remote-port | 3307 | Port to connect to the remote server on | |
| 59 | +| --max-connections | 0 | The maximum number of active connections. Defaults to 0 (unlimited) | |
| 60 | +| --min-refresh-interval | 1m | The minimum amount of time to wait between API calls | |
| 61 | +| --periodic-refresh-interval | 5m | Configuration is eagerly refreshed on a schedule. This is the nominal period between API calls. | |
| 62 | +| --api-endpoint | | If specified the URL to use for API calls | |
| 63 | + |
| 64 | +## Development |
| 65 | + |
| 66 | +### Prerequisites |
| 67 | + |
| 68 | +* [Go 1.15+](https://golang.org/dl/) |
| 69 | +* GNU Make |
| 70 | +* [golangci-lint v1.30+](https://golangci-lint.run/usage/install/) |
| 71 | +* Additional Go tools: |
| 72 | + * [golint](https://github.com/golang/lint) |
| 73 | + * [gofumpt](https://github.com/mvdan/gofumpt) |
| 74 | + * [gofumports](https://github.com/mvdan/gofumpt) |
| 75 | + * [gci](https://github.com/daixiang0/gci) |
| 76 | + |
| 77 | +### Build |
| 78 | + |
| 79 | +To build `db-auth-gateway`, simply run make without any arguments. |
| 80 | + |
| 81 | +The resulting binary will be written to: `./target/db-auth-gateway`. |
| 82 | + |
| 83 | +```shell script |
| 84 | +make |
| 85 | +``` |
| 86 | + |
| 87 | +### Test |
| 88 | + |
| 89 | +Before committing any code you should always lint and test your changes. |
| 90 | + |
| 91 | +#### Code Linting |
| 92 | + |
| 93 | +```shell script |
| 94 | +make lint |
| 95 | +``` |
| 96 | + |
| 97 | +#### Running the Tests |
| 98 | + |
| 99 | +First start the Google API mock using `docker-compose`: |
| 100 | + |
| 101 | +```shell script |
| 102 | +make start_mock |
| 103 | +``` |
| 104 | + |
| 105 | +Then run the tests: |
| 106 | + |
| 107 | +```shell script |
| 108 | +make test |
| 109 | +``` |
| 110 | + |
| 111 | +### End to End Testing |
| 112 | + |
| 113 | +You run the end to end tests with: |
| 114 | + |
| 115 | +```shell script |
| 116 | +make e2e |
| 117 | +``` |
| 118 | + |
| 119 | +The tests will start a local instance of `db-auth-gateway` and verify it is able |
| 120 | +to connect to and query the Postgres database, and Google API mock. |
| 121 | + |
| 122 | +## Acknowledgements |
| 123 | + |
| 124 | +1. [The Go Gopher](https://blog.golang.org/gopher) by [Renee French](http://reneefrench.blogspot.com/), licensed under the [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/). |
| 125 | +1. https://github.com/GoogleCloudPlatform/cloudsql-proxy |
| 126 | +1. https://github.com/jbenet/go-context |
0 commit comments