My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com









Tuesday, February 18, 2020

Pulse Secure Duo Authentication using phone call for VPN connection if you have multiple phones registered

Many of my corporate readers have asked me to write on this topic. And, when it actually happened with me as well, I was prompted to write. So, my readers this is on your demand ;)

Sometimes, you have more than one phone numbers registered in your Duo account. And you need to authenticate for your VPN connection.

Normally, you can do following:

  1. phone
  2. push to Duo application on that mobile
  3. VPN key copied from application, etc.
In phone case, you get a call on the phone registered. It will then ask you to press any key, in case you expect this call. If you press any number, the call gets authenticated.

But, if you have say 3 phones registered then what to do?

The solution is pretty simple!
Use - phone or phone2 or phone3 command etc.



Command: phone 
It shall call that phone. For first phone, default phone would work. For second phone, you need to use phone2 command. And so on.

Cheers ;)





Friday, January 31, 2020

Build using Maven - MVN and POM.xml

In order to build a Maven project you can use one of the following:

  • mvn clean install
  • mvn clean package etc.

This would build/compile and run the tests as well.

However, if you want to run the tests only then what would you do?

Sound similar? Yeah! we all often land up in this situation as an application developer. And, the solution is simple.


You can use mvn test to run unit test in Maven. Few examples :
# Run all the unit test classes.
$ mvn test

# Run a single test class.
$ mvn -Dtest=TestApp1 test

# Run multiple test classes.
$ mvn -Dtest=TestApp1,TestApp2 test

# Run a single test method from a test class.
$ mvn -Dtest=TestApp1#methodname test

# Run all test methods that match pattern 'testHello*' from a test class.
$ mvn -Dtest=TestApp1#testHello* test

# Run all test methods match pattern 'testHello*' and 'testMagic*' from a test class.
$ mvn -Dtest=TestApp1#testHello*+testMagic* test

The default maven-surefire-plugin is outdated, make sure update to the latest to support new features, like pattern matching or run a single test method, and etc.
pom.xml
 <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>

        </plugins>
    </build>

Thursday, January 30, 2020

Mapping template, Velocity Language & JsonPath: Guide to Api Gateway mapping templates mechanism

Amazon’s API Gateway provides the facilities to map an incoming request’s payload to match the required format of an integration backend. It uses the concept of “models” and “mapping templates” to specify the mapping between the client payload and the server payload.

The API Gateway mapping templates are used to transform an incoming payload into a different format. API Gateway allows you to define input mapping templates, for mapping the incoming request from a client to a server format, and output mapping templates, for mapping the outgoing response from the server to a client format.

Let us all remember that the mappings are defined using the Velocity Template Language combined with JSONPath expressions.

Velocity is a Java-based templating engine that uses a template language to reference objects in Java code. Velocity can be used to generate web XML, HTML or SQL from templates, for example. Here, we will use it to define the mapping between an input and output model.
A Velocity Template Language (VTL) template is composed of statements which begin with the # character and a followed by a directive. Also, it has references, which begin with the $ character. More generally, references begin with $ and are used to get or set something. Directives begin with # and are used to do something.
We can use the statement above to define an HTML template that sets a reference to a value, and retrieves that reference.

  
  #set( $foo = "Velocity" )
  Hello $foo World!
  
For more on VTL, consult the user guide.

In API Gateway mapping templates, the input to your Velocity template is defined by the reference variable $input. To access particular portions of the input JSON document within the template, use JSONPath.
JSONPath provides an XPath-like method of referring to a JSON structure. IN JSONPath, the abstract name $ refers to the root-level object. From this root-level object, you can use dot-notation to select more restrictive portions of the document. 
For example, to reference the playerId of the player model, you would use the following JSONPath expression.
$.playerId
or, you can use a bracket-notation to perform the same selection.
#['playerId']
More complex selections can be done using * for a wildcard, @ to refer to the current node in the document, array indexing, or recursive descent.

Now, we can put together a mapping template for our Player model. This template will just copy the input to the output with new names for field properties. In particular, it will convert an input document of
{
    "id": "AJamal",
    "alias": "Aha Jamal",
    "displayName": "Anwar Jamal Faiz",
    "profilePhotoUrl": "https://api.example.com/player/AJamal/avatar.png"
}
to an output document of
{
    "id": "AJamal",    "alias": "Aha Jamal",    "name": "Anwar Jamal Faiz",    "photo": "https://api.example.com/player/AJamal/avatar.png"}
The mapping to perform the above transformation is specified below, with some inline VTL comments added.
## Set the variable inputRoot to the root JSON document
#set($inputRoot = $input.path('$')) {

    ## Assign output properties to input properties
    "id": "$inputRoot.playerId",
    "alias": "$inputRoot.alias",
    "name": "$inputRoot.displayName",
    "photo": "$inputRoot.profilePhotoUrl"
}
Enjoy!!

Json schema validation for Api Gateway request

Amazon’s API Gateway provides the facilities to map an incoming request’s payload to match the required format of an integration backend. It uses the concept of “models” and “mapping templates” to specify the mapping between the client payload and the server payload.
A model defines the structure of the incoming payload using JSON Schema. The model is an optional, but not required, piece of API Gateway. By providing a model, you make it easier to define the upcoming mapping template that actually does the transformation between the client and server.
For example, we can define a incoming Player model using the following JSON payload.
{
    "id": "AJamal",
    "alias": "Aha Jamal",
    "displayName": "Anwar jamal Faiz",
    "photoUrl": "https://api.example.com/player/AJamal/avatar.png"
}


JSON Schema is a vocabulary allowing you validate JSON documents. The particular JSON payload being validated is called an instance, and the document describing what a valid payload looks like is called the schema.
JSON Schema is used to describe a JSON document, so it helps to understand what, exactly, a JSON document is composed of these primitives:
  • objects: {"field1": "value1", "field2": "value2"}
  • arrays: ["first", "second", "third"]
  • numbers: 42 or 3.1415926
  • strings: "Lorem ipsum dolor sit amet"
  • booleans: true or false
  • null: null
The responsibility of JSON Schema is to describe a JSON document built from the these primitives.
JSON Schema is itself specified using JSON with predefined semantics for keywords and values. Going back to our Player example, we can specify the JSON Schema for the incoming document as follows:
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "id": { "type": "string" },
        "alias": { "type": "string" },
        "displayName": { "type": "string" },
        "photoUrl": { "type": "string" }
    }
}
This JSON Schema follows the Draft 4 specification and simply declares the types expected for each field in the request. 

For more details consult this guide.

Sending Request Body can be other than JSON in API Gateway in AWS

Can you accept a form request e.g. the classic application/x-www-form-urlencoded body content type. Answer is yes.

You could even use application/xml or any other format from your request but you have to tweak some settings.

Default mapping template in response:
{
    "body" : $input.json('$')
}

Now, if you send a typical application/JSON body…

{
    "foo": "bar"
}

you’ll get is a JSON object under the body key:

{
    "body" : {
       "foo": "bar"
    }
}


Now if you send a different format, say some XML you’ll receive the body key as a string:

{
    "body" : "my request body"
}

However, if you do so then you need your lambda to do the parsing further.

And Ah! ofcourse you need to redeploy your Api gateway after above changes.

Cheers :)