Thursday, July 23, 2020

Fix 'Resource not found' error when accessing blob in azure storage account's container

There are two level of access control when accessing blob data from url of azure storage account's blob container. If either of them reject the public access of the blob, then accessing the url will fail and get an "resource not found" error.

The first level of access control is from storage account's configuration level, it affects all data access belonging to this storage account. "Allow Blob public access" must be set to enabled as below:
 

The second level access control is in specific container's access level setting. First select the containers in the container list, and then click 'Change access level" link to set the access level to allow public access as shown below


When resource not found error happens, it may come from either of the two access configuration.

Tuesday, July 21, 2020

Test node js npm package in browser with parcel bundle

Usually node js app runs under node js environment, and testing a npm package within a console node app is inconvenient. It would be easier if npm package can be tested easily from browser as a web app.

Parcel bundle from https://parceljs.org/ can convert a node js app into a regular web app, so as to test and debug a npm package from browser. The below steps show how to test the npm package https://www.npmjs.com/package/financejs from browser.

1. run 
npm install -g parcel-bundler
to install parcel bundle

2. run
npm init
to initialize npm package file.

3. Add browserslist configuration in generated package.json to specify the allowed browsers
{
  "name": "npmtest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
"browserslist": [ "last 1 Edge version", "last 1 Chrome version", "last 1 Firefox version" ],
  "license": "ISC",
  "dependencies": {
    "financejs": "^4.1.0"
  }
}

 
4. create index.html with necessary ui element as below
<!-- index.html -->
<!DOCTYPE html>
<html>

<body>
    <input type="text" id="input"/>
    <button id="test">test</button>
    <p><b>Status:</b></p>
    <p id="status" style="height:160px; width: 593px; overflow: scroll;" />
  </body>

<script src="./index.js"></script>
</html>

5. create index.js using regular node js code as below. Basically, it imports financejs package, and then call CAGR method to get the required rate in order to double the amount in the specified years input by user.

const input = document.getElementById("input");
const testButton = document.getElementById("test");
const status = document.getElementById("status");

testButton.addEventListener("click", onclick);
const reportStatus = message => {
    status.innerHTML += `${message}<br/>`;
    status.scrollTop = status.scrollHeight;
}

var Finance = require('financejs');
var finance = new Finance();

function onclick() {
    var data = input.value;
    // To calculate Amortization
    let rate =  finance.CAGR(10000, 20000, data);
    reportStatus("rate is: " + rate);
}

6. From Windows command line tool or visual studio code terminal, run the below command to start the web app
parcel index.html
Based on the port number shows in console output, open browser to localhost:xxxx to debug the npm package from browser js debugger.

Sunday, July 19, 2020

Angular connects to Azure storage account through Spring boot service API

In regular web architecture, angular (front end), spring boot (api service), and backend data storage service work to together to handle the client request. 
For azure deployment, one common task is connecting spring boot project to azure data storage and database service (sql or no sql). As spring boot project is just a regular java project, so the way how to access azure service from java sdk applies to the spring boot project. The below steps describe how to connect spring boot app (i.e. java sdk project) to azure storage account service.

(Note, spring-azure-starter-storage or azure-storage-spring-boot-starter has been deprecated by Microsoft, so do not use it anymore)  

1. add below azure-storage-blob dependency module in spring boot pom file
    <!-- https://mvnrepository.com/artifact/com.azure/azure-storage-blob -->
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-blob</artifactId>
        <version>12.7.0</version>
    </dependency>

2. in Azure storage account settings, select the container's items and be sure the access level is set to private (no anonymous access), so that those blob can only be accessed by spring boot project. You can verify this by accessing the blob url from browser, and you should get a resource not found error. Copy the access key and container name for later use.

3. azure-storage-blob provides few helper classes to access the data from azure container account, please refer https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-java
for more details.
In constructor, create a containerClient instanceas below
    public StorageService(){
        this.blobServiceClient = new BlobServiceClientBuilder().connectionString(connecString).buildClient();
        this.blobContainerClient = this.blobServiceClient.getBlobContainerClient(containerName);
    }

3.1 upload a file to container
In RestController class, create a method to handle client side angular web app's http request
    @PostMapping("/storages")
    public void createBlob(@RequestBody Map<String, Object> inputData) throws IOException {
        String name = (String) inputData.get("name");
        String data = (String) inputData.get("data");
        service.uploadBlob(name, data);
    }

Create a java function in service class to upload a blob on BlobClient object as below
    public void uploadBlob(String name, String strData) throws IOException {
        BlobClient blobClient = this.blobContainerClient.getBlobClient(name);
        InputStream targetStream = new ByteArrayInputStream(strData.getBytes());
        blobClient.upload(targetStream, targetStream.available(), true);
    }

From client side angular app, a xhr http request will be sent to spring boot app when user
clicks a button on html page:
  onCreateStorage() {
      const data = {
        name: this.nameField.value,
        data: this.dataField.value,
      };

      this.http.post(this.serviceUrl + '/storages/', data).subscribe((resp) => {
        this.resultField.setValue(resp);
      });
    }

In similar way, the other operations (like download, delete etc) can also be implemented.

Saturday, July 11, 2020

OAuth2 and OpenID difference regarding JWT and token inspection endpoint

OAuth2 is a popular framework for authenticating user with third party identity provider (authentication server). Basically user first authenticates to the third party IDP to get grant code, and then the client or web app can request the access token with grant code from authorization server. Usually authentication server and authorization server are from the save third party id provider.

For OAuth2, access token is sent by the client app to resource server when the client requests the data from resource server, however, the oAuth2 does not specify the content or format of the access token. From resource server's point of view, it is just a simple string without any special information for the client who sends the request. So the resource server must contact authorization server to validate the access token in order to find out whether it can give the permission to the client request. This is why oAuth2 authorization server needs to expose a token introspection endpoint url for resource server to validate the token. 

With Oauth2 token introspection endpoint, OAuth2 authorization server can support single sign off, as authorization server can invalidate an issued token at anytime, and return failure for all following token validation requests sent from resource server.

However, it is not very efficient if resource server needs to send a token validation request to authorization for every client side resource request. That is what has been improved by OpenID. which is built on top of OAuth2.

OpenID introduces JWT (json web token) to represent the token format, so it includes all the required information that resource server needs to know in order to decide whether it should accept the client request or not, so there is no need to send an extra request to token introspection server for this purpose.

JWT is a based 64 encoded json string containing user id information, grant scope, token expiration time, and signing algorithm, and signing signature. Note the information included in token are not confidential, so anyone can read it. However, since the token is digit signed with authorization server's private key, can be easily verified by anyone using the authorization server's public key, so the token cannot be changed by someone for malicious purpose. 

JWT has significantly simplified the token validation logic, as the authorization server can just create a token and then digit sign it, and forget about it. Anyone gets the token can validate the token by just using the authorization server's public key, without actually communicating with authorization server. One drawback of OpenID is there is no simple way to support single sign off, as once a token is singed and sent out by authorization server, it will be always accepted by resource server until the token is expired.