Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example : Banking Account Service with File Store Auth using scopes #1057

Merged
merged 238 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
238 commits
Select commit Hold shift + click to select a range
0f959a1
Example for Auth using file user store
harshalkh Oct 8, 2023
1a5b843
Example of file user store
harshalkh Oct 9, 2023
718bb9a
Example for file user store
harshalkh Oct 9, 2023
3a83629
Update api_gateway.bal
harshalkh Oct 11, 2023
398b08f
Update api_gateway.bal
harshalkh Oct 11, 2023
beda76c
Example for Auth using file user store
harshalkh Oct 11, 2023
9385140
Update api_gateway.bal
harshalkh Oct 12, 2023
5b9fb05
Update api_gateway.bal
harshalkh Oct 12, 2023
edc9590
Update api_gateway.bal
harshalkh Oct 12, 2023
0fd1cbe
Real World Example for Auth
harshalkh Oct 13, 2023
660acf0
Real World Example for Auth
harshalkh Oct 13, 2023
e8eca8a
Update api_gateway.bal
harshalkh Oct 13, 2023
4f4f710
Delete examples/banking-accounts-service/Ballerina.toml
harshalkh Oct 13, 2023
1ecd914
Write a Real World Example for Auth
harshalkh Oct 13, 2023
17bcf05
Write a Real World Example for Auth
harshalkh Oct 13, 2023
bfb653e
Write a Real World Example for Auth
harshalkh Oct 13, 2023
d763e0b
Update api_gateway.bal
harshalkh Oct 13, 2023
bacdd46
Update api_gateway.bal
harshalkh Oct 13, 2023
7a2f151
Create api_gateway_test.bal
harshalkh Oct 13, 2023
506e9f0
Update api_gateway_test.bal
harshalkh Oct 13, 2023
054d256
Update api_gateway_test.bal
harshalkh Oct 13, 2023
2b76d16
Update api_gateway_test.bal
harshalkh Oct 13, 2023
8a0ac78
Update api_gateway.bal
harshalkh Oct 13, 2023
13554c2
Update api_gateway.bal
harshalkh Oct 13, 2023
46ca05c
Update api_gateway_test.bal
harshalkh Oct 13, 2023
ada3147
Update api_gateway_test.bal
harshalkh Oct 13, 2023
d13758f
Update api_gateway_test.bal
harshalkh Oct 13, 2023
d917c1e
Update api_gateway_test.bal
harshalkh Oct 13, 2023
9b3d5e0
Update api_gateway_test.bal
harshalkh Oct 13, 2023
2899a83
Update api_gateway_test.bal
harshalkh Oct 13, 2023
84dfe97
Update api_gateway_test.bal
harshalkh Oct 13, 2023
6f11597
Update api_gateway_test.bal
harshalkh Oct 13, 2023
e46c5a8
Update api_gateway_test.bal
harshalkh Oct 13, 2023
ef9e7a1
Update api_gateway.bal
harshalkh Oct 13, 2023
aa56f0e
Update api_gateway.bal
harshalkh Oct 13, 2023
cd6d196
Update api_gateway.bal
harshalkh Oct 13, 2023
9c73bb0
Rename public.cert to public.crt
harshalkh Oct 13, 2023
584d4de
Create private.key
harshalkh Oct 13, 2023
35bfb1d
Create public.crt
harshalkh Oct 13, 2023
166e530
Update Config.toml
harshalkh Oct 13, 2023
4e764ef
Update api_gateway.bal
harshalkh Oct 13, 2023
7d61ac3
Update api_gateway.bal
harshalkh Oct 13, 2023
41ec9e7
Update api_gateway.bal
harshalkh Oct 13, 2023
ccdab85
Update api_gateway.bal
harshalkh Oct 13, 2023
8655e4e
Update api_gateway_test.bal
harshalkh Oct 13, 2023
ef89387
Update api_gateway_test.bal
harshalkh Oct 13, 2023
c7c576d
Update api_gateway_test.bal
harshalkh Oct 13, 2023
0794e00
Update Config.toml
harshalkh Oct 13, 2023
cbaf61a
Update Config.toml
harshalkh Oct 13, 2023
3b32b81
Update api_gateway_test.bal
harshalkh Oct 13, 2023
5d272dd
Update api_gateway_test.bal
harshalkh Oct 13, 2023
c6346ec
Update api_gateway_test.bal
harshalkh Oct 13, 2023
490d9a7
Create Config.toml
harshalkh Oct 13, 2023
b626bea
Update Config.toml
harshalkh Oct 14, 2023
c078d4a
Update Config.toml
harshalkh Oct 14, 2023
9972df0
Update Config.toml
harshalkh Oct 14, 2023
5d729e6
Update api_gateway.bal
harshalkh Oct 14, 2023
73616a8
Update api_gateway.bal
harshalkh Oct 14, 2023
5810d25
Update api_gateway.bal
harshalkh Oct 14, 2023
974df5b
Update api_gateway.bal
harshalkh Oct 14, 2023
b28d10b
Update api_gateway_test.bal
harshalkh Oct 14, 2023
b507a01
Update api_gateway_test.bal
harshalkh Oct 14, 2023
20ce0f5
Update api_gateway_test.bal
harshalkh Oct 14, 2023
762166b
Update api_gateway_test.bal
harshalkh Oct 14, 2023
1edcf6f
Update api_gateway_test.bal
harshalkh Oct 14, 2023
64c2f5b
Update api_gateway_test.bal
harshalkh Oct 14, 2023
0d0504d
Update api_gateway_test.bal
harshalkh Oct 14, 2023
065b74d
Update api_gateway_test.bal
harshalkh Oct 14, 2023
16b7282
Update api_gateway.bal
harshalkh Oct 14, 2023
4524fee
Update api_gateway.bal
harshalkh Oct 14, 2023
cdd32b1
Update api_gateway.bal
harshalkh Oct 14, 2023
c4b660f
Update api_gateway.bal
harshalkh Oct 14, 2023
9d7f412
Update api_gateway.bal
harshalkh Oct 14, 2023
0890065
Update api_gateway.bal
harshalkh Oct 14, 2023
7ba0d5e
Update api_gateway.bal
harshalkh Oct 14, 2023
ec35f0a
Update api_gateway.bal
harshalkh Oct 14, 2023
f166898
Update api_gateway_test.bal
harshalkh Oct 14, 2023
d0fa13d
Update api_gateway_test.bal
harshalkh Oct 14, 2023
3d73098
Update api_gateway_test.bal
harshalkh Oct 14, 2023
7b177df
Update api_gateway_test.bal
harshalkh Oct 14, 2023
f581f81
Update api_gateway_test.bal
harshalkh Oct 14, 2023
f4b9c18
Update api_gateway_test.bal
harshalkh Oct 14, 2023
b6c1479
Update api_gateway.bal
harshalkh Oct 14, 2023
b7b5a4e
Update api_gateway_test.bal
harshalkh Oct 14, 2023
8ece7c2
Update api_gateway.bal
harshalkh Oct 14, 2023
f7daa3c
Update api_gateway_test.bal
harshalkh Oct 14, 2023
0478e12
Update api_gateway_test.bal
harshalkh Oct 14, 2023
3c70931
Update api_gateway.bal
harshalkh Oct 14, 2023
e918c29
Update api_gateway_test.bal
harshalkh Oct 14, 2023
7718305
Update api_gateway_test.bal
harshalkh Oct 14, 2023
b02ad45
Update api_gateway.bal
harshalkh Oct 14, 2023
c7ab918
Update api_gateway.bal
harshalkh Oct 14, 2023
a5a321d
Update api_gateway_test.bal
harshalkh Oct 14, 2023
8b355f7
Update api_gateway.bal
harshalkh Oct 14, 2023
3c037ac
Update api_gateway.bal
harshalkh Oct 14, 2023
4c21a3e
Update api_gateway.bal
harshalkh Oct 14, 2023
2f5c89b
Update api_gateway.bal
harshalkh Oct 14, 2023
8e5961c
Update api_gateway.bal
harshalkh Oct 14, 2023
cc060f7
Update api_gateway.bal
harshalkh Oct 14, 2023
1738ce7
Update api_gateway_test.bal
harshalkh Oct 14, 2023
6f854f5
Update api_gateway_test.bal
harshalkh Oct 14, 2023
4617520
Update api_gateway_test.bal
harshalkh Oct 14, 2023
b9b4ab6
Update api_gateway_test.bal
harshalkh Oct 14, 2023
0a91eb3
Update api_gateway_test.bal
harshalkh Oct 14, 2023
53b182d
Update api_gateway_test.bal
harshalkh Oct 14, 2023
1ccbd90
Update api_gateway_test.bal
harshalkh Oct 14, 2023
53b4369
Update api_gateway_test.bal
harshalkh Oct 14, 2023
f1d4b82
Update api_gateway.bal
harshalkh Oct 14, 2023
4b12175
Update api_gateway.bal
harshalkh Oct 14, 2023
280ab72
Update api_gateway.bal
harshalkh Oct 15, 2023
74d8c6b
Update api_gateway.bal
harshalkh Oct 15, 2023
10d7c76
Update api_gateway.bal
harshalkh Oct 15, 2023
7ffa293
Update api_gateway.bal
harshalkh Oct 15, 2023
9801471
Update api_gateway.bal
harshalkh Oct 15, 2023
e1a8243
Update api_gateway_test.bal
harshalkh Oct 15, 2023
6781acc
Update api_gateway_test.bal
harshalkh Oct 15, 2023
0e2ed25
Update api_gateway_test.bal
harshalkh Oct 15, 2023
6b38a4d
Update api_gateway.bal
harshalkh Oct 15, 2023
41af59b
Update api_gateway.bal
harshalkh Oct 15, 2023
da53752
Update api_gateway.bal
harshalkh Oct 15, 2023
d038499
Update api_gateway.bal
harshalkh Oct 15, 2023
baf8495
Update api_gateway.bal
harshalkh Oct 15, 2023
2d6020d
Update api_gateway.bal
harshalkh Oct 15, 2023
a3852ab
Update api_gateway_test.bal
harshalkh Oct 15, 2023
fb5503f
Update api_gateway_test.bal
harshalkh Oct 15, 2023
526e59c
Update api_gateway_test.bal
harshalkh Oct 15, 2023
c3ac0a5
Update api_gateway_test.bal
harshalkh Oct 15, 2023
6b9d93c
Update api_gateway_test.bal
harshalkh Oct 15, 2023
b622cfd
Update api_gateway_test.bal
harshalkh Oct 15, 2023
abc4fd5
Update api_gateway_test.bal
harshalkh Oct 15, 2023
deed70e
Update api_gateway_test.bal
harshalkh Oct 15, 2023
7d866c9
Update Config.toml
harshalkh Oct 15, 2023
3ecc241
Update Config.toml
harshalkh Oct 15, 2023
f93d8ce
Update Ballerina.toml
harshalkh Oct 15, 2023
2cb4d47
Update api_gateway.bal
harshalkh Oct 15, 2023
cc408e8
Update api_gateway.bal
harshalkh Oct 15, 2023
961bc1c
Update api_gateway.bal
harshalkh Oct 15, 2023
abb98a3
Update api_gateway.bal
harshalkh Oct 15, 2023
747b79f
Update api_gateway.bal
harshalkh Oct 15, 2023
eb0a2ab
Update api_gateway.bal
harshalkh Oct 15, 2023
50affd2
Update Config.toml
harshalkh Oct 15, 2023
ac69eae
Update api_gateway.bal
harshalkh Oct 15, 2023
ec94f48
Update api_gateway.bal
harshalkh Oct 15, 2023
a20788e
Update api_gateway.bal
harshalkh Oct 15, 2023
4cc73fa
Update api_gateway.bal
harshalkh Oct 15, 2023
39b136f
Update api_gateway.bal
harshalkh Oct 15, 2023
eab26fa
Update api_gateway.bal
harshalkh Oct 15, 2023
0a70851
Update api_gateway.bal
harshalkh Oct 15, 2023
d34f8ce
Update api_gateway.bal
harshalkh Oct 15, 2023
45bf184
Update api_gateway.bal
harshalkh Oct 15, 2023
f245714
Update api_gateway.bal
harshalkh Oct 15, 2023
dea980d
Update api_gateway.bal
harshalkh Oct 15, 2023
29f7438
Update api_gateway.bal
harshalkh Oct 15, 2023
cab8a62
Update api_gateway.bal
harshalkh Oct 15, 2023
3bdebe5
Update api_gateway.bal
harshalkh Oct 15, 2023
fc964b1
Update api_gateway.bal
harshalkh Oct 15, 2023
4f48312
Update api_gateway.bal
harshalkh Oct 15, 2023
8a2c3a1
Update api_gateway.bal
harshalkh Oct 15, 2023
c553869
Update api_gateway.bal
harshalkh Oct 15, 2023
e632971
Update api_gateway.bal
harshalkh Oct 15, 2023
2e6b918
Update api_gateway_test.bal
harshalkh Oct 15, 2023
d2ef62d
Update api_gateway.bal
harshalkh Oct 15, 2023
8e44096
Update api_gateway_test.bal
harshalkh Oct 15, 2023
12ce8e9
Update api_gateway_test.bal
harshalkh Oct 15, 2023
1b7284d
Update api_gateway.bal
harshalkh Oct 15, 2023
5d4765e
Update api_gateway.bal
harshalkh Oct 15, 2023
9a954d5
Update api_gateway.bal
harshalkh Oct 15, 2023
9b1ceb7
Update api_gateway.bal
harshalkh Oct 15, 2023
b52733e
Update api_gateway.bal
harshalkh Oct 15, 2023
520e84f
Update api_gateway.bal
harshalkh Oct 15, 2023
7f9efad
Update api_gateway.bal
harshalkh Oct 15, 2023
d83b2f1
Update api_gateway.bal
harshalkh Oct 15, 2023
ff62601
Update api_gateway.bal
harshalkh Oct 15, 2023
28308ad
Update api_gateway.bal
harshalkh Oct 15, 2023
3ea83e6
Update api_gateway.bal
harshalkh Oct 15, 2023
54b4861
Update api_gateway.bal
harshalkh Oct 15, 2023
8fde9dd
Update api_gateway.bal
harshalkh Oct 15, 2023
1fabe65
Update api_gateway.bal
harshalkh Oct 15, 2023
2327f9d
Update api_gateway.bal
harshalkh Oct 15, 2023
afce66c
Update api_gateway.bal
harshalkh Oct 15, 2023
ee7a03d
Update api_gateway.bal
harshalkh Oct 15, 2023
c432a6c
Update api_gateway.bal
harshalkh Oct 15, 2023
0aab9c2
Update api_gateway.bal
harshalkh Oct 15, 2023
115260a
Update api_gateway.bal
harshalkh Oct 15, 2023
8d509af
Update api_gateway.bal
harshalkh Oct 15, 2023
96c67ec
Update api_gateway.bal
harshalkh Oct 15, 2023
8e6928c
Update api_gateway.bal
harshalkh Oct 15, 2023
0970f8e
Update api_gateway.bal
harshalkh Oct 15, 2023
ce7db1d
Update api_gateway.bal
harshalkh Oct 15, 2023
c868e3a
Update api_gateway.bal
harshalkh Oct 15, 2023
0f637f5
Update api_gateway.bal
harshalkh Oct 15, 2023
89a300f
Update api_gateway_test.bal
harshalkh Oct 15, 2023
2edd987
Update api_gateway.bal
harshalkh Oct 15, 2023
83c66c6
Update api_gateway.bal
harshalkh Oct 15, 2023
9657e7e
Update api_gateway.bal
harshalkh Oct 15, 2023
87a89cc
Update api_gateway.bal
harshalkh Oct 15, 2023
c4304de
Update api_gateway.bal
harshalkh Oct 15, 2023
9e1de33
Update api_gateway.bal
harshalkh Oct 15, 2023
a2c01db
Update api_gateway.bal
harshalkh Oct 15, 2023
1665d41
Update api_gateway.bal
harshalkh Oct 15, 2023
271afb8
Update api_gateway.bal
harshalkh Oct 15, 2023
ed79c97
Update api_gateway.bal
harshalkh Oct 15, 2023
3c4f9de
Update api_gateway.bal
harshalkh Oct 15, 2023
ee5d5dd
Update api_gateway.bal
harshalkh Oct 15, 2023
b5a6c9d
Update api_gateway.bal
harshalkh Oct 15, 2023
2c43252
Update api_gateway_test.bal
harshalkh Oct 15, 2023
fec07fa
Update api_gateway_test.bal
harshalkh Oct 15, 2023
56ff2e4
Update api_gateway_test.bal
harshalkh Oct 15, 2023
1c8ecf3
Create README.md
harshalkh Oct 16, 2023
49fc59d
Update README.md
harshalkh Oct 16, 2023
7653e02
Update README.md
harshalkh Oct 16, 2023
8b51132
Update README.md
harshalkh Oct 16, 2023
1cd02a9
Update api_gateway.bal
harshalkh Oct 16, 2023
f149b3a
Update api_gateway_test.bal
harshalkh Oct 16, 2023
daa5f70
Update Config.toml
harshalkh Oct 16, 2023
a36e34e
Update README.md
harshalkh Oct 16, 2023
41debfe
Update README.md
harshalkh Oct 16, 2023
56baf08
Example : Banking Account Service with File Store Auth using scopes
harshalkh Oct 16, 2023
260515f
Merge branch 'master' into patch-1
harshalkh Oct 16, 2023
2304da7
Apply suggestions from code review
harshalkh Oct 17, 2023
604ab79
Apply suggestions from code review
harshalkh Oct 17, 2023
6d6a1eb
Update examples/banking-accounts-service/README.md
harshalkh Oct 17, 2023
b728ca1
Update README.md
harshalkh Oct 17, 2023
6b276b5
Update as per review comment for query
harshalkh Oct 17, 2023
cbefa56
Merge branch 'master' into patch-1
harshalkh Oct 17, 2023
f34a58e
Update examples/banking-accounts-service/api_gateway.bal
harshalkh Oct 19, 2023
e97bfd3
Update api_gateway.bal
harshalkh Oct 19, 2023
0dc6d53
Update api_gateway.bal
harshalkh Oct 19, 2023
01959b7
Update api_gateway.bal
harshalkh Oct 19, 2023
da66220
Merge branch 'master' into patch-1
harshalkh Oct 19, 2023
27788ce
Update README.md
harshalkh Oct 20, 2023
5af7260
Update examples/banking-accounts-service/api_gateway.bal
harshalkh Oct 20, 2023
9c9c1f7
Update examples/banking-accounts-service/README.md
harshalkh Oct 20, 2023
e402bcb
Update examples/banking-accounts-service/README.md
harshalkh Oct 20, 2023
c48ce48
Update examples/banking-accounts-service/README.md
harshalkh Oct 20, 2023
d6e7fc0
Merge branch 'master' into patch-1
harshalkh Oct 20, 2023
80ed77b
Update api_gateway.bal
harshalkh Oct 20, 2023
6140a7f
Update api_gateway.bal
harshalkh Oct 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/banking-accounts-service/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
org = "auth"
name = "banking_account_service"
version = "1.0.0"

