1717
1818package org .apache .apisix .plugin .runner .server ;
1919
20+ import java .io .IOException ;
21+ import java .nio .file .FileSystems ;
2022import java .nio .file .Files ;
2123import java .nio .file .Path ;
2224import 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 ;
2330import java .util .HashMap ;
2431import java .util .List ;
2532import java .util .Map ;
2633import java .util .stream .Collectors ;
34+ import java .util .Optional ;
35+ import java .util .Set ;
2736
2837import org .slf4j .Logger ;
2938import org .slf4j .LoggerFactory ;
3241import org .springframework .beans .factory .annotation .Value ;
3342import org .springframework .boot .CommandLineRunner ;
3443import org .springframework .stereotype .Component ;
44+
45+ import com .google .common .base .Splitter ;
3546import com .google .common .cache .Cache ;
3647import io .netty .bootstrap .ServerBootstrap ;
3748import io .netty .channel .ChannelFuture ;
6374public 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