Skip to content

Commit 9102242

Browse files
authored
Merge pull request #866 from oracle/develop-REST-cert-chain-tests
Develop rest cert chain tests
2 parents dce7aef + a9d33df commit 9102242

File tree

6 files changed

+414
-63
lines changed

6 files changed

+414
-63
lines changed

integration-tests/src/test/java/oracle/kubernetes/operator/ITOperator.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import oracle.kubernetes.operator.utils.ExecCommand;
1212
import oracle.kubernetes.operator.utils.ExecResult;
1313
import oracle.kubernetes.operator.utils.Operator;
14+
import oracle.kubernetes.operator.utils.Operator.RESTCertType;
1415
import oracle.kubernetes.operator.utils.TestUtils;
1516
import org.junit.AfterClass;
1617
import org.junit.Assume;
@@ -34,6 +35,7 @@ public class ITOperator extends BaseTest {
3435
private static final String opForDelYamlFile1 = "operator_del1.yaml";
3536
private static final String opForDelYamlFile2 = "operator_del2.yaml";
3637
private static final String opForBackwardCompatibility = "operator_bc.yaml";
38+
private static final String opForRESTCertChain = "operator_chain.yaml";
3739

3840
// property file used to customize domain properties for domain inputs yaml
3941
private static String domain1YamlFile = "domain1.yaml";
@@ -61,6 +63,7 @@ public class ITOperator extends BaseTest {
6163
private static Operator operatorForDel2;
6264

6365
private static Operator operatorForBackwardCompatibility;
66+
private static Operator operatorForRESTCertChain;
6467

6568
private static boolean QUICKTEST;
6669
private static boolean SMOKETEST;
@@ -403,7 +406,7 @@ public void test8CreateDomainOnExistingDir() throws Exception {
403406
*
404407
* @throws Exception
405408
*/
406-
// @Test
409+
// //@DisabledTest
407410
public void testACreateDomainApacheLB() throws Exception {
408411
Assume.assumeFalse(QUICKTEST);
409412

@@ -598,13 +601,35 @@ public void testRESTIdentityBackwardCompatibility() throws Exception {
598601
logTestBegin("testRESTIdentityBackwardCompatibility");
599602
logger.info("Checking if operatorForBackwardCompatibility is running, if not creating");
600603
if (operatorForBackwardCompatibility == null) {
601-
operatorForBackwardCompatibility = TestUtils.createOperator(opForBackwardCompatibility, true);
604+
operatorForBackwardCompatibility =
605+
TestUtils.createOperator(opForBackwardCompatibility, RESTCertType.LEGACY);
602606
}
607+
operatorForBackwardCompatibility.verifyOperatorExternalRESTEndpoint();
603608
logger.info("Operator using legacy REST identity created successfully");
604609
operatorForBackwardCompatibility.destroy();
605610
logger.info("SUCCESS - testRESTIdentityBackwardCompatibility");
606611
}
607612

613+
/**
614+
* Create operator and enable external rest endpoint using a certificate chain. This test uses the
615+
* operator backward compatibility operator because that operator is destroyed.
616+
*
617+
* @throws Exception
618+
*/
619+
@Test
620+
public void testOperatorRESTUsingCertificateChain() throws Exception {
621+
Assume.assumeFalse(QUICKTEST);
622+
623+
logTestBegin("testOperatorRESTUsingCertificateChain");
624+
logger.info("Checking if operatorForBackwardCompatibility is running, if not creating");
625+
if (operatorForRESTCertChain == null) {
626+
operatorForRESTCertChain = TestUtils.createOperator(opForRESTCertChain, RESTCertType.CHAIN);
627+
}
628+
operatorForRESTCertChain.verifyOperatorExternalRESTEndpoint();
629+
logger.info("Operator using legacy REST identity created successfully");
630+
logger.info("SUCCESS - testOperatorRESTUsingCertificateChain");
631+
}
632+
608633
/**
609634
* Create Operator and create domain with some junk value for t3 channel public address and using
610635
* custom situational config override replace with valid public address using secret Verify the

integration-tests/src/test/java/oracle/kubernetes/operator/utils/Operator.java

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
/** Operator class with all the utility methods for Operator. */
1818
public class Operator {
1919

20+
public static enum RESTCertType {
21+
/*self-signed certificate and public key stored in a kubernetes tls secret*/
22+
SELF_SIGNED,
23+
/*Certificate signed by an auto-created CA signed by an auto-created root certificate,
24+
* both and stored in a kubernetes tls secret*/
25+
CHAIN,
26+
/*Certificate and public key, and stored in a kubernetes tls secret*/
27+
LEGACY
28+
};
29+
2030
public static final String CREATE_OPERATOR_SCRIPT_MESSAGE =
2131
"The Oracle WebLogic Server Kubernetes Operator is deployed";
2232

@@ -36,6 +46,7 @@ public class Operator {
3646

3747
private static int maxIterationsOp = BaseTest.getMaxIterationsPod(); // 50 * 5 = 250 seconds
3848
private static int waitTimeOp = BaseTest.getWaitTimePod();
49+
private static RESTCertType restCertType = RESTCertType.SELF_SIGNED;
3950

4051
/**
4152
* Takes operator input properties which needs to be customized and generates a operator input
@@ -44,9 +55,10 @@ public class Operator {
4455
* @param inputYaml
4556
* @throws Exception
4657
*/
47-
public Operator(String inputYaml, boolean useLegacyRESTIdentity) throws Exception {
58+
public Operator(String inputYaml, RESTCertType restCertType) throws Exception {
59+
this.restCertType = restCertType;
4860
initialize(inputYaml);
49-
generateInputYaml(useLegacyRESTIdentity);
61+
generateInputYaml();
5062
callHelmInstall();
5163
}
5264

@@ -195,8 +207,7 @@ public void scale(String domainUid, String clusterName, int numOfMS) throws Exce
195207
.append(clusterName)
196208
.append("/scale");
197209

198-
TestUtils.makeOperatorPostRestCall(
199-
operatorNS, myOpRestApiUrl.toString(), myJsonObjStr, userProjectsDir);
210+
TestUtils.makeOperatorPostRestCall(this, myOpRestApiUrl.toString(), myJsonObjStr);
200211
// give sometime to complete
201212
logger.info("Wait 30 sec for scaling to complete...");
202213
Thread.sleep(30 * 1000);
@@ -217,7 +228,23 @@ public void verifyDomainExists(String domainUid) throws Exception {
217228
.append(externalRestHttpsPort)
218229
.append("/operator/latest/domains/")
219230
.append(domainUid);
220-
TestUtils.makeOperatorGetRestCall(operatorNS, myOpRestApiUrl.toString(), userProjectsDir);
231+
TestUtils.makeOperatorGetRestCall(this, myOpRestApiUrl.toString());
232+
}
233+
234+
/**
235+
* Verify the Operator's REST Api is working fine over TLS
236+
*
237+
* @throws Exception
238+
*/
239+
public void verifyOperatorExternalRESTEndpoint() throws Exception {
240+
// Operator REST external API URL to scale
241+
StringBuffer myOpRestApiUrl =
242+
new StringBuffer("https://")
243+
.append(TestUtils.getHostName())
244+
.append(":")
245+
.append(externalRestHttpsPort)
246+
.append("/operator/");
247+
TestUtils.makeOperatorGetRestCall(this, myOpRestApiUrl.toString());
221248
}
222249

223250
public Map<String, Object> getOperatorMap() {
@@ -258,23 +285,28 @@ private String getExecFailure(String cmd, ExecResult result) throws Exception {
258285
}
259286

260287
private void generateInputYaml() throws Exception {
261-
generateInputYaml(false);
262-
}
263-
264-
private void generateInputYaml(boolean useLegacyRESTIdentity) throws Exception {
265288
Path parentDir =
266289
Files.createDirectories(Paths.get(userProjectsDir + "/weblogic-operators/" + operatorNS));
267290
generatedInputYamlFile = parentDir + "/weblogic-operator-values.yaml";
268291
TestUtils.createInputFile(operatorMap, generatedInputYamlFile);
269292
StringBuilder sb = new StringBuilder(200);
270293
sb.append(BaseTest.getProjectRoot());
271-
if (useLegacyRESTIdentity) {
272-
sb.append(
273-
"/integration-tests/src/test/resources/scripts/legacy-generate-external-rest-identity.sh ");
274-
} else {
275-
sb.append("/kubernetes/samples/scripts/rest/generate-external-rest-identity.sh ");
276-
sb.append(" -n ");
277-
sb.append(operatorNS);
294+
switch (restCertType) {
295+
case LEGACY:
296+
sb.append(
297+
"/integration-tests/src/test/resources/scripts/legacy-generate-external-rest-identity.sh ");
298+
break;
299+
case CHAIN:
300+
sb.append(
301+
"/integration-tests/src/test/resources/scripts/generate-external-rest-identity-chain.sh ");
302+
sb.append(" -n ");
303+
sb.append(operatorNS);
304+
break;
305+
case SELF_SIGNED:
306+
sb.append("/kubernetes/samples/scripts/rest/generate-external-rest-identity.sh ");
307+
sb.append(" -n ");
308+
sb.append(operatorNS);
309+
break;
278310
}
279311
sb.append(" DNS:");
280312
sb.append(TestUtils.getHostName());
@@ -395,4 +427,16 @@ private void initialize(String yamlFile) throws Exception {
395427
operatorNS);
396428
}
397429
}
430+
431+
public String getOperatorNamespace() {
432+
return operatorNS;
433+
}
434+
435+
public String getUserProjectsDir() {
436+
return userProjectsDir;
437+
}
438+
439+
public RESTCertType getRestCertType() {
440+
return restCertType;
441+
}
398442
}

integration-tests/src/test/java/oracle/kubernetes/operator/utils/PEMImporter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.security.KeyStoreException;
1515
import java.security.NoSuchAlgorithmException;
1616
import java.security.PrivateKey;
17+
import java.security.cert.Certificate;
1718
import java.security.cert.CertificateException;
1819
import java.security.cert.CertificateFactory;
1920
import java.security.cert.X509Certificate;
@@ -34,12 +35,11 @@ public class PEMImporter {
3435
* @param certificatePem the certificate(s) PEM file
3536
* @param the password to set to protect the private key
3637
*/
37-
public static KeyStore createKeyStore(
38-
File privateKeyPem, File certificatePem, final String password)
38+
public static KeyStore createKeyStore(File certificatePem, final String password)
3939
throws Exception, KeyStoreException, IOException, NoSuchAlgorithmException,
4040
CertificateException {
4141
// Import certificate pem file
42-
final X509Certificate[] cert = createCertificates(certificatePem);
42+
final X509Certificate[] certChain = createCertificates(certificatePem);
4343

4444
// Create a Keystore obj if the type "JKS"
4545
final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
@@ -48,10 +48,13 @@ public static KeyStore createKeyStore(
4848
keystore.load(null);
4949

5050
// Import private key
51-
final PrivateKey key = createPrivateKey(privateKeyPem);
51+
// final PrivateKey key = createPrivateKey(privateKeyPem);
5252

5353
// Load cert and key files into the Keystore obj and create it
54-
keystore.setKeyEntry(privateKeyPem.getName(), key, password.toCharArray(), cert);
54+
// keystore.setKeyEntry(privateKeyPem.getName(), key, password.toCharArray(), cert);
55+
for (Certificate cert : certChain) {
56+
keystore.setCertificateEntry("operator", cert);
57+
}
5558

5659
return keystore;
5760
}

0 commit comments

Comments
 (0)