479832 Use system properties for gcloud config for GCloudDatastore session manager

This commit is contained in:
Jan Bartel 2015-10-15 19:43:34 +11:00
parent 71bd387a58
commit e00b56967a
23 changed files with 113 additions and 173 deletions

View File

@ -4,27 +4,18 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server"> <Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- ============================================================================================== --> <!-- ============================================================================================== -->
<!-- GCloud configuration from property file --> <!-- GCloud configuration. -->
<!-- Note: passwords stored in the property file can use jetty obfuscation (see --> <!-- Note: passwords can use jetty obfuscation. See -->
<!-- https://www.eclipse.org/jetty/documentation/current/configuring-security-secure-passwords.html --> <!-- https://www.eclipse.org/jetty/documentation/current/configuring-security-secure-passwords.html -->
<!-- ============================================================================================== --> <!-- ============================================================================================== -->
<Call id="gconf" class="org.eclipse.jetty.gcloud.session.GCloudConfiguration" name="fromFile">
<Arg><Property name="jetty.base" default="."/>/<Property name="jetty.gcloudSession.configFile" default="etc/gcloud.props"/></Arg>
</Call>
<!-- ============================================================================================== -->
<!-- Alternate GCloud configuration from properties -->
<!-- Note: passwords can use jetty obfuscation (see -->
<!-- https://www.eclipse.org/jetty/documentation/current/configuring-security-secure-passwords.html -->
<!-- ============================================================================================== -->
<!--
<New id="gconf" class="org.eclipse.jetty.gcloud.session.GCloudConfiguration"> <New id="gconf" class="org.eclipse.jetty.gcloud.session.GCloudConfiguration">
<!-- To contact remote gclouddatastore set the following properties in start.ini -->
<!-- Either set jetty.gcloudSession.projectId or use system property/env var DATASTORE_DATASET-->
<Set name="projectId"><Property name="jetty.gcloudSession.projectId"/></Set> <Set name="projectId"><Property name="jetty.gcloudSession.projectId"/></Set>
<Set name="p12File"><Property name="jetty.gcloudSession.p12File"/></Set> <Set name="p12File"><Property name="jetty.gcloudSession.p12File"/></Set>
<Set name="serviceAccount"><Property name="jetty.gcloudSession.serviceAccount"/></Set> <Set name="serviceAccount"><Property name="jetty.gcloudSession.serviceAccount"/></Set>
<Set name="password"><Property name="jetty.gcloudSession.password"/></Set> <Set name="password"><Property name="jetty.gcloudSession.password"/></Set>
</New> </New>
-->
<!-- ===================================================================== --> <!-- ===================================================================== -->

View File

@ -53,23 +53,39 @@ https://github.com/GoogleCloudPlatform/gcloud-java
http://www.apache.org/licenses/LICENSE-2.0.html http://www.apache.org/licenses/LICENSE-2.0.html
[ini-template] [ini-template]
## GCloudDatastore Session config
## Unique identifier for this node in the cluster ## Unique identifier for this node in the cluster
# jetty.gcloudSession.workerName=node1 # jetty.gcloudSession.workerName=node1
## Name of properties files containing gcloud config
#jetty.gcloudSession.configFilet=etc/gcloud.props
##Alternative to properties file, individual properties ## GCloudDatastore Session config
## the gcloud projectId ## If running inside Google cloud all configuration is provided by
## environment variables and you do not need to set anything in this file.
##
## If running externally to Google:
## To contact the remote gcloud datastore:
## 1. set the DATASTORE_DATASET System property/environment variable to the name of your project
## or alternatively set the jetty.gcloudSession.projectId property below.
## 2. set the jetty.gcloudSession.p12File, jetty.gcloudSession.serviceAccount and
## jetty.gcloudSession.password (supports obfuscation) below.
##
## To contact a local dev gcloud datastore server:
## 1. set the DATASTORE_DATASET System property/environment variable to the name of your project.
## 2. set the DATASTORE_HOST System property/environment variable to the url of the dev server
## as described at https://cloud.google.com/datastore/docs/tools/devserver#setting_environment_variables
## The gcloud projectId
## Set this property to connect to remote gcloud datastore.
## Or, set the DATASTORE_DATASET System property/env variable instead.
#jetty.gcloudSession.projectId= #jetty.gcloudSession.projectId=
## the p12 file associated with the project ## The p12 file associated with the project.
## Set this property to connect to remote gcloud datastore
#jetty.gcloudSession.p12File= #jetty.gcloudSession.p12File=
## the serviceAccount for the Datastore ## The serviceAccount for the Datastore.
## Set this property to connect to to remote gcloud datastore
#jetty.gcloudSession.serviceAccount= #jetty.gcloudSession.serviceAccount=
## the password (can be obfuscated) ## The password (can be obfuscated).
## Set this property to connect to remote gcloud datastore
#jetty.gcloudSession.password= #jetty.gcloudSession.password=

View File

@ -46,8 +46,10 @@ public class GCloudConfiguration
public static final String SERVICE_ACCOUNT = "serviceAccount"; public static final String SERVICE_ACCOUNT = "serviceAccount";
private String _projectId; private String _projectId;
private String _p12Filename;
private File _p12File; private File _p12File;
private String _serviceAccount; private String _serviceAccount;
private String _passwordSet;
private String _password; private String _password;
private AuthCredentials _authCredentials; private AuthCredentials _authCredentials;
private DatastoreOptions _options; private DatastoreOptions _options;
@ -109,7 +111,8 @@ public class GCloudConfiguration
public void setP12File (String file) public void setP12File (String file)
{ {
checkForModification(); checkForModification();
_p12File = new File(file); _p12Filename = file;
} }
@ -119,12 +122,12 @@ public class GCloudConfiguration
_serviceAccount = serviceAccount; _serviceAccount = serviceAccount;
} }
public void setPassword (String pwd) public void setPassword (String pwd)
{ {
checkForModification(); checkForModification();
Password p = new Password(pwd); _passwordSet = pwd;
_password = p.toString();
} }
@ -133,10 +136,29 @@ public class GCloudConfiguration
{ {
if (_options == null) if (_options == null)
{ {
_options = DatastoreOptions.builder() if (_passwordSet == null && _p12Filename == null && _serviceAccount == null)
.projectId(_projectId) {
.authCredentials(getAuthCredentials()) //When no values are explicitly presented for auth info, we are either running
.build(); //1. inside GCE environment, in which case all auth info is derived from the environment
//2. outside the GCE environment, but using a local gce dev server, in which case you
// need to set the following 2 environment/system properties
// DATASTORE_HOST: eg http://localhost:9999 - this is the host and port of a local development server
// DATASTORE_DATASET: eg myProj - this is the name of your project
_options = DatastoreOptions.defaultInstance();
}
else
{
//When running externally to GCE, you need to provide
//explicit auth info. You can either set the projectId explicitly, or you can set the
//DATASTORE_DATASET env/system property
_p12File = new File(_p12Filename);
Password p = new Password(_passwordSet);
_password = p.toString();
_options = DatastoreOptions.builder()
.projectId(_projectId)
.authCredentials(getAuthCredentials())
.build();
}
} }
return _options; return _options;
} }
@ -152,11 +174,6 @@ public class GCloudConfiguration
{ {
if (_password == null) if (_password == null)
throw new IllegalStateException("No password"); throw new IllegalStateException("No password");
if (_projectId == null)
throw new IllegalStateException("No project id");
if (_projectId == null)
throw new IllegalStateException("No project id");
if (_p12File == null || !_p12File.exists()) if (_p12File == null || !_p12File.exists())
throw new IllegalStateException("No p12 file: "+(_p12File==null?"null":_p12File.getAbsolutePath())); throw new IllegalStateException("No p12 file: "+(_p12File==null?"null":_p12File.getAbsolutePath()));

View File

@ -97,8 +97,8 @@
<configuration> <configuration>
<skipTests>false</skipTests> <skipTests>false</skipTests>
<systemPropertyVariables> <systemPropertyVariables>
<test.projectId>jetty9-work</test.projectId> <DATASTORE_DATASET>jetty9-work</DATASTORE_DATASET>
<test.port>8088</test.port> <DATASTORE_HOST>http://localhost:8088</DATASTORE_HOST>
</systemPropertyVariables> </systemPropertyVariables>
</configuration> </configuration>
</plugin> </plugin>

View File

@ -37,11 +37,7 @@ public class ClientCrossContextSessionTest extends AbstractClientCrossContextSes
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -36,11 +36,7 @@ public class ForwardedSessionTest extends AbstractForwardedSessionTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -38,18 +38,16 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.JarResource; import org.eclipse.jetty.util.resource.JarResource;
import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.Resource;
import com.google.api.client.util.Strings; import com.google.api.client.util.Strings;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.Datastore; import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreFactory; import com.google.gcloud.datastore.DatastoreFactory;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.Entity; import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.GqlQuery; import com.google.gcloud.datastore.GqlQuery;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.ProjectionEntity; import com.google.gcloud.datastore.ProjectionEntity;
import com.google.gcloud.datastore.Query; import com.google.gcloud.datastore.Query;
import com.google.gcloud.datastore.Query.ResultType; import com.google.gcloud.datastore.Query.ResultType;
@ -65,34 +63,6 @@ import com.google.gcloud.datastore.StructuredQuery.Projection;
public class GCloudSessionTestSupport public class GCloudSessionTestSupport
{ {
/**
* GCloudTestConfiguration
*
* Specialization of GCloudConfiguration for gcd test environment
*
*/
public class GCloudTestConfiguration extends GCloudConfiguration
{
int _port;
public GCloudTestConfiguration(String projectId, int port)
{
setProjectId(projectId);
_port = port;
}
@Override
public DatastoreOptions getDatastoreOptions() throws Exception
{
return DatastoreOptions.builder()
.projectId(_projectId)
.host("http://localhost:" + _port)
.build();
}
}
private static class ProcessOutputReader implements Runnable private static class ProcessOutputReader implements Runnable
{ {
private InputStream _is; private InputStream _is;
@ -138,40 +108,55 @@ public class GCloudSessionTestSupport
public static String DEFAULT_PROJECTID = "jetty9-work"; public static String DEFAULT_PROJECTID = "jetty9-work";
public static int DEFAULT_PORT = 8088; public static String DEFAULT_PORT = "8088";
public static String DEFAULT_HOST = "http://localhost:"+DEFAULT_PORT;
public static String DEFAULT_GCD_ZIP = "gcd-v1beta2-rev1-2.1.2b.zip"; public static String DEFAULT_GCD_ZIP = "gcd-v1beta2-rev1-2.1.2b.zip";
public static String DEFAULT_GCD_UNPACKED = "gcd-v1beta2-rev1-2.1.2b"; public static String DEFAULT_GCD_UNPACKED = "gcd-v1beta2-rev1-2.1.2b";
public static String DEFAULT_DOWNLOAD_URL = "http://storage.googleapis.com/gcd/tools/"; public static String DEFAULT_DOWNLOAD_URL = "http://storage.googleapis.com/gcd/tools/";
String _projectId; String _projectId;
int _port; String _testServerUrl;
String _testPort;
File _datastoreDir; File _datastoreDir;
File _gcdInstallDir; File _gcdInstallDir;
File _gcdUnpackedDir; File _gcdUnpackedDir;
Datastore _ds; Datastore _ds;
public GCloudSessionTestSupport (String projectId, int port, File gcdInstallDir) public GCloudSessionTestSupport (File gcdInstallDir)
{ {
_projectId = projectId;
if (_projectId == null)
_projectId = DEFAULT_PROJECTID;
_port = port;
if (_port <= 0)
_port = DEFAULT_PORT;
_gcdInstallDir = gcdInstallDir; _gcdInstallDir = gcdInstallDir;
if (_gcdInstallDir == null) if (_gcdInstallDir == null)
_gcdInstallDir = new File (System.getProperty("java.io.tmpdir")); _gcdInstallDir = new File (System.getProperty("java.io.tmpdir"));
_projectId = System.getProperty("DATASTORE_DATASET", System.getenv("DATASTORE_DATASET"));
if (_projectId == null)
{
_projectId = DEFAULT_PROJECTID;
System.setProperty("DATASTORE_DATASET", _projectId);
}
_testServerUrl = System.getProperty("DATASTORE_HOST", System.getenv("DATASTORE_HOST"));
if (_testServerUrl == null)
{
_testServerUrl = DEFAULT_HOST;
_testPort = DEFAULT_PORT;
System.setProperty("DATASTORE_HOST", _testServerUrl);
}
else
{
int i = _testServerUrl.lastIndexOf(':');
_testPort = _testServerUrl.substring(i+1);
}
} }
public GCloudSessionTestSupport () public GCloudSessionTestSupport ()
{ {
this(null,0, null); this(null);
} }
public GCloudConfiguration getConfiguration () public GCloudConfiguration getConfiguration ()
{ {
return new GCloudTestConfiguration(_projectId, _port); return new GCloudConfiguration();
} }
@ -251,11 +236,11 @@ public class GCloudSessionTestSupport
processBuilder.redirectErrorStream(true); processBuilder.redirectErrorStream(true);
if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")) if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows"))
{ {
processBuilder.command("cmd", "/C", new File(_gcdUnpackedDir, "gcd.cmd").getAbsolutePath(), "start", "--testing", "--allow_remote_shutdown","--port="+String.valueOf(_port), _projectId); processBuilder.command("cmd", "/C", new File(_gcdUnpackedDir, "gcd.cmd").getAbsolutePath(), "start", "--testing", "--allow_remote_shutdown","--port="+_testPort, _projectId);
} }
else else
{ {
processBuilder.command("bash", new File(_gcdUnpackedDir, "gcd.sh").getAbsolutePath(), "start", "--testing", "--allow_remote_shutdown", "--port="+String.valueOf(_port), _projectId); processBuilder.command("bash", new File(_gcdUnpackedDir, "gcd.sh").getAbsolutePath(), "start", "--testing", "--allow_remote_shutdown", "--port="+_testPort, _projectId);
} }
System.err.println("Starting datastore"); System.err.println("Starting datastore");
@ -270,7 +255,7 @@ public class GCloudSessionTestSupport
throws Exception throws Exception
{ {
//Send request to terminate test datastore //Send request to terminate test datastore
URL url = new URL("http", "localhost", _port, "/_ah/admin/quit"); URL url = new URL("http", "localhost", Integer.parseInt(_testPort.trim()), "/_ah/admin/quit");
HttpURLConnection con = (HttpURLConnection) url.openConnection(); HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST"); con.setRequestMethod("POST");
con.setDoOutput(true); con.setDoOutput(true);

View File

@ -34,14 +34,13 @@ public class ImmortalSessionTest extends AbstractImmortalSessionTest
{ {
static GCloudSessionTestSupport _testSupport; static GCloudSessionTestSupport _testSupport;
/**
* @throws Exception
*/
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -36,11 +36,7 @@ public class InvalidationSessionTest extends AbstractInvalidationSessionTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -37,11 +37,7 @@ public class LastAccessTimeTest extends AbstractLastAccessTimeTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -37,11 +37,7 @@ public class LocalSessionScavengingTest extends AbstractLocalSessionScavengingTe
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -42,11 +42,7 @@ public class NewSessionTest extends AbstractNewSessionTest
@Before @Before
public void setup () throws Exception public void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -37,11 +37,7 @@ public class OrphanedSessionTest extends AbstractOrphanedSessionTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -37,11 +37,7 @@ public class ReentrantRequestSessionTest extends AbstractReentrantRequestSession
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -39,11 +39,7 @@ public class RemoveSessionTest extends AbstractRemoveSessionTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -37,11 +37,7 @@ public class SameNodeLoadTest extends AbstractSameNodeLoadTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -38,11 +38,7 @@ public class ServerCrossContextSessionTest extends AbstractServerCrossContextSes
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -40,11 +40,7 @@ public class SessionExpiryTest extends AbstractSessionExpiryTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -38,11 +38,7 @@ public class SessionInvalidateAndCreateTest extends AbstractSessionInvalidateAnd
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -37,11 +37,7 @@ public class SessionMigrationTest extends AbstractSessionMigrationTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -37,11 +37,7 @@ public class SessionRenewTest extends AbstractSessionRenewTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -38,11 +38,7 @@ public class SessionValueSavingTest extends AbstractSessionValueSavingTest
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }

View File

@ -39,11 +39,7 @@ public class StopSessionManagerPreserveSessionTest extends AbstractStopSessionMa
@BeforeClass @BeforeClass
public static void setup () throws Exception public static void setup () throws Exception
{ {
String projectId = System.getProperty("test.projectId", null); _testSupport = new GCloudSessionTestSupport();
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport.setUp(); _testSupport.setUp();
} }