From b1e67bd785d46232a4aae5d49f7ab179ebc8bd52 Mon Sep 17 00:00:00 2001 From: Ivan Bakalov Date: Thu, 18 Jan 2024 21:17:11 +0200 Subject: [PATCH] Check that environment variables are provided (#24) If configuration settings aren't provided via the Corefile, raise an error if they also aren't provided via environment variables. --- setup.go | 8 ++++++++ setup_test.go | 3 +++ 2 files changed, 11 insertions(+) diff --git a/setup.go b/setup.go index 83b5a2b..4098768 100644 --- a/setup.go +++ b/setup.go @@ -169,10 +169,18 @@ func setup(c *caddy.Controller) error { // Keep this environment variable name consistent across all our integrations (e.g. SDKs, Terraform provider). accessToken = os.Getenv("DNSIMPLE_TOKEN") } + // Still blank, return error + if accessToken == "" { + return plugin.Error("dnsimple", c.Err("access token must be provided via the Corefile or DNSIMPLE_TOKEN environment variable")) + } if opts.accountId == "" { opts.accountId = os.Getenv("DNSIMPLE_ACCOUNT_ID") } + // Still blank, return error + if opts.accountId == "" { + return plugin.Error("dnsimple", c.Err("account ID must be provided via the Corefile or DNSIMPLE_TOKEN environment variable")) + } if baseUrl == "" { // Default to production diff --git a/setup_test.go b/setup_test.go index e451eb0..e78d2ac 100644 --- a/setup_test.go +++ b/setup_test.go @@ -9,6 +9,9 @@ import ( ) func TestSetupDNSimple(t *testing.T) { + t.Setenv("DNSIMPLE_TOKEN", "token") + t.Setenv("DNSIMPLE_ACCOUNT_ID", "12345") + fakeClient := new(fakeDNSimpleClient) newDnsimpleService = func(ctx context.Context, options Options, accessToken, baseUrl string) (dnsimpleService, error) { return fakeClient, nil