1

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?

karthik_varma_k
  • 353
  • 4
  • 26
  • 2
    Why aren't you just using the OCI Java SDK operations to call those APIs? Why are you trying to sign and make the request yourself manually? Using the OCI Java SDK you can provide the key/passphrase via the config file: https://docs.cloud.oracle.com/iaas/Content/API/Concepts/sdkconfig.htm , or at runtime: https://github.com/oracle/oci-java-sdk/blob/bab8f17502f344d3f50165ba951d3d33a1d6315c/bmc-examples/src/main/java/SimpleAuthenticationDetailsProviderExample.java – Joe Apr 02 '19 at 20:33
  • I tried with Java-SDK and it worked well. Now, I am trying with REST API as well just to check and whenever it is needed i can switch to REST, But getting the message 'NotAuthenticated' for POST call where as GET call is success. – karthik_varma_k Apr 03 '19 at 05:47
  • 1
    When would using REST API directly be needed for you? For any operations not in the SDK yet, you can still use the Java SDK to call those operations, it will handle the signing for you. Have you seen https://docs.cloud.oracle.com/iaas/Content/API/SDKDocs/javasdkconcepts.htm#rawrequests ? – Joe Apr 03 '19 at 16:56
  • Thanks for the inputs [Joe](https://stackoverflow.com/users/1102677/joe), will use SDK. – karthik_varma_k Apr 04 '19 at 09:53

0 Answers0