Skip to content

Commit af1dd74

Browse files
authored
added support for WDT use_encryption flag (#138)
* added support for WDT -use_encryption * protect the password field to prevent logging
1 parent 7544bfc commit af1dd74

File tree

11 files changed

+318
-185
lines changed

11 files changed

+318
-185
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptions.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import java.util.List;
1515
import java.util.regex.Matcher;
1616
import java.util.regex.Pattern;
17-
import java.util.stream.Collectors;
18-
import java.util.stream.Stream;
1917

2018
import com.oracle.weblogic.imagetool.api.model.CachedFile;
2119
import com.oracle.weblogic.imagetool.cachestore.CacheStore;
@@ -27,6 +25,7 @@
2725
import com.oracle.weblogic.imagetool.util.ARUUtil;
2826
import com.oracle.weblogic.imagetool.util.AdditionalBuildCommands;
2927
import com.oracle.weblogic.imagetool.util.Constants;
28+
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
3029
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
3130
import com.oracle.weblogic.imagetool.util.Utils;
3231
import com.oracle.weblogic.imagetool.util.ValidationResult;
@@ -90,15 +89,15 @@ private void handleAdditionalBuildCommands() throws IOException {
9089
}
9190
}
9291

93-
void runDockerCommand(String dockerfile, List<String> command) throws IOException, InterruptedException {
94-
logger.info("docker cmd = " + String.join(" ", command));
92+
void runDockerCommand(String dockerfile, DockerBuildCommand command) throws IOException, InterruptedException {
93+
logger.info("docker cmd = " + command.toString());
9594

9695
if (dryRun) {
9796
System.out.println("########## BEGIN DOCKERFILE ##########");
9897
System.out.println(dockerfile);
9998
System.out.println("########## END DOCKERFILE ##########");
10099
} else {
101-
Utils.runDockerCommand(command, dockerLog);
100+
command.run(dockerLog);
102101
}
103102
}
104103

@@ -108,32 +107,27 @@ void runDockerCommand(String dockerfile, List<String> command) throws IOExceptio
108107
*
109108
* @return list of options
110109
*/
111-
List<String> getInitialBuildCmd() {
110+
DockerBuildCommand getInitialBuildCmd(String contextFolder) {
112111

113112
logger.entering();
114-
List<String> cmdBuilder = Stream.of("docker", "build",
115-
"--force-rm=true", "--no-cache").collect(Collectors.toList());
113+
DockerBuildCommand cmdBuilder = new DockerBuildCommand(contextFolder);
116114

117-
cmdBuilder.add("--tag");
118-
cmdBuilder.add(imageTag);
115+
cmdBuilder.setTag(imageTag);
119116

120117
if (!Utils.isEmptyString(httpProxyUrl)) {
121-
cmdBuilder.add(Constants.BUILD_ARG);
122-
cmdBuilder.add("http_proxy=" + httpProxyUrl);
118+
cmdBuilder.addBuildArg("http_proxy", httpProxyUrl);
123119
}
124120

125121
if (!Utils.isEmptyString(httpsProxyUrl)) {
126-
cmdBuilder.add(Constants.BUILD_ARG);
127-
cmdBuilder.add("https_proxy=" + httpsProxyUrl);
122+
cmdBuilder.addBuildArg("https_proxy", httpsProxyUrl);
128123
}
129124

130125
if (!Utils.isEmptyString(nonProxyHosts)) {
131-
cmdBuilder.add(Constants.BUILD_ARG);
132-
cmdBuilder.add("no_proxy=" + nonProxyHosts);
126+
cmdBuilder.addBuildArg("no_proxy", nonProxyHosts);
133127
}
134128

135129
if (dockerPath != null && Files.isExecutable(dockerPath)) {
136-
cmdBuilder.set(0, dockerPath.toAbsolutePath().toString());
130+
cmdBuilder.setDockerPath(dockerPath.toAbsolutePath().toString());
137131
}
138132
logger.exiting();
139133
return cmdBuilder;
@@ -189,15 +183,13 @@ boolean applyingPatches() {
189183
* Builds a list of build args to pass on to docker with the required patches.
190184
* Also, creates links to patches directory under build context instead of copying over.
191185
*
192-
* @return list of strings
186+
* @param previousInventory existing inventory found in the "from" image
193187
* @throws Exception in case of error
194188
*/
195-
List<String> handlePatchFiles(String previousInventory) throws Exception {
189+
void handlePatchFiles(String previousInventory) throws Exception {
196190
logger.entering();
197-
List<String> retVal = new LinkedList<>();
198-
199191
if (!applyingPatches()) {
200-
return retVal;
192+
return;
201193
}
202194

203195
String toPatchesPath = createPatchesTempDirectory().toAbsolutePath().toString();
@@ -254,8 +246,7 @@ List<String> handlePatchFiles(String previousInventory) throws Exception {
254246
if (!patchLocations.isEmpty()) {
255247
dockerfileOptions.setPatchingEnabled();
256248
}
257-
logger.exiting(retVal.size());
258-
return retVal;
249+
logger.exiting();
259250
}
260251

261252
private Path createPatchesTempDirectory() throws IOException {
@@ -266,11 +257,6 @@ private Path createPatchesTempDirectory() throws IOException {
266257

267258

268259
void installOpatchInstaller(String tmpDir, String opatchBugNumber) throws Exception {
269-
// opatch patch now is in the format #####_opatch in the cache store
270-
// So the version passing to the constructor of CachedPatchFile is also "opatch".
271-
// since opatch releases is on it's own and there is not really a patch to opatch
272-
// and the version is embedded in the zip file version.txt
273-
274260
String filePath =
275261
new CachedPatchFile(Constants.OPATCH_PATCH_TYPE, opatchBugNumber, userId, password).resolve(cacheStore);
276262
String filename = new File(filePath).getName();

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CreateImage.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
2121
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
2222
import com.oracle.weblogic.imagetool.util.Constants;
23+
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
2324
import com.oracle.weblogic.imagetool.util.Utils;
2425
import picocli.CommandLine.ArgGroup;
2526
import picocli.CommandLine.Command;
@@ -63,12 +64,12 @@ public CommandResponse call() throws Exception {
6364
install.copyFiles(cacheStore, tmpDir);
6465
dockerfileOptions.setMiddlewareInstall(install);
6566

66-
List<String> cmdBuilder = getInitialBuildCmd();
67+
DockerBuildCommand cmdBuilder = getInitialBuildCmd(tmpDir);
6768
// build wdt args if user passes --wdtModelPath
68-
wdtOptions.handleWdtArgsIfRequired(dockerfileOptions, tmpDir, installerType);
69+
wdtOptions.handleWdtArgs(dockerfileOptions, cmdBuilder, tmpDir);
6970

7071
// resolve required patches
71-
cmdBuilder.addAll(handlePatchFiles(null));
72+
handlePatchFiles(null);
7273

7374
// If patching, patch OPatch first
7475
if (applyingPatches()) {
@@ -92,8 +93,6 @@ public CommandResponse call() throws Exception {
9293
String dockerfile = Utils.writeDockerfile(tmpDir + File.separator + "Dockerfile",
9394
"Create_Image.mustache", dockerfileOptions, dryRun);
9495

95-
// add directory to pass the context
96-
cmdBuilder.add(tmpDir);
9796
runDockerCommand(dockerfile, cmdBuilder);
9897
} catch (Exception ex) {
9998
logger.fine("**ERROR**", ex);

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/RebaseImage.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
2020
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
2121
import com.oracle.weblogic.imagetool.util.Constants;
22+
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
2223
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
2324
import com.oracle.weblogic.imagetool.util.Utils;
2425
import picocli.CommandLine.Command;
@@ -109,16 +110,14 @@ public CommandResponse call() throws Exception {
109110
return new CommandResponse(-1, Utils.getMessage("IMG-0025"));
110111
}
111112

112-
List<String> cmdBuilder = getInitialBuildCmd();
113+
DockerBuildCommand cmdBuilder = getInitialBuildCmd(tmpDir);
113114

114115
if (adminPort != null) {
115-
cmdBuilder.add(Constants.BUILD_ARG);
116-
cmdBuilder.add("ADMIN_PORT=" + adminPort);
116+
cmdBuilder.addBuildArg("ADMIN_PORT", adminPort);
117117
}
118118

119119
if (managedServerPort != null) {
120-
cmdBuilder.add(Constants.BUILD_ARG);
121-
cmdBuilder.add("MANAGED_SERVER_PORT=" + managedServerPort);
120+
cmdBuilder.addBuildArg("MANAGED_SERVER_PORT", managedServerPort);
122121
}
123122

124123
if (dockerfileOptions.isRebaseToNew()) {
@@ -131,7 +130,7 @@ public CommandResponse call() throws Exception {
131130
dockerfileOptions.setMiddlewareInstall(install);
132131

133132
// resolve required patches
134-
cmdBuilder.addAll(handlePatchFiles(null));
133+
handlePatchFiles(null);
135134

136135
// If patching, patch OPatch first
137136
if (applyingPatches()) {
@@ -157,7 +156,6 @@ public CommandResponse call() throws Exception {
157156
"Rebase_Image.mustache", dockerfileOptions, dryRun);
158157

159158
// add directory to pass the context
160-
cmdBuilder.add(tmpDir);
161159
runDockerCommand(dockerfile, cmdBuilder);
162160
} catch (Exception ex) {
163161
return new CommandResponse(-1, ex.getMessage());

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/UpdateImage.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.time.Duration;
99
import java.time.Instant;
1010
import java.util.Base64;
11-
import java.util.List;
1211
import java.util.Properties;
1312
import java.util.UUID;
1413
import java.util.concurrent.Callable;
@@ -19,6 +18,7 @@
1918
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
2019
import com.oracle.weblogic.imagetool.util.ARUUtil;
2120
import com.oracle.weblogic.imagetool.util.Constants;
21+
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
2222
import com.oracle.weblogic.imagetool.util.Utils;
2323
import com.oracle.weblogic.imagetool.wdt.WdtOperation;
2424
import picocli.CommandLine.ArgGroup;
@@ -134,21 +134,19 @@ public CommandResponse call() throws Exception {
134134
}
135135
}
136136

137-
List<String> cmdBuilder = getInitialBuildCmd();
137+
DockerBuildCommand cmdBuilder = getInitialBuildCmd(tmpDir);
138138

139139
// build wdt args if user passes --wdtModelPath
140-
wdtOptions.handleWdtArgsIfRequired(dockerfileOptions, tmpDir, installerType);
140+
wdtOptions.handleWdtArgs(dockerfileOptions, cmdBuilder, tmpDir);
141141
dockerfileOptions.setWdtCommand(wdtOperation);
142142

143143
// resolve required patches
144-
cmdBuilder.addAll(handlePatchFiles(lsinventoryText));
144+
handlePatchFiles(lsinventoryText);
145145

146146
// create dockerfile
147147
String dockerfile = Utils.writeDockerfile(tmpDir + File.separator + "Dockerfile",
148148
"Update_Image.mustache", dockerfileOptions, dryRun);
149149

150-
// add directory to pass the context
151-
cmdBuilder.add(tmpDir);
152150
runDockerCommand(dockerfile, cmdBuilder);
153151
} catch (Exception ex) {
154152
return new CommandResponse(-1, ex.getMessage());

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/WdtOptions.java

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
import com.oracle.weblogic.imagetool.api.model.CachedFile;
1515
import com.oracle.weblogic.imagetool.cachestore.CacheStore;
1616
import com.oracle.weblogic.imagetool.cachestore.CacheStoreFactory;
17-
import com.oracle.weblogic.imagetool.installer.FmwInstallerType;
1817
import com.oracle.weblogic.imagetool.installer.InstallerType;
1918
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
2019
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
20+
import com.oracle.weblogic.imagetool.util.DockerBuildCommand;
2121
import com.oracle.weblogic.imagetool.util.DockerfileOptions;
22+
import com.oracle.weblogic.imagetool.util.Utils;
2223
import picocli.CommandLine.Option;
2324

2425
public class WdtOptions {
@@ -33,44 +34,53 @@ public class WdtOptions {
3334
* @param tmpDir the tmp directory which is passed to docker as the build context directory
3435
* @throws IOException in case of error
3536
*/
36-
void handleWdtArgsIfRequired(DockerfileOptions dockerfileOptions, String tmpDir,
37-
FmwInstallerType installerType) throws IOException {
38-
logger.entering(tmpDir);
37+
void handleWdtArgs(DockerfileOptions dockerfileOptions, DockerBuildCommand cmdBuilder, String tmpDir)
38+
throws IOException {
3939

40-
if (wdtModelPath != null) {
41-
dockerfileOptions.setWdtEnabled();
42-
dockerfileOptions.setWdtModelOnly(wdtModelOnly);
40+
if (wdtModelPath == null) {
41+
return;
42+
}
4343

44-
List<String> modelList = addWdtFilesAsList(wdtModelPath, "model", tmpDir);
44+
logger.entering(tmpDir);
45+
String encryptionKey = Utils.getPasswordFromInputs(encryptionKeyStr, encryptionKeyFile, encryptionKeyEnv);
46+
if (encryptionKey != null) {
47+
dockerfileOptions.setWdtUseEncryption(true);
48+
cmdBuilder.addBuildArg("WDT_ENCRYPTION_KEY", encryptionKey, true);
49+
}
4550

46-
dockerfileOptions.setWdtModels(modelList);
51+
dockerfileOptions.setWdtEnabled();
52+
dockerfileOptions.setWdtModelOnly(wdtModelOnly);
4753

48-
dockerfileOptions.setWdtDomainType(wdtDomainType);
49-
dockerfileOptions.setRunRcu(runRcu);
54+
List<String> modelList = addWdtFilesAsList(wdtModelPath, "model", tmpDir);
5055

51-
if (wdtArchivePath != null) {
56+
dockerfileOptions.setWdtModels(modelList);
5257

53-
List<String> archiveList = addWdtFilesAsList(wdtArchivePath, "archive", tmpDir);
58+
dockerfileOptions.setWdtDomainType(wdtDomainType);
59+
dockerfileOptions.setRunRcu(runRcu);
5460

55-
dockerfileOptions.setWdtArchives(archiveList);
56-
}
57-
dockerfileOptions.setDomainHome(wdtDomainHome);
61+
if (wdtArchivePath != null) {
5862

59-
dockerfileOptions.setJavaOptions(wdtJavaOptions);
63+
List<String> archiveList = addWdtFilesAsList(wdtArchivePath, "archive", tmpDir);
6064

61-
if (wdtVariablesPath != null && Files.isRegularFile(wdtVariablesPath)) {
62-
String wdtVariableFilename = wdtVariablesPath.getFileName().toString();
63-
Files.copy(wdtVariablesPath, Paths.get(tmpDir, wdtVariableFilename));
64-
//Until WDT supports multiple variable files, take single file argument from CLI and convert to list
65-
dockerfileOptions.setWdtVariables(Collections.singletonList(wdtVariableFilename));
66-
}
65+
dockerfileOptions.setWdtArchives(archiveList);
66+
}
67+
dockerfileOptions.setDomainHome(wdtDomainHome);
6768

68-
dockerfileOptions.setWdtStrictValidation(wdtStrictValidation);
69+
dockerfileOptions.setJavaOptions(wdtJavaOptions);
6970

70-
CachedFile wdtInstaller = new CachedFile(InstallerType.WDT, wdtVersion);
71-
Path wdtfile = wdtInstaller.copyFile(cacheStore, tmpDir);
72-
dockerfileOptions.setWdtInstallerFilename(wdtfile.getFileName().toString());
71+
if (wdtVariablesPath != null && Files.isRegularFile(wdtVariablesPath)) {
72+
String wdtVariableFilename = wdtVariablesPath.getFileName().toString();
73+
Files.copy(wdtVariablesPath, Paths.get(tmpDir, wdtVariableFilename));
74+
//Until WDT supports multiple variable files, take single file argument from CLI and convert to list
75+
dockerfileOptions.setWdtVariables(Collections.singletonList(wdtVariableFilename));
7376
}
77+
78+
dockerfileOptions.setWdtStrictValidation(wdtStrictValidation);
79+
80+
CachedFile wdtInstaller = new CachedFile(InstallerType.WDT, wdtVersion);
81+
Path wdtfile = wdtInstaller.copyFile(cacheStore, tmpDir);
82+
dockerfileOptions.setWdtInstallerFilename(wdtfile.getFileName().toString());
83+
7484
logger.exiting();
7585
}
7686

@@ -161,4 +171,26 @@ private List<String> addWdtFilesAsList(Path fileArg, String type, String tmpDir)
161171
@SuppressWarnings("FieldCanBeLocal")
162172
private boolean wdtStrictValidation = false;
163173

174+
@Option(
175+
names = {"--wdtEncryptionKey"},
176+
interactive = true,
177+
arity = "0..1",
178+
paramLabel = "<passphrase>",
179+
description = "Enter the passphrase to decrypt the WDT model"
180+
)
181+
private String encryptionKeyStr;
182+
183+
@Option(
184+
names = {"--wdtEncryptionKeyEnv"},
185+
paramLabel = "<environment variable name>",
186+
description = "environment variable containing the passphrase to decrypt the WDT model"
187+
)
188+
private String encryptionKeyEnv;
189+
190+
@Option(
191+
names = {"--wdtEncryptionKeyFile"},
192+
paramLabel = "<passphrase file>",
193+
description = "path to file the passphrase to decrypt the WDT model"
194+
)
195+
private Path encryptionKeyFile;
164196
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Constants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public final class Constants {
2727
public static final String DEFAULT_JDK_VERSION = "8u202";
2828
public static final String DEFAULT_META_FILE = ".metadata";
2929
public static final String DELETE_ALL_FOR_SURE = "deleteAll4Sure";
30-
public static final String BUILD_ARG = "--build-arg";
3130
public static final String FILE_CACHE = "FILE";
3231
public static final String HTTP = "http";
3332
public static final String HTTPS = "https";

0 commit comments

Comments
 (0)