Skip to content

Commit

Permalink
Merge branch 'fossology:main' into feat/issue-37
Browse files Browse the repository at this point in the history
  • Loading branch information
dvjsharma committed Jan 11, 2024
2 parents e5d4687 + ee4be56 commit ad17bb4
Show file tree
Hide file tree
Showing 18 changed files with 519 additions and 137 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only
# SPDX-FileCopyrightText: FOSSology contributors

# How long the token can be valid
TOKEN_HOUR_LIFESPAN=24
# Secret key to sign tokens (openssl rand -hex 32)
API_SECRET=some-random-string
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*.so
*.dylib
/laas
/.env

# Test binary, built with `go test -c`
*.test
Expand Down
61 changes: 41 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,35 @@ and changes.
- **audits** table has the data of audits that are done in obligations or licenses
- **change_logs** table has all the change history of a particular audit.

![alt text](./docs/assets/licensedb_erd.png)
![ER Diagram](./docs/assets/licensedb_erd.png)

### APIs
## APIs

There are multiple API endpoints for licenses, obligations, user and audit
endpoints.

### API endpoints

| # | Method | API Endpoints | Examples | Descriptions |
| --- | --------- | ---------------------------------- | ------------------------------------- | ------------------------------------------------------------------------------------- |
| 1 | **GET** | `/api/licenses/:shortname` | /api/licenses/MIT | Gets all data related to licenses by their shortname |
| 2 | **GET** | `/api/licenses/` | /api/licenses/copyleft="t"&active="t" | Get filter the licenses as per the filters |
| 3 | **POST** | `/api/licenses` | /api/licenses | Create a license with unique shortname |
| 4 | **POST** | `/api/licenses/search` | /api/licenses/search | Get the licenses with the post request filtered by field, search term and type |
| 5 | **PATCH** | `/api/licenses/:shortname` | /api/licenses/MIT | It updates the particular fields as requested of the license with shortname |
| 6 | **GET** | `/api/users` | /api/users | Get all the users and their data |
| 7 | **GET** | `/api/users/:id` | /api/users/1 | Get data relate to user by its id |
| 8 | **POST** | `/api/users` | /api/users | Create a user with unique data |
| 9 | **GET** | `/api/obligations` | /api/obligations | Get all the obligations |
| 10 | **GET** | `/api/obligation/:topic` | /api/obligation/topic | Gets all data related to obligations by their topic |
| 11 | **POST** | `/api/obligations` | /api/obligations | Create an obligation as well as add it to obligation map |
| 12 | **PATCH** | `/api/obligations/:topic` | /api/obligations | It updates the particular fields as requested of the obligation with topic |
| 13 | **GET** | `/api/audit` | /api/audit | Get the audit history of all the licenses and obligations |
| 14 | **GET** | `/api/audit/:audit_id` | /api/audit/1 | Get the data of a particular audit by its id |
| 15 | **GET** | `/api/audit/:audit_id/changes` | /api/audit/1/changes | Get the change logs of the particular audit id |
| 16 | **GET** | `/api/audit/:audit_id/changes/:id` | /api/audit/1/changes/2 | Get a particular change log of the particular audit id |
Check the OpenAPI documentation for the API endpoints at
[cmd/laas/docs/swagger.yaml](https://github.com/fossology/LicenseDb/blob/main/cmd/laas/docs/swagger.yaml).

The same can be viewed by Swagger UI plugin after installing and running the
tool at [http://localhost:8080/swagger/index.html](http://localhost:8080/swagger/index.html).

### Authentication

To get the access token, send a POST request to `/api/v1/login` with the
username and password.

```bash
curl -X POST "http://localhost:8080/api/v1/login" \
-H "accept: application/json" -H "Content-Type: application/json" \
-d "{ \"username\": \"<username>\", \"password\": \"<password>\"}"
```

As the response of the request, a JWT will be returned. Use this JWT with the
`Authorization` header (as `-H "Authorization: <JWT>"`) to access endpoints
requiring authentication.

## Prerequisite

Expand All @@ -75,6 +77,14 @@ cd LicenseDb
go build ./cmd/laas
```

- Create the `.env` file in the root directory of the project and change the
values of the environment variables as per your requirement.

```bash
cp .env.example .env
vim .env
```

- Run the executable.

```bash
Expand All @@ -87,6 +97,17 @@ go build ./cmd/laas
go run ./cmd/laas
```

### Create first user
Connect to the database using `psql` with the following command.
```bash
psql -h localhost -p 5432 -U fossy -d fossology
```

Run the following query to create the first user.
```sql
INSERT INTO users (username, userpassword, userlevel) VALUES ('<username>', '<password>', 'admin');
```

### Generating Swagger Documentation
1. Install [swag](https://github.com/swaggo/swag) using the following command.
```bash
Expand Down
91 changes: 75 additions & 16 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const docTemplate = `{
"get": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Get all audit records from the server",
Expand Down Expand Up @@ -62,7 +62,7 @@ const docTemplate = `{
"get": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Get a specific audit records by ID",
Expand Down Expand Up @@ -112,7 +112,7 @@ const docTemplate = `{
"get": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Get changelogs of an audit record",
Expand Down Expand Up @@ -162,7 +162,7 @@ const docTemplate = `{
"get": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Get a specific changelog of an audit record by its ID",
Expand Down Expand Up @@ -333,7 +333,7 @@ const docTemplate = `{
"post": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Create a new license in the service",
Expand Down Expand Up @@ -428,7 +428,7 @@ const docTemplate = `{
"patch": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Update a license in the service",
Expand Down Expand Up @@ -495,6 +495,46 @@ const docTemplate = `{
}
}
},
"/login": {
"post": {
"description": "Login to get JWT token",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Users"
],
"summary": "Login",
"operationId": "Login",
"parameters": [
{
"description": "Login credentials",
"name": "user",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/models.UserLogin"
}
}
],
"responses": {
"200": {
"description": "JWT token",
"schema": {
"type": "object",
"properties": {
"token": {
"type": "string"
}
}
}
}
}
}
},
"/obligation_maps/license/{license}": {
"get": {
"description": "Get obligation maps for a given license shortname",
Expand Down Expand Up @@ -577,7 +617,7 @@ const docTemplate = `{
"put": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Replaces the license list of an obligation topic with the given list in the obligation map.",
Expand Down Expand Up @@ -634,7 +674,7 @@ const docTemplate = `{
"patch": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Add or remove licenses from obligation map for a given obligation topic",
Expand Down Expand Up @@ -736,7 +776,7 @@ const docTemplate = `{
"post": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Create an obligation and associate it with licenses",
Expand Down Expand Up @@ -831,7 +871,7 @@ const docTemplate = `{
"delete": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Deactivate an obligation",
Expand Down Expand Up @@ -870,7 +910,7 @@ const docTemplate = `{
"patch": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Update an existing obligation record",
Expand Down Expand Up @@ -982,7 +1022,7 @@ const docTemplate = `{
"get": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Get all service users",
Expand Down Expand Up @@ -1015,7 +1055,7 @@ const docTemplate = `{
"post": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Create a new service user",
Expand Down Expand Up @@ -1067,7 +1107,7 @@ const docTemplate = `{
"get": {
"security": [
{
"BasicAuth": []
"ApiKeyAuth": []
}
],
"description": "Get a single user by ID",
Expand Down Expand Up @@ -1812,6 +1852,22 @@ const docTemplate = `{
}
}
},
"models.UserLogin": {
"type": "object",
"required": [
"password",
"username"
],
"properties": {
"password": {
"type": "string"
},
"username": {
"type": "string",
"example": "fossy"
}
}
},
"models.UserResponse": {
"type": "object",
"properties": {
Expand All @@ -1832,8 +1888,11 @@ const docTemplate = `{
}
},
"securityDefinitions": {
"BasicAuth": {
"type": "basic"
"ApiKeyAuth": {
"description": "Token from /login endpoint",
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}`
Expand Down
Loading

0 comments on commit ad17bb4

Please sign in to comment.