Phil_K
Enthusiast
Enthusiast

vROPS Rest API Java trusting certificate issue?

Jump to solution

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.

0 Kudos
1 Solution

Accepted Solutions
PluRav
Enthusiast
Enthusiast

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

0 Kudos
10 Replies
Phillip_K
Contributor
Contributor

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.

0 Kudos
PluRav
Enthusiast
Enthusiast

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.

0 Kudos
Phil_K
Enthusiast
Enthusiast

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.

0 Kudos
PluRav
Enthusiast
Enthusiast

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();

0 Kudos
Phil_K
Enthusiast
Enthusiast

Still get this error:

client.HttpClientFactory - No verification of server certificates

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

0 Kudos
PluRav
Enthusiast
Enthusiast

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

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

0 Kudos
Phil_K
Enthusiast
Enthusiast

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)

0 Kudos
PluRav
Enthusiast
Enthusiast

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();

    }

}

0 Kudos
Phil_K
Enthusiast
Enthusiast

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!

0 Kudos
PluRav
Enthusiast
Enthusiast
Great, glad i could be of help 🙂
0 Kudos