Skip to content

Commit

Permalink
Creating a new script to ban nodes (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 authored Jun 14, 2024
1 parent a7a7ae9 commit c839360
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
38 changes: 38 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Scripts

## Create JWT Token

This script is used to create JWT Token representing an existing user which can then be used to invoke the API Server as an alternative authentication method to firebase auth.

```bash
export JWT_SECRET
go run ./scripts/create-jwt-token --user-id "<existing user id>"
```

Notes:

1. You need to use the same values as the [JWT_SECRET](./../docker-compose.yml#L20) environment variables defined for the API Server.
2. By default, the token will [expire in 30 days](./create-jwt-token/main.go#L14), which can be overriden using `--expiry` flag.

## Ban a Publisher

This script is used to invoke ban publisher API using jwt token as authentication.

```bash
go run ./scripts/ban-publisher \
--base-url "<base url of the API>" \
--token "<jwt token representing an admin user>" \
--publisher-id "<publisher id to be banned>"
```

## Ban a Node

This script is used to invoke ban publisher API using jwt token as authentication.

```bash
go run ./scripts/ban-node \
--base-url "<base url of the API>" \
--token "<jwt token representing an admin user>" \
--publisher-id "<publisher id of the node>"
--node-id "<node id to be banned>"
```
59 changes: 59 additions & 0 deletions scripts/ban-node/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
)

var pubID = flag.String("publisher-id", "", "Pubisher ID of the node")
var nodeID = flag.String("node-id", "", "Node ID to be banned")
var token = flag.String("token", os.Getenv("JWT_TOKEN"), "JWT token to use for authentication")
var baseURL = flag.String("base-url", os.Getenv("REGISTRY_BASE_URL"), "Base url of registry service")

func main() {
flag.Parse()

if pubID == nil || *pubID == "" {
flag.PrintDefaults()
log.Fatalf("Flag `--publisher-id` must be set to non-empty string.\n")
}
if nodeID == nil || *nodeID == "" {
flag.PrintDefaults()
log.Fatalf("Flag `--node-id` must be set to non-empty string.\n")
}
if token == nil || *token == "" {
flag.PrintDefaults()
log.Fatalf("Flag `--token` or environment variable `JWT_TOKEN` must be set to non-empty string.\n")
}
if baseURL == nil || *baseURL == "" {
flag.PrintDefaults()
log.Fatalf("Flag `--base-url` or environment variable `BASE_URL` must be set to non-empty string.\n")
}

u, err := url.Parse(*baseURL)
if err != nil {
log.Fatalf("Invalid base url :%v .\n", err)
}
u = u.JoinPath("publishers", *pubID, "nodes", *nodeID, "ban")

req, _ := http.NewRequest(http.MethodPost, u.String(), nil)
req.Header.Set("Authorization", "Bearer "+*token)
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("Failed to send HTTP request :%v\n", err)
}
b, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("Failed to read HTTP response :%v\n", err)
}
if res.StatusCode > 299 {
log.Fatalf("Received non-success response: \nStatus: %d\nBody: \n\n%v\n", res.StatusCode, string(b))
}
fmt.Printf("Publisher's node '%s' has been banned\n", *pubID)

}

0 comments on commit c839360

Please sign in to comment.