accessing vendor-table.json from code

This commit is contained in:
iadgovuser58 2024-05-31 14:47:52 -04:00 committed by chubtub
parent 38eb0e03a9
commit 14b13ee8a8
2 changed files with 106 additions and 22 deletions

View File

@ -2,14 +2,17 @@ package hirs.utils;
import com.eclipsesource.json.Json; import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject; import com.eclipsesource.json.JsonObject;
import hirs.utils.tpm.eventlog.uefi.UefiGuid;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import java.io.BufferedReader;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
@ -97,4 +100,32 @@ public final class JsonUtils {
return jsonObject; return jsonObject;
} }
public static JsonObject getJsonObject(final String jsonFilename, final Charset charset) {
// find the file and load it
JsonObject jsonObject = new JsonObject();
try {
InputStream jsonIs = UefiGuid.class
.getClassLoader().getResourceAsStream("vendor-table2.json");
jsonObject = Json.parse(new InputStreamReader(jsonIs,
charset)).asObject();
} catch (IOException e) {
throw new RuntimeException(e);
}
// if (Files.notExists(jsonPath)) {
// log.warn(String.format("No file found at %s.", jsonPath.toString()));
// } else {
// try {
// InputStream inputStream = new FileInputStream(jsonPath.toString());
// jsonObject = Json.parse(new InputStreamReader(inputStream,
// charset)).asObject();
// } catch (IOException ex) {
// // add log file thing here indication issue with JSON File
// jsonObject = new JsonObject();
// }
// }
return jsonObject;
}
} }

View File

@ -3,13 +3,20 @@ package hirs.utils.tpm.eventlog.uefi;
import com.eclipsesource.json.JsonObject; import com.eclipsesource.json.JsonObject;
import hirs.utils.HexUtils; import hirs.utils.HexUtils;
import hirs.utils.JsonUtils; import hirs.utils.JsonUtils;
import hirs.utils.rim.ReferenceManifestValidator;
import org.apache.commons.io.IOUtils;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -65,33 +72,79 @@ public class UefiGuid {
System.out.println("XXXX EMPTYYYYYYY, before trying to grab file from code"); System.out.println("XXXX EMPTYYYYYYY, before trying to grab file from code");
System.out.println("XXXX getClass: " + getClass()); uefiVendorRef = JsonUtils.getJsonObject("vendor-table2.json",
System.out.println("XXXX getClassResource: " + getClass().getResource("/vendor-table2.json")); StandardCharsets.UTF_8);
// System.out.println("XXXX getClass: " + getClass());
// System.out.println("XXXX getClassResource: " + getClass().getResource("/vendor-table2.json"));
//
//
// InputStream inpStr = UefiGuid.class
// .getClassLoader().getResourceAsStream("vendor-table2.json");
// System.out.println("XXXX InputStream: " + inpStr);
//
// Source inpSource = new StreamSource(
// ReferenceManifestValidator.class.getClassLoader()
// .getResourceAsStream("vendor-table2.json"));
// System.out.println("XXXX Source: " + inpSource);
//
// String path = this.getClass().getClassLoader().getResource("vendor-table2.json").toExternalForm();
// System.out.println("XXXX External Form: " + path);
try { // JsonReader reader = new JsonReader(new InputStreamReader(inpStr, "UTF-8"));
// private static final String EK_PUBLIC_PATH = "/tpm2/ek.pub";
// Source source = new StreamSource( // try {
// getClass().getClassLoader().getResourceAsStream("identity_transform.xslt")); // BufferedReader streamReader = new BufferedReader(new InputStreamReader(inpStr, "UTF-8"));
// StringBuilder responseStrBuilder = new StringBuilder();
//
Path fPath = Paths.get(getClass().getResource("/vendor-table2.json").toURI()); // String inputStr;
// URL url = ClassLoader.getSystemResources("vendor-table.json").nextElement(); // while ((inputStr = streamReader.readLine()) != null)
// URL url = ClassLoader.getSystemResources("vendor-table.json"); // responseStrBuilder.append(inputStr);
// Path fPath = Paths.get(url.toURI()); //
uefiVendorRef = JsonUtils.getSpecificJsonObject(fPath, // String test = responseStrBuilder.toString();
"VendorTable"); //// Gson gson = new Gson();
//
} // JsonObject testjj = parser.
// catch (IOException e) { //
// System.out.print("XXXX IOException"); // String temp = "hello";
//
//// JsonObject jsonObject = new JsonObject(responseStrBuilder.toString());
//
// } catch (UnsupportedEncodingException e) {
// throw new RuntimeException(e);
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// byte[] rawLogBytes = IOUtils.toByteArray(inpStr);
// Path fPath2 = Paths.get(inpStr.toURI());
// try {
//// private static final String EK_PUBLIC_PATH = "/tpm2/ek.pub";
//
//// Source source = new StreamSource(
//// getClass().getClassLoader().getResourceAsStream("identity_transform.xslt"));
//
//
// Path fPath = Paths.get(getClass().getResource("/vendor-table2.json").toURI());
//// URL url = ClassLoader.getSystemResources("vendor-table.json").nextElement();
//// URL url = ClassLoader.getSystemResources("vendor-table.json");
//// Path fPath = Paths.get(url.toURI());
// uefiVendorRef = JsonUtils.getSpecificJsonObject(fPath,
// "VendorTable");
//
// }
//// catch (IOException e) {
//// System.out.print("XXXX IOException");
//// throw new RuntimeException(e);
//// }
// catch (URISyntaxException e) {
// System.out.print("XXXX URISyntaxException");
// throw new RuntimeException(e); // throw new RuntimeException(e);
// } // }
catch (URISyntaxException e) {
System.out.print("XXXX URISyntaxException");
throw new RuntimeException(e);
}
} }
if(isVendorTableReferenceHandleEmpty()) { if(isVendorTableReferenceHandleEmpty()) {