Skip to content

Commit 1150acd

Browse files
authored
feat: add Android 19 compatible FileDataStoreFactory implementation (googleapis#1070)
* ci: add a animal sniffer check configuration for Android API level 19 * build(deps): update animal-sniffer-maven-plugin version * feat: add Android comptible FileDataStoreFactory implementation This is branched from the original implementation prior to switching to NIO for Windows compatibility. * ci: allow java.nio.file in google-http-client * chore: fix lint * build: put animal-sniffer-maven-plugin back to 1.17 * fix: remove reflection from file permission settings File setReadable(), setWritable(), setExecutable() was added in API level 9, so we should not need to protect android users from missing methods as we support Android API 19+. * chore: run formatter * chore: remove docs mentioning Java 1.5 * fix: move member assignment after input validation
1 parent 05160aa commit 1150acd

File tree

4 files changed

+185
-8
lines changed

4 files changed

+185
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.api.client.extensions.android.util.store;
17+
18+
import com.google.api.client.util.IOUtils;
19+
import com.google.api.client.util.Maps;
20+
import com.google.api.client.util.store.AbstractDataStoreFactory;
21+
import com.google.api.client.util.store.AbstractMemoryDataStore;
22+
import com.google.api.client.util.store.DataStore;
23+
import java.io.File;
24+
import java.io.FileInputStream;
25+
import java.io.FileOutputStream;
26+
import java.io.IOException;
27+
import java.io.Serializable;
28+
import java.util.logging.Logger;
29+
30+
/**
31+
* Thread-safe file implementation of a credential store.
32+
*
33+
* <p>For security purposes, the file's permissions are set to be accessible only by the file's
34+
* owner.
35+
*
36+
* <p>Note: This class was branched from the primary implementation in google-http-client to allow
37+
* the mainline implementation to support Windows file permissions.
38+
*
39+
* @since 1.36
40+
* @author Yaniv Inbar
41+
*/
42+
public class FileDataStoreFactory extends AbstractDataStoreFactory {
43+
44+
private static final Logger LOGGER = Logger.getLogger(FileDataStoreFactory.class.getName());
45+
46+
/** Directory to store data. */
47+
private final File dataDirectory;
48+
49+
/** @param dataDirectory data directory */
50+
public FileDataStoreFactory(File dataDirectory) throws IOException {
51+
dataDirectory = dataDirectory.getCanonicalFile();
52+
// error if it is a symbolic link
53+
if (IOUtils.isSymbolicLink(dataDirectory)) {
54+
throw new IOException("unable to use a symbolic link: " + dataDirectory);
55+
}
56+
// create parent directory (if necessary)
57+
if (!dataDirectory.exists() && !dataDirectory.mkdirs()) {
58+
throw new IOException("unable to create directory: " + dataDirectory);
59+
}
60+
this.dataDirectory = dataDirectory;
61+
setPermissionsToOwnerOnly(dataDirectory);
62+
}
63+
64+
/** Returns the data directory. */
65+
public final File getDataDirectory() {
66+
return dataDirectory;
67+
}
68+
69+
@Override
70+
protected <V extends Serializable> DataStore<V> createDataStore(String id) throws IOException {
71+
return new FileDataStore<V>(this, dataDirectory, id);
72+
}
73+
74+
/**
75+
* File data store that inherits from the abstract memory data store because the key-value pairs
76+
* are stored in a memory cache, and saved in the file (see {@link #save()} when changing values.
77+
*
78+
* @param <V> serializable type of the mapped value
79+
*/
80+
static class FileDataStore<V extends Serializable> extends AbstractMemoryDataStore<V> {
81+
82+
/** File to store data. */
83+
private final File dataFile;
84+
85+
FileDataStore(FileDataStoreFactory dataStore, File dataDirectory, String id)
86+
throws IOException {
87+
super(dataStore, id);
88+
this.dataFile = new File(dataDirectory, id);
89+
// error if it is a symbolic link
90+
if (IOUtils.isSymbolicLink(dataFile)) {
91+
throw new IOException("unable to use a symbolic link: " + dataFile);
92+
}
93+
// create new file (if necessary)
94+
if (dataFile.createNewFile()) {
95+
keyValueMap = Maps.newHashMap();
96+
// save the credentials to create a new file
97+
save();
98+
} else {
99+
// load credentials from existing file
100+
keyValueMap = IOUtils.deserialize(new FileInputStream(dataFile));
101+
}
102+
}
103+
104+
@Override
105+
public void save() throws IOException {
106+
IOUtils.serialize(keyValueMap, new FileOutputStream(dataFile));
107+
}
108+
109+
@Override
110+
public FileDataStoreFactory getDataStoreFactory() {
111+
return (FileDataStoreFactory) super.getDataStoreFactory();
112+
}
113+
}
114+
115+
/**
116+
* Attempts to set the given file's permissions such that it can only be read, written, and
117+
* executed by the file's owner.
118+
*
119+
* @param file the file's permissions to modify
120+
*/
121+
static void setPermissionsToOwnerOnly(File file) {
122+
// Disable access by other users if O/S allows it and set file permissions to readable and
123+
// writable by user.
124+
try {
125+
if (!file.setReadable(false, false)
126+
|| !file.setWritable(false, false)
127+
|| !file.setExecutable(false, false)) {
128+
LOGGER.warning("unable to change permissions for everybody: " + file);
129+
}
130+
if (!file.setReadable(true, true)
131+
|| !file.setWritable(true, true)
132+
|| !file.setExecutable(true, true)) {
133+
LOGGER.warning("unable to change permissions for owner: " + file);
134+
}
135+
} catch (SecurityException exception) {
136+
// ignored
137+
} catch (IllegalArgumentException exception) {
138+
// ignored
139+
}
140+
}
141+
}

google-http-client/pom.xml

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@
2626
<ignoredUnusedDeclaredDependencies>io.opencensus:opencensus-impl</ignoredUnusedDeclaredDependencies>
2727
</configuration>
2828
</plugin>
29+
<plugin>
30+
<groupId>org.codehaus.mojo</groupId>
31+
<artifactId>animal-sniffer-maven-plugin</artifactId>
32+
<executions>
33+
<execution>
34+
<id>android</id>
35+
<goals>
36+
<goal>check</goal>
37+
</goals>
38+
<configuration>
39+
<ignores>
40+
<ignore>java.nio.file.*</ignore>
41+
</ignores>
42+
</configuration>
43+
</execution>
44+
</executions>
45+
</plugin>
2946
</plugins>
3047
</pluginManagement>
3148
<plugins>

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
* <p>For security purposes, the file's permissions are set such that the file is only accessible by
4646
* the file's owner.
4747
*
48+
* <p>Note: this class is not compatible with Android lower than API level 26 (Oreo). For an
49+
* implementation compatible with Android < 26, please use
50+
* com.google.api.client.extensions.android.util.store.FileDataStoreFactory which is provided by
51+
* com.google.http-client:google-http-client-android.
52+
*
4853
* @since 1.16
4954
* @author Yaniv Inbar
5055
*/
@@ -61,7 +66,6 @@ public class FileDataStoreFactory extends AbstractDataStoreFactory {
6166
/** @param dataDirectory data directory */
6267
public FileDataStoreFactory(File dataDirectory) throws IOException {
6368
dataDirectory = dataDirectory.getCanonicalFile();
64-
this.dataDirectory = dataDirectory;
6569
// error if it is a symbolic link
6670
if (IOUtils.isSymbolicLink(dataDirectory)) {
6771
throw new IOException("unable to use a symbolic link: " + dataDirectory);
@@ -70,6 +74,7 @@ public FileDataStoreFactory(File dataDirectory) throws IOException {
7074
if (!dataDirectory.exists() && !dataDirectory.mkdirs()) {
7175
throw new IOException("unable to create directory: " + dataDirectory);
7276
}
77+
this.dataDirectory = dataDirectory;
7378

7479
if (IS_WINDOWS) {
7580
setPermissionsToOwnerOnlyWindows(dataDirectory);

pom.xml

+21-7
Original file line numberDiff line numberDiff line change
@@ -499,18 +499,32 @@
499499
<plugin>
500500
<groupId>org.codehaus.mojo</groupId>
501501
<artifactId>animal-sniffer-maven-plugin</artifactId>
502-
<configuration>
503-
<signature>
504-
<groupId>org.codehaus.mojo.signature</groupId>
505-
<artifactId>java17</artifactId>
506-
<version>1.0</version>
507-
</signature>
508-
</configuration>
509502
<executions>
510503
<execution>
504+
<id>java7</id>
511505
<goals>
512506
<goal>check</goal>
513507
</goals>
508+
<configuration>
509+
<signature>
510+
<groupId>org.codehaus.mojo.signature</groupId>
511+
<artifactId>java17</artifactId>
512+
<version>1.0</version>
513+
</signature>
514+
</configuration>
515+
</execution>
516+
<execution>
517+
<id>android</id>
518+
<goals>
519+
<goal>check</goal>
520+
</goals>
521+
<configuration>
522+
<signature>
523+
<groupId>net.sf.androidscents.signature</groupId>
524+
<artifactId>android-api-level-19</artifactId>
525+
<version>4.4.2_r4</version>
526+
</signature>
527+
</configuration>
514528
</execution>
515529
</executions>
516530
</plugin>

0 commit comments

Comments
 (0)