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

[data-plane] Add the caching for OIDC JWT token to tokenprovider #3663

Merged
2 changes: 1 addition & 1 deletion control-plane/pkg/contract/contract.pb.go
Leo6Leo marked this conversation as resolved.
Show resolved Hide resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,51 @@
*/
package dev.knative.eventing.kafka.broker.core.oidc;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import dev.knative.eventing.kafka.broker.core.NamespacedName;
import io.fabric8.kubernetes.api.model.authentication.TokenRequest;
import io.fabric8.kubernetes.api.model.authentication.TokenRequestBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;

import java.util.concurrent.TimeUnit;

public class TokenProvider {

private final KubernetesClient kubernetesClient;
private final Cache<String, String> tokenCache;

public TokenProvider(KubernetesClient kubernetesClient) {
this.kubernetesClient = kubernetesClient;

Check warning on line 33 in data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java

View check run for this annotation

Codecov / codecov/patch

data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java#L32-L33

Added lines #L32 - L33 were not covered by tests
creydr marked this conversation as resolved.
Show resolved Hide resolved

this.tokenCache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS) // 1 hour expiration after write
creydr marked this conversation as resolved.
Show resolved Hide resolved
.maximumSize(1000)
creydr marked this conversation as resolved.
Show resolved Hide resolved
.build();
}

Check warning on line 39 in data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java

View check run for this annotation

Codecov / codecov/patch

data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java#L35-L39

Added lines #L35 - L39 were not covered by tests

public String getToken(NamespacedName serviceAccount, String audience) {
String cacheKey = serviceAccount.namespace() + "/" + serviceAccount.name() + "/" + audience;
String token = tokenCache.getIfPresent(cacheKey);

Check warning on line 43 in data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java

View check run for this annotation

Codecov / codecov/patch

data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java#L42-L43

Added lines #L42 - L43 were not covered by tests

public TokenProvider() {
Config clientConfig = new ConfigBuilder().build();
if (token == null) {
// If the token is not in the cache, request a new one
token = requestToken(serviceAccount, audience);

Check warning on line 47 in data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java

View check run for this annotation

Codecov / codecov/patch

data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java#L47

Added line #L47 was not covered by tests

// If token is successfully retrieved, cache it
if (token != null) {
tokenCache.put(cacheKey, token);

Check warning on line 51 in data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java

View check run for this annotation

Codecov / codecov/patch

data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java#L51

Added line #L51 was not covered by tests
}
}

kubernetesClient =
new KubernetesClientBuilder().withConfig(clientConfig).build();
return token;

Check warning on line 55 in data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java

View check run for this annotation

Codecov / codecov/patch

data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java#L55

Added line #L55 was not covered by tests
}

public String requestToken(NamespacedName serviceAccount, String audience) {
private String requestToken(NamespacedName serviceAccount, String audience) {
TokenRequest tokenRequest = new TokenRequestBuilder()
.withNewSpec()
.withAudiences(audience)
.withExpirationSeconds(3600L)
.withExpirationSeconds(3600L) // 1 hour

Check warning on line 62 in data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java

View check run for this annotation

Codecov / codecov/patch

data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenProvider.java#L62

Added line #L62 was not covered by tests
.endSpec()
.build();

Expand Down
Loading