View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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 File _p12File;
50      private String _serviceAccount;
51      private String _password;
52      private AuthCredentials _authCredentials;
53      private DatastoreOptions _options;
54      
55      /**
56       * Generate a configuration from a properties file
57       * 
58       * @param propsFile
59       * @return
60       * @throws IOException
61       */
62      public static GCloudConfiguration fromFile(String propsFile)
63      throws IOException
64      {
65          if (propsFile == null)
66              throw new IllegalArgumentException ("Null properties file");
67          
68          File f = new File(propsFile);
69          if (!f.exists())
70              throw new IllegalArgumentException("No such file "+f.getAbsolutePath());
71          Properties props = new Properties();
72          try (FileInputStream is=new FileInputStream(f))
73          {
74              props.load(is);
75          }
76          
77          GCloudConfiguration config = new GCloudConfiguration();
78          config.setProjectId(props.getProperty(PROJECT_ID));
79          config.setP12File(props.getProperty(P12));
80          config.setPassword(props.getProperty(PASSWORD));
81          config.setServiceAccount(props.getProperty(SERVICE_ACCOUNT));
82          return config;
83      }
84      
85      
86      
87      public String getProjectId()
88      {
89          return _projectId;
90      }
91  
92      public File getP12File()
93      {
94          return _p12File;
95      }
96  
97      public String getServiceAccount()
98      {
99          return _serviceAccount;
100     }
101 
102 
103     public void setProjectId(String projectId)
104     {
105         checkForModification();
106         _projectId = projectId;
107     }
108 
109     public void setP12File (String file)
110     {
111         checkForModification();
112         _p12File = new File(file);
113     }
114     
115     
116     public void setServiceAccount (String serviceAccount)
117     {
118         checkForModification();
119         _serviceAccount = serviceAccount;
120     }
121     
122     
123     public void setPassword (String pwd)
124     {
125         checkForModification();
126         Password p = new Password(pwd);
127         _password = p.toString();
128     }
129 
130 
131     public DatastoreOptions getDatastoreOptions ()
132             throws Exception
133     {
134         if (_options == null)
135         {
136             _options = DatastoreOptions.builder()
137                     .projectId(_projectId)
138                     .authCredentials(getAuthCredentials())
139                     .build();
140         }
141         return _options;
142     }
143 
144     /**
145      * @return
146      * @throws Exception
147      */
148     public AuthCredentials getAuthCredentials()
149     throws Exception
150     {
151         if (_authCredentials == null)
152         {
153             if (_password == null)
154                 throw new IllegalStateException("No password");
155             if (_projectId == null)
156                 throw new IllegalStateException("No project id");
157 
158             if (_projectId == null)
159                 throw new IllegalStateException("No project id");
160 
161             if (_p12File == null || !_p12File.exists())
162                 throw new IllegalStateException("No p12 file: "+(_p12File==null?"null":_p12File.getAbsolutePath()));
163 
164             if (_serviceAccount == null)
165                 throw new IllegalStateException("No service account");
166 
167             char[] pwdChars = _password.toCharArray();
168             KeyStore keystore = KeyStore.getInstance("PKCS12");
169             keystore.load(new FileInputStream(getP12File()), pwdChars);
170             PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", pwdChars);
171             _authCredentials = AuthCredentials.createFor(getServiceAccount(), privateKey);
172         }
173         return _authCredentials;
174     }
175     
176     /**
177      * @throws IllegalStateException
178      */
179     protected void checkForModification () throws IllegalStateException
180     {
181         if (_authCredentials != null || _options != null)
182             throw new IllegalStateException("Cannot modify auth configuration after datastore initialized");     
183     }
184 }