View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  
20  package org.eclipse.jetty.gcloud.session;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.security.KeyStore;
26  import java.security.PrivateKey;
27  import java.util.Properties;
28  
29  import org.eclipse.jetty.util.security.Password;
30  
31  import com.google.gcloud.AuthCredentials;
32  import com.google.gcloud.datastore.DatastoreOptions;
33  
34  
35  
36  /**
37   * GCloudConfiguration
38   *
39   *
40   */
41  public class GCloudConfiguration
42  {
43      public static final String PROJECT_ID = "projectId";
44      public static final String P12 = "p12";
45      public static final String PASSWORD = "password";
46      public static final String SERVICE_ACCOUNT = "serviceAccount";
47      
48      private String _projectId;
49      private String _p12Filename;
50      private File _p12File;
51      private String _serviceAccount;
52      private String _passwordSet;
53      private String _password;
54      private AuthCredentials _authCredentials;
55      private DatastoreOptions _options;
56      
57      /**
58       * Generate a configuration from a properties file
59       * 
60       * @param propsFile
61       * @return
62       * @throws IOException
63       */
64      public static GCloudConfiguration fromFile(String propsFile)
65      throws IOException
66      {
67          if (propsFile == null)
68              throw new IllegalArgumentException ("Null properties file");
69          
70          File f = new File(propsFile);
71          if (!f.exists())
72              throw new IllegalArgumentException("No such file "+f.getAbsolutePath());
73          Properties props = new Properties();
74          try (FileInputStream is=new FileInputStream(f))
75          {
76              props.load(is);
77          }
78          
79          GCloudConfiguration config = new GCloudConfiguration();
80          config.setProjectId(props.getProperty(PROJECT_ID));
81          config.setP12File(props.getProperty(P12));
82          config.setPassword(props.getProperty(PASSWORD));
83          config.setServiceAccount(props.getProperty(SERVICE_ACCOUNT));
84          return config;
85      }
86      
87      
88      
89      public String getProjectId()
90      {
91          return _projectId;
92      }
93  
94      public File getP12File()
95      {
96          return _p12File;
97      }
98  
99      public String getServiceAccount()
100     {
101         return _serviceAccount;
102     }
103 
104 
105     public void setProjectId(String projectId)
106     {
107         checkForModification();
108         _projectId = projectId;
109     }
110 
111     public void setP12File (String file)
112     {
113         checkForModification();
114         _p12Filename = file;
115 
116     }
117     
118     
119     public void setServiceAccount (String serviceAccount)
120     {
121         checkForModification();
122         _serviceAccount = serviceAccount;
123     }
124     
125 
126     public void setPassword (String pwd)
127     {
128         checkForModification();
129         _passwordSet = pwd;
130 
131     }
132 
133 
134     public DatastoreOptions getDatastoreOptions ()
135             throws Exception
136     {
137         if (_options == null)
138         {
139             if (_passwordSet == null && _p12Filename == null && _serviceAccount == null)
140             {
141                 //When no values are explicitly presented for auth info, we are either running
142                 //1. inside GCE environment, in which case all auth info is derived from the environment
143                 //2. outside the GCE environment, but using a local gce dev server, in which case you
144                 //   need to set the following 2 environment/system properties
145                 //          DATASTORE_HOST: eg http://localhost:9999 - this is the host and port of a local development server
146                 //          DATASTORE_DATASET: eg myProj - this is the name of your project          
147                 _options = DatastoreOptions.defaultInstance();
148             }
149             else
150             {
151                 //When running externally to GCE, you need to provide
152                 //explicit auth info. You can either set the projectId explicitly, or you can set the
153                 //DATASTORE_DATASET env/system property
154                 _p12File = new File(_p12Filename);
155                 Password p = new Password(_passwordSet);
156                 _password = p.toString();
157                 _options = DatastoreOptions.builder()
158                         .projectId(_projectId)
159                         .authCredentials(getAuthCredentials())
160                         .build();
161             }
162         }
163         return _options;
164     }
165 
166     /**
167      * @return
168      * @throws Exception
169      */
170     public AuthCredentials getAuthCredentials()
171     throws Exception
172     {
173         if (_authCredentials == null)
174         {
175             if (_password == null)
176                 throw new IllegalStateException("No password");
177 
178             if (_p12File == null || !_p12File.exists())
179                 throw new IllegalStateException("No p12 file: "+(_p12File==null?"null":_p12File.getAbsolutePath()));
180 
181             if (_serviceAccount == null)
182                 throw new IllegalStateException("No service account");
183 
184             char[] pwdChars = _password.toCharArray();
185             KeyStore keystore = KeyStore.getInstance("PKCS12");
186             keystore.load(new FileInputStream(getP12File()), pwdChars);
187             PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", pwdChars);
188             _authCredentials = AuthCredentials.createFor(getServiceAccount(), privateKey);
189         }
190         return _authCredentials;
191     }
192     
193     /**
194      * @throws IllegalStateException
195      */
196     protected void checkForModification () throws IllegalStateException
197     {
198         if (_authCredentials != null || _options != null)
199             throw new IllegalStateException("Cannot modify auth configuration after datastore initialized");     
200     }
201 }