[build-options]
observabilityIncluded = true
17 changes: 17 additions & 0 deletions examples/banking-accounts-service/Config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[ballerina.auth.users]]
username = "alice"
password = "alice@123"
scopes = ["read-account", "read-balance", "funds-transfer"]

[[ballerina.auth.users]]
username = "bob"
password = "bob@123"
scopes = ["read-account", "read-balance"]

[[ballerina.auth.users]]
username = "david"
password = "david@123"
scopes = ["read-account"]

[ballerina.log]
level = "INFO"
119 changes: 119 additions & 0 deletions examples/banking-accounts-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Secured Banking Account Management Service with File Store Basic Auth with Scopes

[![Star on Github](https://img.shields.io/badge/-Star%20on%20Github-blue?style=social&logo=github)](https://github.com/ballerina-platform/module-ballerina-auth)

_Authors_: [@harshalkh](https://github.com/harshalkh) \
_Reviewers_: @ThisaruGuruge @DimuthuMadushan \
_Created_: 2023/10/16 \
_Updated_: 2023/10/16

## Overview

This guide explains how to secure the 'Banking Account Management Service' (RESTful service) with Basic Auth using File Store in Ballerina.

The end-user (customer) in this example, Alice, Bob and David, interacts with the system using the web/mobile app provided. This web/mobile app acts as a 'Client' on behalf of the user’s actions and calls to the 'API Gateway'. The 'API Gateway' routes the requests to 'Banking Service', which is responsible for processing the requests for the customer.

> **NOTE:** For this guide, since discussion is about the File Store based Basic Auth security aspects, focus is on the network
interactions once the 'API Gateway' receives a request. The transaction data is stored locally in [table](https://ballerina.io/learn/by-example/table/)

- The 'API Gateway' intercepts the request from the end-user, extracts the credentials (username and password which is
concatenated with a `:` and Base64 encoded), and then talks to File Store Listener to validate the credentials.
- After validating the credentials, the 'API Gateway' talks to 'Banking Account Service' with mTLS (mutual TLS).
- The 'Banking Account Service' uses table data to process customer request based on their authorization scopes.

## Implementation

- You can get started with the 'API Gateway', which is responsible to authorize the requests using Basic Auth with the use of File user store and forward the request to the actual microservice via mTLS (mutual TLS). In this scenario, it is 'Banking Account Service'. The 'API Gateway' service is secured by setting the `auth` attribute of `http:ServiceConfig` with the Basic Auth - File user store configurations, so that the Ballerina HTTP service knows how to validate the credentials with the configured File user store from Config.toml. Once validated, the business logic defined inside the resource will get executed. In this case, it will call the 'Banking Account Service' via mTLS and return the response to the 'Client'.
- In addition to declarative approach for Authentication and Authorization, service uses [Imperative Approach](https://ballerina.io/spec/http/#912-imperative-approach) as service needs to have granular control on authorization of customer. For example knowing customer id of user to fetch account details, available balance before proceeding for execution of payment.

> **NOTE:** The rest of the components such as Database Management System are not implemented as the main purpose of this article is to showcase the Basic Auth functionalities. But for the completeness of the story, the API Gateway will return a response from the data stored on in-memory tables.

## Testing

You can run the 'API Gateway' that we developed above, in our local environment. In order to run this service you need to setup prerequisite of Ballerina. You can refer documentation [here](https://ballerina.io/learn/get-started/)

Now, navigate to [`examples`](../) directory and execute the following command.
```shell
$ bal run banking-accounts-service
```

The successful execution of the service should show us the following output.
```shell
Compiling source
auth/banking_account_service:1.0.0

Running executable
```

Now, you can test authentication and authorization checks being enforced on different actions by sending HTTP requests.
This example uses the Unit Tests to test each scenario as follows.

#### Without authentication

```ballerina
http:Response response = check testClient->get("/accounts/account");
test:assertEquals(response.statusCode, http:STATUS_UNAUTHORIZED);
```

#### Authenticating as anonymous user

```ballerina
map<string|string[]> headers = {
"Authorization": "Basic random"
};
http:Response response = check testClient->get("/accounts/account", headers);
test:assertEquals(response.statusCode, http:STATUS_UNAUTHORIZED);
```

#### Detailed scenarios:

| Scenario\User | Alice | Bob | David |
| --- | --- | --- | --- |
| Scopes | `read-account` `read-balance` `funds-transfer` | `read-account` `read-balance` | `read-account` |
| Accessing `GET /accounts/account` | `200` Account Details for Alice | `200` Account Details for Bob | `200` Account Details for David |
| Accessing `GET /accounts/balance` | `200` Account Details with Balance for Alice | `200` Account Details with Balance for Bob | `403` Forbidden |
| Accessing `POST /payments/transfer` where transaction amount within available balance | `200` Response with unique paymentId and status as SUCCESS | `403` Forbidden | `403` Forbidden |
| Accessing `POST /payments/transfer` where transaction amount higher than available balance| `200` Response with unique paymentId and status as FAILED | `403` Forbidden | `403` Forbidden |



## Deployment

Once the development is done, you can deploy the service using any of the methods that are listed below.

### Deploying Locally

Now, you can build Ballerina executable files (.jar) of the components that we developed above. Open the terminal and
navigate to [`examples/banking-account-service`](../banking-accounts-service/), and execute the following command for
each of them.

```shell
harshalkh marked this conversation as resolved.
Show resolved Hide resolved
$ bal build
```

The successful execution of the above command should show us the following outputs in order.

```shell
harshalkh marked this conversation as resolved.
Show resolved Hide resolved
Compiling source
auth/api_gateway:1.0.0

Generating executable
target/bin/api_gateway.jar
```

Once the `*.jar` file is created inside the `target/bin` directories, we can run the components with the following commands in order.

```shell
harshalkh marked this conversation as resolved.
Show resolved Hide resolved
$ bal run target/bin/api_gateway.jar
```

### Deploying Code to Cloud

Ballerina code to cloud supports generating the deployment artifacts of the Docker and Kubernetes.
Refer to [Code to Cloud](https://ballerina.io/learn/code-to-cloud-deployment/) guide for more information.

## Observability

HTTP/HTTPS based Ballerina services and any client connectors are observable by default.
[Observing Ballerina Code](https://ballerina.io/learn/observe-ballerina-programs/#provide-observability-in-ballerina) guide provides
information on enabling Ballerina service observability with some of its supported systems.
180 changes: 180 additions & 0 deletions examples/banking-accounts-service/api_gateway.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/auth;
import ballerina/http;
import ballerina/io;
import ballerina/uuid;

type Balance record {|
string name;
string amount;
string currency;
|};

type AccountWithBalances record {|
string id;
string accountNumber;
readonly string customerId;
string customerName;
string productType;
string status;
Balance[] balances;
|};

type PaymentRequest readonly & record {|
string amount;
string currency;
string creditor;
|};

type PaymentResponse readonly & record {|
string id;
string status;
string failureReason?;
|};

table<AccountWithBalances> key(customerId) accountBalances = table [
{
id: "vgshdkrokjhbbb",
accountNumber: "1234 1234 1234",
customerId: "alice",
customerName: "Alice Alice",
productType: "Savings Account",
status: "Active",
balances: [
{ name: "Available", amount: "1000", currency: "INR" },
{ name: "Ledger", amount: "1000", currency: "INR" },
{ name: "Uncleared", amount: "0", currency: "INR" }
]
},
{
id: "vgksurbkfldppd",
accountNumber: "1234 1234 6789",
customerId: "bob",
customerName: "Bob Bob",
productType: "Current Account",
status: "Active",
balances: [
{ name: "Available", amount: "10000", currency: "INR" },
{ name: "Ledger", amount: "1000", currency: "INR" },
{ name: "Uncleared", amount: "0", currency: "INR" }
]
},
{
id: "vgskspwldkdddn",
accountNumber: "1234 1234 2345",
customerId: "david",
customerName: "David David",
productType: "Savings Account",
status: "Active",
balances: [
{ name: "Available", amount: "8000", currency: "INR" },
{ name: "Ledger", amount: "1000", currency: "INR" },
{ name: "Uncleared", amount: "0", currency: "INR" }
]
}
];

listener http:Listener apiGateway = new (9090,
secureSocket = {
key: {
certFile: "../banking-accounts-service/resources/public.crt",
keyFile: "../banking-accounts-service/resources/private.key"
}
}
);

// Imperative approach as we need to know about customer authozation details for filtering data
// https://ballerina.io/spec/http/#912-imperative-approach
http:FileUserStoreConfig config = {};
http:ListenerFileUserStoreBasicAuthHandler handler = new (config);

@http:ServiceConfig {
auth: [
{
fileUserStoreConfig: {},
scopes: ["read-account"]
}
]
}
service /accounts on apiGateway {
resource function get account(@http:Header string? Authorization) returns AccountWithBalances[] {
string customerId = getCustomerId(Authorization);
AccountWithBalances[] accountBalance = from AccountWithBalances account in accountBalances
where account.customerId == customerId
select account;
AccountWithBalances[] accountBalance1 = accountBalance.clone();
accountBalance1[0].balances = [];
return accountBalance1;
DimuthuMadushan marked this conversation as resolved.
Show resolved Hide resolved
}

@http:ResourceConfig {
auth: [
{
fileUserStoreConfig: {},
scopes: ["read-balance"]
}
]
}
resource function get balances(@http:Header string? Authorization) returns AccountWithBalances[] {
string customerId = getCustomerId(Authorization);
AccountWithBalances[] accountBalance = from AccountWithBalances account in accountBalances
where account.customerId == customerId
select account;
return accountBalance;
}
}

@http:ServiceConfig {
auth: [
{
fileUserStoreConfig: {},
scopes: ["funds-transfer"]
}
]
}
service /payments on apiGateway {
resource function post transfer(@http:Payload PaymentRequest paymentRequest, @http:Header string? Authorization) returns PaymentResponse {
string customerId = getCustomerId(Authorization);
AccountWithBalances[] accountBalance = from AccountWithBalances account in accountBalances
where account.customerId == customerId
select account;
boolean balAvailable = accountBalance[0].balances
.filter(bal => bal.name=="Available").some(bal1 => bal1.amount>=paymentRequest.amount);
if !balAvailable {
io:println("Insufficient Balance in account");
return {
id: uuid:createType4AsString(),
status: "FAILED",
failureReason: "Insufficient Balance in account"
};
}
return {
id: uuid:createType4AsString(),
status: "SUCCESS"
};
}
}

public function getCustomerId(string? authorization) returns string {
auth:UserDetails|http:Unauthorized authn = handler.authenticate(authorization is () ? "" : authorization);
string customerId = "";
if authn is auth:UserDetails {
customerId = authn.username;
}
return customerId;
}
28 changes: 28 additions & 0 deletions examples/banking-accounts-service/resources/private.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCBXKLp9WJUuJko
SfDn3HM2LWVxiHrP11me6D4X2AqoUiCZ1w976q1LuBlzWNhONYwJvHXewUuRmo0d
4NRp2ma0qjdMN246XWyozpzPfJdvovfiT+V6HnSbMg82ZppfF3e85Cgrg6mSA7Wf
faaHr62SnexseZ9ioNCuRUrW2Zg208l99IItP4K4iGclQlyjs3S6drH7JJk3rv8B
f76wmmoCEAhknOtGLq543dEUIjFgNdIy/GsSY52fiA4LSjUCv27kGn03nayqoZs3
XHqM0qLdt9SPXbYe7l75j0o6Z9tyGFCKXY/zVwiLfRUqDIOfkN5YS4Ohit52Tj7q
oBmvEzD7AgMBAAECggEAXM/F4u23OummmQ1T1kaIMpqnaalt06jCGAywYBMUsmca
FMYDyfg5lVXkjKl1p8crTeD1AHjWawTjskgYnkmf3ocxXXF3mFBnIUX7o7HURLg7
+RcxoUgwiRiFaZZ7szX3JoLbfzzbcHNQ37kavccBVWwQsFMiU3Tlw+LbKwK6/row
LYsQPx7gT4u7hViat4vQDTYcgyjvvFCiek4ndL6O9K49MxIMU678UXB6ia5iUevy
vgEfcYkKQ5EQ38qS3ZwsubPvj4633jvAJRr/hJD8XINZC74kTXeV3BGH2LlpQOEq
kWkOypwYNjnXtt1JO8+Iu6mEXKUoiIBPfGrJ3vDSQQKBgQDmYPc7kfYan/LHjJRv
iE2CwbC26yVA6+BEPQv9z7jChO9Q6cUbGvM8EEVNpC9nmFogkslzJhz55HP84QZL
u3ptU+D96ncq6zkBqxBfRnZG++D36+XRXIwzz3h+g1Nwrl0y0MFbwlkMm3ZqJdd6
pZz1FZGd6zvQftW8m7jPSKHuswKBgQCPv6czFOZR6bI+qCQdaORpe9JGoAduOD+4
YKl96s0eiAKhkGhFCrMd6GJwWRkpNcfwB+J9sMahORbfvwiYanI56h7Vi30DFPRb
m1m8dLkr6z+8bxMxKJaMXIIjy3UDamgDr7QHInNUih2iGvtB8QqZ0aobsB2XIxZg
qESTMcpYmQKBgHSwSqneraQgvgz7FLhFdtUzHDoacr0mfGqz7R37F99XDAyUy+SF
ywvyRdgkwGodjhEPqH/tnyGn6GP+6nxzknhL0xtppkCT8kT5C4rmmsQrknChCL/5
u34GqUaTaDEb8FLrz/SVRRuQpvLvBey2dADjkuVFH//kLoig64P6iyLnAoGBAIlF
g+2L78YZXVXoS1SqbjUtQUigWXgvzunLpQ/Rwb9+MsUGmgwUg6fz2s1eyGBKM3xM
i0VsIsKjOezBCPxD6oDTyk4yvlbLE+7HE5KcBJikNmFD0RgIonu3e6+jA0MXweyD
RW/qviflHRdInNgDzxPE3KVEMX26zAvRpGrMCWdBAoGAdQ5SvX+mAC3cKqoQ9Zal
lSqWoyjfzP5EaVRG8dtoLxbznQGTTvtHXc65/MznX/L9qkWCS6Eb4HH5M3hFNY46
LNIzGQLznE1odwv7H5B8c0/m3DrKTxbh8bYcrR1BW5/nKZNNW7k1O6OjEozvAajK
JQdp3KBU9S8CmBjGrRpJ2qw=
-----END PRIVATE KEY-----
21 changes: 21 additions & 0 deletions examples/banking-accounts-service/resources/public.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDdzCCAl+gAwIBAgIEfP3e8zANBgkqhkiG9w0BAQsFADBkMQswCQYDVQQGEwJV
UzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxDTALBgNVBAoT
BFdTTzIxDTALBgNVBAsTBFdTTzIxEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0xNzEw
MjQwNTQ3NThaFw0zNzEwMTkwNTQ3NThaMGQxCzAJBgNVBAYTAlVTMQswCQYDVQQI
EwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzENMAsGA1UEChMEV1NPMjENMAsG
A1UECxMEV1NPMjESMBAGA1UEAxMJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEAgVyi6fViVLiZKEnw59xzNi1lcYh6z9dZnug+F9gKqFIg
mdcPe+qtS7gZc1jYTjWMCbx13sFLkZqNHeDUadpmtKo3TDduOl1sqM6cz3yXb6L3
4k/leh50mzIPNmaaXxd3vOQoK4OpkgO1n32mh6+tkp3sbHmfYqDQrkVK1tmYNtPJ
ffSCLT+CuIhnJUJco7N0unax+ySZN67/AX++sJpqAhAIZJzrRi6ueN3RFCIxYDXS
MvxrEmOdn4gOC0o1Ar9u5Bp9N52sqqGbN1x6jNKi3bfUj122Hu5e+Y9KOmfbchhQ
il2P81cIi30VKgyDn5DeWEuDoYredk4+6qAZrxMw+wIDAQABozEwLzAOBgNVHQ8B
Af8EBAMCBaAwHQYDVR0OBBYEFNmtrQ36j6tUGhKrfW9qWWE7KFzMMA0GCSqGSIb3
DQEBCwUAA4IBAQAv3yOwgbtOu76eJMl1BCcgTFgaMUBZoUjK9Un6HGjKEgYz/YWS
ZFlY/qH5rT01DWQevUZB626d5ZNdzSBZRlpsxbf9IE/ursNHwHx9ua6fB7yHUCzC
1ZMp1lvBHABi7wcA+5nbV6zQ7HDmBXFhJfbgH1iVmA1KcvDeBPSJ/scRGasZ5q2W
3IenDNrfPIUhD74tFiCiqNJO91qD/LO+++3XeZzfPh8NRKkiPX7dB8WJ3YNBuQAv
gRWTISpSSXLmqMb+7MPQVgecsepZdk8CwkRLxh3RKPJMjigmCgyvkSaoDMKAYC3i
YjfUTiJ57UeqoSl0IaOFJ0wfZRFh+UytlDZa
-----END CERTIFICATE-----
17 changes: 17 additions & 0 deletions examples/banking-accounts-service/tests/Config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[ballerina.auth.users]]
username = "alice"
password = "alice@123"
scopes = ["read-account", "read-balance", "funds-transfer"]

[[ballerina.auth.users]]
username = "bob"
password = "bob@123"
scopes = ["read-account", "read-balance"]

[[ballerina.auth.users]]
username = "david"
password = "david@123"
scopes = ["read-account"]

[ballerina.log]
level = "INFO"
Loading