Skip to content

Commit

Permalink
479832 Use system properties for gcloud config for GCloudDatastore se…
Browse files Browse the repository at this point in the history
…ssion manager
  • Loading branch information
janbartel committed Oct 15, 2015
1 parent 71bd387 commit e00b569
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 173 deletions.
Expand Up @@ -4,27 +4,18 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server">

<!-- ============================================================================================== -->
<!-- GCloud configuration from property file -->
<!-- Note: passwords stored in the property file can use jetty obfuscation (see -->
<!-- GCloud configuration. -->
<!-- Note: passwords can use jetty obfuscation. See -->
<!-- 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">
<!-- 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="p12File"><Property name="jetty.gcloudSession.p12File"/></Set>
<Set name="serviceAccount"><Property name="jetty.gcloudSession.serviceAccount"/></Set>
<Set name="password"><Property name="jetty.gcloudSession.password"/></Set>
</New>
-->


<!-- ===================================================================== -->
Expand Down
Expand Up @@ -53,23 +53,39 @@ https://github.com/GoogleCloudPlatform/gcloud-java
http://www.apache.org/licenses/LICENSE-2.0.html

[ini-template]
## GCloudDatastore Session config

## Unique identifier for this node in the cluster
# jetty.gcloudSession.workerName=node1

## Name of properties files containing gcloud config
#jetty.gcloudSession.configFilet=etc/gcloud.props

##Alternative to properties file, individual properties
## the gcloud projectId
## GCloudDatastore Session config
## 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=

## 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=

## the serviceAccount for the Datastore
## The serviceAccount for the Datastore.
## Set this property to connect to to remote gcloud datastore
#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=
Expand Up @@ -46,8 +46,10 @@ public class GCloudConfiguration
public static final String SERVICE_ACCOUNT = "serviceAccount";

private String _projectId;
private String _p12Filename;
private File _p12File;
private String _serviceAccount;
private String _passwordSet;
private String _password;
private AuthCredentials _authCredentials;
private DatastoreOptions _options;
Expand Down Expand Up @@ -109,7 +111,8 @@ public void setProjectId(String projectId)
public void setP12File (String file)
{
checkForModification();
_p12File = new File(file);
_p12Filename = file;

}


Expand All @@ -119,12 +122,12 @@ public void setServiceAccount (String serviceAccount)
_serviceAccount = serviceAccount;
}


public void setPassword (String pwd)
{
checkForModification();
Password p = new Password(pwd);
_password = p.toString();
_passwordSet = pwd;

}


Expand All @@ -133,10 +136,29 @@ public DatastoreOptions getDatastoreOptions ()
{
if (_options == null)
{
_options = DatastoreOptions.builder()
.projectId(_projectId)
.authCredentials(getAuthCredentials())
.build();
if (_passwordSet == null && _p12Filename == null && _serviceAccount == null)
{
//When no values are explicitly presented for auth info, we are either running
//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;
}
Expand All @@ -152,11 +174,6 @@ public AuthCredentials getAuthCredentials()
{
if (_password == null)
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())
throw new IllegalStateException("No p12 file: "+(_p12File==null?"null":_p12File.getAbsolutePath()));
Expand Down
4 changes: 2 additions & 2 deletions tests/test-sessions/test-gcloud-sessions/pom.xml
Expand Up @@ -97,8 +97,8 @@
<configuration>
<skipTests>false</skipTests>
<systemPropertyVariables>
<test.projectId>jetty9-work</test.projectId>
<test.port>8088</test.port>
<DATASTORE_DATASET>jetty9-work</DATASTORE_DATASET>
<DATASTORE_HOST>http://localhost:8088</DATASTORE_HOST>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
Expand Up @@ -37,11 +37,7 @@ public class ClientCrossContextSessionTest extends AbstractClientCrossContextSes
@BeforeClass
public static void setup () throws Exception
{
String projectId = System.getProperty("test.projectId", null);
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport = new GCloudSessionTestSupport();
_testSupport.setUp();
}

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

Expand Down
Expand Up @@ -38,18 +38,16 @@
import java.util.List;
import java.util.Locale;


import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.JarResource;
import org.eclipse.jetty.util.resource.Resource;

import com.google.api.client.util.Strings;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreFactory;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.GqlQuery;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.ProjectionEntity;
import com.google.gcloud.datastore.Query;
import com.google.gcloud.datastore.Query.ResultType;
Expand All @@ -65,34 +63,6 @@
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 InputStream _is;
Expand Down Expand Up @@ -138,40 +108,55 @@ public void run()


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_UNPACKED = "gcd-v1beta2-rev1-2.1.2b";
public static String DEFAULT_DOWNLOAD_URL = "http://storage.googleapis.com/gcd/tools/";


String _projectId;
int _port;
String _testServerUrl;
String _testPort;
File _datastoreDir;
File _gcdInstallDir;
File _gcdUnpackedDir;
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;
if (_gcdInstallDir == null)
_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 ()
{
this(null,0, null);
this(null);
}

public GCloudConfiguration getConfiguration ()
{
return new GCloudTestConfiguration(_projectId, _port);
return new GCloudConfiguration();
}


Expand Down Expand Up @@ -251,11 +236,11 @@ public void startDatastore()
processBuilder.redirectErrorStream(true);
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
{
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");
Expand All @@ -270,7 +255,7 @@ public void stopDatastore()
throws Exception
{
//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();
con.setRequestMethod("POST");
con.setDoOutput(true);
Expand Down
Expand Up @@ -34,14 +34,13 @@ public class ImmortalSessionTest extends AbstractImmortalSessionTest
{
static GCloudSessionTestSupport _testSupport;

/**
* @throws Exception
*/
@BeforeClass
public static void setup () throws Exception
{
String projectId = System.getProperty("test.projectId", null);
String port = System.getProperty("test.port","0");
_testSupport = new GCloudSessionTestSupport(projectId,
Integer.parseInt(port),
null);
_testSupport = new GCloudSessionTestSupport();
_testSupport.setUp();
}

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

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

Expand Down

0 comments on commit e00b569

Please sign in to comment.