dimanche 29 décembre 2019

Testing an API endpoint with Lambda + API Gateway

I'm trying to create and test an API endpoint using AWS Lambda and API Gateway. I can test my function successfully using Lambda Test, but when I try to test my endpoint it gives:

{ "message": "Internal server error" }

This is my handler class:

package com.amazonaws.lambda.gandhi.conversion.api;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;

import com.amazonaws.lambda.gandhi.conversion.api.Response.AuthClientCredentialResponse;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import com.amazonaws.lambda.gandhi.conversion.api.utils.ClientAuthPOJO;

public class AuthClientCredentialServiceHandler implements RequestHandler<ClientAuthPOJO, Object> {

    private AuthClientCredentialResponse authClientCredentialResponse;

    private static final SecureRandom RANDOM = new SecureRandom();
    public static int MAX_CLIENT_KEY = 10;
    public static int CLIENT_SECRET_LENGTH = 69;

    @Override
    public AuthClientCredentialResponse handleRequest(ClientAuthPOJO clientIdSecret, Context context) {
        String clientSecret;
        try {
            context.getLogger().log("Input: "
                    + clientIdSecret);
            String clientId = clientIdSecret.getClientId();
            clientSecret = generateClientSecretKey();
            Map<String, String> clientCredsMap = getClientCredentials();
            if (clientCredsMap.size() > MAX_CLIENT_KEY) {
                throw new RuntimeException(String.format("Max limit is %d, Please delete some keys", MAX_CLIENT_KEY));
            }
            clientCredsMap.forEach((k, v) -> {
                if (clientId.equals(k)) {
                    throw new RuntimeException("Client Already exists");
                }
            });
            storeClientCredentials(clientId, clientSecret);
            AuthClientCredentialResponse authClientCredentialResponse = AuthClientCredentialResponse.builder().success(
                    true).clientId(clientId).clientSecret(clientSecret).build();
            this.authClientCredentialResponse = authClientCredentialResponse;
        } catch (Exception e) {
            throw new RuntimeException(
                    "Failed to generate client secret: "
                            + e.getMessage());
        }
        return authClientCredentialResponse;
    }

    private String generateClientSecretKey() throws NoSuchAlgorithmException, InvalidKeySpecException {

        String clientSecret = RandomStringUtils.randomAlphanumeric(CLIENT_SECRET_LENGTH);
        System.out.printf("clientSecret: %s%n", clientSecret);
        return clientSecret;
    }

    private void storeClientCredentials(String clientId, String clientSecret) throws IOException {
        /*
         * TODO:
         * Some logic to store clientCredentials to a file or DB. Decide later.
         */
        System.out.println("temp ClientCredentials stored");
    }

    public Map<String, String> getClientCredentials() throws IOException {
        /*
         * TODO:
         * Some logic to fetch clientCredentials from file or DB. Decide later.
         */
        Map<String, String> clientCredMap = new HashMap<String, String>();
        clientCredMap.put("1", "secretKey1");
        clientCredMap.put("2", "secretKey2");
        clientCredMap.put("3", "secretKey3");
        clientCredMap.put("4", "secretKey4");
        return clientCredMap;
    }

}

My input class:

package com.amazonaws.lambda.gandhi.conversion.api.utils;

public class ClientAuthPOJO {
    String clientId;
    String clientSecret;

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public ClientAuthPOJO(String clientId, String clientSecret) {
        super();
        this.clientId = clientId;
        this.clientSecret = clientSecret;
    }

    public ClientAuthPOJO() {
    }

}

My test object in lambda: enter image description here

My test for endpoint in API Gateway: enter image description here

Can someone please help me figure out the problem in creating the function or API Gateway?

Aucun commentaire:

Enregistrer un commentaire