Skip to content

Commit d025f48

Browse files
committed
Retrieve internal certificate through RestServer
1 parent 941f27f commit d025f48

File tree

12 files changed

+111
-78
lines changed

12 files changed

+111
-78
lines changed

operator/src/main/java/oracle/kubernetes/operator/Main.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public Thread newThread(Runnable r) {
143143
new AtomicReference<>(DateTime.now());
144144

145145
private static String principal;
146-
private static RestServer restServer = null;
147146
private static KubernetesVersion version = null;
148147

149148
static final String READINESS_PROBE_FAILURE_EVENT_FILTER =
@@ -450,13 +449,13 @@ private static Collection<String> getTargetNamespaces(String tnValue, String nam
450449

451450
private static void startRestServer(String principal, Collection<String> targetNamespaces)
452451
throws Exception {
453-
restServer = new RestServer(new RestConfigImpl(principal, targetNamespaces));
454-
restServer.start(container);
452+
RestServer.create(new RestConfigImpl(principal, targetNamespaces));
453+
RestServer.getInstance().start(container);
455454
}
456455

457456
private static void stopRestServer() {
458-
restServer.stop();
459-
restServer = null;
457+
RestServer.getInstance().stop();
458+
RestServer.destroy();
460459
}
461460

462461
private static void startLivenessThread() {

operator/src/main/java/oracle/kubernetes/operator/OperatorConstants.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

operator/src/main/java/oracle/kubernetes/operator/TuningParameters.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,4 @@ public boolean equals(Object o) {
259259
public WatchTuning getWatchTuning();
260260

261261
public PodTuning getPodTuning();
262-
263-
public String getFileContents(String path);
264-
265-
public boolean checkFileExists(String path);
266262
}

operator/src/main/java/oracle/kubernetes/operator/TuningParametersImpl.java

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
package oracle.kubernetes.operator;
66

7-
import java.io.File;
87
import java.io.IOException;
9-
import java.nio.file.Files;
10-
import java.nio.file.Paths;
118
import java.util.concurrent.ScheduledExecutorService;
129
import java.util.concurrent.locks.ReadWriteLock;
1310
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -137,38 +134,4 @@ public PodTuning getPodTuning() {
137134
lock.readLock().unlock();
138135
}
139136
}
140-
141-
// path - a file containing a base64 encoded string containing the operator's cert in pem format
142-
public String getFileContents(String path) {
143-
LOGGER.entering(path);
144-
// in pem format
145-
String result = null;
146-
if (checkFileExists(path)) {
147-
try {
148-
result = new String(Files.readAllBytes(Paths.get(path)));
149-
} catch (Throwable t) {
150-
LOGGER.warning("Can't read " + path, t);
151-
}
152-
}
153-
// do not include the certificate data in the log message
154-
LOGGER.exiting();
155-
return result;
156-
}
157-
158-
public boolean checkFileExists(String path) {
159-
LOGGER.entering(path);
160-
File f = new File(path);
161-
boolean result = false;
162-
if (f.exists()) {
163-
if (f.isFile()) {
164-
result = true;
165-
} else {
166-
LOGGER.warning(path + " is not a file");
167-
}
168-
} else {
169-
LOGGER.warning(path + " does not exist");
170-
}
171-
LOGGER.exiting(result);
172-
return result;
173-
}
174137
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import oracle.kubernetes.operator.DomainStatusUpdater;
1818
import oracle.kubernetes.operator.KubernetesConstants;
1919
import oracle.kubernetes.operator.LabelConstants;
20-
import oracle.kubernetes.operator.OperatorConstants;
2120
import oracle.kubernetes.operator.PodAwaiterStepFactory;
2221
import oracle.kubernetes.operator.ProcessingConstants;
2322
import oracle.kubernetes.operator.TuningParameters;
2423
import oracle.kubernetes.operator.logging.MessageKeys;
24+
import oracle.kubernetes.operator.rest.RestServer;
2525
import oracle.kubernetes.operator.steps.DefaultResponseStep;
2626
import oracle.kubernetes.operator.work.Component;
2727
import oracle.kubernetes.operator.work.NextAction;
@@ -132,7 +132,7 @@ protected Map<String, String> getPodAnnotations() {
132132
}
133133

134134
private String getInternalOperatorCertFile(TuningParameters tuningParameters) {
135-
return tuningParameters.getFileContents(OperatorConstants.INTERNAL_CERTIFICATE);
135+
return RestServer.getInstance().getInternalCertificate();
136136
}
137137
}
138138

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
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;
710
import java.util.Collection;
8-
import oracle.kubernetes.operator.OperatorConstants;
9-
import oracle.kubernetes.operator.TuningParameters;
1011
import oracle.kubernetes.operator.logging.LoggingFacade;
1112
import oracle.kubernetes.operator.logging.LoggingFactory;
1213
import oracle.kubernetes.operator.rest.backend.RestBackend;
1314

1415
/** RestConfigImpl provides the WebLogic Operator REST api configuration. */
15-
public class RestConfigImpl implements RestConfig, OperatorConstants {
16+
public class RestConfigImpl implements RestConfig {
1617

1718
private static LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
1819

1920
private final String principal;
2021
private final Collection<String> targetNamespaces;
2122

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";
2226
private static final String INTERNAL_CERTIFICATE_KEY =
2327
INTERNAL_REST_IDENTITY_DIR + "internalOperatorKey";
2428
private static final String EXTERNAL_REST_IDENTITY_DIR = OPERATOR_DIR + "external-identity/";
@@ -58,12 +62,12 @@ public int getInternalHttpsPort() {
5862

5963
@Override
6064
public String getOperatorExternalCertificateData() {
61-
return TuningParameters.getInstance().getFileContents(EXTERNAL_CERTIFICATE);
65+
return getCertificate(EXTERNAL_CERTIFICATE);
6266
}
6367

6468
@Override
6569
public String getOperatorInternalCertificateData() {
66-
return TuningParameters.getInstance().getFileContents(INTERNAL_CERTIFICATE);
70+
return getCertificate(INTERNAL_CERTIFICATE);
6771
}
6872

6973
@Override
@@ -104,13 +108,47 @@ public RestBackend getBackend(String accessToken) {
104108
return result;
105109
}
106110

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+
107128
// path - a file containing the operator's private key in pem format (cleartext)
108129
private String getKey(String path) {
109130
LOGGER.entering(path);
110-
if (!TuningParameters.getInstance().checkFileExists(path)) {
131+
if (!checkFileExists(path)) {
111132
path = null;
112133
}
113134
LOGGER.exiting(path);
114135
return path;
115136
}
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+
}
116154
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class RestServer {
4949
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
5050
private static final int CORE_POOL_SIZE = 3;
5151

52+
private static RestServer INSTANCE = null;
53+
5254
private RestConfig config;
5355

5456
// private String baseHttpUri;
@@ -63,14 +65,29 @@ public class RestServer {
6365
SSL_PROTOCOL
6466
}; // ONLY support TLSv1.2 (by default, we would get TLSv1 and TLSv1.1 too)
6567

68+
public static synchronized void create(RestConfig restConfig) {
69+
if (INSTANCE == null) {
70+
INSTANCE = new RestServer(restConfig);
71+
}
72+
// throw new IllegalStateException();
73+
}
74+
75+
public static synchronized RestServer getInstance() {
76+
return INSTANCE;
77+
}
78+
79+
public static void destroy() {
80+
INSTANCE = null;
81+
}
82+
6683
/**
6784
* Constructs the WebLogic Operator REST server.
6885
*
6986
* @param config - contains the REST server's configuration, which includes the hostnames and port
7087
* numbers that the ports run on, the certificates and private keys for ssl, and the backend
7188
* implementation that does the real work behind the REST api.
7289
*/
73-
public RestServer(RestConfig config) {
90+
private RestServer(RestConfig config) {
7491
LOGGER.entering();
7592
this.config = config;
7693
baseExternalHttpsUri = "https://" + config.getHost() + ":" + config.getExternalHttpsPort();
@@ -168,6 +185,18 @@ public void stop() {
168185
LOGGER.exiting();
169186
}
170187

188+
public String getInternalCertificate() {
189+
try {
190+
return readCertFromDataOrFile(
191+
this.config.getOperatorInternalCertificateData(),
192+
this.config.getOperatorInternalCertificateFile());
193+
} catch (IOException e) {
194+
LOGGER.warning("Unable to read internal certificate data", e);
195+
}
196+
197+
return null;
198+
}
199+
171200
private HttpServer createExternalHttpsServer(Container container) throws Exception {
172201
LOGGER.entering();
173202
HttpServer result =
@@ -321,6 +350,13 @@ private KeyManager[] createKeyManagers(
321350
return result;
322351
}
323352

353+
private static String readCertFromDataOrFile(String data, String file) throws IOException {
354+
if (data != null && data.length() > 0) {
355+
return data;
356+
}
357+
return new String(Files.readAllBytes(new File(file).toPath()));
358+
}
359+
324360
private static byte[] readFromDataOrFile(String data, String file) throws IOException {
325361
if (data != null && data.length() > 0) {
326362
return Base64.decodeBase64(data);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +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;
3133
import oracle.kubernetes.operator.steps.DomainPresenceStep;
3234
import oracle.kubernetes.operator.utils.WlsDomainConfigSupport;
3335
import oracle.kubernetes.operator.work.Step;
@@ -66,13 +68,17 @@ public void setUp() {
6668
mementos.add(testSupport.install());
6769

6870
testSupport.addDomainPresenceInfo(domainPresenceInfo);
71+
72+
RestServer.create(new RestTest.TestRestConfigImpl());
6973
}
7074

7175
@After
7276
public void tearDown() throws Exception {
7377
for (Memento memento : mementos) memento.revert();
7478

7579
testSupport.throwOnCompletionFailure();
80+
81+
RestServer.destroy();
7682
}
7783

7884
@Test

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import oracle.kubernetes.operator.LabelConstants;
32-
import oracle.kubernetes.operator.OperatorConstants;
3332
import oracle.kubernetes.operator.PodAwaiterStepFactory;
3433
import oracle.kubernetes.operator.ProcessingConstants;
3534
import oracle.kubernetes.operator.VersionConstants;
35+
import oracle.kubernetes.operator.rest.RestTest;
3636
import oracle.kubernetes.operator.work.FiberTestSupport;
3737
import oracle.kubernetes.operator.work.Packet;
3838
import oracle.kubernetes.operator.work.Step;
@@ -42,6 +42,7 @@
4242

4343
@SuppressWarnings("SameParameterValue")
4444
public class AdminPodHelperTest extends PodHelperTestBase {
45+
static final String INTERNAL_OPERATOR_CERT_FILE_PARAM = "internalOperatorCert";
4546
private static final String INTERNAL_OPERATOR_CERT_ENV_NAME = "INTERNAL_OPERATOR_CERT";
4647
private static final String CERTFILE = "certfile";
4748

@@ -209,10 +210,10 @@ public void whenAdminPodCreated_containerHasStartServerCommand() {
209210

210211
@Test
211212
public void whenAdminPodCreated_hasOperatorCertEnvVariable() {
212-
putTuningParameter(OperatorConstants.INTERNAL_CERTIFICATE, CERTFILE);
213+
// putTuningParameter(INTERNAL_OPERATOR_CERT_FILE_PARAM, CERTFILE);
213214
assertThat(
214215
getCreatedPodSpecContainer().getEnv(),
215-
hasEnvVar(INTERNAL_OPERATOR_CERT_ENV_NAME, CERTFILE));
216+
hasEnvVar(INTERNAL_OPERATOR_CERT_ENV_NAME, RestTest.OP_CERT_DATA));
216217
}
217218

218219
@Test

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
import oracle.kubernetes.operator.PodAwaiterStepFactory;
7878
import oracle.kubernetes.operator.ProcessingConstants;
7979
import oracle.kubernetes.operator.VersionConstants;
80+
import oracle.kubernetes.operator.rest.RestServer;
81+
import oracle.kubernetes.operator.rest.RestTest;
8082
import oracle.kubernetes.operator.utils.WlsDomainConfigSupport;
8183
import oracle.kubernetes.operator.wlsconfig.NetworkAccessPoint;
8284
import oracle.kubernetes.operator.wlsconfig.WlsDomainConfig;
@@ -170,6 +172,8 @@ public void setUp() throws Exception {
170172
mementos.add(TuningParametersStub.install());
171173
mementos.add(UnitTestHash.install());
172174

175+
RestServer.create(new RestTest.TestRestConfigImpl());
176+
173177
WlsDomainConfigSupport configSupport = new WlsDomainConfigSupport(DOMAIN_NAME);
174178
configSupport.addWlsServer(ADMIN_SERVER, ADMIN_PORT);
175179
if (!ADMIN_SERVER.equals(serverName)) configSupport.addWlsServer(serverName, listenPort);
@@ -197,6 +201,8 @@ public void tearDown() throws Exception {
197201

198202
testSupport.throwOnCompletionFailure();
199203
testSupport.verifyAllDefinedResponsesInvoked();
204+
205+
RestServer.destroy();
200206
}
201207

202208
private DomainPresenceInfo createDomainPresenceInfo(Domain domain) {

0 commit comments

Comments
 (0)