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

feat: add support for a config file #612

Merged
merged 1 commit into from
Mar 28, 2024
Merged
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,30 @@ principal as the user you want to log in as, and start the Proxy like so:
Note: when using the query-string syntax, the instance URI and query parameters
must be wrapped in quotes.

### Config file

The Proxy supports a configuration file. Supported file types are TOML, JSON,
and YAML. Load the file with the `--config-file` flag:

```shell
./alloydb-auth-proxy --config-file /path/to/config.[toml|json|yaml]
```

The configuration file format supports all flags. The key names should match
the flag names. For example:

``` toml
# use instance-uri-0, instance-uri-1, etc.
# for multiple instances
instance-uri = "<INSTANCE_URI>"
auto-iam-authn = true
debug = true
debug-logs = true
```

Run `./alloydb-auth-proxy --help` for more details.


## Running behind a Socks5 proxy

The AlloyDB Auth Proxy includes support for sending requests through a SOCKS5
Expand Down
140 changes: 140 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import "testing"

func assert[T comparable](t *testing.T, want, got T) {
t.Helper()
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}

func TestNewCommandWithConfigFile(t *testing.T) {
tcs := []struct {
desc string
args []string
setup func()
assert func(t *testing.T, c *Command)
}{
{
desc: "toml config file",
args: []string{"--config-file", "testdata/config-toml.toml"},
setup: func() {},
assert: func(t *testing.T, c *Command) {
assert(t, 1, len(c.conf.Instances))
assert(t, true, c.conf.Debug)
assert(t, 5555, c.conf.Port)
assert(t, true, c.conf.DebugLogs)
assert(t, true, c.conf.AutoIAMAuthN)
},
},
{
desc: "yaml config file",
args: []string{"--config-file", "testdata/config-yaml.yaml"},
setup: func() {},
assert: func(t *testing.T, c *Command) {
assert(t, 1, len(c.conf.Instances))
assert(t, true, c.conf.Debug)
},
},
{
desc: "json config file",
args: []string{"--config-file", "testdata/config-json.json"},
setup: func() {},
assert: func(t *testing.T, c *Command) {
assert(t, 1, len(c.conf.Instances))
assert(t, true, c.conf.Debug)
},
},
{
desc: "config file with two instances",
args: []string{"--config-file", "testdata/two-instances.toml"},
setup: func() {},
assert: func(t *testing.T, c *Command) {
assert(t, 2, len(c.conf.Instances))
},
},
{
desc: "argument takes precedence over environment variable",
args: []string{sampleURI},
setup: func() {
t.Setenv("ALLOYDB_PROXY_INSTANCE_URI", altURI)
},
assert: func(t *testing.T, c *Command) {
assert(t, sampleURI, c.conf.Instances[0].Name)
},
},
{
desc: "environment variable takes precedence over config file",
args: []string{"--config-file", "testdata/config.json"},
setup: func() {
t.Setenv("ALLOYDB_PROXY_INSTANCE_URI", altURI)
},
assert: func(t *testing.T, c *Command) {
assert(t, altURI, c.conf.Instances[0].Name)
},
},
{
desc: "CLI flag takes precedence over environment variable",
args: []string{sampleURI, "--debug"},
setup: func() {
t.Setenv("ALLOYDB_PROXY_DEBUG", "false")
},
assert: func(t *testing.T, c *Command) {
assert(t, true, c.conf.Debug)
},
},
{
desc: "CLI flag takes precedence over config file",
args: []string{
sampleURI,
"--config-file", "testdata/config.toml",
"--debug=false",
},
setup: func() {},
assert: func(t *testing.T, c *Command) {
assert(t, false, c.conf.Debug)
},
},
{
desc: "environment variable takes precedence over config file",
args: []string{
sampleURI,
"--config-file", "testdata/config.toml",
},
setup: func() {
t.Setenv("ALLOYDB_PROXY_DEBUG", "false")
},
assert: func(t *testing.T, c *Command) {
assert(t, false, c.conf.Debug)
},
},
}

for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
tc.setup()

cmd, err := invokeProxyCommand(tc.args)
if err != nil {
t.Fatalf("want error = nil, got = %v", err)
}

tc.assert(t, cmd)
})
}
}
Loading
Loading