Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9dc103581b9ffaf54e3a72af93f65c1115743a23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//


package org.eclipse.jetty.gcloud.session;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.util.Properties;

import org.eclipse.jetty.util.security.Password;

import com.google.gcloud.AuthCredentials;
import com.google.gcloud.datastore.DatastoreOptions;



/**
 * GCloudConfiguration
 *
 *
 */
public class GCloudConfiguration
{
    public static final String PROJECT_ID = "projectId";
    public static final String P12 = "p12";
    public static final String PASSWORD = "password";
    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;
    
    /**
     * Generate a configuration from a properties file
     * 
     * @param propsFile
     * @return
     * @throws IOException
     */
    public static GCloudConfiguration fromFile(String propsFile)
    throws IOException
    {
        if (propsFile == null)
            throw new IllegalArgumentException ("Null properties file");
        
        File f = new File(propsFile);
        if (!f.exists())
            throw new IllegalArgumentException("No such file "+f.getAbsolutePath());
        Properties props = new Properties();
        try (FileInputStream is=new FileInputStream(f))
        {
            props.load(is);
        }
        
        GCloudConfiguration config = new GCloudConfiguration();
        config.setProjectId(props.getProperty(PROJECT_ID));
        config.setP12File(props.getProperty(P12));
        config.setPassword(props.getProperty(PASSWORD));
        config.setServiceAccount(props.getProperty(SERVICE_ACCOUNT));
        return config;
    }
    
    
    
    public String getProjectId()
    {
        return _projectId;
    }

    public File getP12File()
    {
        return _p12File;
    }

    public String getServiceAccount()
    {
        return _serviceAccount;
    }


    public void setProjectId(String projectId)
    {
        checkForModification();
        _projectId = projectId;
    }

    public void setP12File (String file)
    {
        checkForModification();
        _p12Filename = file;

    }
    
    
    public void setServiceAccount (String serviceAccount)
    {
        checkForModification();
        _serviceAccount = serviceAccount;
    }
    

    public void setPassword (String pwd)
    {
        checkForModification();
        _passwordSet = pwd;

    }


    public DatastoreOptions getDatastoreOptions ()
            throws Exception
    {
        if (_options == null)
        {
            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;
    }

    /**
     * @return
     * @throws Exception
     */
    public AuthCredentials getAuthCredentials()
    throws Exception
    {
        if (_authCredentials == null)
        {
            if (_password == null)
                throw new IllegalStateException("No password");

            if (_p12File == null || !_p12File.exists())
                throw new IllegalStateException("No p12 file: "+(_p12File==null?"null":_p12File.getAbsolutePath()));

            if (_serviceAccount == null)
                throw new IllegalStateException("No service account");

            char[] pwdChars = _password.toCharArray();
            KeyStore keystore = KeyStore.getInstance("PKCS12");
            keystore.load(new FileInputStream(getP12File()), pwdChars);
            PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", pwdChars);
            _authCredentials = AuthCredentials.createFor(getServiceAccount(), privateKey);
        }
        return _authCredentials;
    }
    
    /**
     * @throws IllegalStateException
     */
    protected void checkForModification () throws IllegalStateException
    {
        if (_authCredentials != null || _options != null)
            throw new IllegalStateException("Cannot modify auth configuration after datastore initialized");     
    }
}

Back to the top