0
votes

*Note: I posted the sample POSTMAN request I am sending at the bottom of this post

I have an Azure functions app, that is an HTTP trigger in one java class, and it's end point is /api/devices/{id}, with the host ofcourse being http://localhost:7071 and the id being a serial number i.e. length 10 alphanumeric string

I run to clean & to package the azure functions locally.:

./gradlew clean
./gradlew build
./gradlew azureFunctionsPackage 

Then, I have tried running (per this article https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-maven-intellij):

./gradlew azureFunctionsRun -DenableDebug

I've also tried "right-clicking" the azureFunctionsRun task in the Gradle GUI/plugin (far right toolbar of IntelliJ where the other Gradle Tasks are listed i.e. build) and hit 'Debug' but whichever method of debugging I attempt, none of my breakpoints get hit.

I am hitting the endpoints with POSTMAN, and I know I'm using the correct headers and such because I tried this on the DEV and send the exact requests via POSTMAN and it works. I am getting a "500 internal server" request but it's still jumping into some of the code logic in the java code (inside the Azure Function App) (there are methods being hit because I have logs in my code):

11:19:23 PM: Executing task 'azureFunctionsRun'...

Starting Gradle Daemon...
Connected to the target VM, address: '127.0.0.1:58691', transport: 'socket'
Gradle Daemon started in 2 s 517 ms

What is the correct way to attach a process to an Azure Function and debug locally? I want to send my HTTP request via POSTMAN locally , and debug this Azure function! You can see even though I get a 500 response, the log.info("Source IP of the client is {}") is being hit inside the try {} catch block with the main logic, so the break points should be being hit! I included all the information you need below, if you want the host.json or local.settings.json , please let me know. I've been trying to debug now via intelliJ for a few weeks and I'm really getting annoyed I can't get this working.

Here is the azure function code below:

@FunctionName("edgegw_bootstrap_handler")
    public HttpResponseMessage httpFunctionHandler(
            @HttpTrigger(name = "req",
                    methods = {HttpMethod.GET},
                    authLevel = AuthorizationLevel.ANONYMOUS,
                    route = "devices/{id}")
                    HttpRequestMessage<Optional<String>> request,
            @BindingName("id") final String deviceId,
            final ExecutionContext context) {

        //TODO refactor: move the business logic to a separate class
        log.info("Processing a request for device id {}", deviceId);
        final String registrationKey = keyProvider.get(EDGE_REGISTRATION_KEY);

        try {
            String clientIp = request.getHeaders().get(HEADER_XFORWARDED_KEY).split("\\s*,\\s*")[0];
            log.info("Source IP of the client is {}", clientIp);

            // we don't want to validate client IP in DEV
            if (LoadLine.thisLoadLine() != LoadLine.Dev) {
                log.info("Validating client IP");
                Optional<EdgegwConfig> edgeGw = dataGateway.getEdgeGwConfigCrud().get(deviceId);
                String oamIp = edgeGw.get().getServerConfig().getEpsIpData().getEpsOam().getIp();

                if (!isIpValid(clientIp, oamIp)) {
                    log.warn(WARN("Received request with invalid source IP {}"), clientIp);
                    return request.createResponseBuilder(HttpStatus.BAD_REQUEST).build();
                }
                log.info("Received client IP is valid");
            }

            if (!request.getHeaders().getOrDefault(HEADER_AUTHORIZATION_KEY, UNAUTHORIZED).equals(HEADER_AUTHORIZATION_PREFIX + registrationKey)) {
                log.warn(WARN("Received unauthorized request from {}"), clientIp);
                return request.createResponseBuilder(HttpStatus.BAD_REQUEST).build();
            }

            String result = getSecrets(deviceId);
            return request.createResponseBuilder((result != null) ? HttpStatus.OK : HttpStatus.BAD_REQUEST).
                    header("Content-Type", "application/json; charset=UTF-8").
                    body(result).build();

        } catch (Exception e) {
            log.error(ERROR("Server exception occurred: {}"), e.getMessage());
            return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }

    } 

