How to read .pem file with a passphrase in Oracle Cloud Infrastructure?

1.1k Views Asked by At

I need to monitor Oracle Cloud Compute VM using REST API. I found the following lines of code for Signing with headers in Oracle documentation.

String privateKeyFilename = "/.oci/oci_api_key.pem";
PrivateKey privateKey = loadPrivateKey(privateKeyFilename);      
RequestSigner signer = new RequestSigner(apiKey, privateKey);

loadPrivateKey(privateKeyFilename) Method

private static PrivateKey loadPrivateKey(String privateKeyFilename) {
    System.out.println(SystemUtils.getUserHome().toString() + Paths.get(privateKeyFilename));
    try (InputStream privateKeyStream = Files
            .newInputStream(Paths.get(SystemUtils.getUserHome().toString() + privateKeyFilename))) {
        return PEM.readPrivateKey(privateKeyStream);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException("Invalid format for private key");
    } catch (IOException e) {
        throw new RuntimeException("Failed to load private key");
    }
}

Do these lines will cover reading the file with passphrase. Any inputs?

0

There are 0 best solutions below