Skip to content

Commit 9255664

Browse files
committed
rpc: introduce handy RpcAuthTypeUnix#ofCurrentUnixUser method
Motivation: In situations, where we need to use currently logged user with AUTH_SYS a handy shortcut can be used to generate corresponding RpcAuthTypeUnix object. Modification: Introduce RpcAuthTypeUnix#ofCurrentUnixUser that generated an instance of RpcAuthTypeUnix object by getting logged users uid, gid and gids from UnixSystem class. Update animal-sniffer-maven-plugin configuration to allow usage of classes from com.sun.security.auth package at they are a part of official JDK. Result: An instance of RpcAuthTypeUnix can be generated for currently logged user. Acked-by: Paul Millar Target: master
1 parent 9f8fde0 commit 9255664

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

oncrpc4j-core/src/main/java/org/dcache/oncrpc4j/rpc/RpcAuthTypeUnix.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009 - 2018 Deutsches Elektronen-Synchroton,
2+
* Copyright (c) 2009 - 2019 Deutsches Elektronen-Synchroton,
33
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
44
*
55
* This library is free software; you can redistribute it and/or modify
@@ -21,6 +21,7 @@
2121

2222
import com.sun.security.auth.UnixNumericGroupPrincipal;
2323
import com.sun.security.auth.UnixNumericUserPrincipal;
24+
import com.sun.security.auth.module.UnixSystem;
2425
import org.dcache.oncrpc4j.xdr.XdrAble;
2526
import org.dcache.oncrpc4j.xdr.XdrDecodingStream;
2627
import org.dcache.oncrpc4j.xdr.XdrEncodingStream;
@@ -29,10 +30,14 @@
2930

3031
import javax.security.auth.Subject;
3132
import java.io.IOException;
33+
import java.net.InetAddress;
3234
import java.security.Principal;
35+
import java.time.Instant;
3336
import java.util.Arrays;
3437
import java.util.Set;
3538

39+
import static com.google.common.base.Preconditions.checkState;
40+
3641
public class RpcAuthTypeUnix implements RpcAuth, XdrAble {
3742

3843
private final int _type = RpcAuthType.UNIX;
@@ -137,4 +142,32 @@ public int gid() {
137142
public int[] gids() {
138143
return _gids;
139144
}
145+
146+
/**
147+
* Get {@link RpcAuthTypeUnix} corresponding to the UNIX user running
148+
* this application.
149+
* @return RPC auth corresponding to UNIX credentials.
150+
* @throws IllegalStateException
151+
* @throws IOException if failed to get information to build UNIX credentials.
152+
*/
153+
public static RpcAuthTypeUnix ofCurrentUnixUser() throws
154+
IllegalStateException, IOException {
155+
156+
String os = System.getProperty("os.name");
157+
checkState(!os.toLowerCase().startsWith("windows"), "%s platform not supported", os);
158+
159+
UnixSystem user = new UnixSystem();
160+
161+
long uid = user.getUid();
162+
long gid = user.getGid();
163+
long[] gids = user.getGroups();
164+
165+
return new RpcAuthTypeUnix(
166+
(int)uid,
167+
(int)gid,
168+
Arrays.stream(gids).mapToInt(l -> (int)l).toArray(),
169+
(int)Instant.now().getEpochSecond(),
170+
InetAddress.getLocalHost().getHostName()
171+
);
172+
}
140173
}

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<maven-pmd-plugin.version>3.0.1</maven-pmd-plugin.version>
5757

5858
<!-- version of other plugins-->
59-
<animal-sniffer-maven-plugin>1.15</animal-sniffer-maven-plugin>
59+
<animal-sniffer-maven-plugin>1.18</animal-sniffer-maven-plugin>
6060
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
6161
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
6262
<maven-release-plugin>2.4.2</maven-release-plugin>
@@ -132,6 +132,10 @@
132132
<artifactId>java18</artifactId>
133133
<version>1.0</version>
134134
</signature>
135+
<ignores>
136+
<!-- part of official jdk -->
137+
<ignore>com.sun.security.auth.*</ignore>
138+
</ignores>
135139
</configuration>
136140
<executions>
137141
<execution>
@@ -143,6 +147,7 @@
143147
</execution>
144148
</executions>
145149
</plugin>
150+
146151
<plugin>
147152
<groupId>org.apache.maven.plugins</groupId>
148153
<artifactId>maven-release-plugin</artifactId>

0 commit comments

Comments
 (0)