~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/jgdi/src/com/sun/grid/jgdi/management/SSLHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2006 by Sun Microsystems, Inc
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
package com.sun.grid.jgdi.management;
 
33
 
 
34
import javax.net.ssl.KeyManager;
 
35
 
 
36
 
 
37
import com.sun.grid.security.login.GECATrustManager;
 
38
import com.sun.grid.security.login.GECAKeyManager;
 
39
import java.io.File;
 
40
import java.security.KeyStore;
 
41
import java.util.HashMap;
 
42
import java.util.Map;
 
43
import java.util.concurrent.locks.Lock;
 
44
import java.util.concurrent.locks.ReentrantLock;
 
45
import javax.net.ssl.SSLContext;
 
46
import javax.net.ssl.SSLSocketFactory;
 
47
import javax.net.ssl.TrustManager;
 
48
 
 
49
/**
 
50
 * Helper class for SSL setup.
 
51
 */
 
52
public final class SSLHelper {
 
53
 
 
54
    private final static String SSL_PROTOCOL = "SSL";
 
55
    private static SSLContext ctx;
 
56
    private static final GECAKeyManager keyManager = new GECAKeyManager();
 
57
    private static final GECATrustManager trustManager = new GECATrustManager();
 
58
    private static final Lock lock = new ReentrantLock();
 
59
    
 
60
    private SSLHelper(File caTop) {
 
61
        trustManager.setCaTop(caTop);
 
62
    }
 
63
    
 
64
    private static final Lock instanceLock = new ReentrantLock();
 
65
    private static final Map<File,SSLHelper> instanceMap = new HashMap<File,SSLHelper>();
 
66
    
 
67
    /**
 
68
     * Get the instance of the SSLHelper by the caTop directory
 
69
     * @param caTop the caTop directory
 
70
     * @return the SSLHelper
 
71
     */
 
72
    public static SSLHelper getInstanceByCaTop(File caTop) {
 
73
        SSLHelper ret = null;
 
74
        instanceLock.lock();
 
75
        try {
 
76
            ret = instanceMap.get(caTop);
 
77
            
 
78
            if(ret == null) {
 
79
                ret = new SSLHelper(caTop);
 
80
                instanceMap.put(caTop, ret);
 
81
            }
 
82
        } finally {
 
83
            instanceLock.unlock();
 
84
        }
 
85
        return ret;
 
86
    }
 
87
 
 
88
    /**
 
89
     * Get the instance of the SSLHelper
 
90
     * @param sgeRoot  the sge root directory of the addressed cluster
 
91
     * @param cell     the cell name of of the addressed cluster
 
92
     * @return the SSLHelper
 
93
     */
 
94
    public static SSLHelper getInstance(File sgeRoot, String cell) {
 
95
        File caTop = new File(sgeRoot, cell + File.separator + "common" + File.separator + "sgeCA");
 
96
        return getInstanceByCaTop(caTop);
 
97
    }
 
98
            
 
99
            
 
100
    private void initSSLContext() {
 
101
        lock.lock();
 
102
        try {
 
103
            if(ctx == null) {
 
104
                try {
 
105
                    ctx = SSLContext.getInstance(SSL_PROTOCOL);
 
106
                    ctx.init(new KeyManager[]{keyManager},
 
107
                            new TrustManager[]{trustManager},
 
108
                            null);
 
109
                } catch (Exception ex) {
 
110
                    throw new SecurityException("Cannot create SSLContext", ex);
 
111
                }
 
112
            }
 
113
        } finally {
 
114
            lock.unlock();
 
115
        }
 
116
    }
 
117
 
 
118
    /**
 
119
     * Set the keystore for the JGDI ssl context
 
120
     * @param ks   the keystore
 
121
     * @param pw   the password for the keystore
 
122
     */
 
123
    void setKeystore(KeyStore ks, char [] pw) {
 
124
        lock.lock();
 
125
        try {
 
126
            initSSLContext();
 
127
            keyManager.setKeystore(ks, pw);
 
128
        } finally {
 
129
            lock.unlock();
 
130
        }
 
131
    }
 
132
 
 
133
    /**
 
134
     * Set the keystore for the JGDI ssl context
 
135
     * @param keystore   the keystore file
 
136
     * @param pw   the password for the keystore
 
137
     */
 
138
    void setKeystore(File keystore, char [] pw) {
 
139
        lock.lock();
 
140
        try {
 
141
            initSSLContext();
 
142
            keyManager.setKeystore(keystore, pw);
 
143
        } finally {
 
144
            lock.unlock();
 
145
        }
 
146
    }
 
147
 
 
148
    /**
 
149
     * Reset the JGDI ssl context
 
150
     */
 
151
    void reset() {
 
152
        lock.lock();
 
153
        try {
 
154
            ctx = null;
 
155
            keyManager.reset();
 
156
        } finally {
 
157
            lock.unlock();
 
158
        }
 
159
    }
 
160
 
 
161
    /**
 
162
     *  Get the ssl socket factory for the application
 
163
     *  @return the socket factor for the application
 
164
     */
 
165
    SSLSocketFactory getSocketFactory() {
 
166
        lock.lock();
 
167
        try {
 
168
            if (ctx == null) {
 
169
                return (SSLSocketFactory) SSLSocketFactory.getDefault();
 
170
            } else {
 
171
                return ctx.getSocketFactory();
 
172
            }
 
173
        } finally {
 
174
            lock.unlock();
 
175
        }
 
176
    }
 
177
}