Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7bd354d

Browse files
committed
FIX Restrict socket permissions and manage ACLs if needed
1 parent ed6e82a commit 7bd354d

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

.github/workflows/runner-e2e.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jobs:
5555
run: |
5656
cd dist
5757
tar -zxvf apache-apisix-java-plugin-runner-*bin.tar.gz
58+
chmod 777 /tmp/runner.sock
5859
java -jar -DAPISIX_LISTEN_ADDRESS=unix:/tmp/runner.sock -DAPISIX_CONF_EXPIRE_TIME=3600 ./apisix-runner-bin/apisix-java-plugin-runner.jar &
5960
6061
- name: startup apisix

docs/en/latest/how-it-works.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ Note: If you see some error logs like
6464
phase_func(): failed to connect to the unix socket unix:/tmp/runner.sock: permission denied
6565
```
6666

67-
in the `error.log` of APISIX, you can change the permissions of this file for debug, execute commands like
67+
in the `error.log` of APISIX, ensure the APISIX user is provided rights on the socket.
68+
This can be done by starting the APISIX Java pugin runner with a system property to grant
69+
APISIX the right to read and write to the Java plugin through a unix ACL.
6870

6971
```shell
70-
chmod 766 /tmp/runner.sock
72+
java ... -Dsocket.allowed.users=apisixUser1,apisixUser2 ...
7173
```
7274

7375
To get more detailed debugging information, you can modify the output level of the log.
@@ -118,3 +120,10 @@ then add the following configure in the `config.yaml` file of APISIX
118120
ext-plugin:
119121
cmd: ['java', '-jar', '-Xmx4g', '-Xms4g', '/path/to/apisix-runner-bin/apisix-java-plugin-runner.jar']
120122
```
123+
124+
If running on a different user grant APISIX the unix ACL to the socket:
125+
126+
```yaml
127+
ext-plugin:
128+
cmd: ['java', '-jar', '-Xmx4g', '-Xms4g', '-Dsocket.allowed.users=apisixUser', '/path/to/apisix-runner-bin/apisix-java-plugin-runner.jar']
129+
```

runner-core/src/main/java/org/apache/apisix/plugin/runner/server/ApplicationRunner.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@
1717

1818
package org.apache.apisix.plugin.runner.server;
1919

20+
import java.io.IOException;
21+
import java.nio.file.FileSystems;
2022
import java.nio.file.Files;
2123
import java.nio.file.Path;
2224
import java.nio.file.Paths;
25+
import java.nio.file.attribute.AclEntry;
26+
import java.nio.file.attribute.AclEntryPermission;
27+
import java.nio.file.attribute.AclEntryType;
28+
import java.nio.file.attribute.AclFileAttributeView;
29+
import java.nio.file.attribute.PosixFilePermission;
2330
import java.util.HashMap;
2431
import java.util.List;
2532
import java.util.Map;
2633
import java.util.stream.Collectors;
34+
import java.util.Optional;
35+
import java.util.Set;
2736

2837
import org.slf4j.Logger;
2938
import org.slf4j.LoggerFactory;
@@ -32,6 +41,8 @@
3241
import org.springframework.beans.factory.annotation.Value;
3342
import org.springframework.boot.CommandLineRunner;
3443
import org.springframework.stereotype.Component;
44+
45+
import com.google.common.base.Splitter;
3546
import com.google.common.cache.Cache;
3647
import io.netty.bootstrap.ServerBootstrap;
3748
import io.netty.channel.ChannelFuture;
@@ -63,6 +74,8 @@
6374
public class ApplicationRunner implements CommandLineRunner {
6475

6576
private final Logger logger = LoggerFactory.getLogger(ApplicationRunner.class);
77+
private static final List<String> SOCKET_ALLOWED_USERS = Splitter.on(',')
78+
.splitToList(System.getProperty("socket.allowed.users", ""));
6679

6780
@Value("${socket.file}")
6881
private String socketFile;
@@ -114,7 +127,7 @@ public void start(String path) throws Exception {
114127
try {
115128
initServerBootstrap(bootstrap);
116129
ChannelFuture future = bootstrap.bind(new DomainSocketAddress(path)).sync();
117-
Runtime.getRuntime().exec("chmod 777 " + socketFile);
130+
manageSocketPermissions(socketFile);
118131
logger.warn("java runner is listening on the socket file: {}", socketFile);
119132

120133
future.channel().closeFuture().sync();
@@ -123,6 +136,40 @@ public void start(String path) throws Exception {
123136
}
124137
}
125138

139+
private static void manageSocketPermissions(String pathString) throws IOException {
140+
Set<PosixFilePermission> permissions = Set.of(
141+
PosixFilePermission.OWNER_READ,
142+
PosixFilePermission.OWNER_WRITE,
143+
PosixFilePermission.OWNER_EXECUTE);
144+
Path path = Paths.get(pathString);
145+
Files.setPosixFilePermissions(path, permissions);
146+
147+
if (!SOCKET_ALLOWED_USERS.isEmpty()) {
148+
Optional.ofNullable(Files.getFileAttributeView(path, AclFileAttributeView.class))
149+
.orElseThrow(() -> new UnsupportedOperationException("ACLs are not supported on this filesystem."))
150+
.setAcl(SOCKET_ALLOWED_USERS.stream()
151+
.map(ApplicationRunner::computeAclEntry)
152+
.collect(Collectors.toList()));
153+
}
154+
}
155+
156+
private static AclEntry computeAclEntry(String user) {
157+
try {
158+
return AclEntry.newBuilder()
159+
.setType(AclEntryType.ALLOW)
160+
.setPrincipal(FileSystems.getDefault()
161+
.getUserPrincipalLookupService()
162+
.lookupPrincipalByName(user))
163+
.setPermissions(
164+
AclEntryPermission.READ_DATA,
165+
AclEntryPermission.WRITE_DATA,
166+
AclEntryPermission.EXECUTE)
167+
.build();
168+
} catch (Exception e) {
169+
throw new RuntimeException(e);
170+
}
171+
}
172+
126173
private void initServerBootstrap(ServerBootstrap bootstrap) {
127174
bootstrap.childHandler(new ChannelInitializer<DomainSocketChannel>() {
128175
@Override

0 commit comments

Comments
 (0)