VMware Cloud Community
Phil_K
Enthusiast
Enthusiast
Jump to solution

vROPS Rest API Java trusting certificate issue?

PluRav helped me out on my other discussion, but I decided to start a new discussion centered around server certificates. When I run this code:

Client client = ClientConfig.builder().serverUrl("https://{ip}/suite-api")

    .basicAuth("user", "pass")

    .useJson()

    .build()

    .newClient();


AdapterInstancesInfoDto adapters = client.adapterInstancesClient().list();

I get two error messages:

client.HttpClientFactory - No verification of server certificates

client.RestTemplate - Get "url" resulted in 401 unauthorized; invoking handler error

I suspect I need to tell the code to trust the self-signed certificate but I'm unsure how to do that.

Any help would be appreciated.

Reply
0 Kudos
1 Solution

Accepted Solutions
PluRav
Enthusiast
Enthusiast
Jump to solution

Give this a try, do you get any output now?

public static void main(String[] args) {

    try {

        Client client = ClientConfig

            .builder()

            .serverUrl("https://123.45.67.89/suite-api")

            .basicAuth("admin", "password")

            .verify("false")

            .useJson()

            .build()

            .newClient();

           

        //retrieve list of all adapter instances

        AdapterInstancesInfoDto adapterInstances = client.adapterInstancesClient().list();

           

        //get actual set of adapters

        Set<AdapterInstanceInfoDto> adapters = adapterInstances.getAdapterInstancesInfoDto();

           

        //iterate the set

        for (AdapterInstanceInfoDto adapter : adapters) {

            //print to console

            System.out.println(ReflectionToStringBuilder.reflectionToString(adapter, ToStringStyle.MULTI_LINE_STYLE));

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

View solution in original post

Reply
0 Kudos
10 Replies
Phillip_K
Contributor
Contributor
Jump to solution

From the documentation is seems I need to use HttpClientFactory.SuiteApiTrustManager package.

I just don't know how to implement the X509Certificate method here. Any help would be appreciated.

Reply
0 Kudos
PluRav
Enthusiast
Enthusiast
Jump to solution

Heya, are you running exactly this code?

If yes,i'd start from checking if a user "user" exists in your vrops Smiley Wink

Also, from what i remember the first error - "No verification..." is actually just a warning.

Reply
0 Kudos
Phil_K
Enthusiast
Enthusiast
Jump to solution

Of course I stripped out the credentials for this post. But yes, when I input actual valid credentials into the code that is the error I get.

Reply
0 Kudos
PluRav
Enthusiast
Enthusiast
Jump to solution

Sorry, just wanted to be sure.

Do you need to validate the certificate? You might be able to skip it.

To do so:

Client client = ClientConfig.builder().serverUrl("https://{ip}/suite-api")

    .basicAuth("user", "pass")

   .verify("false")

    .useJson()

    .build()

    .newClient();

Reply
0 Kudos
Phil_K
Enthusiast
Enthusiast
Jump to solution

Still get this error:

client.HttpClientFactory - No verification of server certificates

Then I get build succeeds. But there is no output in the console.

Reply
0 Kudos
PluRav
Enthusiast
Enthusiast
Jump to solution

Hmm, strange. I don't remember having similar problems.

Can you add the whole code you're running as well as it's output?

Reply
0 Kudos
Phil_K
Enthusiast
Enthusiast
Jump to solution

package restapi;

import com.vmware.ops.api.client.Client;

import com.vmware.ops.api.client.Client.ClientConfig;

import com.vmware.ops.api.model.adapter.AdapterInstanceInfoDto.AdapaterInstanceInfoDto;

public class Restapi{

public static void main(string[] args) {

Client client = ClientConfig.builder().serverUrl("https://{ip}/suite-api")

    .basicAuth("user", "pass")

    .verify("false")

    .useJson()

    .build()

    .newClient();


AdapterInstancesInfoDto adapters = client.adapterInstancesClient().list();

}

};

output:

run:

0 [main] DEBUG com.vmware.ops.api.client.HttpClientFactory - No verification of server certificates

Build Successful: (Run Time 6 Seconds)

Reply
0 Kudos
PluRav
Enthusiast
Enthusiast
Jump to solution

Give this a try, do you get any output now?

public static void main(String[] args) {

    try {

        Client client = ClientConfig

            .builder()

            .serverUrl("https://123.45.67.89/suite-api")

            .basicAuth("admin", "password")

            .verify("false")

            .useJson()

            .build()

            .newClient();

           

        //retrieve list of all adapter instances

        AdapterInstancesInfoDto adapterInstances = client.adapterInstancesClient().list();

           

        //get actual set of adapters

        Set<AdapterInstanceInfoDto> adapters = adapterInstances.getAdapterInstancesInfoDto();

           

        //iterate the set

        for (AdapterInstanceInfoDto adapter : adapters) {

            //print to console

            System.out.println(ReflectionToStringBuilder.reflectionToString(adapter, ToStringStyle.MULTI_LINE_STYLE));

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

Reply
0 Kudos
Phil_K
Enthusiast
Enthusiast
Jump to solution

Thank You. That worked. Can't believe I didn't realize I needed an output statement. It seems I have a lot of learning left to do!

Reply
0 Kudos
PluRav
Enthusiast
Enthusiast
Jump to solution

Great, glad i could be of help 🙂
Reply
0 Kudos