Skip to content

Commit 9c345be

Browse files
committed
Implement feedback
1 parent 4300330 commit 9c345be

File tree

4 files changed

+108
-23
lines changed

4 files changed

+108
-23
lines changed

README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ spec:
205205
successThreshold: 1
206206
timeoutSeconds: 5
207207
env:
208-
- name: STACKIT_SERVICE_ACCOUNT_TOKEN
208+
- name: AUTH_TOKEN
209209
valueFrom:
210210
secretKeyRef:
211211
name: external-dns-stackit-webhook
@@ -219,6 +219,8 @@ The configuration of the STACKIT webhook can be accomplished through command lin
219219
Below are the options that are available.
220220

221221
- `--project-id`/`PROJECT_ID` (required): Specifies the project id of the STACKIT project.
222+
- `--auth-token`/`AUTH_TOKEN` (required if `auth-key-path` is not set): Defines the authentication token for the STACKIT API. Mutually exclusive with 'auth-key-path'.
223+
- `--auth-key-path`/`AUTH_KEY_PATH` (required if `auth-token` is not set): Defines the file path of the service account key for the STACKIT API. Mutually exclusive with 'auth-token'.
222224
- `--worker`/`WORKER` (optional): Specifies the number of workers to employ for querying the API. Given that we
223225
need to iterate over all zones and records, it can be parallelized. However, it is important to avoid
224226
setting this number excessively high to prevent receiving 429 rate limiting from the API (default 10).
@@ -230,14 +232,6 @@ Below are the options that are available.
230232
- `--log-level`/`LOG_LEVEL` (optional): Defines the log level (default "info"). Possible values are: debug, info, warn,
231233
error.
232234

