feat: add whitelist management hooks for querying and mutating whitelist entries
This commit is contained in:
@@ -156,4 +156,31 @@ export async function getCrowdsecKeyStatus(): Promise<CrowdSecKeyStatus> {
|
||||
return resp.data
|
||||
}
|
||||
|
||||
export default { startCrowdsec, stopCrowdsec, statusCrowdsec, importCrowdsecConfig, exportCrowdsecConfig, listCrowdsecFiles, readCrowdsecFile, writeCrowdsecFile, listCrowdsecDecisions, banIP, unbanIP, getCrowdsecKeyStatus }
|
||||
export interface CrowdSecWhitelistEntry {
|
||||
uuid: string
|
||||
ip_or_cidr: string
|
||||
reason: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface AddWhitelistPayload {
|
||||
ip_or_cidr: string
|
||||
reason: string
|
||||
}
|
||||
|
||||
export const listWhitelists = async (): Promise<CrowdSecWhitelistEntry[]> => {
|
||||
const resp = await client.get<{ whitelist: CrowdSecWhitelistEntry[] }>('/admin/crowdsec/whitelist')
|
||||
return resp.data.whitelist
|
||||
}
|
||||
|
||||
export const addWhitelist = async (data: AddWhitelistPayload): Promise<CrowdSecWhitelistEntry> => {
|
||||
const resp = await client.post<CrowdSecWhitelistEntry>('/admin/crowdsec/whitelist', data)
|
||||
return resp.data
|
||||
}
|
||||
|
||||
export const deleteWhitelist = async (uuid: string): Promise<void> => {
|
||||
await client.delete(`/admin/crowdsec/whitelist/${uuid}`)
|
||||
}
|
||||
|
||||
export default { startCrowdsec, stopCrowdsec, statusCrowdsec, importCrowdsecConfig, exportCrowdsecConfig, listCrowdsecFiles, readCrowdsecFile, writeCrowdsecFile, listCrowdsecDecisions, banIP, unbanIP, getCrowdsecKeyStatus, listWhitelists, addWhitelist, deleteWhitelist }
|
||||
|
||||
38
frontend/src/hooks/useCrowdSecWhitelist.ts
Normal file
38
frontend/src/hooks/useCrowdSecWhitelist.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
|
||||
import { listWhitelists, addWhitelist, deleteWhitelist, type AddWhitelistPayload } from '../api/crowdsec'
|
||||
import { toast } from '../utils/toast'
|
||||
|
||||
export const useWhitelistEntries = () =>
|
||||
useQuery({
|
||||
queryKey: ['crowdsec-whitelist'],
|
||||
queryFn: listWhitelists,
|
||||
})
|
||||
|
||||
export const useAddWhitelist = () => {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (data: AddWhitelistPayload) => addWhitelist(data),
|
||||
onSuccess: () => {
|
||||
toast.success('Whitelist entry added')
|
||||
queryClient.invalidateQueries({ queryKey: ['crowdsec-whitelist'] })
|
||||
},
|
||||
onError: (err: unknown) => {
|
||||
toast.error(err instanceof Error ? err.message : 'Failed to add whitelist entry')
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteWhitelist = () => {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (uuid: string) => deleteWhitelist(uuid),
|
||||
onSuccess: () => {
|
||||
toast.success('Whitelist entry removed')
|
||||
queryClient.invalidateQueries({ queryKey: ['crowdsec-whitelist'] })
|
||||
},
|
||||
onError: (err: unknown) => {
|
||||
toast.error(err instanceof Error ? err.message : 'Failed to remove whitelist entry')
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user