Skip to content

Commit 211998c

Browse files
committed
[#2089] ssl cert tests and documentation
1 parent 2a251a8 commit 211998c

File tree

8 files changed

+221
-36
lines changed

8 files changed

+221
-36
lines changed

documentation/manual/production.textile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ bc. # X509 certificates
189189
certificate.key.file=conf/host.key
190190
certificate.file=conf/host.cert
191191
# In case your key file is password protected
192-
certificate.password=secret
192+
# certificate.key.file=conf/host.pass.key
193+
# certificate.password=secret
193194
trustmanager.algorithm=JKS
194195

195196
If you are using keystore:
@@ -202,8 +203,13 @@ Note that the values above are the default values.
202203

203204
You can generate self-signed certificates using *openssl*:
204205

205-
bc. openssl genrsa 1024 > host.key
206-
openssl req -new -x509 -nodes -sha1 -days 365 -key host.key > host.cert
206+
bc. openssl genrsa -des3 -passout pass:secret -out host.pass.key 2048
207+
openssl rsa -passin pass:secret -in host.pass.key -out host.key
208+
openssl req -new -key host.key -out host.csr -subj '/C=GB/ST=Test State or Province/L=Test Locality/O=Organization Name/OU=Organizational Unit Name/CN=Common Name/emailAddress=test@email.address'
209+
openssl x509 -req -days 3650 -in host.csr -signkey host.key -out host.cert
210+
211+
note. the first command creates a password-protected-key ('host.pass.key').
212+
the second command converts/writes the same key ('host.key') without password protection.
207213

208214
If you are using the Java keystore mechanism, then the following properties can be configured in your @application.conf@ file:
209215

framework/dependencies.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ require: &allDependencies
4242
- oauth.signpost -> signpost-core 1.2.1.2
4343
- org.apache.geronimo.specs -> geronimo-servlet_2.5_spec 1.2
4444
- org.apache.ivy -> ivy 2.4.0
45-
- org.bouncycastle -> bcprov-jdk15 1.46
45+
- org.bouncycastle -> bcprov-jdk15on 157
46+
- org.bouncycastle -> bcpkix-jdk15on 157
4647
- org.codehaus.groovy -> groovy-all 2.4.11
4748
- org.eclipse.jdt.core 3.12.3
4849
- org.hibernate -> hibernate-core 5.2.10.patched
758 KB
Binary file not shown.
-1.73 MB
Binary file not shown.
3.59 MB
Binary file not shown.

framework/src/play/server/ssl/SslHttpServerContextFactory.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package play.server.ssl;
22

3+
import org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory;
34
import org.bouncycastle.jce.provider.BouncyCastleProvider;
4-
import org.bouncycastle.openssl.PEMReader;
5-
import org.bouncycastle.openssl.PasswordFinder;
5+
import org.bouncycastle.openssl.PEMDecryptorProvider;
6+
import org.bouncycastle.openssl.PEMEncryptedKeyPair;
7+
import org.bouncycastle.openssl.PEMKeyPair;
8+
import org.bouncycastle.openssl.PEMParser;
9+
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
10+
import org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder;
611
import play.Logger;
712
import play.Play;
813

914
import javax.net.ssl.*;
15+
import java.io.File;
1016
import java.io.FileInputStream;
1117
import java.io.FileReader;
1218
import java.net.Socket;
1319
import java.security.*;
1420
import java.security.cert.X509Certificate;
15-
import java.util.ArrayList;
16-
import java.util.List;
21+
import java.util.Collection;
1722
import java.util.Properties;
1823

1924
public class SslHttpServerContextFactory {
@@ -84,18 +89,22 @@ public PEMKeyManager() {
8489
final Properties p = Play.configuration;
8590
String keyFile = p.getProperty("certificate.key.file", "conf/host.key");
8691

87-
try (PEMReader keyReader = new PEMReader(new FileReader(Play.getFile(keyFile)), new PEMPasswordFinder())) {
88-
key = ((KeyPair) keyReader.readObject()).getPrivate();
89-
90-
try (PEMReader reader = new PEMReader(new FileReader(Play.getFile(p.getProperty("certificate.file", "conf/host.cert"))))) {
91-
X509Certificate cert;
92-
List<X509Certificate> chainVector = new ArrayList<>();
93-
94-
while ((cert = (X509Certificate) reader.readObject()) != null) {
95-
chainVector.add(cert);
96-
}
97-
chain = chainVector.toArray(new X509Certificate[1]);
92+
try (PEMParser keyReader = new PEMParser(new FileReader(Play.getFile(keyFile)))) {
93+
final Object object = keyReader.readObject();
94+
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
95+
final KeyPair keyPair;
96+
if (object instanceof PEMEncryptedKeyPair) {
97+
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder()
98+
.build(Play.configuration.getProperty("certificate.password", "secret").toCharArray());
99+
keyPair = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv));
100+
} else {
101+
keyPair = converter.getKeyPair((PEMKeyPair) object);
98102
}
103+
key = keyPair.getPrivate();
104+
105+
final File hostCertFile = Play.getFile(p.getProperty("certificate.file", "conf/host.cert"));
106+
final Collection collection = new CertificateFactory().engineGenerateCertificates(new FileInputStream(hostCertFile));
107+
chain = (X509Certificate[]) collection.toArray(new X509Certificate[collection.size()]);
99108
} catch (Exception e) {
100109
Logger.error(e, "Failed to initialize PEMKeyManager from file %s", keyFile);
101110
}
@@ -136,12 +145,4 @@ public PrivateKey getPrivateKey(String s) {
136145
return key;
137146
}
138147
}
139-
140-
private static class PEMPasswordFinder implements PasswordFinder {
141-
@Override
142-
public char[] getPassword() {
143-
return Play.configuration.getProperty("certificate.password", "secret").toCharArray();
144-
}
145-
}
146-
147148
}

resources/application-skel/conf/application.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ mail.smtp=mock
231231
# For SSL, use the play.ssl.netty.pipeline property
232232
# play.ssl.netty.pipeline = play.server.FlashPolicyHandler,org.jboss.netty.handler.codec.http.HttpRequestDecoder,play.server.StreamChunkAggregator,org.jboss.netty.handler.codec.http.HttpResponseEncoder,org.jboss.netty.handler.codec.http.HttpContentCompressor,org.jboss.netty.handler.stream.ChunkedWriteHandler,play.server.ssl.SslPlayHandler
233233

234+
# # X509 certificates
235+
# # the following values are default values
236+
# certificate.key.file=conf/host.key
237+
# # certificate.password used only if certificate.key.file is password protected
238+
# certificate.password=secret
239+
# certificate.file=conf/host.cert
240+
# trustmanager.algorithm=JKS
241+
# keystore.algorithm=JKS
242+
# keystore.password=secret
243+
# keystore.file=conf/certificate.jks
234244

235245
# Open file from errors pages
236246
# ~~~~~

samples-and-tests/i-am-a-developer/tests.py

Lines changed: 176 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,179 @@
11
#!/usr/bin/python
22

3-
import unittest
43
import os
54
import shutil
6-
import sys
5+
import ssl
76
import subprocess
8-
import re
7+
import sys
8+
import threading
99
import time
10+
import unittest
1011
import urllib2
12+
1113
import mechanize
12-
import threading
14+
1315

1416
# --- TESTS
1517

1618
class IamADeveloper(unittest.TestCase):
17-
19+
20+
def testSSLConfig(self):
21+
22+
# Testing ssl config
23+
step('Hello, I am testing SSL config')
24+
25+
self.working_directory = bootstrapWorkingDirectory('i-am-testing-ssl-config-here')
26+
27+
# play new job-app
28+
step('Create a new project')
29+
30+
self.play = callPlay(self, ['new', '%s/sslconfigapp' % self.working_directory, '--name=SSLCONFIGAPP'])
31+
self.assert_(waitFor(self.play, 'The new application will be created'))
32+
self.assert_(waitFor(self.play, 'OK, the application is created'))
33+
self.assert_(waitFor(self.play, 'Have fun!'))
34+
35+
self.play.wait()
36+
37+
app = '%s/sslconfigapp' % self.working_directory
38+
39+
step('Add config and files')
40+
insert(app, "app/controllers/Application.java", 13, ' Logger.info("I am ssl secured!");')
41+
42+
edit(app, "conf/application.conf", 32, 'http.port=-1')
43+
edit(app, "conf/application.conf", 33, 'https.port=9000')
44+
edit(app, "conf/application.conf", 232,
45+
'play.ssl.netty.pipeline = play.server.FlashPolicyHandler,org.jboss.netty.handler.codec.http.HttpRequestDecoder,play.server.StreamChunkAggregator,org.jboss.netty.handler.codec.http.HttpResponseEncoder,org.jboss.netty.handler.codec.http.HttpContentCompressor,org.jboss.netty.handler.stream.ChunkedWriteHandler,play.server.ssl.SslPlayHandler')
46+
create(app, 'conf/host.key')
47+
insert(app, "conf/host.key", 1, '-----BEGIN RSA PRIVATE KEY-----')
48+
insert(app, "conf/host.key", 2, 'MIIEpQIBAAKCAQEAoOx9pCR7rZ50S9FotKVD2+aC36Hj4TkXZTZwEnh/fWyuiH2O')
49+
insert(app, "conf/host.key", 3, 'Paj/dTw60Jvll4jshlnRHfJ6yfc/o7YlDUanLrQJm7I3/t3YNgqYg3WXeUTl+GrN')
50+
insert(app, "conf/host.key", 4, 'Hn/3QgFGYqKobu8kfrwP4IapQRqlq4ZSdlR/bWpxnYSCZoXeeoimoSUcLlqD5dw7')
51+
insert(app, "conf/host.key", 5, '7v2BlG2gqL5+lr5Fx4mDC12vczoUMRg88+VuA1ezU4cuXDe2MbpJMd7rqGN0xK4b')
52+
insert(app, "conf/host.key", 6, 'CwkFtSJqBM1TH/Czr1S52hKrDTTys9PVw+eZSKO7BCk+PDq5jjx337XOWiO0kSHf')
53+
insert(app, "conf/host.key", 7, 'V64x68xTojfzTzF304byr2Ytq6DjNbpZKwdYBwIDAQABAoIBAQCc6z7w6mp3uIWq')
54+
insert(app, "conf/host.key", 8, '0P6K+ISdT7/aliCCJIu9tEHAoSOgiHQAwH4NflfsV9j6RqqxA2Gw+LBDxYkanDDA')
55+
insert(app, "conf/host.key", 9, 'UQL8WSL5FbIw0q5rpqQIvnhN6ELWi+q8PFjcHuhawqeB0x7vXd52fqf0xxsQUw2t')
56+
insert(app, "conf/host.key", 10, 'noOWw3qmlR9I/Eez9WImlk314RwDzc/bUsfBQhMKbNVHxstR8Q9YQQMp+xb9dqbL')
57+
insert(app, "conf/host.key", 11, '3lfz3O70Q/Xc/JxXIOkqcfyoIT9CvpJf2MT1tkd1xolAV+4UJQwKQURlMKqcp7Yi')
58+
insert(app, "conf/host.key", 12, 'NIxqv27ZGuhdzPCSFy3zcCIYMxXVvU+oSncGMlBpyf8ONDH2wZ7/nOtaz4Kf9tNZ')
59+
insert(app, "conf/host.key", 13, 'ZcqtXd1RAoGBAM7DFMBd78hkJhLztXO5PqB3O87f438aDlQfIGDzi9/KD+Jy1TRz')
60+
insert(app, "conf/host.key", 14, 'tJMLjmhPIOuy477k6+P3MmF3KeIjFzZg2Je56++rdpdX+E09Ts4s1gZkUAAfEyeI')
61+
insert(app, "conf/host.key", 15, 'QJ53lrXJu0ShmXODSyEc+rtaUgsM0geL7EtacmrUQQI9yKbrUHmHw0glAoGBAMc+')
62+
insert(app, "conf/host.key", 16, '9D13ne8bFLQ7upY6GuidgvG+ilAKaJ1YWNWjolTIV86RCEYNmgqxF0BzGT2Db55L')
63+
insert(app, "conf/host.key", 17, 'Myt5epDOKJr0RRi7ddidUJFOAOfm/yciPbr+D34LCnj6rkdauAhYsjfjuWDNLHyf')
64+
insert(app, "conf/host.key", 18, 'hjpBvvtMfqWE79vfIwVCKOy9xUVjqfZY2KDBu4G7AoGBAMSmjooXzgOOHRhRavdR')
65+
insert(app, "conf/host.key", 19, '7Nq6DMxJ7RnqMk6X/De57ANBL7J0/YsRsWFZ0GwtNmZ2kl3xZNpBNk21BMTsExvJ')
66+
insert(app, "conf/host.key", 20, 'KLfGQTyGnBh9ts/fy6AUzMrvhZdX9uPWl38gxtrHr7Eq8cQHz+ECqwaedQHFg81h')
67+
insert(app, "conf/host.key", 21, 'q7BPqhspHVuAX+NCVBwCoB1xAoGBAME20mC9G6GgUE6LUWCXDjsfa7kEPlpqDZLv')
68+
insert(app, "conf/host.key", 22, '9o2ONkAjW8sMJ8rPK99MZjDwrLxTNi153TA+iFXeJdBGKq9WMmyR+Ww/CW/ZOPt5')
69+
insert(app, "conf/host.key", 23, 'IAWyk9F14Xz6E4FMfwRRBtpd8gnmTUq449CgqxRE1Ner93Hvi6VwyADz8lZc1Jf5')
70+
insert(app, "conf/host.key", 24, 'BnG2DSA7AoGAAWRtgCEkhR/9GyLyAqoUd45FQdRdwIiDwRUsuazSMF2g+FSIfXqR')
71+
insert(app, "conf/host.key", 25, 'MgEidXuKYTIRgsiDmgy6fy3XkSzaR1ehjC1uUyyiUzEd+guG9tURrRygR8S6VGw3')
72+
insert(app, "conf/host.key", 26, 'mxX+1gneJnzA2cBminkc28ohIQegHEqKKif5gRsc2md+LsvDNR93io4=')
73+
insert(app, "conf/host.key", 27, '-----END RSA PRIVATE KEY-----')
74+
create(app, 'conf/host.pass.key')
75+
insert(app, "conf/host.pass.key", 1, '-----BEGIN RSA PRIVATE KEY-----')
76+
insert(app, "conf/host.pass.key", 2, 'Proc-Type: 4,ENCRYPTED')
77+
insert(app, "conf/host.pass.key", 3, 'DEK-Info: DES-EDE3-CBC,FC6F4AA83014298F')
78+
insert(app, "conf/host.pass.key", 4, '')
79+
insert(app, "conf/host.pass.key", 5, 'ZxpC4NYQsMYCOfpMg3iRbQ5UQDBp50NGnT+wBgHnhTqXVUsIZ0x4eFvFKmIoGFne')
80+
insert(app, "conf/host.pass.key", 6, 'hX2pnIMFpOJs4tRIItFyvjcwAARRZxg9KCkjL8cPBhNL4LNExYOTKE8QfTzTb9/l')
81+
insert(app, "conf/host.pass.key", 7, 'DoF5EJraNwvXKlVNh9wrROW7oMJFqhkVRQN+lMnczTGPznnjbBvOr69ypU8/NWX/')
82+
insert(app, "conf/host.pass.key", 8, 'JFgLYqBUnOPUKCaqxEuNzP632jOkhSdXmtl4ft1JFx/uoJG4rCGw5zOVHnTsCMbs')
83+
insert(app, "conf/host.pass.key", 9, 'aWfzfYgnreKvSmwk+5J/0aHR14sXoJpPOk1KvJ3U347cJ/RB1hnnShAdEmYxqPmc')
84+
insert(app, "conf/host.pass.key", 10, '7Hp2BXt86qlFs9SEBwptPtGmF+YAW7HdcgU0M1ONJ0/GysT4RWFJr5VO4QQWpQT/')
85+
insert(app, "conf/host.pass.key", 11, 'DrX8odwKVSQHekmsJz4hD0CXj2v8KU7crbEtTemj3koxnbEn7gcZoGtTMmz37hZS')
86+
insert(app, "conf/host.pass.key", 12, 'qJOolpPqHFV7WtheZ/+5ztSJ91eUgRqKTt1gLgQ6wbaCFfgsPIIRAjuklWnAyKxM')
87+
insert(app, "conf/host.pass.key", 13, '0dxRb7pTCDLewZ7V2g9MzkF46r+eTCIw31NJC6EUsOYaj46bYbmdK5Smjqgc1z5S')
88+
insert(app, "conf/host.pass.key", 14, 'jQGSFUUA+MRlLhx0e/old3fK1oUY1kujcDZcz57arykFDxNHSseFIauJOUeiw0Tp')
89+
insert(app, "conf/host.pass.key", 15, '5nZJYtg4yWTEbLMi+iegu/pYZSbuy8APojIgPupg0FiFOED23J2ziXQs8ZxaG7w6')
90+
insert(app, "conf/host.pass.key", 16, 'oc6SxWrubxCGt0dlEHAQnAB5eVZGcKCH4hVaF4w85j/oWf0Tw/kFAD1MqyiBPes3')
91+
insert(app, "conf/host.pass.key", 17, 'BcrDyO4AJWpmocMZ5ERVkPhx1rqyRrpaYBMdTJ2LoQaKIGeDucfW3Iap0mk+jT31')
92+
insert(app, "conf/host.pass.key", 18, 'RTVYNlCqoU1+oACqpV4mRQGW0BDIENvazCb+VJ0qHkedrM/Bx0Gxnx7jrlptOYEn')
93+
insert(app, "conf/host.pass.key", 19, '2rU53bOIdwGw9+MjDV+jLKnxuwh56SI5wJzSBCr38jLlA/SgPDM+8K9AjeCJg0w5')
94+
insert(app, "conf/host.pass.key", 20, 'C4Na4pDa3tSRwV2WsDJcLnWN+L1NoFNNMnePGzZHCBWaFI9WM2sZI5LsM+gZt37k')
95+
insert(app, "conf/host.pass.key", 21, 'EnR/r8rn5Vig7hwxntW7D6IAka2Tkfl0Y+uvl373EGIv9d61/x6cxomPbYGwH0Sn')
96+
insert(app, "conf/host.pass.key", 22, '6Emz3so5pXUuP8w2Gx7FNI9m7r+xOAfe87Eplc5DZiwtWyeSLOKDOnkwTxNdFMhk')
97+
insert(app, "conf/host.pass.key", 23, 'GerNKG4RrMB5GEU0oI1rkMPlK4vf/K9ynHqLq5HjH839EzWH7aeqlo8059WMZ0Jz')
98+
insert(app, "conf/host.pass.key", 24, 'qecDXcEZ2K9RkUPqGC2wdAGTyea/ElEWmplAWfqVHkD497IShQfTgJ23oLxFTDhd')
99+
insert(app, "conf/host.pass.key", 25, 'IUso3Xj50N1U2+4JbYABv9zaXLRK+qTEPkTmeQHo+CJC0iIVQwGtQS9p3IcuLzKd')
100+
insert(app, "conf/host.pass.key", 26, 's3wqL1Durxe+YVfHNqTYh2uC6eclSwA/21uDa59B37oK9Aymdzujps7IJQ147QWN')
101+
insert(app, "conf/host.pass.key", 27, '4e39vDDrfPMthKiQAWm4f3+vduLxzShDgzLyVPDaYVfPAlD7UETz0x6eNCTZXDjg')
102+
insert(app, "conf/host.pass.key", 28, 'S4JMnjhH8EFrzKdnUH40oeWa9RKKo5RwvRRRGNgR23OzcibI+54kl5DsMTI229+G')
103+
insert(app, "conf/host.pass.key", 29, 'PDd5V4m+ahdfaPsM9DMr1mWGSN/hoLDJtMFPOiZP5R6OSTi99Tj5KJiglSdjmb6u')
104+
insert(app, "conf/host.pass.key", 30, '-----END RSA PRIVATE KEY-----')
105+
create(app, 'conf/host.cert')
106+
insert(app, "conf/host.cert", 1, '-----BEGIN CERTIFICATE-----')
107+
insert(app, "conf/host.cert", 2, 'MIID4DCCAsgCCQCdj5qAy7MGoTANBgkqhkiG9w0BAQsFADCBsTEfMB0GA1UECAwW')
108+
insert(app, "conf/host.cert", 3, 'VGVzdCBTdGF0ZSBvciBQcm92aW5jZTEWMBQGA1UEBwwNVGVzdCBMb2NhbGl0eTEa')
109+
insert(app, "conf/host.cert", 4, 'MBgGA1UECgwRT3JnYW5pemF0aW9uIE5hbWUxITAfBgNVBAsMGE9yZ2FuaXphdGlv')
110+
insert(app, "conf/host.cert", 5, 'bmFsIFVuaXQgTmFtZTEUMBIGA1UEAwwLQ29tbW9uIE5hbWUxITAfBgkqhkiG9w0B')
111+
insert(app, "conf/host.cert", 6, 'CQEWEnRlc3RAZW1haWwuYWRkcmVzczAeFw0xNzA1MjkxMjUyMDVaFw0yNzA1Mjcx')
112+
insert(app, "conf/host.cert", 7, 'MjUyMDVaMIGxMR8wHQYDVQQIDBZUZXN0IFN0YXRlIG9yIFByb3ZpbmNlMRYwFAYD')
113+
insert(app, "conf/host.cert", 8, 'VQQHDA1UZXN0IExvY2FsaXR5MRowGAYDVQQKDBFPcmdhbml6YXRpb24gTmFtZTEh')
114+
insert(app, "conf/host.cert", 9, 'MB8GA1UECwwYT3JnYW5pemF0aW9uYWwgVW5pdCBOYW1lMRQwEgYDVQQDDAtDb21t')
115+
insert(app, "conf/host.cert", 10, 'b24gTmFtZTEhMB8GCSqGSIb3DQEJARYSdGVzdEBlbWFpbC5hZGRyZXNzMIIBIjAN')
116+
insert(app, "conf/host.cert", 11, 'BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoOx9pCR7rZ50S9FotKVD2+aC36Hj')
117+
insert(app, "conf/host.cert", 12, '4TkXZTZwEnh/fWyuiH2OPaj/dTw60Jvll4jshlnRHfJ6yfc/o7YlDUanLrQJm7I3')
118+
insert(app, "conf/host.cert", 13, '/t3YNgqYg3WXeUTl+GrNHn/3QgFGYqKobu8kfrwP4IapQRqlq4ZSdlR/bWpxnYSC')
119+
insert(app, "conf/host.cert", 14, 'ZoXeeoimoSUcLlqD5dw77v2BlG2gqL5+lr5Fx4mDC12vczoUMRg88+VuA1ezU4cu')
120+
insert(app, "conf/host.cert", 15, 'XDe2MbpJMd7rqGN0xK4bCwkFtSJqBM1TH/Czr1S52hKrDTTys9PVw+eZSKO7BCk+')
121+
insert(app, "conf/host.cert", 16, 'PDq5jjx337XOWiO0kSHfV64x68xTojfzTzF304byr2Ytq6DjNbpZKwdYBwIDAQAB')
122+
insert(app, "conf/host.cert", 17, 'MA0GCSqGSIb3DQEBCwUAA4IBAQAw+cuEp3wbLcTIzKCrZ7KzH3zaMtzIU5ZAjTkt')
123+
insert(app, "conf/host.cert", 18, '66QSFALq/ZvAswAybpWKb+2EZZ8iV477W0nFJUkHIOrOav4qWJfmPtdp2k6d2Eey')
124+
insert(app, "conf/host.cert", 19, 'cYQjrD9ghV7aKtKCstFdXo4h23FNaKb+kHSXjvEuf8EuDWilXKrjczmJAmGpBeSE')
125+
insert(app, "conf/host.cert", 20, 'nUVGGYYMAKf+ndkuSYYnJs/V823o9npSiy0Ke83Z64Co04+yos+BMIuDIhP/+LOp')
126+
insert(app, "conf/host.cert", 21, 'pesqro66VwKswcG9O/sjSCaiFgljlQARB4xKBSwR5py8hKDBKfoWnvCpaFPLS34P')
127+
insert(app, "conf/host.cert", 22, 'rGPQp900aMtDjORTe2ZP2EP/rMSm7w/PL8djNVMtgFKzY2Tc')
128+
insert(app, "conf/host.cert", 23, '-----END CERTIFICATE-----')
129+
130+
131+
# Run the newly created application
132+
step('Run our ssl-application')
133+
134+
self.play = callPlay(self, ['run', app])
135+
#wait for play to be ready
136+
self.assert_(waitFor(self.play, 'Listening for HTTPS on port 9000'))
137+
138+
step("Send request to https")
139+
140+
browser = mechanize.Browser()
141+
response = browser.open('https://localhost:9000/')
142+
143+
step("check that ssl message is logged")
144+
self.assert_(waitFor(self.play, 'I am ssl secured!'))
145+
146+
step("stop play")
147+
killPlay('https')
148+
self.play.wait()
149+
150+
#now we're going to manually configure log4j to log debug messages
151+
step('using key file with password')
152+
153+
insert(app, "conf/application.conf", 236,
154+
'certificate.key.file = conf/host.pass.key')
155+
156+
# re-run the application with new setting
157+
step('re-run our ssl-application')
158+
159+
self.play = callPlay(self, ['run', app])
160+
#wait for play to be ready
161+
self.assert_(waitFor(self.play, 'Listening for HTTPS on port 9000'))
162+
163+
step("Send request to https")
164+
165+
browser = mechanize.Browser()
166+
response = browser.open('https://localhost:9000/')
167+
168+
step("check that ssl message is logged")
169+
self.assert_(waitFor(self.play, 'I am ssl secured!'))
170+
171+
step("stop play")
172+
killPlay('https')
173+
self.play.wait()
174+
175+
step("done testing ssl config")
176+
18177
def testLogLevelsAndLog4jConfig(self):
19178

20179
# Testing job developing
@@ -80,8 +239,7 @@ def testLogLevelsAndLog4jConfig(self):
80239
insert(app, "conf/log4j.xml", 15, ' <appender-ref ref="console"/>')
81240
insert(app, "conf/log4j.xml", 16, ' </root>')
82241
insert(app, "conf/log4j.xml", 17, '</log4j:configuration>')
83-
84-
242+
85243
# Run the newly created application
86244
step('re-run our logger-application')
87245

@@ -689,9 +847,9 @@ def timeout(process):
689847
killPlay()
690848
timeoutOccurred = True
691849

692-
def killPlay():
850+
def killPlay(http = 'http'):
693851
try:
694-
urllib2.urlopen('http://localhost:9000/@kill')
852+
urllib2.urlopen('%s://localhost:9000/@kill' % http)
695853
except:
696854
pass
697855

@@ -748,4 +906,13 @@ def rename(app, fro, to):
748906
os.rename(os.path.join(app, fro), os.path.join(app, to))
749907

750908
if __name__ == '__main__':
909+
# thanks to: https://stackoverflow.com/a/35960702/3221476
910+
try:
911+
_create_unverified_https_context = ssl._create_unverified_context
912+
except AttributeError:
913+
# Legacy Python that doesn't verify HTTPS certificates by default
914+
pass
915+
else:
916+
# Handle target environment that doesn't support HTTPS verification
917+
ssl._create_default_https_context = _create_unverified_https_context
751918
unittest.main()

0 commit comments

Comments
 (0)