Skip to content

Commit 1c860b6

Browse files
ddsharpejshum2479
authored andcommitted
cache addPatch fixes (#14)
* WLS install should ignore prerequisites, similar to current production WLS Dockerfiles on Github * increase line length max to 120 * cache addPatch no longer requires username and password, but now optional
1 parent 36dd891 commit 1c860b6

File tree

5 files changed

+84
-43
lines changed

5 files changed

+84
-43
lines changed

build-tools/src/main/resources/weblogic-image-tool/checkstyle/customized_google_checks.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<property name="allowNonPrintableEscapes" value="true"/>
4343
</module>
4444
<module name="LineLength">
45-
<property name="max" value="100"/>
45+
<property name="max" value="120"/>
4646
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
4747
</module>
4848
<module name="AvoidStarImport"/>

imagetool/src/main/java/com/oracle/weblogic/imagetool/api/model/AbstractFile.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.oracle.weblogic.imagetool.api.model;
66

77
import com.oracle.weblogic.imagetool.api.FileResolver;
8+
import com.oracle.weblogic.imagetool.api.meta.CacheStore;
89

910
import java.nio.file.Files;
1011
import java.nio.file.Paths;
@@ -15,22 +16,23 @@
1516
*/
1617
public abstract class AbstractFile implements FileResolver {
1718

18-
private final Logger logger = Logger.getLogger(AbstractFile.class.getName());
19-
20-
protected String key;
19+
private String key;
2120
protected CachePolicy cachePolicy;
2221
protected String userId;
2322
protected String password;
2423

25-
public AbstractFile(String key, CachePolicy cachePolicy, String userId, String password) {
26-
this.key = key;
24+
public AbstractFile(String id, String version, CachePolicy cachePolicy, String userId, String password) {
25+
this.key = generateKey(id, version);
2726
this.cachePolicy = cachePolicy;
2827
this.userId = userId;
2928
this.password = password;
30-
logger.finer("Exiting AbstractFile construction: key=" + key);
3129
}
3230

33-
protected boolean isFileOnDisk(String filePath) {
31+
public static String generateKey(String id, String version) {
32+
return id + CacheStore.CACHE_KEY_SEPARATOR + version;
33+
}
34+
35+
public static boolean isFileOnDisk(String filePath) {
3436
return filePath != null && Files.isRegularFile(Paths.get(filePath));
3537
}
3638

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/AddPatchEntry.java

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.oracle.weblogic.imagetool.cli.cache;
66

77
import com.oracle.weblogic.imagetool.api.meta.CacheStore;
8+
import com.oracle.weblogic.imagetool.api.model.AbstractFile;
89
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
910
import com.oracle.weblogic.imagetool.api.model.WLSInstallerType;
1011
import com.oracle.weblogic.imagetool.util.ARUUtil;
@@ -21,6 +22,7 @@
2122
import java.io.IOException;
2223
import java.nio.file.Files;
2324
import java.nio.file.Path;
25+
import java.util.logging.Logger;
2426

2527
@Command(
2628
name = "addPatch",
@@ -29,7 +31,7 @@
2931
)
3032
public class AddPatchEntry extends CacheOperation {
3133

32-
String password;
34+
private final Logger logger = Logger.getLogger(AddPatchEntry.class.getName());
3335

3436
public AddPatchEntry() {
3537
}
@@ -40,40 +42,77 @@ public AddPatchEntry(boolean isCLIMode) {
4042

4143
@Override
4244
public CommandResponse call() throws Exception {
43-
password = handlePasswordOptions();
45+
String password = handlePasswordOptions();
46+
4447
if (patchId != null && !patchId.isEmpty()
45-
&& userId != null && !userId.isEmpty()
46-
&& password != null && !password.isEmpty()
47-
&& location != null && Files.exists(location) && Files.isRegularFile(location)) {
48+
&& location != null && Files.exists(location)
49+
&& Files.isRegularFile(location)) {
50+
4851
String patchNumber;
4952
if (patchId.matches(Constants.PATCH_ID_REGEX)) {
5053
patchNumber = patchId.substring(1);
5154
} else {
5255
return new CommandResponse(-1, "Invalid patch id format: " + patchId);
5356
}
54-
SearchResult result = ARUUtil.getPatchDetail(type.toString(), version, patchNumber, userId, password);
55-
if (result.isSuccess()) {
56-
Document document = result.getResults();
57-
String patchDigest = XPathUtil.applyXPathReturnString(document, "string"
58-
+ "(/results/patch[1]/files/file/digest[@type='SHA-256']/text())");
59-
String localDigest = DigestUtils.sha256Hex(new FileInputStream(location.toFile()));
60-
61-
if (localDigest.equalsIgnoreCase(patchDigest)) {
62-
String releaseNumber = XPathUtil.applyXPathReturnString(document,
63-
"string(/results/patch[1]/release/@id)");
64-
String key = patchNumber + CacheStore.CACHE_KEY_SEPARATOR + releaseNumber;
65-
cacheStore.addToCache(key, location.toAbsolutePath().toString());
66-
return new CommandResponse(0, String.format(
67-
"Added Patch entry %s=%s for %s", key, location.toAbsolutePath(), type));
68-
} else {
69-
return new CommandResponse(-1, String.format(
70-
"Local file sha-256 digest %s != patch digest %s", localDigest, patchDigest));
71-
}
57+
if (userId != null && !userId.isEmpty() && password != null && !password.isEmpty() ) {
58+
return validateAndAddToCache(patchNumber, password);
59+
} else {
60+
logger.info("Skipping patch validation, username and password were not provided");
61+
return addToCache(patchNumber);
7262
}
73-
} else {
74-
return new CommandResponse(-1, "Invalid arguments");
7563
}
76-
return null;
64+
65+
String msg = "Invalid arguments";
66+
if (patchId == null || patchId.isEmpty()) {
67+
msg += " : --patchId was not supplied";
68+
}
69+
if (location == null || !Files.exists(location) || !Files.isRegularFile(location)) {
70+
msg += " : --path is invalid";
71+
}
72+
73+
return new CommandResponse(-1, msg);
74+
}
75+
76+
/**
77+
* Validate local patch file's digest against the digest stored in ARU.
78+
* @param patchNumber the ARU patch number without the 'p'
79+
* @param password the password to be used for the ARU query (Oracle Support credential)
80+
* @return true if the local file digest matches the digest stored in Oracle ARU
81+
* @throws Exception if the ARU call to get patch details failed
82+
*/
83+
private CommandResponse validateAndAddToCache(String patchNumber, String password) throws Exception {
84+
boolean matches = false;
85+
86+
SearchResult searchResult = ARUUtil.getPatchDetail(type.toString(), version, patchNumber, userId, password);
87+
88+
if (searchResult.isSuccess()) {
89+
Document document = searchResult.getResults();
90+
String patchDigest = XPathUtil.applyXPathReturnString(document, "string"
91+
+ "(/results/patch[1]/files/file/digest[@type='SHA-256']/text())");
92+
String localDigest = DigestUtils.sha256Hex(new FileInputStream(location.toFile()));
93+
94+
if (localDigest.equalsIgnoreCase(patchDigest)) {
95+
return addToCache(patchNumber);
96+
} else {
97+
return new CommandResponse(-1, String.format(
98+
"Local file sha-256 digest %s != patch digest %s", localDigest, patchDigest));
99+
}
100+
}
101+
102+
return new CommandResponse(-1, String.format("Unable to find patchId %s on Oracle Support", patchId));
103+
}
104+
105+
/**
106+
* Add patch to the cache.
107+
* @param patchNumber the patchId (minus the 'p') of the patch to add
108+
* @return CLI command response
109+
*/
110+
private CommandResponse addToCache(String patchNumber) {
111+
String key = AbstractFile.generateKey(patchNumber, version);
112+
cacheStore.addToCache(key, location.toAbsolutePath().toString());
113+
String msg = String.format("Added Patch entry %s=%s for %s", key, location.toAbsolutePath(), type);
114+
logger.info(msg);
115+
return new CommandResponse(0, msg);
77116
}
78117

79118
/**
@@ -119,7 +158,6 @@ private String handlePasswordOptions() throws IOException {
119158
@Option(
120159
names = {"--user"},
121160
paramLabel = "<support email>",
122-
required = true,
123161
description = "Oracle Support email id"
124162
)
125163
private String userId;

imagetool/src/main/java/com/oracle/weblogic/imagetool/impl/InstallerFile.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class InstallerFile extends AbstractFile {
2626
private final Logger logger = Logger.getLogger(InstallerFile.class.getName());
2727

2828
public InstallerFile(CachePolicy cachePolicy, InstallerType type, String version, String userId, String password) {
29-
super(type.toString() + CacheStore.CACHE_KEY_SEPARATOR + version, cachePolicy, userId, password);
29+
super(type.toString(), version, cachePolicy, userId, password);
3030
this.type = type;
3131
}
3232

@@ -36,11 +36,11 @@ public InstallerFile(CachePolicy cachePolicy, InstallerType type, String version
3636
@Override
3737
public String resolve(CacheStore cacheStore) throws Exception {
3838
// check entry exists in cache
39-
String filePath = cacheStore.getValueFromCache(key);
39+
String filePath = cacheStore.getValueFromCache(getKey());
4040
switch (cachePolicy) {
4141
case ALWAYS:
4242
if (!isFileOnDisk(filePath)) {
43-
throw new Exception("CachePolicy prohibits download. Please add cache entry for key: " + key);
43+
throw new Exception("CachePolicy prohibits download. Please add cache entry for key: " + getKey());
4444
}
4545
break;
4646
case FIRST:
@@ -56,6 +56,7 @@ public String resolve(CacheStore cacheStore) throws Exception {
5656
}
5757

5858
private String downloadInstaller(CacheStore cacheStore) throws IOException {
59+
String key = getKey();
5960
String urlPath = cacheStore.getValueFromCache(key + "_url");
6061
if (urlPath != null) {
6162
String fileName = new URL(urlPath).getPath();

imagetool/src/main/java/com/oracle/weblogic/imagetool/impl/PatchFile.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class PatchFile extends AbstractFile {
2323
private final Logger logger = Logger.getLogger(PatchFile.class.getName());
2424

2525
public PatchFile(CachePolicy cachePolicy, String category, String version, String patchId, String userId, String password) {
26-
super(null, cachePolicy, userId, password);
26+
super(patchId, version, cachePolicy, userId, password);
2727
this.category = category;
2828
this.version = version;
2929
this.patchId = patchId;
@@ -43,8 +43,7 @@ public String resolve(CacheStore cacheStore) throws Exception {
4343
}
4444
}
4545
}
46-
key = patchId + CacheStore.CACHE_KEY_SEPARATOR + version;
47-
String filePath = cacheStore.getValueFromCache(key);
46+
String filePath = cacheStore.getValueFromCache(getKey());
4847
boolean fileExists = isFileOnDisk(filePath);
4948
switch (cachePolicy) {
5049
case ALWAYS:
@@ -68,9 +67,10 @@ private String downloadPatch(CacheStore cacheStore) throws IOException {
6867
// try downloading it
6968
List<String> patches = ARUUtil.getPatchesFor(category, version, Collections.singletonList(patchId),
7069
userId, password, cacheStore.getCacheDir());
70+
String patchKey = getKey();
7171
// we ignore the release number coming from ARUUtil patchId_releaseNumber=/path/to/patch.zip
72-
patches.forEach(x -> cacheStore.addToCache(key, x.substring(x.indexOf('=') + 1)));
73-
String filePath = cacheStore.getValueFromCache(key);
72+
patches.forEach(x -> cacheStore.addToCache(patchKey, x.substring(x.indexOf('=') + 1)));
73+
String filePath = cacheStore.getValueFromCache(patchKey);
7474
if (!isFileOnDisk(filePath)) {
7575
throw new IOException(String.format("Failed to find patch %s for product category %s, version %s",
7676
patchId, category, version));

0 commit comments

Comments
 (0)