55package com .oracle .weblogic .imagetool .cli .cache ;
66
77import com .oracle .weblogic .imagetool .api .meta .CacheStore ;
8- import com .oracle .weblogic .imagetool .api .model .AbstractFile ;
98import com .oracle .weblogic .imagetool .api .model .CommandResponse ;
109import com .oracle .weblogic .imagetool .api .model .WLSInstallerType ;
11- import com .oracle .weblogic .imagetool .util .ARUUtil ;
1210import com .oracle .weblogic .imagetool .util .Constants ;
13- import com .oracle .weblogic .imagetool .util .SearchResult ;
1411import com .oracle .weblogic .imagetool .util .Utils ;
15- import com .oracle .weblogic .imagetool .util .XPathUtil ;
16- import org .apache .commons .codec .digest .DigestUtils ;
17- import org .w3c .dom .Document ;
18- import picocli .CommandLine .Command ;
19- import picocli .CommandLine .Option ;
2012
21- import java .io .FileInputStream ;
22- import java .io .IOException ;
2313import java .nio .file .Files ;
2414import java .nio .file .Path ;
15+ import java .util .ArrayList ;
16+ import java .util .List ;
2517import java .util .logging .Logger ;
2618
19+ import picocli .CommandLine .Command ;
20+ import picocli .CommandLine .Option ;
21+
2722@ Command (
2823 name = "addPatch" ,
2924 description = "Add cache entry for wls|fmw patch or psu" ,
@@ -42,24 +37,17 @@ public AddPatchEntry(boolean isCLIMode) {
4237
4338 @ Override
4439 public CommandResponse call () throws Exception {
45- String password = handlePasswordOptions ();
4640
4741 if (patchId != null && !patchId .isEmpty ()
4842 && location != null && Files .exists (location )
4943 && Files .isRegularFile (location )) {
5044
51- String patchNumber ;
52- if (patchId .matches (Constants .PATCH_ID_REGEX )) {
53- patchNumber = patchId .substring (1 );
54- } else {
55- return new CommandResponse (-1 , "Invalid patch id format: " + patchId );
56- }
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 );
45+ List <String > patches = new ArrayList <>();
46+ patches .add (patchId );
47+ if (!Utils .validatePatchIds (patches , true )) {
48+ return new CommandResponse (-1 , "Patch ID validation failed" );
6249 }
50+ return addToCache (patchId );
6351 }
6452
6553 String msg = "Invalid arguments" ;
@@ -81,25 +69,7 @@ public CommandResponse call() throws Exception {
8169 * @throws Exception if the ARU call to get patch details failed
8270 */
8371 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 ));
72+ return addToCache (patchNumber );
10373 }
10474
10575 /**
@@ -108,33 +78,31 @@ private CommandResponse validateAndAddToCache(String patchNumber, String passwor
10878 * @return CLI command response
10979 */
11080 private CommandResponse addToCache (String patchNumber ) {
111- String key = AbstractFile .generateKey (patchNumber , version );
81+ logger .info ("adding key " + patchNumber );
82+ String key = patchNumber ;
11283
11384 // Check if it is an Opatch patch
11485 String opatchNumber = Utils .getOpatchVersionFromZip (location .toAbsolutePath ().toString ());
11586 if (opatchNumber != null ) {
11687 int lastSeparator = key .lastIndexOf (CacheStore .CACHE_KEY_SEPARATOR );
11788 key = key .substring (0 ,lastSeparator ) + CacheStore .CACHE_KEY_SEPARATOR + Constants .OPATCH_PATCH_TYPE ;
11889 }
90+ logger .info ("adding key " + key );
91+ if (cacheStore .getValueFromCache (key ) != null ) {
92+ String error = String .format ("Cache key %s already exists, remove it first" , key );
93+ logger .severe (error );
94+ throw new IllegalArgumentException (error );
95+ }
11996 cacheStore .addToCache (key , location .toAbsolutePath ().toString ());
12097 String msg = String .format ("Added Patch entry %s=%s for %s" , key , location .toAbsolutePath (), type );
12198 logger .info (msg );
12299 return new CommandResponse (0 , msg );
123100 }
124101
125- /**
126- * Determines the support password by parsing the possible three input options
127- *
128- * @return String form of password
129- * @throws IOException in case of error
130- */
131- private String handlePasswordOptions () throws IOException {
132- return Utils .getPasswordFromInputs (passwordStr , passwordFile , passwordEnv );
133- }
134102
135103 @ Option (
136104 names = {"--patchId" },
137- description = "Patch number. Ex: p28186730 " ,
105+ description = "Patch number. Ex: 28186730 " ,
138106 required = true
139107 )
140108 private String patchId ;
@@ -147,13 +115,6 @@ private String handlePasswordOptions() throws IOException {
147115 )
148116 private WLSInstallerType type ;
149117
150- @ Option (
151- names = {"--version" },
152- description = "version of mw this patch is for. Ex: 12.2.1.3.0" ,
153- required = true ,
154- defaultValue = Constants .DEFAULT_WLS_VERSION
155- )
156- private String version ;
157118
158119 @ Option (
159120 names = {"--path" },
@@ -162,31 +123,4 @@ private String handlePasswordOptions() throws IOException {
162123 )
163124 private Path location ;
164125
165- @ Option (
166- names = {"--user" },
167- paramLabel = "<support email>" ,
168- description = "Oracle Support email id"
169- )
170- private String userId ;
171-
172- @ Option (
173- names = {"--password" },
174- paramLabel = "<password for support user id>" ,
175- description = "Password for support userId"
176- )
177- String passwordStr ;
178-
179- @ Option (
180- names = {"--passwordEnv" },
181- paramLabel = "<environment variable>" ,
182- description = "environment variable containing the support password"
183- )
184- String passwordEnv ;
185-
186- @ Option (
187- names = {"--passwordFile" },
188- paramLabel = "<password file>" ,
189- description = "path to file containing just the password"
190- )
191- Path passwordFile ;
192126}
0 commit comments