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

Add support for ipv6 ip address in user injection #4399

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/main/java/org/opensearch/security/auth/UserInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ public TransportAddress getTransportAddress() {
}

public void setTransportAddress(String addr) throws UnknownHostException, IllegalArgumentException {
// format is ip:port
String[] ipAndPort = addr.split(":");
if (ipAndPort.length != 2) {
int lastColonIndex = addr.lastIndexOf(':');
if (lastColonIndex == -1) {
DarshitChanpura marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException("Remote address must have format ip:port");
}

InetAddress iAdress = InetAddress.getByName(ipAndPort[0]);
int port = Integer.parseInt(ipAndPort[1]);
String ip = addr.substring(0, lastColonIndex);
String portString = addr.substring(lastColonIndex + 1);

InetAddress iAdress = InetAddress.getByName(ip);
int port = Integer.parseInt(portString);

this.transportAddress = new TransportAddress(iAdress, port);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,58 @@ public void testValidInjectUser() {
assertEquals(injectedUser.getRoles(), roles);
}

@Test
public void testValidInjectUserIpV6() {
HashSet<String> roles = new HashSet<>();
roles.addAll(Arrays.asList("role1", "role2"));
threadContext.putTransient(
ConfigConstants.OPENDISTRO_SECURITY_INJECTED_USER,
"user|role1,role2|2001:db8:3333:4444:5555:6666:7777:8888:9200"
derek-ho marked this conversation as resolved.
Show resolved Hide resolved
);
UserInjector.InjectedUser injectedUser = userInjector.getInjectedUser();
assertEquals("user", injectedUser.getName());
assertEquals(9200, injectedUser.getTransportAddress().getPort());
assertEquals("2001:db8:3333:4444:5555:6666:7777:8888", injectedUser.getTransportAddress().getAddress());
derek-ho marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void testValidInjectUserIpV6ShortFormat() {
HashSet<String> roles = new HashSet<>();
roles.addAll(Arrays.asList("role1", "role2"));
threadContext.putTransient(ConfigConstants.OPENDISTRO_SECURITY_INJECTED_USER, "user|role1,role2|2001:db8::1:9200");
UserInjector.InjectedUser injectedUser = userInjector.getInjectedUser();
assertEquals("user", injectedUser.getName());
assertEquals(9200, injectedUser.getTransportAddress().getPort());
assertEquals("2001:db8::1", injectedUser.getTransportAddress().getAddress());
}

@Test
public void testInvalidInjectUserIpV6() {
HashSet<String> roles = new HashSet<>();
roles.addAll(Arrays.asList("role1", "role2"));
threadContext.putTransient(
ConfigConstants.OPENDISTRO_SECURITY_INJECTED_USER,
"user|role1,role2|2001:db8:3333:5555:6666:7777:8888:9200"
);
User injectedUser = userInjector.getInjectedUser();
assertNull(injectedUser);
}

@Test
public void testValidInjectUserBracketsIpV6() {
HashSet<String> roles = new HashSet<>();
roles.addAll(Arrays.asList("role1", "role2"));
threadContext.putTransient(
ConfigConstants.OPENDISTRO_SECURITY_INJECTED_USER,
"user|role1,role2|[2001:db8:3333:4444:5555:6666:7777:8888]:9200"
derek-ho marked this conversation as resolved.
Show resolved Hide resolved
);
UserInjector.InjectedUser injectedUser = userInjector.getInjectedUser();
assertEquals("user", injectedUser.getName());
assertEquals(roles, injectedUser.getRoles());
assertEquals(9200, injectedUser.getTransportAddress().getPort());
assertEquals("2001:db8:3333:4444:5555:6666:7777:8888", injectedUser.getTransportAddress().getAddress());
}

@Test
public void testInvalidInjectUser() {
HashSet<String> roles = new HashSet<>();
Expand Down
Loading