1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
59
60
61
62
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
142
143
144
145
146
147 _options = DatastoreOptions.defaultInstance();
148 }
149 else
150 {
151
152
153
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
168
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
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 }