Home > Net >  SSL connect certificate trouble with Rest Assured
SSL connect certificate trouble with Rest Assured

Time:07-02

there is a problem, I have been fighting for several days - how to establish an SSL connection via Rest Assured? Here are my attempts, but with this code:

public class ApiTests {
    @BeforeAll
    static void setUp() {
        RestAssured.baseURI = "my host";
    }

    @Test
    void createUserTest() throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        String keyStorePass = "my pass";
        try (FileInputStream fis = new FileInputStream("D:\\Idea_projects\\TestApi\\src\\test\\resources\\my_cert.p12")) {
            keyStore.load(fis, keyStorePass.toCharArray());
        }

        given()
                .keyStore(keyStore)
                .trustStore("C:\\Program Files\\Java\\jdk-11.0.13\\lib\\security\\cacerts", "changeit")
                .contentType(JSON)
                .when()
                .post("users")
                .then()
                .statusCode(200)
                .body("name", is("Valentin"))
                .body("job", is("qa"));
    }
}

Throws exception:

No signature of method: io.restassured.config.SSLConfig.keyStore() is applicable for argument types: (java.security.KeyStore) values: [java.security.KeyStore@504497fa]
Possible solutions: keyStore(java.lang.String), keyStore(java.io.File, java.lang.String), keyStore(java.lang.String, java.lang.String), getKeyStore(), trustStore(java.security.KeyStore)
groovy.lang.MissingMethodException: No signature of method: io.restassured.config.SSLConfig.keyStore() is applicable for argument types: (java.security.KeyStore) values: [java.security.KeyStore@504497fa]
Possible solutions: keyStore(java.lang.String), keyStore(java.io.File, java.lang.String), keyStore(java.lang.String, java.lang.String), getKeyStore(), trustStore(java.security.KeyStore)

I tried to use relaxedHTTPSValidation(), but it says:

Received fatal alert: bad_certificate
javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate

I.e. as I understand it, I still need to add a certificate somehow, but how? I can't imagine already. If someone can tell me what the problem is or what is wrong in my code, please help :(

CodePudding user response:

If suddenly someone is faced with a similar problem, then here is the solution. You must have 2 certificates: server and client. Then you put the server one in the cacerts repository in Java with the command:

keytool -importcert -storetype PKCS12 -keystore trustStore.p12 -storepass password -alias mpzCA -file root.crt -noprompt

Then, in the code, do as below:

public class DealerProtocolTests {
    
        @Test
        void sendDealerRequest() throws Exception {
            RestAssured.config = RestAssured.config().sslConfig(
                    new SSLConfig()
                            .trustStore("src/test/resources/certificate/SERVER.p12", "password")
                            .keyStore("src/test/resources/certificate/CLIENT.p12", "password"));
            
            Response response = null;
             response =
                    given()
                            .config(config)
                            .spec(xmlSpec)
                            .basePath("/xmlInteface")
                            .body("")
                            .when()
                            .get()
                            .then()
                            .statusCode(200)
                            .log().all()
                            .extract().response();
    
            System.out.println(response.prettyPrint());
        }
    }
  • Related