Skip to content

Commit 941f27f

Browse files
committed
Refactor get certificate contents to be shared between REST and Admin pod configuration
1 parent cc2a22b commit 941f27f

File tree

6 files changed

+33
-57
lines changed

6 files changed

+33
-57
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package oracle.kubernetes.operator;
2+
3+
public interface OperatorConstants {
4+
static final String OPERATOR_DIR = "/operator/";
5+
static final String INTERNAL_REST_IDENTITY_DIR = OPERATOR_DIR + "internal-identity/";
6+
static final String INTERNAL_CERTIFICATE = INTERNAL_REST_IDENTITY_DIR + "internalOperatorCert";
7+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,6 @@ public boolean equals(Object o) {
261261
public PodTuning getPodTuning();
262262

263263
public String getFileContents(String path);
264+
265+
public boolean checkFileExists(String path);
264266
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,35 @@ public PodTuning getPodTuning() {
140140

141141
// path - a file containing a base64 encoded string containing the operator's cert in pem format
142142
public String getFileContents(String path) {
143+
LOGGER.entering(path);
143144
// in pem format
144145
String result = null;
145146
if (checkFileExists(path)) {
146147
try {
147148
result = new String(Files.readAllBytes(Paths.get(path)));
148149
} catch (Throwable t) {
150+
LOGGER.warning("Can't read " + path, t);
149151
}
150152
}
151153
// do not include the certificate data in the log message
154+
LOGGER.exiting();
152155
return result;
153156
}
154157

155-
private boolean checkFileExists(String path) {
158+
public boolean checkFileExists(String path) {
159+
LOGGER.entering(path);
156160
File f = new File(path);
157161
boolean result = false;
158-
if (f.exists() && f.isFile()) {
159-
result = true;
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");
160170
}
161-
171+
LOGGER.exiting(result);
162172
return result;
163173
}
164174
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import oracle.kubernetes.operator.DomainStatusUpdater;
1818
import oracle.kubernetes.operator.KubernetesConstants;
1919
import oracle.kubernetes.operator.LabelConstants;
20+
import oracle.kubernetes.operator.OperatorConstants;
2021
import oracle.kubernetes.operator.PodAwaiterStepFactory;
2122
import oracle.kubernetes.operator.ProcessingConstants;
2223
import oracle.kubernetes.operator.TuningParameters;
@@ -33,10 +34,6 @@ public class PodHelper {
3334
private PodHelper() {}
3435

3536
static class AdminPodStepContext extends PodStepContext {
36-
private static final String OPERATOR_DIR = "/operator/";
37-
private static final String INTERNAL_REST_IDENTITY_DIR = OPERATOR_DIR + "internal-identity/";
38-
private static final String INTERNAL_OPERATOR_CERTIFICATE =
39-
INTERNAL_REST_IDENTITY_DIR + "internalOperatorCert";
4037
static final String INTERNAL_OPERATOR_CERT_ENV = "INTERNAL_OPERATOR_CERT";
4138

4239
AdminPodStepContext(Step conflictStep, Packet packet) {
@@ -135,7 +132,7 @@ protected Map<String, String> getPodAnnotations() {
135132
}
136133

137134
private String getInternalOperatorCertFile(TuningParameters tuningParameters) {
138-
return tuningParameters.getFileContents(INTERNAL_OPERATOR_CERTIFICATE);
135+
return tuningParameters.getFileContents(OperatorConstants.INTERNAL_CERTIFICATE);
139136
}
140137
}
141138

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

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,21 @@
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;
8+
import oracle.kubernetes.operator.OperatorConstants;
9+
import oracle.kubernetes.operator.TuningParameters;
1110
import oracle.kubernetes.operator.logging.LoggingFacade;
1211
import oracle.kubernetes.operator.logging.LoggingFactory;
1312
import oracle.kubernetes.operator.rest.backend.RestBackend;
1413

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

1817
private static LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
1918

2019
private final String principal;
2120
private final Collection<String> targetNamespaces;
2221

23-
private static final String OPERATOR_DIR = "/operator/";
24-
private static final String INTERNAL_REST_IDENTITY_DIR = OPERATOR_DIR + "internal-identity/";
25-
private static final String INTERNAL_CERTIFICATE =
26-
INTERNAL_REST_IDENTITY_DIR + "internalOperatorCert";
2722
private static final String INTERNAL_CERTIFICATE_KEY =
2823
INTERNAL_REST_IDENTITY_DIR + "internalOperatorKey";
2924
private static final String EXTERNAL_REST_IDENTITY_DIR = OPERATOR_DIR + "external-identity/";
@@ -63,12 +58,12 @@ public int getInternalHttpsPort() {
6358

6459
@Override
6560
public String getOperatorExternalCertificateData() {
66-
return getCertificate(EXTERNAL_CERTIFICATE);
61+
return TuningParameters.getInstance().getFileContents(EXTERNAL_CERTIFICATE);
6762
}
6863

6964
@Override
7065
public String getOperatorInternalCertificateData() {
71-
return getCertificate(INTERNAL_CERTIFICATE);
66+
return TuningParameters.getInstance().getFileContents(INTERNAL_CERTIFICATE);
7267
}
7368

7469
@Override
@@ -109,47 +104,13 @@ public RestBackend getBackend(String accessToken) {
109104
return result;
110105
}
111106

112-
// path - a file containing a base64 encoded string containing the operator's cert in pem format
113-
private String getCertificate(String path) {
114-
LOGGER.entering(path);
115-
// in pem format
116-
String result = null;
117-
if (checkFileExists(path)) {
118-
try {
119-
result = new String(Files.readAllBytes(Paths.get(path)));
120-
} catch (Throwable t) {
121-
LOGGER.warning("Can't read " + path, t);
122-
}
123-
}
124-
// do not include the certificate data in the log message
125-
LOGGER.exiting();
126-
return result;
127-
}
128-
129107
// path - a file containing the operator's private key in pem format (cleartext)
130108
private String getKey(String path) {
131109
LOGGER.entering(path);
132-
if (!checkFileExists(path)) {
110+
if (!TuningParameters.getInstance().checkFileExists(path)) {
133111
path = null;
134112
}
135113
LOGGER.exiting(path);
136114
return path;
137115
}
138-
139-
private boolean checkFileExists(String path) {
140-
LOGGER.entering(path);
141-
File f = new File(path);
142-
boolean result = false;
143-
if (f.exists()) {
144-
if (f.isFile()) {
145-
result = true;
146-
} else {
147-
LOGGER.warning(path + " is not a file");
148-
}
149-
} else {
150-
LOGGER.warning(path + " does not exist");
151-
}
152-
LOGGER.exiting(result);
153-
return result;
154-
}
155116
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import oracle.kubernetes.operator.LabelConstants;
32+
import oracle.kubernetes.operator.OperatorConstants;
3233
import oracle.kubernetes.operator.PodAwaiterStepFactory;
3334
import oracle.kubernetes.operator.ProcessingConstants;
3435
import oracle.kubernetes.operator.VersionConstants;
@@ -41,8 +42,6 @@
4142

4243
@SuppressWarnings("SameParameterValue")
4344
public class AdminPodHelperTest extends PodHelperTestBase {
44-
private static final String INTERNAL_OPERATOR_CERT_FILE_PARAM =
45-
"/operator/internal-identity/internalOperatorCert";
4645
private static final String INTERNAL_OPERATOR_CERT_ENV_NAME = "INTERNAL_OPERATOR_CERT";
4746
private static final String CERTFILE = "certfile";
4847

@@ -210,7 +209,7 @@ public void whenAdminPodCreated_containerHasStartServerCommand() {
210209

211210
@Test
212211
public void whenAdminPodCreated_hasOperatorCertEnvVariable() {
213-
putTuningParameter(INTERNAL_OPERATOR_CERT_FILE_PARAM, CERTFILE);
212+
putTuningParameter(OperatorConstants.INTERNAL_CERTIFICATE, CERTFILE);
214213
assertThat(
215214
getCreatedPodSpecContainer().getEnv(),
216215
hasEnvVar(INTERNAL_OPERATOR_CERT_ENV_NAME, CERTFILE));

0 commit comments

Comments
 (0)