aboutsummaryrefslogtreecommitdiff
path: root/internal/node/secondary/endpoint_internal.go
blob: f60d6d817f96dbe70cacdceb33a6f1d0a1e17a42 (plain)
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
package secondary

// This file implements internal HTTP handler callbacks for secondary nodes.

import (
	"context"
	"crypto/ed25519"
	"fmt"
	"net/http"

	"git.sigsum.org/log-go/internal/node/handler"
	"git.sigsum.org/sigsum-go/pkg/log"
	"git.sigsum.org/sigsum-go/pkg/merkle"
	"git.sigsum.org/sigsum-go/pkg/types"
)

func getTreeHeadToCosign(ctx context.Context, c handler.Config, w http.ResponseWriter, _ *http.Request) (int, error) {
	s := c.(Secondary)
	log.Debug("handling get-tree-head-to-cosign request")

	signedTreeHead := func() (*types.SignedTreeHead, error) {
		tctx, cancel := context.WithTimeout(ctx, s.Config.Deadline)
		defer cancel()
		th, err := treeHeadFromTrillian(tctx, s.TrillianClient)
		if err != nil {
			return nil, fmt.Errorf("getting tree head: %w", err)
		}
		namespace := merkle.HashFn(s.Signer.Public().(ed25519.PublicKey))
		sth, err := th.Sign(s.Signer, namespace)
		if err != nil {
			return nil, fmt.Errorf("signing tree head: %w", err)
		}
		return sth, nil
	}

	sth, err := signedTreeHead()
	if err != nil {
		return http.StatusInternalServerError, err
	}
	if err := sth.ToASCII(w); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}