documented rate limiting, added check if file already exists and error out if it does, and small swagger edits

This commit is contained in:
grumbulon 2023-01-20 20:33:56 -05:00
parent 6c709b97a1
commit 27fd45a1f9
6 changed files with 40 additions and 19 deletions

View file

@ -19,7 +19,7 @@ const docTemplate = `{
"paths": {
"/api/login": {
"post": {
"description": "login",
"description": "login to Pomme\nRate limited: 5 requests every 5 second",
"consumes": [
"application/json"
],
@ -29,7 +29,7 @@ const docTemplate = `{
"tags": [
"accounts"
],
"summary": "auth a regular user",
"summary": "authenticate as a regular user",
"parameters": [
{
"type": "string",
@ -69,7 +69,7 @@ const docTemplate = `{
"Bearer": []
}
],
"description": "parse your zonefile -- you must specify \"Bearer\" before entering your token",
"description": "parse your zonefile\nRate limited: 10 requests every 10 second\nyou must specify \"Bearer\" before entering your token",
"consumes": [
"multipart/form-data"
],
@ -119,7 +119,7 @@ const docTemplate = `{
"Bearer": []
}
],
"description": "upload a file -- you must specify \"Bearer\" before entering your token",
"description": "upload takes files from the user and stores it locally to be parsed. Uploads are associated with a specific user.\nRate limited: 10 requests every 10 second\nyou must specify \"Bearer\" before entering your token",
"consumes": [
"multipart/form-data"
],

View file

@ -10,7 +10,7 @@
"paths": {
"/api/login": {
"post": {
"description": "login",
"description": "login to Pomme\nRate limited: 5 requests every 5 second",
"consumes": [
"application/json"
],
@ -20,7 +20,7 @@
"tags": [
"accounts"
],
"summary": "auth a regular user",
"summary": "authenticate as a regular user",
"parameters": [
{
"type": "string",
@ -60,7 +60,7 @@
"Bearer": []
}
],
"description": "parse your zonefile -- you must specify \"Bearer\" before entering your token",
"description": "parse your zonefile\nRate limited: 10 requests every 10 second\nyou must specify \"Bearer\" before entering your token",
"consumes": [
"multipart/form-data"
],
@ -110,7 +110,7 @@
"Bearer": []
}
],
"description": "upload a file -- you must specify \"Bearer\" before entering your token",
"description": "upload takes files from the user and stores it locally to be parsed. Uploads are associated with a specific user.\nRate limited: 10 requests every 10 second\nyou must specify \"Bearer\" before entering your token",
"consumes": [
"multipart/form-data"
],

View file

@ -37,7 +37,9 @@ paths:
post:
consumes:
- application/json
description: login
description: |-
login to Pomme
Rate limited: 5 requests every 5 second
parameters:
- description: Username
in: query
@ -60,15 +62,17 @@ paths:
description: Unauthorized
schema:
$ref: '#/definitions/api.httpError'
summary: auth a regular user
summary: authenticate as a regular user
tags:
- accounts
/api/parse:
post:
consumes:
- multipart/form-data
description: parse your zonefile -- you must specify "Bearer" before entering
your token
description: |-
parse your zonefile
Rate limited: 10 requests every 10 second
you must specify "Bearer" before entering your token
parameters:
- description: Zonefile name
in: query
@ -100,8 +104,10 @@ paths:
post:
consumes:
- multipart/form-data
description: upload a file -- you must specify "Bearer" before entering your
token
description: |-
upload takes files from the user and stores it locally to be parsed. Uploads are associated with a specific user.
Rate limited: 10 requests every 10 second
you must specify "Bearer" before entering your token
parameters:
- description: Zonefile to upload
in: formData

View file

@ -31,8 +31,11 @@ type httpInternalServerError struct {
// Auth godoc
//
// @Summary auth a regular user
// @Description login
// @Summary authenticate as a regular user
// @Description login to Pomme
//
// @Description Rate limited: 5 requests every 5 second
//
// @Tags accounts
// @Accept json
// @Produce json

View file

@ -34,7 +34,11 @@ type Zone struct {
// Upload godoc
//
// @Summary upload a zonefile
// @Description upload a file -- you must specify "Bearer" before entering your token
// @Description upload takes files from the user and stores it locally to be parsed. Uploads are associated with a specific user.
//
// @Description Rate limited: 10 requests every 10 second
// @Description you must specify "Bearer" before entering your token
//
// @Tags DNS
// @Accept mpfd
// @Produce json
@ -71,7 +75,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
}
if err = util.MakeLocal(name[0], claims["username"].(string), buf); err != nil {
internalServerError(w, "internal server error")
internalServerError(w, err.Error())
return
}
@ -98,7 +102,11 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
// Parse godoc
//
// @Summary parse your zonefile
// @Description parse your zonefile -- you must specify "Bearer" before entering your token
// @Description parse your zonefile
//
// @Description Rate limited: 10 requests every 10 second
// @Description you must specify "Bearer" before entering your token
//
// @Tags DNS
// @Accept mpfd
// @Produce json

View file

@ -7,6 +7,10 @@ import (
)
func MakeLocal(filename, username string, buf bytes.Buffer) error {
if _, err := os.Stat(fmt.Sprintf("/tmp/tmpfile-%s-%s", filename, username)); !os.IsNotExist(err) {
return fmt.Errorf("file %s already exists: %w", filename, err)
}
defer buf.Reset()
f, err := os.Create("/tmp/tmpfile-" + filename + "-" + username) //nolint: gosec