Skip to content

Commit 65c6719

Browse files
author
Damien Metzler
committed
test: cover OAUTHBEARER passthrough getters and Go token provider helpers
Adds unit tests for the previously uncovered patch lines flagged by Codecov: - KubernetesCredentials SASLJaasConfig/SASLLoginCallbackHandlerClass (decode, absent, null secret, null data) - Credentials default methods returning null via a minimal implementation - Go HasTokenProvider (common.go) and UnsupportedTokenProvider/errorTokenProvider (token_provider.go)
1 parent 2f79c36 commit 65c6719

4 files changed

Lines changed: 197 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2025 The Knative Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package oauth
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestHasTokenProvider(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
data map[string][]byte
29+
want bool
30+
}{
31+
{
32+
name: "tokenProvider present",
33+
data: map[string][]byte{saslTokenProviderKey: []byte(mskAccessTokenProvider)},
34+
want: true,
35+
},
36+
{
37+
name: "tokenProvider present but empty still counts as set",
38+
data: map[string][]byte{saslTokenProviderKey: []byte("")},
39+
want: true,
40+
},
41+
{
42+
name: "tokenProvider absent",
43+
data: map[string][]byte{saslAWSRegion: []byte(defaultAWSRegion)},
44+
want: false,
45+
},
46+
{
47+
name: "empty data",
48+
data: map[string][]byte{},
49+
want: false,
50+
},
51+
}
52+
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
assert.Equal(t, tt.want, HasTokenProvider(tt.data))
56+
})
57+
}
58+
}

‎control-plane/pkg/security/oauth/token_provider_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,15 @@ func TestNewTokenProvider(t *testing.T) {
139139
})
140140
}
141141
}
142+
143+
func TestUnsupportedTokenProvider(t *testing.T) {
144+
const msg = "OAUTHBEARER without a Go token provider is not supported by the control plane"
145+
146+
provider := UnsupportedTokenProvider(msg)
147+
assert.NotNil(t, provider)
148+
149+
token, err := provider.Token()
150+
assert.Nil(t, token)
151+
assert.Error(t, err)
152+
assert.Contains(t, err.Error(), msg)
153+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright © 2018 Knative Authors (knative-dev@googlegroups.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dev.knative.eventing.kafka.broker.core.security;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.apache.kafka.common.security.auth.SecurityProtocol;
21+
import org.junit.jupiter.api.Test;
22+
23+
public class CredentialsTest {
24+
25+
/**
26+
* A minimal {@link Credentials} implementation that does not override the
27+
* OAUTHBEARER passthrough default methods, exercising their default (null) behaviour.
28+
*/
29+
private static final class MinimalCredentials implements Credentials {
30+
@Override
31+
public String caCertificates() {
32+
return null;
33+
}
34+
35+
@Override
36+
public String userCertificate() {
37+
return null;
38+
}
39+
40+
@Override
41+
public String userKey() {
42+
return null;
43+
}
44+
45+
@Override
46+
public String SASLMechanism() {
47+
return null;
48+
}
49+
50+
@Override
51+
public String SASLUsername() {
52+
return null;
53+
}
54+
55+
@Override
56+
public String SASLPassword() {
57+
return null;
58+
}
59+
60+
@Override
61+
public SecurityProtocol securityProtocol() {
62+
return null;
63+
}
64+
65+
@Override
66+
public boolean skipClientAuth() {
67+
return false;
68+
}
69+
}
70+
71+
@Test
72+
public void defaultSASLJaasConfigReturnsNull() {
73+
assertThat(new MinimalCredentials().SASLJaasConfig()).isNull();
74+
}
75+
76+
@Test
77+
public void defaultSASLLoginCallbackHandlerClassReturnsNull() {
78+
assertThat(new MinimalCredentials().SASLLoginCallbackHandlerClass()).isNull();
79+
}
80+
}

‎data-plane/core/src/test/java/dev/knative/eventing/kafka/broker/core/security/KubernetesCredentialsTest.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,53 @@ public void getKubernetesCredentialsFromNullSecretData() {
9696
assertAll(new HashMap<>(), credentials);
9797
}
9898

99+
@Test
100+
public void saslJaasConfigAndCallbackHandlerAreDecoded() {
101+
final var data = Map.of(
102+
KubernetesCredentials.SASL_MECHANISM, "OAUTHBEARER",
103+
KubernetesCredentials.SASL_JAAS_CONFIG_KEY,
104+
"org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required;",
105+
KubernetesCredentials.SASL_LOGIN_CALLBACK_HANDLER_CLASS_KEY, "com.example.MyCallbackHandler");
106+
107+
final var credentials = getKubernetesCredentialsFromSecretData(data);
108+
109+
assertThat(credentials.SASLJaasConfig())
110+
.isEqualTo("org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required;");
111+
assertThat(credentials.SASLLoginCallbackHandlerClass()).isEqualTo("com.example.MyCallbackHandler");
112+
}
113+
114+
@Test
115+
public void saslJaasConfigAndCallbackHandlerAbsentReturnNull() {
116+
final var data = Map.of(KubernetesCredentials.SASL_MECHANISM, "OAUTHBEARER");
117+
118+
final var credentials = getKubernetesCredentialsFromSecretData(data);
119+
120+
assertThat(credentials.SASLJaasConfig()).isNull();
121+
assertThat(credentials.SASLLoginCallbackHandlerClass()).isNull();
122+
}
123+
124+
@Test
125+
public void saslJaasConfigAndCallbackHandlerNullOnNullSecret() {
126+
final Secret secret = null;
127+
final var credentials = new KubernetesCredentials(secret);
128+
129+
assertThat(credentials.SASLJaasConfig()).isNull();
130+
assertThat(credentials.SASLLoginCallbackHandlerClass()).isNull();
131+
}
132+
133+
@Test
134+
public void saslJaasConfigAndCallbackHandlerNullOnNullSecretData() {
135+
final var credentials = new KubernetesCredentials(new SecretBuilder()
136+
.withNewMetadata()
137+
.withNamespace("ns1")
138+
.withName("name1")
139+
.endMetadata()
140+
.build());
141+
142+
assertThat(credentials.SASLJaasConfig()).isNull();
143+
assertThat(credentials.SASLLoginCallbackHandlerClass()).isNull();
144+
}
145+
99146
@Test
100147
public void unknownSecurityProtocolReturnsNull() {
101148
final var data = Map.of(KubernetesCredentials.SECURITY_PROTOCOL, "SASSO_PLAINTEXT");

0 commit comments

Comments
 (0)