Skip to content

Commit 4f57092

Browse files
committed
Move certificate processing to new Certificates class
1 parent 89cd614 commit 4f57092

File tree

12 files changed

+262
-142
lines changed

12 files changed

+262
-142
lines changed

operator/src/main/java/oracle/kubernetes/operator/helpers/PodHelper.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import oracle.kubernetes.operator.ProcessingConstants;
2222
import oracle.kubernetes.operator.TuningParameters;
2323
import oracle.kubernetes.operator.logging.MessageKeys;
24-
import oracle.kubernetes.operator.rest.RestServer;
2524
import oracle.kubernetes.operator.steps.DefaultResponseStep;
25+
import oracle.kubernetes.operator.utils.Certificates;
2626
import oracle.kubernetes.operator.work.Component;
2727
import oracle.kubernetes.operator.work.NextAction;
2828
import oracle.kubernetes.operator.work.Packet;
@@ -96,9 +96,7 @@ V1Pod withNonHashedElements(V1Pod pod) {
9696
}
9797

9898
private V1EnvVar internalCertEnvValue() {
99-
return new V1EnvVar()
100-
.name(INTERNAL_OPERATOR_CERT_ENV)
101-
.value(getInternalOperatorCertFile(TuningParameters.getInstance()));
99+
return new V1EnvVar().name(INTERNAL_OPERATOR_CERT_ENV).value(getInternalOperatorCertFile());
102100
}
103101

104102
private Optional<V1Container> getContainer(V1Pod v1Pod) {
@@ -131,8 +129,8 @@ protected Map<String, String> getPodAnnotations() {
131129
return getServerSpec().getPodAnnotations();
132130
}
133131

134-
private String getInternalOperatorCertFile(TuningParameters tuningParameters) {
135-
return RestServer.getInstance().getInternalCertificateAsBase64PEM();
132+
private String getInternalOperatorCertFile() {
133+
return Certificates.getOperatorInternalCertificateData();
136134
}
137135
}
138136

@@ -356,7 +354,7 @@ private Step deletePod(String name, String namespace, Step next) {
356354
}
357355
}
358356