233-
## Authentication
234-
235-
The STACKIT webhook uses the [STACKIT Go SDK](https://github.com/stackitcloud/stackit-sdk-go) and therefore inherits its
236-
options for authentication: You can use either Token or Key authentication flows. The example above uses the Token flow
237-
for authentication by providing the `STACKIT_SERVICE_ACCOUNT_TOKEN` environment variable in the `Deployment`. For more
238-
information on how to provide e.g. a Service Account Key to be used by the SDK, see
239-
[authentication options for the STACKIT Go SDK](https://github.com/stackitcloud/stackit-sdk-go?tab=readme-ov-file#authentication).
240-
241235
## FAQ
242236

243237
### 1. Issue with Creating Service using External DNS Annotation

cmd/webhook/cmd/root.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ package cmd
33
import (
44
"fmt"
55
"log"
6-
"net/http"
76
"strings"
8-
"time"
97

108
"github.com/spf13/cobra"
119
"github.com/spf13/pflag"
1210
"github.com/spf13/viper"
1311
"github.com/stackitcloud/external-dns-stackit-webhook/internal/stackitprovider"
1412
"github.com/stackitcloud/external-dns-stackit-webhook/pkg/api"
1513
"github.com/stackitcloud/external-dns-stackit-webhook/pkg/metrics"
16-
stackitconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
14+
"github.com/stackitcloud/external-dns-stackit-webhook/pkg/stackit"
1715
"go.uber.org/zap"
1816
"go.uber.org/zap/zapcore"
1917
"sigs.k8s.io/external-dns/endpoint"
2018
)
2119

2220
var (
23-
apiPort string
24-
baseUrl string
25-
projectID string
26-
worker int
27-
domainFilter []string
28-
dryRun bool
29-
logLevel string
21+
apiPort string
22+
authBearerToken string
23+
authKeyPath string
24+
baseUrl string
25+
projectID string
26+
worker int
27+
domainFilter []string
28+
dryRun bool
29+
logLevel string
3030
)
3131

3232
var rootCmd = &cobra.Command{
@@ -44,6 +44,11 @@ var rootCmd = &cobra.Command{
4444

4545
endpointDomainFilter := endpoint.DomainFilter{Filters: domainFilter}
4646

47+
stackitConfigOptions, err := stackit.SetConfigOptions(baseUrl, authBearerToken, authKeyPath)
48+
if err != nil {
49+
panic(err)
50+
}
51+
4752
stackitProvider, err := stackitprovider.NewStackitDNSProvider(
4853
logger.With(zap.String("component", "stackitprovider")),
4954
// ExternalDNS provider config
@@ -54,10 +59,7 @@ var rootCmd = &cobra.Command{
5459
Workers: worker,
5560
},
5661
// STACKIT client SDK config
57-
stackitconfig.WithHTTPClient(&http.Client{
58-
Timeout: 10 * time.Second,
59-
}),
60-
stackitconfig.WithEndpoint(baseUrl),
62+
stackitConfigOptions...,
6163
)
6264
if err != nil {
6365
panic(err)
@@ -111,6 +113,8 @@ func init() {
111113
cobra.OnInitialize(initConfig)
112114

113115
rootCmd.PersistentFlags().StringVar(&apiPort, "api-port", "8888", "Specifies the port to listen on.")
116+
rootCmd.PersistentFlags().StringVar(&authBearerToken, "auth-token", "", "Defines the authentication token for the STACKIT API. Mutually exclusive with 'auth-key-path'.")
117+
rootCmd.PersistentFlags().StringVar(&authKeyPath, "auth-key-path", "", "Defines the file path of the service account key for the STACKIT API. Mutually exclusive with 'auth-token'.")
114118
rootCmd.PersistentFlags().StringVar(&baseUrl, "base-url", "https://dns.api.stackit.cloud", " Identifies the Base URL for utilizing the API.")
115119
rootCmd.PersistentFlags().StringVar(&projectID, "project-id", "", "Specifies the project id of the STACKIT project.")
116120
rootCmd.PersistentFlags().IntVar(&worker, "worker", 10, "Specifies the number "+

pkg/stackit/options.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package stackit
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"time"
7+
8+
stackitconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
9+
)
10+
11+
// SetConfigOptions sets the default config options for the STACKIT
12+
// client and determines which type of authorization to use, depending on the
13+
// passed bearerToken and keyPath parameters. If no baseURL or an invalid
14+
// combination of auth options is given (neither or both), the function returns
15+
// an error.
16+
func SetConfigOptions(baseURL, bearerToken, keyPath string) ([]stackitconfig.ConfigurationOption, error) {
17+
if len(baseURL) == 0 {
18+
return nil, fmt.Errorf("base-url is required")
19+
}
20+
21+
options := []stackitconfig.ConfigurationOption{
22+
stackitconfig.WithHTTPClient(&http.Client{
23+
Timeout: 10 * time.Second,
24+
}),
25+
stackitconfig.WithEndpoint(baseURL),
26+
}
27+
28+
bearerTokenSet := len(bearerToken) > 0
29+
keyPathSet := len(keyPath) > 0
30+
31+
if (!bearerTokenSet && !keyPathSet) || (bearerTokenSet && keyPathSet) {
32+
return nil, fmt.Errorf("exactly only one of auth-token or auth-key-path is required")
33+
}
34+
35+
if bearerTokenSet {
36+
return append(options, stackitconfig.WithToken(bearerToken)), nil
37+
}
38+
39+
return append(options, stackitconfig.WithServiceAccountKeyPath(keyPath)), nil
40+
}

pkg/stackit/options_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package stackit
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestMissingBaseURL(t *testing.T) {
10+
t.Parallel()
11+
12+
options, err := SetConfigOptions("", "", "")
13+
assert.ErrorContains(t, err, "base-url")
14+
assert.Nil(t, options)
15+
}
16+
17+
func TestBothAuthOptionsMissing(t *testing.T) {
18+
t.Parallel()
19+
20+
options, err := SetConfigOptions("https://example.com", "", "")
21+
assert.ErrorContains(t, err, "auth-token or auth-key-path")
22+
assert.Nil(t, options)
23+
}
24+
25+
func TestBothAuthOptionsSet(t *testing.T) {
26+
t.Parallel()
27+
28+
options, err := SetConfigOptions("https://example.com", "token", "key/path")
29+
assert.ErrorContains(t, err, "auth-token or auth-key-path")
30+
assert.Nil(t, options)
31+
}
32+
33+
func TestBearerTokenSet(t *testing.T) {
34+
t.Parallel()
35+
36+
options, err := SetConfigOptions("https://example.com", "token", "")
37+
assert.NoError(t, err)
38+
assert.Len(t, options, 3)
39+
}
40+
41+
func TestKeyPathSet(t *testing.T) {
42+
t.Parallel()
43+
44+
options, err := SetConfigOptions("https://example.com", "", "key/path")
45+
assert.NoError(t, err)
46+
assert.Len(t, options, 3)
47+
}

0 commit comments

Comments
 (0)