oracle-cloud-infrastructureoci-java-sdk

Can we use the same signer object to sign all the requests?


I need to make mutiple rest api calls for fetching instance, volume and vnic details. Can i reuse the same signer object created for signing the other calls?

Signer object method

   public RequestSigner getSigner(Properties properties, String pemFilePath, String apiKey) {
    InputStream privateKeyStream;
    PrivateKey privateKey = null;
    try {
        privateKeyStream = Files.newInputStream(Paths.get(pemFilePath));
        privateKey = PEM.readPrivateKey(privateKeyStream);
    } catch (InvalidKeySpecException e) {
        // throw new RuntimeException("Invalid format for private key");
        properties.setProperty(OracleCloudConstants.CUSTOM_DC_ERROR,
                FormatUtil.getString("am.webclient.oraclecloud.customdc.invalidformat"));
        AMLog.debug("OracleCloudDataCollector::CheckAuthentication()::Invalid format for private key::"
                + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        properties.setProperty(OracleCloudConstants.CUSTOM_DC_ERROR,
                FormatUtil.getString("am.webclient.oraclecloud.customdc.failedload"));
        AMLog.debug(
                "OracleCloudDataCollector::CheckAuthentication()::Failed to load private key::" + e.getMessage());  //No I18N
        e.printStackTrace();
        // throw new RuntimeException("Failed to load private key");
    }
    RequestSigner signer = null;
    if (privateKey != null) {
        signer = new RequestSigner(apiKey, privateKey);
    }
    return signer;
}

Solution

  • One signer object may be used to sign multiple requests. In fact, the SDK implementation does this too.

    It is not clear what version of the SDK you are using. In version 1.5.7 (the most recent at the time of writing), com.oracle.bmc.http.signing.RequestSigner (https://github.com/oracle/oci-java-sdk/blob/master/bmc-common/src/main/java/com/oracle/bmc/http/signing/RequestSigner.java#L16) is an interface which cannot be new’ed as per the snippet above.