359-
public static List<V1EnvVar> createCopy(List<V1EnvVar> envVars) {
357+
static List<V1EnvVar> createCopy(List<V1EnvVar> envVars) {
360358
ArrayList<V1EnvVar> copy = new ArrayList<>();
361359
if (envVars != null) {
362360
for (V1EnvVar envVar : envVars) {

operator/src/main/java/oracle/kubernetes/operator/rest/RestConfigImpl.java

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
package oracle.kubernetes.operator.rest;
66

7-
import java.io.File;
8-
import java.nio.file.Files;
9-
import java.nio.file.Paths;
107
import java.util.Collection;
118
import oracle.kubernetes.operator.logging.LoggingFacade;
129
import oracle.kubernetes.operator.logging.LoggingFactory;
1310
import oracle.kubernetes.operator.rest.backend.RestBackend;
11+
import oracle.kubernetes.operator.utils.Certificates;
1412

1513
/** RestConfigImpl provides the WebLogic Operator REST api configuration. */
1614
public class RestConfigImpl implements RestConfig {
@@ -20,17 +18,6 @@ public class RestConfigImpl implements RestConfig {
2018
private final String principal;
2119
private final Collection<String> targetNamespaces;
2220

23-
static final String OPERATOR_DIR = "/operator/";
24-
static final String INTERNAL_REST_IDENTITY_DIR = OPERATOR_DIR + "internal-identity/";
25-
static final String INTERNAL_CERTIFICATE = INTERNAL_REST_IDENTITY_DIR + "internalOperatorCert";
26-
private static final String INTERNAL_CERTIFICATE_KEY =
27-
INTERNAL_REST_IDENTITY_DIR + "internalOperatorKey";
28-
private static final String EXTERNAL_REST_IDENTITY_DIR = OPERATOR_DIR + "external-identity/";
29-
private static final String EXTERNAL_CERTIFICATE =
30-
EXTERNAL_REST_IDENTITY_DIR + "externalOperatorCert";
31-
private static final String EXTERNAL_CERTIFICATE_KEY =
32-
EXTERNAL_REST_IDENTITY_DIR + "externalOperatorKey";
33-
3421
/**
3522
* Constructs a RestConfigImpl.
3623
*
@@ -62,12 +49,12 @@ public int getInternalHttpsPort() {
6249

6350
@Override
6451
public String getOperatorExternalCertificateData() {
65-
return getCertificate(EXTERNAL_CERTIFICATE);
52+
return Certificates.getOperatorExternalCertificateData();
6653
}
6754

6855
@Override
6956
public String getOperatorInternalCertificateData() {
70-
return getCertificate(INTERNAL_CERTIFICATE);
57+
return Certificates.getOperatorInternalCertificateData();
7158
}
7259

7360
@Override
@@ -92,12 +79,12 @@ public String getOperatorInternalKeyData() {
9279

9380
@Override
9481
public String getOperatorExternalKeyFile() {
95-
return getKey(EXTERNAL_CERTIFICATE_KEY);
82+
return Certificates.getOperatorExternalKeyFile();
9683
}
9784

9885
@Override
9986
public String getOperatorInternalKeyFile() {
100-
return getKey(INTERNAL_CERTIFICATE_KEY);
87+
return Certificates.getOperatorInternalKeyFile();
10188
}
10289

10390
@Override
@@ -107,48 +94,4 @@ public RestBackend getBackend(String accessToken) {
10794
LOGGER.exiting();
10895
return result;
10996
}
110-
111-
// path - a file containing a base64 encoded string containing the operator's cert in pem format
112-
private String getCertificate(String path) {
113-
LOGGER.entering(path);
114-
// in pem format
115-
String result = null;
116-
if (checkFileExists(path)) {
117-
try {
118-
result = new String(Files.readAllBytes(Paths.get(path)));
119-
} catch (Throwable t) {
120-
LOGGER.warning("Can't read " + path, t);
121-
}
122-
}
123-
// do not include the certificate data in the log message
124-
LOGGER.exiting();
125-
return result;
126-
}
127-
128-
// path - a file containing the operator's private key in pem format (cleartext)
129-
private String getKey(String path) {
130-
LOGGER.entering(path);
131-
if (!checkFileExists(path)) {
132-
path = null;
133-
}
134-
LOGGER.exiting(path);
135-
return path;
136-
}
137-
138-
private boolean checkFileExists(String path) {
139-
LOGGER.entering(path);
140-
File f = new File(path);
141-
boolean result = false;
142-
if (f.exists()) {
143-
if (f.isFile()) {
144-
result = true;
145-
} else {
146-
LOGGER.warning(path + " is not a file");
147-
}
148-
} else {
149-
LOGGER.warning(path + " does not exist");
150-
}
151-
LOGGER.exiting(result);
152-
return result;
153-
}
15497
}

operator/src/main/java/oracle/kubernetes/operator/rest/RestServer.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017, 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1+
// Copyright 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
22
// Licensed under the Universal Permissive License v 1.0 as shown at
33
// http://oss.oracle.com/licenses/upl.
44

@@ -19,7 +19,6 @@
1919
import javax.net.ssl.SSLContext;
2020
import oracle.kubernetes.operator.logging.LoggingFacade;
2121
import oracle.kubernetes.operator.logging.LoggingFactory;
22-
import oracle.kubernetes.operator.logging.MessageKeys;
2322
import oracle.kubernetes.operator.work.Container;
2423
import oracle.kubernetes.operator.work.ContainerResolver;
2524
import org.apache.commons.codec.binary.Base64;
@@ -58,8 +57,8 @@ public class RestServer {
5857
private String baseExternalHttpsUri;
5958
private String baseInternalHttpsUri;
6059

61-
HttpServer externalHttpsServer;
62-
HttpServer internalHttpsServer;
60+
private HttpServer externalHttpsServer;
61+
private HttpServer internalHttpsServer;
6362

6463
private static final String SSL_PROTOCOL = "TLSv1.2";
6564
private static final String[] SSL_PROTOCOLS = {
@@ -133,7 +132,7 @@ private RestServer(RestConfig config) {
133132
*
134133
* @return the uri
135134
*/
136-
public String getExternalHttpsUri() {
135+
String getExternalHttpsUri() {
137136
return baseExternalHttpsUri;
138137
}
139138

@@ -142,7 +141,7 @@ public String getExternalHttpsUri() {
142141
*
143142
* @return the uri
144143
*/
145-
public String getInternalHttpsUri() {
144+
String getInternalHttpsUri() {
146145
return baseInternalHttpsUri;
147146
}
148147

@@ -218,29 +217,6 @@ public void stop() {
218217
LOGGER.exiting();
219218
}
220219

221-
/**
222-
* Gets the internal https port's certificate as a base64 encoded PEM.
223-
*
224-
* @return base64 encoded PEM containing the certificate, or null if unable to read the
225-
* certificate data.
226-
*/
227-
public String getInternalCertificateAsBase64PEM() {
228-
LOGGER.entering();
229-
String internalCert = null;
230-
try {
231-
internalCert =
232-
Base64.encodeBase64String(
233-
readFromDataOrFile(
234-
this.config.getOperatorInternalCertificateData(),
235-
this.config.getOperatorInternalCertificateFile()));
236-
} catch (IOException e) {
237-
LOGGER.warning(MessageKeys.EXCEPTION, e);
238-
}
239-
240-
LOGGER.exiting(internalCert);
241-
return internalCert;
242-
}
243-
244220
private HttpServer createExternalHttpsServer(Container container) throws Exception {
245221
LOGGER.entering();
246222
HttpServer result =
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator.utils;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.function.Function;
12+
import oracle.kubernetes.operator.logging.LoggingFacade;
13+
import oracle.kubernetes.operator.logging.LoggingFactory;
14+
15+
public class Certificates {
16+
private static LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
17+
18+
private static final String OPERATOR_DIR = "/operator/";
19+
private static final String EXTERNAL_ID_DIR = OPERATOR_DIR + "external-identity/";
20+
private static final String INTERNAL_ID_DIR = OPERATOR_DIR + "internal-identity/";
21+
22+
static final String EXTERNAL_CERTIFICATE_KEY = EXTERNAL_ID_DIR + "externalOperatorKey";
23+
static final String EXTERNAL_CERTIFICATE = EXTERNAL_ID_DIR + "externalOperatorCert";
24+
static final String INTERNAL_CERTIFICATE_KEY = INTERNAL_ID_DIR + "internalOperatorKey";
25+
static final String INTERNAL_CERTIFICATE = INTERNAL_ID_DIR + "internalOperatorCert";
26+
27+
private static Function<String, Path> GET_PATH = p -> Paths.get(p);
28+
29+
public static String getOperatorExternalKeyFile() {
30+
return getKeyOrNull(Certificates.EXTERNAL_CERTIFICATE_KEY);
31+
}
32+
33+
public static String getOperatorInternalKeyFile() {
34+
return getKeyOrNull(Certificates.INTERNAL_CERTIFICATE_KEY);
35+
}
36+
37+
private static String getKeyOrNull(String path) {
38+
return isFileExists(GET_PATH.apply(path)) ? path : null;
39+
}
40+
41+
public static String getOperatorExternalCertificateData() {
42+
return getCertificate(Certificates.EXTERNAL_CERTIFICATE);
43+
}
44+
45+
public static String getOperatorInternalCertificateData() {
46+
return getCertificate(Certificates.INTERNAL_CERTIFICATE);
47+
}
48+
49+
private static String getCertificate(String path) {
50+
try {
51+
return new String(Files.readAllBytes(GET_PATH.apply(path)));
52+
} catch (IOException e) {
53+
LOGGER.warning("Can't read certificate at " + path, e);
54+
return null;
55+
}
56+
}
57+
58+
private static boolean isFileExists(Path path) {
59+
return Files.isRegularFile(path);
60+
}
61+
}

operator/src/test/java/oracle/kubernetes/operator/DomainUpPlanTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828
import oracle.kubernetes.operator.helpers.KubernetesTestSupport;
2929
import oracle.kubernetes.operator.helpers.TuningParametersStub;
3030
import oracle.kubernetes.operator.helpers.UnitTestHash;
31-
import oracle.kubernetes.operator.rest.RestServer;
32-
import oracle.kubernetes.operator.rest.RestTest;
3331
import oracle.kubernetes.operator.steps.DomainPresenceStep;
32+
import oracle.kubernetes.operator.utils.InMemoryCertificates;
3433
import oracle.kubernetes.operator.utils.WlsDomainConfigSupport;
3534
import oracle.kubernetes.operator.work.Step;
3635
import oracle.kubernetes.operator.work.TerminalStep;
@@ -66,19 +65,16 @@ private DomainPresenceStep getDomainPresenceStep() {
6665
public void setUp() throws NoSuchFieldException {
6766
mementos.add(TestUtils.silenceOperatorLogger());
6867
mementos.add(testSupport.install());
68+
mementos.add(InMemoryCertificates.install());
6969

7070
testSupport.addDomainPresenceInfo(domainPresenceInfo);
71-
72-
RestServer.create(new RestTest.TestRestConfigImpl());
7371
}
7472

7573
@After
7674
public void tearDown() throws Exception {
7775
for (Memento memento : mementos) memento.revert();
7876

7977
testSupport.throwOnCompletionFailure();
80-
81-
RestServer.destroy();
8278
}
8379

8480
@Test

operator/src/test/java/oracle/kubernetes/operator/helpers/AdminPodHelperTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import oracle.kubernetes.operator.PodAwaiterStepFactory;
3333
import oracle.kubernetes.operator.ProcessingConstants;
3434
import oracle.kubernetes.operator.VersionConstants;
35-
import oracle.kubernetes.operator.rest.RestTest;
35+
import oracle.kubernetes.operator.utils.InMemoryCertificates;
3636
import oracle.kubernetes.operator.work.FiberTestSupport;
3737
import oracle.kubernetes.operator.work.Packet;
3838
import oracle.kubernetes.operator.work.Step;
@@ -117,11 +117,7 @@ protected void verifyPodNotReplacedWhen(PodMutator mutator) {
117117
PodAwaiterStepFactory.class,
118118
(pod, next) -> terminalStep);
119119

120-
/**/
121120
V1Pod existingPod = createPod(testSupport.getPacket());
122-
/*/
123-
V1Pod existingPod = createPodModel();
124-
/**/
125121
mutator.mutate(existingPod);
126122
initializeExistingPod(existingPod);
127123

@@ -210,7 +206,7 @@ public void whenAdminPodCreated_containerHasStartServerCommand() {
210206
public void whenAdminPodCreated_hasOperatorCertEnvVariable() {
211207
assertThat(
212208
getCreatedPodSpecContainer().getEnv(),
213-
hasEnvVar(INTERNAL_OPERATOR_CERT_ENV_NAME, RestTest.OP_CERT_DATA));
209+
hasEnvVar(INTERNAL_OPERATOR_CERT_ENV_NAME, InMemoryCertificates.INTERNAL_CERT_DATA));
214210
}
215211

216212
@Test

operator/src/test/java/oracle/kubernetes/operator/helpers/FileGroupReaderTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1+
// Copyright 2018, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
22
// Licensed under the Universal Permissive License v 1.0 as shown at
33
// http://oss.oracle.com/licenses/upl.
44

@@ -10,7 +10,6 @@
1010
import static org.hamcrest.junit.MatcherAssert.assertThat;
1111

1212
import java.io.IOException;
13-
import java.nio.file.FileSystem;
1413
import java.nio.file.Path;
1514
import java.util.Map;
1615
import oracle.kubernetes.operator.utils.InMemoryFileSystem;
@@ -19,7 +18,7 @@
1918
public class FileGroupReaderTest {
2019

2120
private final FileGroupReader scriptReader = ConfigMapHelper.getScriptReader();
22-
private static FileSystem fileSystem = InMemoryFileSystem.getInstance();
21+
private static InMemoryFileSystem fileSystem = InMemoryFileSystem.createInstance();
2322

2423
@Test
2524
public void afterLoadScriptsFromClasspath_haveScriptNamesAsKeys() {
@@ -29,8 +28,8 @@ public void afterLoadScriptsFromClasspath_haveScriptNamesAsKeys() {
2928

3029
@Test
3130
public void loadFilesFromMemory() throws IOException {
32-
InMemoryFileSystem.defineFile("group/a.b", "1234");
33-
InMemoryFileSystem.defineFile("group/x/c.d", "5678");
31+
fileSystem.defineFile("group/a.b", "1234");
32+
fileSystem.defineFile("group/x/c.d", "5678");
3433

3534
Path p = fileSystem.getPath("group");
3635
Map<String, String> map = FileGroupReader.loadContents(p);

0 commit comments

Comments
 (0)