I know some of my breakpoints should be getting hit because I receive this response in the terminal, after running azureFunctionsRun and sending my HTTP request via postman:

[3/20/2020 4:19:42 AM] Executing HTTP request: {
[3/20/2020 4:19:42 AM]   "requestId": "94c12814-c7f7-4c1d-9680-0a9cbc2597ac",
[3/20/2020 4:19:42 AM]   "method": "GET",
[3/20/2020 4:19:42 AM]   "uri": "/api/devices/2M290702XY"
[3/20/2020 4:19:42 AM] }
[3/20/2020 4:19:42 AM] Executing 'Functions.edgegw_bootstrap_handler' (Reason='This function was programmatically called via the host APIs.', Id=7dbf720b-4c57-4448-8474-872cc1e77e5a)
[3/20/2020 4:19:43 AM] 2020-03-19 23:19:43 [pool-2-thread-1] INFO ::  Processing a request for device id 2M290702XY
[3/20/2020 4:19:43 AM] 2020-03-19 23:19:43 [pool-2-thread-1] INFO ::  Source IP of the client is 10.0.0.61
[3/20/2020 4:19:45 AM] 2020-03-19 23:19:45 [pool-2-thread-1] INFO ::  
[3/20/2020 4:19:45 AM]                                       
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@  @@        @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@@@@@        @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@  @@  @@    @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@  @@@@  @@  @@    @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@        @@        @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@        @@        @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@    @@  @@  @@@@  @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@    @@  @@  @@@@  @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@        @@  @  @  @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@        @@        @@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@@@@@@@@  @@@@@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[3/20/2020 4:19:45 AM] @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  Thank you for using jOOQ 3.11.11
[3/20/2020 4:19:45 AM]                                       
[3/20/2020 4:19:45 AM] 2020-03-19 23:19:45 [pool-2-thread-1] INFO ::  Found document with id 2M290702XY
[3/20/2020 4:19:45 AM] 2020-03-19 23:19:45 [pool-2-thread-1] ERROR::  |EDGE_GW_ERROR| Server exception occurred: The String is not a valid Base64-encoded string.
[3/20/2020 4:19:45 AM] Function "edgegw_bootstrap_handler" (Id: 7dbf720b-4c57-4448-8474-872cc1e77e5a) invoked by Java Worker
[3/20/2020 4:19:45 AM] Executed 'Functions.edgegw_bootstrap_handler' (Succeeded, Id=7dbf720b-4c57-4448-8474-872cc1e77e5a)
[3/20/2020 4:19:45 AM] Executed HTTP request: {
[3/20/2020 4:19:45 AM]   "requestId": "94c12814-c7f7-4c1d-9680-0a9cbc2597ac",
[3/20/2020 4:19:45 AM]   "method": "GET",
[3/20/2020 4:19:45 AM]   "uri": "/api/devices/2M290702XY",
[3/20/2020 4:19:45 AM]   "identities": [
[3/20/2020 4:19:45 AM]     {
[3/20/2020 4:19:45 AM]       "type": "WebJobsAuthLevel",
[3/20/2020 4:19:45 AM]       "level": "Admin"
[3/20/2020 4:19:45 AM]     }
[3/20/2020 4:19:45 AM]   ],
[3/20/2020 4:19:45 AM]   "status": 500,
[3/20/2020 4:19:45 AM]   "duration": 2653
[3/20/2020 4:19:45 AM] }

enter image description here

1

1 Answers

2
votes

Ok, I got this to work, what I had to do is add this to my local.settings.json:

"languageWorkers:java:arguments": "-Djava.net.preferIPv4Stack=true -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"

Then I went into IntelliJ on the top toolbar Run > Edit Configurations then I clicked the "+" sign and added a "Remote" , you can edit the name to something you click but all the other fields should be pre-filled out with the same port you listed in the local.settings.json.

Then, you can run your azure functions like normal (./gradlew azureFunctionsRun if your on gradle, or mvn azure-functions:run on maven, or going into the target or build folder and doing "func start")

then you click ctrl + alt + f5 and attach to the running Java process (it will be under "Java". It won't be under the "Read Only" processes list)