Skip to content

Commit 73e2775

Browse files
authored
[Test] Service Accounts - handle token names with leading hyphen (#70983)
The CLI tool needs an option terminator (--) for another option names that begin with a hyphen. Otherwise it errors out with message of "not recognized option". The service account token name can begin with a hyphen. Hence we need to use -- when it is the case. An example of equivalent command line is ./bin/elasticsearch-service-tokens create elastic/fleet -- -lead-with-hyphen.
1 parent e3cb696 commit 73e2775

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountTokenTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static String randomTokenName() {
101101
Character[]::new,
102102
() -> randomFrom(VALID_TOKEN_NAME_CHARS));
103103
final String name = Arrays.stream(chars).map(String::valueOf).collect(Collectors.joining());
104-
return name.startsWith("_") ? "-" + name.substring(1) : name;
104+
return name.startsWith("_") ? randomAlphaOfLength(1) + name.substring(1) : name;
105105
}
106106

107107
public static String randomInvalidTokenName() {

x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/service/FileTokensToolTests.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,30 @@ public void testParsePrincipalAndTokenName() throws UserException {
127127
"Expected two arguments, service-account-principal and token-name, found extra:"));
128128
}
129129

130-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/70959")
131130
public void testCreateToken() throws Exception {
132-
final String tokenName1 = ServiceAccountTokenTests.randomTokenName();
131+
final String tokenName1 = randomValueOtherThanMany(n -> n.startsWith("-"), ServiceAccountTokenTests::randomTokenName);
133132
execute("create", pathHomeParameter, "elastic/fleet", tokenName1);
134133
assertServiceTokenExists("elastic/fleet/" + tokenName1);
135-
final String tokenName2 = ServiceAccountTokenTests.randomTokenName();
134+
final String tokenName2 = randomValueOtherThanMany(n -> n.startsWith("-") || n.equals(tokenName1),
135+
ServiceAccountTokenTests::randomTokenName);
136136
execute("create", pathHomeParameter, "elastic/fleet", tokenName2);
137137
assertServiceTokenExists("elastic/fleet/" + tokenName2);
138+
// token name with a leading hyphen requires an option terminator
139+
final String tokenName3 = "-" + ServiceAccountTokenTests.randomTokenName().substring(1);
140+
execute("create", pathHomeParameter, "elastic/fleet", "--", tokenName3);
141+
assertServiceTokenExists("elastic/fleet/" + tokenName3);
138142
final String output = terminal.getOutput();
139143
assertThat(output, containsString("SERVICE_TOKEN elastic/fleet/" + tokenName1 + " = "));
140144
assertThat(output, containsString("SERVICE_TOKEN elastic/fleet/" + tokenName2 + " = "));
145+
assertThat(output, containsString("SERVICE_TOKEN elastic/fleet/" + tokenName3 + " = "));
141146
}
142147

143-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/70959")
144148
public void testCreateTokenWithInvalidTokenName() throws Exception {
145149
final String tokenName = ServiceAccountTokenTests.randomInvalidTokenName();
146-
final UserException e = expectThrows(UserException.class,
147-
() -> execute("create", pathHomeParameter, "elastic/fleet", tokenName));
150+
final String[] args = tokenName.startsWith("-") ?
151+
new String[] { "create", pathHomeParameter, "elastic/fleet", "--", tokenName } :
152+
new String[] { "create", pathHomeParameter, "elastic/fleet", tokenName };
153+
final UserException e = expectThrows(UserException.class, () -> execute(args));
148154
assertServiceTokenNotExists("elastic/fleet/" + tokenName);
149155
assertThat(e.getMessage(), containsString(ServiceAccountToken.INVALID_TOKEN_NAME_MESSAGE));
150156
}

0 commit comments

Comments
 (0)