Skip to content

Commit

Permalink
[#9] Add a cli tool to access the api server API
Browse files Browse the repository at this point in the history
The Jolokia api-server comes with a cli (command line interface) tool. When the cli tool starts it connects to the api-server and can access the jolokia endpoints.

The cli tool takes a **command** and requests the api-server using [its api](src/config/openapi.yml) to invoke on the target jolokia endpint, gets back the response and printing the result to the console in JSON format.

It can run in `interactive mode` or in `non-interactive` mode.

To build the cli tool run

```
yarn build
```

which will build both the api-server and cli tool.

To install the cli tool runnable locally run:

```
npm link
```

It will create the cli runnable `jolokia-api-server-cli`

The cli tool needs a running api-server.

To start the cli tool run

```
jolokia-api-server-cli [options]
```

or you can use `yarn`

```
yarn start-cli [options]
```

In this mode, the tool starts and execute a command and then exits.

The syntax for non-interactive mode is:

```
jolokia-api-server-cli <command> -l <api-server-url> -e <jolokia-endpoint-url>
```

If `-l` option is omitted the default is ` https://localhost:9443`

The `-e` option is the target jolokia url. for example

```
-e http://user:[email protected]:8161
```

If the port number part is omitted, the default
is `80` for http and `443` for https.

The `command` is the command to be executed.

Note in non-interactive mode the `command` need be quoted if

1. the command contains options (e.g -e) because the '-' can be interpreted by the shell
2. the command contains '*' char

Example:

```
jolokia-api-server-cli "get queue TEST -a MessageCount RoutingType" -e http://user:[email protected]:8161
```

Alternatively you can use `--` to separate the command:

```
jolokia-api-server-cli -e http://user:[email protected]:8161 -- get @broker2/queue DLQ -a MessageCount
```

In interactive mode the tool starts into a command shell and
accepts user input as a command, then it executes it and went
back to the shell prompt to accept another, until you run the `exit`
command.

The syntax to run the cli in this mode is

```
jolokia-api-server-cli -i
```

When it starts it print the cli title and ready to accept
commands.

With interactive mode the cli can 'caches' a list of jolokia endpoints (added by the `add` command
only available in interactive mode). It takes one of them as `current endpoint` so when user types
a command without specifying target jolokia endpoint, the `current endpoint` will be used.

This is the only available command currently. It can retrive
information from a jolokia endpoint.

The syntax of this command is

```
get <path> <name> <-a attributes...> <-o operations...>
```

It takes a `path` argument, a `name` argument, an optional `-a`(attribute) option and an optional
`-o` (operation) option.

The value of `path` is a string representing a target mbean from which you want to get information.
It takes the form [target endpoint]/[component]. The `target endpoint` in `interactive` mode allows
you to specify which broker you want to retrieve information from. If absent it takes the current broker
cached by the cli. In non-interactive mode that [target endpoint] can be empty if `-e` option is given,
or it is the target remote endpoint name prefix by a `@` char. For example `@broker1/`

The `component` part is the type of the mbean. Currently the supported mbean types are

- `queue`
- `address`
- `acceptor`
- `cluster-connection`

---
**NOTE**

If the target component is the broker itself, the component should be left empty. For example

`get @broker1/ -a Status`

It means to get the value of `Status` attribute of the broker.

---

The <name> argument is the mbean name (e.g. DLQ, testQueue, etc).

The value of `-a` option is a list of attribute names (space or comma separated) to read from the target mbean.
If the value is a `*` it will read all the attributes of the target mbean.

The value of `-o` option is a list of operation names (space or comma separated) to read from the target mbean.
If the value is a `*` it will read all the operations of the target mbean.

examples:

`get @broker1/` - get the current broker mbean information

`get @broker1/*` - get all mbeans registered with the broker mbean

`get @broker1/ -a *` - read all the attributes of the broker mbean

`get @broker1/ -a * -o *` - read information of all attributes and operations of the broker mbean

`get @broker1/queue` (or `get /queue`) - list all the queue mbeans information

`get @broker1/acceptor acceptor0 -a *` - read all attributes of acceptor named `acceptor0`

`get @broker1/queue TEST -a MessageCount RoutingType` - read `MessageCount` and `RoutingType` of queue `TEST`

`get @broker1/queue -o xxx` - read information of operation xxx of queue TEST

The `run` command is used to invoke operations on a jolokia endpoint.

The syntax of this command is

```
run <path> <name> <operation>
```

It takes a `path` argument, a `name` argument and an `operation` to be executed on the mbean.
The syntax for `path` and `name` are the same as for the `get` command.

The operation takes the form <operation-name>([args...]). For example

```
run @broker1/ listAddresses(m)
```
It means to call listAddresses(java.lang.String) method on @broker1 with argument value 'm'.

There are several commands that are only available to interactive mode.

Add a jolokia endpoint to the cli cache. Syntax:

```
add <endpoint name> <url> -u <user> -p <password>
```

example:

```
add ex-aao-ssl https://ex-aao-ssl-wconsj-0-svc-rte-default.apps-crc.testing -u user -p password
```

This command allows user to add a jolokia endpoint that is not managed by the api-server. After the
endpoint is added the user can access the endpoint using the cli commands.
---
**NOTE**

When accessing such jolokia endpoints, the command references it without `@` prefixed, for example

`get ex-aao-ssl/ -a Status`

---

To change `current endpoint`. Syntax:

```
switch <endpoint name>
```

example:

```
switch broker0
```

A command can be simplified if it is targeted at the current endpoint. For example if you
set the current endpoint to `broker0` the command

```
> add broker0 http://127.0.0.2:8161 -u guest -p guest
```
and if you want to get the queues of the broker0, you can do

```
get /queues
```

instead of explicitly:

```
get broker0/queues
```

To list all the jolokia endpoints cached in cli and managed on the api-server. Syntax:

```
> list
[
  "local2(local): http://127.0.0.2:8161",
  "@pod-0: https://broker-pem-wconsj-0-svc-ing-default.artemiscloud.io:443",
  "@pod-1: https://broker-pem-wconsj-1-svc-ing-default.artemiscloud.io:443",
  "@Local: http://127.0.0.1:8161",
  "@restricted: https://artemis-broker-jolokia-0-svc-ing-default.artemiscloud.io:443"
]

```
  • Loading branch information
howardgao committed Dec 3, 2024
1 parent e67fbb9 commit 7b00703
Show file tree
Hide file tree
Showing 13 changed files with 4,412 additions and 715 deletions.
6 changes: 6 additions & 0 deletions .cli.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

CLI_VERSION=1.0.0
CLI_NAME='Api Server CLI'

# to trust jolokia certs
NODE_TLS_REJECT_UNAUTHORIZED='0'
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,6 @@ dist
.vscode


# tmp dir
tmp

272 changes: 271 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ deployed. for example:
```sh
./deploy.sh -i quay.io/<repo-username>/activemq-artemis-jolokia-api-server:1.0.1
```

The optional -ns (or --nosec) argument can be used to disable security.

---

**Note:**

you should enable security in your application. Disable security can only
Expand Down Expand Up @@ -125,4 +127,272 @@ For example `/execBrokerOperation?targetEndpoint=broker1`.
### Direct Proxy

Direct Proxy means a client can pass a broker's endpoint info to the api-server in order to access it via the api-server.
For example the [self-provisioning plugin](https://github.com/artemiscloud/activemq-artemis-self-provisioning-plugin) uses this api to access the jolokia of a broker's jolokia endpoint.
For example the [self-provisioning plugin](https://github.com/artemiscloud/activemq-artemis-self-provisioning-plugin) uses this api to access the jolokia of a broker deployed by it.

# Jolokia api-server Cli tool (work in progress)

The Jolokia api-server comes with a cli (command line interface) tool. When the cli tool starts it connects to the api-server and can access the jolokia endpoints.

The cli tool takes a **command** and requests the api-server using [its api](src/config/openapi.yml) to invoke on the target jolokia endpint, gets back the response and printing the result to the console in JSON format.

It can run in `interactive mode` or in `non-interactive` mode.

To build the cli tool run

```
yarn build
```

which will build both the api-server and cli tool.

To install the cli tool runnable locally run:

```
npm link
```

It will create the cli runnable `jolokia-api-server-cli`

## Running the cli tool

The cli tool needs a running api-server.

To start the cli tool run

```
jolokia-api-server-cli [options]
```

or you can use `yarn`

```
yarn start-cli [options]
```

## Using Cli tool in non-interactive mode

In this mode, the tool starts and execute a command and then exits.

The syntax for non-interactive mode is:

```
jolokia-api-server-cli <command> -l <api-server-url> -e <jolokia-endpoint-url>
```

If `-l` option is omitted the default is ` https://localhost:9443`

The `-e` option is the target jolokia url. for example

```
-e http://user:[email protected]:8161
```

If the port number part is omitted, the default
is `80` for http and `443` for https.

The `command` is the command to be executed.

Note in non-interactive mode the `command` need be quoted if

1. the command contains options (e.g -e) because the '-' can be interpreted by the shell
2. the command contains '\*' char

Example:

```
jolokia-api-server-cli "get queue TEST -a MessageCount RoutingType" -e http://user:[email protected]:8161
```

Alternatively you can use `--` to separate the command:

```
jolokia-api-server-cli -e http://user:[email protected]:8161 -- get @broker2/queue DLQ -a MessageCount
```

## Using Cli tool in interactive mode

In interactive mode the tool starts into a command shell and
accepts user input as a command, then it executes it and went
back to the shell prompt to accept another, until you run the `exit`
command.

The syntax to run the cli in this mode is

```
jolokia-api-server-cli -i
```

When it starts it print the cli title and ready to accept
commands.

With interactive mode the cli can 'caches' a list of jolokia endpoints (added by the `add` command
only available in interactive mode). It takes one of them as `current endpoint` so when user types
a command without specifying target jolokia endpoint, the `current endpoint` will be used.

## Using Cli Commands

### The `get` command

This is the only available command currently. It can retrive
information from a jolokia endpoint.

The syntax of this command is

```
get <path> <name> <-a attributes...> <-o operations...>
```

It takes a `path` argument, a `name` argument, an optional `-a`(attribute) option and an optional
`-o` (operation) option.

The value of `path` is a string representing a target mbean from which you want to get information.
It takes the form [target endpoint]/[component]. The `target endpoint` in `interactive` mode allows
you to specify which broker you want to retrieve information from. If absent it takes the current broker
cached by the cli. In non-interactive mode that [target endpoint] can be empty if `-e` option is given,
or it is the target remote endpoint name prefix by a `@` char. For example `@broker1/`

The `component` part is the type of the mbean. Currently the supported mbean types are

- `queue`
- `address`
- `acceptor`
- `cluster-connection`

---

**NOTE**

If the target component is the broker itself, the component should be left empty. For example

`get @broker1/ -a Status`

It means to get the value of `Status` attribute of the broker.

---

The <name> argument is the mbean name (e.g. DLQ, testQueue, etc).

The value of `-a` option is a list of attribute names (space or comma separated) to read from the target mbean.
If the value is a `*` it will read all the attributes of the target mbean.

The value of `-o` option is a list of operation names (space or comma separated) to read from the target mbean.
If the value is a `*` it will read all the operations of the target mbean.

examples:

`get @broker1/` - get the current broker mbean information

`get @broker1/*` - get all mbeans registered with the broker mbean

`get @broker1/ -a *` - read all the attributes of the broker mbean

`get @broker1/ -a * -o *` - read information of all attributes and operations of the broker mbean

`get @broker1/queue` (or `get /queue`) - list all the queue mbeans information

`get @broker1/acceptor acceptor0 -a *` - read all attributes of acceptor named `acceptor0`

`get @broker1/queue TEST -a MessageCount RoutingType` - read `MessageCount` and `RoutingType` of queue `TEST`

`get @broker1/queue -o xxx` - read information of operation xxx of queue TEST

### The `run` command

The `run` command is used to invoke operations on a jolokia endpoint.

The syntax of this command is

```
run <path> <name> <operation>
```

It takes a `path` argument, a `name` argument and an `operation` to be executed on the mbean.
The syntax for `path` and `name` are the same as for the `get` command.

The operation takes the form <operation-name>([args...]). For example

```
run @broker1/ listAddresses(m)
```

It means to call listAddresses(java.lang.String) method on @broker1 with argument value 'm'.

### Commands exclusive to Interactive mode

There are several commands that are only available to interactive mode.

#### The `add` command

Add a jolokia endpoint to the cli cache. Syntax:

```
add <endpoint name> <url> -u <user> -p <password>
```

example:

```
add ex-aao-ssl https://ex-aao-ssl-wconsj-0-svc-rte-default.apps-crc.testing -u user -p password
```

This command allows user to add a jolokia endpoint that is not managed by the api-server. After the
endpoint is added the user can access the endpoint using the cli commands.

---

**NOTE**

When accessing such jolokia endpoints, the command references it without `@` prefixed, for example

`get ex-aao-ssl/ -a Status`

---

#### The `switch` command

To change `current endpoint`. Syntax:

```
switch <endpoint name>
```

example:

```
switch broker0
```

A command can be simplified if it is targeted at the current endpoint. For example if you
set the current endpoint to `broker0` the command

```
> add broker0 http://127.0.0.2:8161 -u guest -p guest
```

and if you want to get the queues of the broker0, you can do

```
get /queues
```

instead of explicitly:

```
get broker0/queues
```

#### The `list` command

To list all the jolokia endpoints cached in cli and managed on the api-server. Syntax:

```
> list
[
"local2(local): http://127.0.0.2:8161",
"@pod-0: https://broker-pem-wconsj-0-svc-ing-default.artemiscloud.io:443",
"@pod-1: https://broker-pem-wconsj-1-svc-ing-default.artemiscloud.io:443",
"@local: http://127.0.0.1:8161",
"@restricted: https://artemis-broker-jolokia-0-svc-ing-default.artemiscloud.io:443"
]
```
7 changes: 4 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: "node",
roots: ['<rootDir>/src'],
testEnvironment: 'node',
transform: {
"^.+.tsx?$": ["ts-jest",{}],
'^.+.tsx?$': ['ts-jest', {}],
},
};
};
22 changes: 18 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "git+ssh://[email protected]:artemiscloud/activemq-activemq-artemis-jolokia-api-server.git"
},
"scripts": {
"build": "yarn clean && tsc -p tsconfig.json && yarn copy-config",
"build": "yarn clean && tsc -p tsconfig.json && yarn copy-config && chmod +x dist/cli/index.js",
"build-api-doc": "yarn openapi-to-md src/config/openapi.yml api.md && yarn pretty-quick",
"clean": "rm -rf dist",
"copy-config": "cp -r src/config dist/config",
Expand All @@ -22,7 +22,9 @@
"test": "NODE_TLS_REJECT_UNAUTHORIZED=0 TZ=UTC jest --runInBand",
"test:coverage": "yarn run test --watch=false --coverage",
"test:generate-output": "yarn test -- --json --outputFile=.jest-test-results.json",
"ts-node": "ts-node -O '{\"module\":\"commonjs\"}'"
"ts-node": "ts-node -O '{\"module\":\"commonjs\"}'",
"apigen": "apigen-ts src/config/openapi.yml src/cli/api-client.ts",
"start-cli": "node --no-warnings ./dist/cli/index.js"
},
"lint-staged": {
"*.{js,ts,tsx}": [
Expand All @@ -35,7 +37,8 @@
"@types/bcryptjs": "^2.4.6",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jest": "27.5.2",
"@types/figlet": "^1.5.8",
"@types/jest": "29.5.14",
"@types/js-yaml": "4.0.5",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^20.10.4",
Expand All @@ -45,14 +48,17 @@
"@types/webpack": "5.28.1",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"apigen-ts": "^0.2.0",
"base-64": "^1.0.0",
"chromatic": "6.10.1",
"copy-webpack-plugin": "11.0.0",
"cors": "^2.8.5",
"eslint": "^8.10.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-unused-imports": "^3.2.0",
"husky": "8.0.0",
"jest": "^27.5.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^27.5.1",
"lint-staged": "13.1.0",
"nock": "^13.5.4",
Expand All @@ -69,18 +75,26 @@
"typescript": "^4.7.4",
"yaml": "^2.4.5"
},
"bin": {
"jolokia-api-server-cli": "./dist/cli/index.js"
},
"files": [
"./dist"
],
"readme": "README.md",
"_id": "[email protected]",
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
"dependencies": {
"base-64": "^1.0.0",
"bcryptjs": "^2.4.3",
"commander": "^12.1.0",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "4.18.2",
"express-openapi-validator": "5.1.2",
"express-pino-logger": "^7.0.0",
"express-rate-limit": "^7.2.0",
"figlet": "^1.7.0",
"js-yaml": "4.1.0",
"jsonwebtoken": "^9.0.2",
"node-fetch": "2",
Expand Down
Loading

0 comments on commit 7b00703

Please sign in to comment.