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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
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;
Expand All @@ -23,22 +25,48 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;

import java.util.concurrent.TimeUnit;

public class TokenProvider {

private static final long TOKEN_EXPIRATION_SECONDS = 3600L; // 1 hour
private static final long EXPIRATION_BUFFER_TIME_SECONDS = 300L; // 5 minutes
private static final long CACHE_EXPIRATION_TIME =
Leo6Leo marked this conversation as resolved.
Show resolved Hide resolved
TOKEN_EXPIRATION_SECONDS - EXPIRATION_BUFFER_TIME_SECONDS; // Cache tokens for 55 minutes

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

public TokenProvider() {
Config clientConfig = new ConfigBuilder().build();

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

this.tokenCache = CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRATION_TIME, TimeUnit.SECONDS)
.maximumSize(1000)
creydr marked this conversation as resolved.
Show resolved Hide resolved
.build();
}

Check warning on line 49 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#L45-L49

Added lines #L45 - L49 were not covered by tests

public String getToken(NamespacedName serviceAccount, String audience) {
String cacheKey = generateCacheKey(serviceAccount, audience);
String token = tokenCache.getIfPresent(cacheKey);

Check warning on line 53 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#L52-L53

Added lines #L52 - L53 were not covered by tests

if (token == null) {
token = requestToken(serviceAccount, audience);

Check warning on line 56 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#L56

Added line #L56 was not covered by tests
if (token != null) {
tokenCache.put(cacheKey, token);

Check warning on line 58 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#L58

Added line #L58 was not covered by tests
}
}

return token;

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
}

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

Check warning on line 69 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#L69

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

Expand All @@ -54,4 +82,8 @@
return null;
}
}

private String generateCacheKey(NamespacedName serviceAccount, String audience) {
return serviceAccount.namespace() + "/" + serviceAccount.name() + "/" + audience;

Check warning on line 87 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#L87

Added line #L87 was not covered by tests
}
}
Loading