Skip to content

Commit 1700c5e

Browse files
committed
Make the OAuth Principals compatible with K8S
The strimzi user entity operator only allows the username to be the same as the resource name in Kubernetes. Instead of patching the operator we patch the oauth plugin as that is a supported usecase in Strimzi.
1 parent a3a58f9 commit 1700c5e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

oauth-common/src/main/java/io/strimzi/kafka/oauth/common/PrincipalExtractor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import static io.strimzi.kafka.oauth.common.JSONUtil.getClaimFromJWT;
1111

12+
import java.util.Locale;
13+
1214
/**
1315
* An object with logic for extracting a principal name (i.e. a user id) from a JWT token.
1416
* <p>
@@ -113,12 +115,26 @@ private String extractUsername(Extractor extractor, JsonNode json) {
113115
if (extractor.getAttributeName() != null) {
114116
String result = getClaimFromJWT(json, extractor.getAttributeName());
115117
if (result != null && !result.isEmpty()) {
118+
// HACK(cypres): Make the username compatible with Kubernetes names
119+
result = result.toLowerCase(Locale.ROOT)
120+
.replace("@", "-at-")
121+
.replaceAll("[^a-z0-9.-]", "-");
122+
if (result.length() > 253) {
123+
result = result.substring(0, 253);
124+
}
116125
return result;
117126
}
118127
} else {
119128
JsonNode queryResult = extractor.getJSONPathQuery().apply(json);
120129
String result = queryResult == null ? null : queryResult.asText().trim();
121130
if (result != null && !result.isEmpty()) {
131+
// HACK(cypres): Make the username compatible with Kubernetes names
132+
result = result.toLowerCase(Locale.ROOT)
133+
.replace("@", "-at-")
134+
.replaceAll("[^a-z0-9.-]", "-");
135+
if (result.length() > 253) {
136+
result = result.substring(0, 253);
137+
}
122138
return result;
123139
}
124140
}

0 commit comments

Comments
 (0)