File: debug.go

package info (click to toggle)
golang-github-canonical-candid 1.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,016 kB
  • sloc: python: 1,903; sh: 235; makefile: 81
file content (72 lines) | stat: -rw-r--r-- 1,874 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package debug

import (
	"context"
	"net/http"

	"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery"
	"github.com/juju/loggo"
	"github.com/juju/utils/v2/debugstatus"
	"gopkg.in/httprequest.v1"

	"github.com/canonical/candid/internal/identity"
	"github.com/canonical/candid/version"
)

var logger = loggo.GetLogger("candid.internal.debug")

var stdCheckers = []debugstatus.CheckerFunc{
	debugstatus.ServerStartTime,
}

func NewAPIHandler(params identity.HandlerParams) ([]httprequest.Handler, error) {
	h := newDebugAPIHandler(params)
	handlers := []httprequest.Handler{{
		Method: "GET",
		Path:   "/debug/login",
		Handle: h.login,
	}, {
		Method: "POST",
		Path:   "/debug/login",
		Handle: h.login,
	}}
	for _, hnd := range identity.ReqServer.Handlers(h.handler) {
		handlers = append(handlers, hnd)
	}
	return handlers, nil
}

func newDebugAPIHandler(params identity.HandlerParams) *debugAPIHandler {
	h := &debugAPIHandler{
		key:      params.Key,
		location: params.Location,
		teams:    params.DebugTeams,
	}
	checkerFuncs := append(stdCheckers, params.DebugStatusCheckerFuncs...)
	h.hnd = debugstatus.Handler{
		Check: func(ctx context.Context) map[string]debugstatus.CheckResult {
			// TODO (mhilton) re-instate meeting status checks.
			return debugstatus.Check(ctx, checkerFuncs...)
		},
		Version:           debugstatus.Version(version.VersionInfo),
		CheckPprofAllowed: h.checkLogin,
		CheckTraceAllowed: func(r *http.Request) (bool, error) {
			return false, h.checkLogin(r)
		},
	}
	return h
}

type debugAPIHandler struct {
	key      *bakery.KeyPair
	location string
	teams    []string
	hnd      debugstatus.Handler
}

func (h *debugAPIHandler) handler(p httprequest.Params) (*debugstatus.Handler, context.Context, error) {
	return &h.hnd, p.Context, nil
}