Skip to content

Commit dfd77cf

Browse files
committed
http: Add animal-sniffer plugin and fix reported errors
https://code.google.com/p/google-http-java-client/issues/detail?id=232
1 parent 5a43f85 commit dfd77cf

File tree

5 files changed

+98
-11
lines changed

5 files changed

+98
-11
lines changed

google-http-client-appengine/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434
</execution>
3535
</executions>
3636
</plugin>
37+
<!--App Engine uses Java 6 and above-->
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>animal-sniffer-maven-plugin</artifactId>
41+
<configuration>
42+
<signature>
43+
<groupId>org.codehaus.mojo.signature</groupId>
44+
<artifactId>java16</artifactId>
45+
<version>1.0</version>
46+
</signature>
47+
</configuration>
48+
</plugin>
3749
</plugins>
3850
</build>
3951
<dependencies>

google-http-client-findbugs/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
</execution>
2424
</executions>
2525
</plugin>
26+
<!--The animal-sniffer plugin crashes on FindBugs on the icu4j:2.6.1
27+
dependency-->
28+
<plugin>
29+
<groupId>org.codehaus.mojo</groupId>
30+
<artifactId>animal-sniffer-maven-plugin</artifactId>
31+
<configuration>
32+
<skip>true</skip>
33+
</configuration>
34+
</plugin>
2635
</plugins>
2736
</build>
2837
<dependencies>

google-http-client/src/main/java/com/google/api/client/util/IOUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ public static <S extends Serializable> S deserialize(InputStream inputStream) th
178178
try {
179179
return (S) new ObjectInputStream(inputStream).readObject();
180180
} catch (ClassNotFoundException exception) {
181-
throw new IOException("Failed to deserialize object", exception);
181+
IOException ioe = new IOException("Failed to deserialize object");
182+
ioe.initCause(exception);
183+
throw ioe;
182184
} finally {
183185
inputStream.close();
184186
}

google-http-client/src/main/java/com/google/api/client/util/store/FileDataStoreFactory.java

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@
1717
import com.google.api.client.util.Beta;
1818
import com.google.api.client.util.IOUtils;
1919
import com.google.api.client.util.Maps;
20+
import com.google.api.client.util.Throwables;
2021

2122
import java.io.File;
2223
import java.io.FileInputStream;
2324
import java.io.FileOutputStream;
2425
import java.io.IOException;
2526
import java.io.Serializable;
27+
import java.lang.reflect.InvocationTargetException;
28+
import java.lang.reflect.Method;
2629
import java.util.logging.Logger;
2730

2831
/**
2932
* {@link Beta} <br/>
3033
* Thread-safe file implementation of a credential store.
3134
*
35+
* <p>
36+
* For security purposes, the file's permissions are set to be accessible only by the file's owner.
37+
* Note that Java 1.5 does not support manipulating file permissions, and must be done manually or
38+
* using the JNI.
39+
* </p>
40+
*
3241
* @since 1.16
3342
* @author Yaniv Inbar
3443
*/
@@ -54,16 +63,7 @@ public FileDataStoreFactory(File dataDirectory) throws IOException {
5463
if (!dataDirectory.exists() && !dataDirectory.mkdirs()) {
5564
throw new IOException("unable to create directory: " + dataDirectory);
5665
}
57-
// disable access by other users if O/S allows it
58-
if (!dataDirectory.setReadable(false, false) || !dataDirectory.setWritable(false, false)
59-
|| !dataDirectory.setExecutable(false, false)) {
60-
LOGGER.warning("unable to change permissions for everybody: " + dataDirectory);
61-
}
62-
// set file permissions to readable and writable by user
63-
if (!dataDirectory.setReadable(true) || !dataDirectory.setWritable(true)
64-
|| !dataDirectory.setExecutable(true)) {
65-
throw new IOException("unable to set permissions: " + dataDirectory);
66-
}
66+
setPermissionsToOwnerOnly(dataDirectory);
6767
}
6868

6969
/** Returns the data directory. */
@@ -117,4 +117,45 @@ public FileDataStoreFactory getDataStoreFactory() {
117117
return (FileDataStoreFactory) super.getDataStoreFactory();
118118
}
119119
}
120+
121+
/**
122+
* Attempts to set the given file's permissions such that it can only be read, written, and
123+
* executed by the file's owner.
124+
*
125+
* @param file the file's permissions to modify
126+
* @throws IOException
127+
*/
128+
static void setPermissionsToOwnerOnly(File file) throws IOException {
129+
// Disable access by other users if O/S allows it and set file permissions to readable and
130+
// writable by user. Use reflection since JDK 1.5 will not have these methods
131+
try {
132+
Method setReadable = File.class.getMethod("setReadable", boolean.class, boolean.class);
133+
Method setWritable = File.class.getMethod("setWritable", boolean.class, boolean.class);
134+
Method setExecutable = File.class.getMethod("setExecutable", boolean.class, boolean.class);
135+
if (!(Boolean) setReadable.invoke(file, false, false)
136+
|| !(Boolean) setWritable.invoke(file, false, false)
137+
|| !(Boolean) setExecutable.invoke(file, false, false)) {
138+
LOGGER.warning("unable to change permissions for everybody: " + file);
139+
}
140+
if (!(Boolean) setReadable.invoke(file, true, true)
141+
|| !(Boolean) setWritable.invoke(file, true, true)
142+
|| !(Boolean) setExecutable.invoke(file, true, true)) {
143+
LOGGER.warning("unable to change permissions for owner: " + file);
144+
}
145+
} catch (InvocationTargetException exception) {
146+
Throwable cause = exception.getCause();
147+
Throwables.propagateIfPossible(cause, IOException.class);
148+
// shouldn't reach this point, but just in case...
149+
throw new RuntimeException(cause);
150+
} catch (NoSuchMethodException exception) {
151+
LOGGER.warning("Unable to set permissions for " + file
152+
+ ", likely because you are running a version of Java prior to 1.6");
153+
} catch (SecurityException exception) {
154+
// ignored
155+
} catch (IllegalAccessException exception) {
156+
// ignored
157+
} catch (IllegalArgumentException exception) {
158+
// ignored
159+
}
160+
}
120161
}

pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@
357357
<artifactId>clirr-maven-plugin</artifactId>
358358
<version>2.5</version>
359359
</plugin>
360+
<plugin>
361+
<groupId>org.codehaus.mojo</groupId>
362+
<artifactId>animal-sniffer-maven-plugin</artifactId>
363+
<version>1.9</version>
364+
</plugin>
360365
<plugin>
361366
<groupId>org.sonatype.plugins</groupId>
362367
<artifactId>jarjar-maven-plugin</artifactId>
@@ -534,6 +539,24 @@
534539
<logResults>true</logResults>
535540
</configuration>
536541
</plugin>
542+
<plugin>
543+
<groupId>org.codehaus.mojo</groupId>
544+
<artifactId>animal-sniffer-maven-plugin</artifactId>
545+
<configuration>
546+
<signature>
547+
<groupId>org.codehaus.mojo.signature</groupId>
548+
<artifactId>java15</artifactId>
549+
<version>1.0</version>
550+
</signature>
551+
</configuration>
552+
<executions>
553+
<execution>
554+
<goals>
555+
<goal>check</goal>
556+
</goals>
557+
</execution>
558+
</executions>
559+
</plugin>
537560
</plugins>
538561
</build>
539562
<properties>

0 commit comments

Comments
 (0)