Skip to content

Commit 5c42e01

Browse files
committed
Add validation where tokens and token cannot coexist in config
1 parent 73ae81a commit 5c42e01

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

extension/bearertokenauthextension/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The authenticator type has to be set to `bearertokenauth`.
2525

2626
- `token`: Static authorization token that needs to be sent on every gRPC client call as metadata.
2727

28-
- `tokens`: A list of static authorization tokens that needs to be sent on every gRPC client call as metadata.
28+
- `tokens`: A list of static authorization tokens, one of which needs to be sent on every gRPC client call as metadata.
2929

3030
- `filename`: Name of file that contains a authorization token that needs to be sent in every client call.
3131

extension/bearertokenauthextension/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ type Config struct {
2828
var (
2929
_ component.Config = (*Config)(nil)
3030
errNoTokenProvided = errors.New("no bearer token provided")
31+
errTokensAndTokenProvided = errors.New("either tokens or token should be provided, not both")
3132
)
3233

3334
// Validate checks if the extension configuration is valid
3435
func (cfg *Config) Validate() error {
3536
if cfg.BearerToken == "" && len(cfg.Tokens) == 0 && cfg.Filename == "" {
3637
return errNoTokenProvided
3738
}
39+
if cfg.BearerToken != "" && len(cfg.Tokens) > 0 {
40+
return errTokensAndTokenProvided
41+
}
3842
return nil
3943
}

extension/bearertokenauthextension/config_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ func TestLoadConfig(t *testing.T) {
6565
Filename: "file-containing.token",
6666
},
6767
},
68+
{
69+
id: component.NewIDWithName(metadata.Type, "tokensandtoken"),
70+
expected: &Config{
71+
Scheme: "Bearer",
72+
BearerToken: "sometoken",
73+
Tokens: []configopaque.String{"token1", "thistokenalsoworks"},
74+
},
75+
expectedErr: true,
76+
},
77+
{
78+
id: component.NewIDWithName(metadata.Type, "withtokensandfilename"),
79+
expected: &Config{
80+
Scheme: "Bearer",
81+
Tokens: []configopaque.String{"ignoredtoken1", "ignoredtoken2"},
82+
Filename: "file-containing.token",
83+
},
84+
},
6885
}
6986
for _, tt := range tests {
7087
t.Run(tt.id.String(), func(t *testing.T) {

extension/bearertokenauthextension/testdata/config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ bearertokenauth/both:
1616
scheme: "Bearer"
1717
token: "ignoredtoken"
1818
filename: "file-containing.token"
19+
bearertokenauth/withtokensandfilename:
20+
scheme: "Bearer"
21+
tokens:
22+
- "ignoredtoken1"
23+
- "ignoredtoken2"
24+
filename: "file-containing.token"
25+
bearertokenauth/tokensandtoken:
26+
scheme: "Bearer"
27+
tokens:
28+
- "token1"
29+
- "thistokenalsoworks"
30+
token: "my-token"

0 commit comments

Comments
 (0)