Skip to content

Commit ead7a20

Browse files
committed
(feature) List source of API Keys
1 parent 8b7746b commit ead7a20

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

backend/src/api/keys/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ApiResponseHandler from "api-response-handler"
22
import { Request, Response } from "express"
33
import { AppDataSource } from "data-source"
44
import { ApiKey } from "models"
5+
import { ApiKey as ApiKeyType } from "@common/types"
56
import Error404NotFound from "errors/error-404-not-found"
67
import { createApiKey } from "./service"
78
import Error400BadRequest from "errors/error-400-bad-request"
@@ -10,10 +11,11 @@ export const listKeys = async (req: Request, res: Response): Promise<void> => {
1011
const keys = await AppDataSource.getRepository(ApiKey).find()
1112
return ApiResponseHandler.success(
1213
res,
13-
keys.map(v => ({
14+
keys.map<ApiKeyType>(v => ({
1415
name: v.name,
1516
identifier: `metlo.${v.keyIdentifier}`,
16-
created: v.createdAt
17+
created: v.createdAt.toISOString(),
18+
for: v.for
1719
})),
1820
)
1921
}
@@ -41,7 +43,8 @@ export const createKey = async (req: Request, res: Response): Promise<void> => {
4143
apiKey: rawKey,
4244
name: key.name,
4345
identifier: `metlo.${key.keyIdentifier}`,
44-
created: key.createdAt
46+
created: key.createdAt.toISOString(),
47+
for: key.for
4548
})
4649
}
4750

backend/src/api/keys/service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { ApiKey } from "models"
33
import { hasher } from "utils/hash"
44

55
export const createApiKey = (keyName: string): [ApiKey, string] => {
6+
/**
7+
* Generate a Generic API Key by default. If needed for AWS/GCP, modify the `for` attribute in the key object
8+
*/
69
const buf = Buffer.alloc(30)
710
const key = crypto.randomFillSync(buf)
811
const b64Key = key.toString("base64")

backend/src/suricata_setup/gcp-services/gcp_setup.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MachineSpecifications, STEP_RESPONSE } from "@common/types"
2-
import { ConnectionType, GCP_SOURCE_TYPE, GCP_STEPS } from "@common/enums"
2+
import { API_KEY_TYPE, ConnectionType, GCP_SOURCE_TYPE, GCP_STEPS } from "@common/enums"
33
import { GCP_CONN } from "./gcp_apis"
44
import AsyncRetry from "async-retry"
55
import { promisify } from "util"
@@ -839,6 +839,7 @@ export async function push_files({
839839
const endpoint = "api/v1/log-request/single"
840840
const instance_name = instance_url.split("/").at(-1)
841841
let [key, raw] = createApiKey(`Metlo-collector-${id}`)
842+
key.for = API_KEY_TYPE.GCP
842843
let api_key = await AppDataSource.getRepository(ApiKey).save(key)
843844

844845
try {
@@ -869,11 +870,11 @@ export async function push_files({
869870

870871
const fileMap = [
871872
path.normalize(`${__dirname}/../generics/scripts/install.sh`) +
872-
` ${instance_name}:~/install.sh`,
873+
` ${instance_name}:~/install.sh`,
873874
path.normalize(`${__dirname}/../generics/scripts/install-deps.sh`) +
874-
` ${instance_name}:~/install-deps.sh`,
875+
` ${instance_name}:~/install-deps.sh`,
875876
path.normalize(`${__dirname}/../generics/scripts/suricata.yaml`) +
876-
` ${instance_name}:~/suricata.yaml`,
877+
` ${instance_name}:~/suricata.yaml`,
877878
filepath_rules_out + ` ${instance_name}:"~/local.rules"`,
878879
filepath_ingestor_out + ` ${instance_name}:~/metlo-ingestor.service`,
879880
]

backend/src/suricata_setup/ssh-services/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConnectionType } from "@common/enums"
1+
import { API_KEY_TYPE, ConnectionType } from "@common/enums"
22
import { STEP_RESPONSE } from "@common/types"
33
import { createApiKey } from "api/keys/service"
44
import { randomUUID } from "crypto"
@@ -69,6 +69,7 @@ export async function push_files({
6969
const endpoint = "api/v1/log-request/single"
7070
let conn = new SSH_CONN(keypair, remote_machine_url, username)
7171
let [key, raw] = createApiKey(`Metlo-collector-${id}`)
72+
key.for = API_KEY_TYPE.AWS
7273
let api_key = await AppDataSource.getRepository(ApiKey).save(key)
7374
try {
7475
let filepath_ingestor = `${__dirname}/../generics/scripts/metlo-ingestor-${randomUUID()}.service`

common/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
GCP_SOURCE_TYPE,
1818
AuthType,
1919
AttackType,
20+
API_KEY_TYPE,
2021
} from "./enums"
2122
import { Test, Request as TestRequest } from "@metlo/testing"
2223
import "axios"
@@ -491,4 +492,5 @@ export interface ApiKey {
491492
name: string
492493
identifier: string
493494
created: string
495+
for: API_KEY_TYPE
494496
}

frontend/src/components/Keys/list.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Button, useColorMode } from "@chakra-ui/react"
1+
import { Badge, Box, Button, useColorMode } from "@chakra-ui/react"
22
import { ApiKey } from "@common/types"
33
import { deleteKey } from "api/keys"
44
import EmptyView from "components/utils/EmptyView"
@@ -29,7 +29,7 @@ const ListKeys: React.FC<ListKeysInterface> = ({ keys, setKeys }) => {
2929
let columns: Array<TableColumn<ApiKey>> = [
3030
{
3131
name: "Name",
32-
sortable: false,
32+
sortable: true,
3333
selector: (row: ApiKey) => row.name,
3434
cell: (row: ApiKey) => row.name,
3535
id: "name",
@@ -49,6 +49,12 @@ const ListKeys: React.FC<ListKeysInterface> = ({ keys, setKeys }) => {
4949
DateTime.fromISO(row.created).toFormat("yyyy-MM-dd"),
5050
id: "created_at",
5151
},
52+
{
53+
name: "Key Used For",
54+
sortable: false,
55+
selector: (row: ApiKey) => row.for,
56+
cell: (row: ApiKey) => <Badge fontFamily={"mono"}>{row.for}</Badge>,
57+
},
5258
{
5359
name: "",
5460
sortable: false,

0 commit comments

Comments
 (0)