influxdb/http
Sam Arnold e5ccbb8831
fix: forbid reading OSS buckets for a token with only write permissions (#23148)
* fix: forbid reading OSS buckets for a token with only write permissions

We previously enabled write tokens to also find DBRP buckets, in order to allow
the legacy /write (not /api/v2/write) endpoint to read the DBRP mappings and
find the real bucket id to write to.

This had the unintended consequency of allowing tokens with only write permissions
to read data in buckets via the legacy /query (not /api/v2/query) endpoint with
InfluxQL.

This change fixes the behaviour to allow writing to /write with a write-only
token, while forbidding reading from /query.

* fix: nanosecond precision in tests
2022-02-24 09:59:14 -05:00
..
influxdb chore(query/influxql): remove the influxql transpiler (#23000) 2022-01-31 11:34:37 -05:00
legacy fix: forbid reading OSS buckets for a token with only write permissions (#23148) 2022-02-24 09:59:14 -05:00
metric refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
mock refactor: rewrite imports to include the /v2 suffix for version 2 2020-04-03 12:39:20 -05:00
mocks fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
points chore: use io/os over ioutil (#22656) 2021-10-12 16:55:07 -05:00
README.md chore(http): update error handling example in readme (#13874) 2019-05-13 12:50:25 -07:00
api_handler.go chore(query/influxql): remove the influxql transpiler (#23000) 2022-01-31 11:34:37 -05:00
api_handler_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
auth_service.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
auth_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
authentication_middleware.go refactor: consolidate session-handling code (#22626) 2021-10-06 22:37:02 -04:00
authentication_test.go refactor: consolidate session-handling code (#22626) 2021-10-06 22:37:02 -04:00
backup_service.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
backup_service_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
check_service.go feat: expose task id to checks and notification rules (#21236) 2021-04-15 16:03:39 -07:00
check_test.go build(flux): update flux to v0.148.0 (#23047) 2022-01-11 11:30:01 -08:00
client.go chore: delete old-style DBRP mapping (#22339) 2021-08-30 18:27:11 -04:00
config.go feat: api/v2/config endpoint displays runtime configuration (#23003) 2021-12-23 09:27:39 -05:00
config_test.go feat: api/v2/config endpoint displays runtime configuration (#23003) 2021-12-23 09:27:39 -05:00
debug.go add spans to multiple services 2019-03-07 12:12:31 -07:00
delete_handler.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
delete_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
document_service.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
document_service_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
document_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
duration.go refactor: automated move of errors and id from root to kit (#21101) 2021-03-30 14:10:02 -04:00
duration_test.go feat(http) add microsecond and nanosecond durations 2018-09-25 15:15:06 -05:00
errors.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
errors_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
handler.go feat: set X-Influxdb-Version and X-Influxdb-Build headers (#22535) 2021-09-22 07:30:45 -04:00
handler_test.go feat(http): add config option to disable metrics endpoint in influxd (#20963) 2021-03-15 15:11:23 -04:00
health.go feat(api/health): report commit 2020-05-26 14:03:52 +02:00
health_test.go fix: correct various typos (#19987) 2020-11-11 13:54:21 -05:00
label_service.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
label_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
legacy.go chore(query/influxql): remove the influxql transpiler (#23000) 2022-01-31 11:34:37 -05:00
middleware.go chore: remove unused user http code (#23011) 2021-12-20 15:17:18 -05:00
middleware_test.go fix(http): don't log bodies of V1 write requests 2020-11-23 14:48:20 -05:00
notification_endpoint.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
notification_endpoint_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
notification_rule.go feat: expose task id to checks and notification rules (#21236) 2021-04-15 16:03:39 -07:00
notification_rule_test.go refactor: refactor for tasks backport (#21117) 2021-04-07 14:42:55 -04:00
paging_test.go chore: update staticcheck and fix newly identified lint checks (#18737) 2020-06-26 18:54:09 -05:00
platform_handler.go feat: add `ui-disabled` flag to run server with UI disabled (#21910) 2021-07-21 19:52:57 -06:00
proxy_handler.go chore(http): Let the callers convert these to http.Handler. 2020-05-08 09:02:06 -04:00
query.go chore(query/influxql): remove the influxql transpiler (#23000) 2022-01-31 11:34:37 -05:00
query_handler.go chore(query/influxql): remove the influxql transpiler (#23000) 2022-01-31 11:34:37 -05:00
query_handler_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
query_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
ready.go fix(http): fix /ready response content type 2020-11-05 19:20:57 +01:00
ready_test.go fix(http): fix /ready response content type 2020-11-05 19:20:57 +01:00
redoc.go chore: modify/delete some chronograf refs (#21802) 2021-07-08 09:14:03 -05:00
requests.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
requests_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
resources.go feat: add route to return platform known resources (#22135) 2021-08-10 09:18:06 -06:00
resources_test.go feat: add auth to remotes & replications APIs (#22744) 2021-10-26 11:32:35 -04:00
restore_service.go feat: return new operator token during backup overwrite (#22629) 2021-10-07 09:07:09 -04:00
router.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
router_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
scraper_service.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
scraper_service_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
source_proxy_service.go chore(query/influxql): remove the influxql transpiler (#23000) 2022-01-31 11:34:37 -05:00
source_service.go chore(query/influxql): remove the influxql transpiler (#23000) 2022-01-31 11:34:37 -05:00
source_service_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
swagger.yml chore: add comment to unused swagger definition pointing to replacement (#21283) 2021-05-06 11:23:31 -06:00
swaggerV1Compat.yml fix(influxql): set correct Content-Type on v1 query responses (#20565) 2021-01-22 07:31:52 -08:00
task_service.go feat: passing `type=basic` returns task metadata without query text (#22728) 2021-10-22 09:14:58 -04:00
task_service_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
telegraf.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
telegraf_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
tokens.go feat: Add Bearer token auth (#22498) 2021-10-13 12:02:32 -07:00
tokens_test.go feat: Add Bearer token auth (#22498) 2021-10-13 12:02:32 -07:00
ua.go refactor(http): normalize user-agent names 2019-12-04 15:02:00 -06:00
ua_test.go refactor(http): normalize user-agent names 2019-12-04 15:02:00 -06:00
user_resource_mapping_service.go chore: remove unused user http code (#23011) 2021-12-20 15:17:18 -05:00
user_resource_mapping_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
variable_service.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
variable_test.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
write_handler.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00
write_handler_test.go fix: allow empty reqeust bodies to write API (#22574) 2021-09-24 09:51:39 -04:00
write_usage_recorder.go fix: more expressive errors (#22448) 2021-09-13 15:12:35 -04:00

README.md

HTTP Handler Style Guide

HTTP Handler

  • Each handler should implement http.Handler
    • This can be done by embedding a httprouter.Router (a light weight HTTP router that supports variables in the routing pattern and matches against the request method)
  • Required services should be exported on the struct
// ThingHandler represents an HTTP API handler for things.
type ThingHandler struct {
	// embedded httprouter.Router as a lazy way to implement http.Handler
	*httprouter.Router

	ThingService         platform.ThingService
	AuthorizationService platform.AuthorizationService

	Logger               *zap.Logger
}

HTTP Handler Constructor

  • Routes should be declared in the constructor
// NewThingHandler returns a new instance of ThingHandler.
func NewThingHandler() *ThingHandler {
	h := &ThingHandler{
		Router: httprouter.New(),
		Logger: zap.Nop(),
	}

	h.HandlerFunc("POST", "/api/v2/things", h.handlePostThing)
	h.HandlerFunc("GET", "/api/v2/things", h.handleGetThings)

	return h
}

Route handlers (http.HandlerFuncs)

  • Each route handler should have an associated request struct and decode function
  • The decode function should take a context.Context and an *http.Request and return the associated route request struct
type postThingRequest struct {
	Thing *platform.Thing
}

func decodePostThingRequest(ctx context.Context, r *http.Request) (*postThingRequest, error) {
	t := &platform.Thing{}
	if err := json.NewDecoder(r.Body).Decode(t); err != nil {
		return nil, err
	}

	return &postThingRequest{
		Thing: t,
	}, nil
}
  • Route http.HandlerFuncs should separate the decoding and encoding of HTTP requests/response from actual handler logic
// handlePostThing is the HTTP handler for the POST /api/v2/things route.
func (h *ThingHandler) handlePostThing(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	req, err := decodePostThingRequest(ctx, r)
	if err != nil {
		EncodeError(ctx, err, w)
		return
	}

	// Do stuff here
	if err := h.ThingService.CreateThing(ctx, req.Thing); err != nil {
		EncodeError(ctx, err, w)
		return
	}

	if err := encodeResponse(ctx, w, http.StatusCreated, req.Thing); err != nil {
		h.Logger.Info("encoding response failed", zap.Error(err))
		return
	}
}
  • http.HandlerFunc's that require particular encoding of http responses should implement an encode response function