File: loglist_refresher.go

package info (click to toggle)
golang-github-google-certificate-transparency 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,764 kB
  • sloc: sh: 606; makefile: 103; sql: 16
file content (109 lines) | stat: -rw-r--r-- 3,144 bytes parent folder | download | duplicates (3)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package submission

import (
	"bytes"
	"fmt"
	"net/http"
	"sync"
	"time"

	"github.com/google/certificate-transparency-go/loglist3"
	"github.com/google/certificate-transparency-go/x509util"
)

const (
	// HttpClientTimeout timeout for Log list reader http client.
	httpClientTimeout = 10 * time.Second
)

// LogListData wraps info on external LogList, keeping its JSON source and time
// of download.
type LogListData struct {
	JSON         []byte
	List         *loglist3.LogList
	DownloadTime time.Time
}

// LogListRefresher is interface for Log List updates watcher.
type LogListRefresher interface {
	Refresh() (*LogListData, error)
	LastJSON() []byte
	Source() string
}

// logListRefresherImpl regularly reads Log-list and emits notifications when
// updates/errors observed. Implements LogListRefresher interface.
type logListRefresherImpl struct {
	// updateMu limits LogListRefresherImpl to a single Refresh() at a time.
	updateMu sync.RWMutex
	lastJSON []byte
	path     string
	client   *http.Client
}

// NewCustomLogListRefresher creates and inits a LogListRefresherImpl instance.
func NewCustomLogListRefresher(client *http.Client, llPath string) LogListRefresher {
	return &logListRefresherImpl{
		path:   llPath,
		client: client,
	}
}

// NewLogListRefresher creates and inits a LogListRefresherImpl instance using
// default http.Client
func NewLogListRefresher(llPath string) LogListRefresher {
	return NewCustomLogListRefresher(&http.Client{Timeout: httpClientTimeout}, llPath)
}

// Refresh fetches the log list and returns its source, formed LogList and
// timestamp if source has changed compared to previous Refresh.
func (llr *logListRefresherImpl) Refresh() (*LogListData, error) {
	llr.updateMu.Lock()
	defer llr.updateMu.Unlock()

	t := time.Now()
	json, err := x509util.ReadFileOrURL(llr.path, llr.client)
	if err != nil {
		return nil, fmt.Errorf("failed to read %q: %v", llr.path, err)
	}

	if bytes.Equal(json, llr.lastJSON) {
		return nil, nil
	}

	ll, err := loglist3.NewFromJSON(json)
	if err != nil {
		return nil, fmt.Errorf("failed to parse %q: %v", llr.path, err)
	}
	llr.lastJSON = json
	return &LogListData{JSON: json, List: ll, DownloadTime: t}, nil
}

// LastJSON returns last version of Log list in JSON.
func (llr *logListRefresherImpl) LastJSON() []byte {
	llr.updateMu.Lock()
	defer llr.updateMu.Unlock()
	if llr.lastJSON == nil {
		return []byte{}
	}
	return llr.lastJSON
}

// Source exposes internal Log list path.
func (llr *logListRefresherImpl) Source() string {
	return llr.path
}