Skip to content

Commit d61b7fc

Browse files
torsmcopybara-github
authored andcommitted
Add reporting of client ids to the config manager stats collector
PiperOrigin-RevId: 802565927
1 parent ff6ebda commit d61b7fc

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

fleetspeak/src/client/internal/config/manager.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ type Manager struct {
6262
//
6363
// The labels parameter defines what client labels the client should
6464
// report to the server.
65-
// TODO(b/297019580): Consider defining and consuming a more specific `ConfigManagerCollector`
66-
// interface here, containing only the methods that Manager actually cares about.
6765
func StartManager(cfg *config.Configuration, configChanges chan<- *fspb.ClientInfoData, c stats.ConfigManagerCollector) (*Manager, error) {
6866
if cfg == nil {
6967
return nil, errors.New("configuration must be provided")
@@ -107,7 +105,7 @@ func StartManager(cfg *config.Configuration, configChanges chan<- *fspb.ClientIn
107105
r.AddRevokedSerials(r.state.RevokedCertSerials)
108106
r.AddRevokedSerials(cfg.RevokedCertSerials)
109107

110-
if r.state.ClientKey == nil {
108+
if len(r.state.GetClientKey()) == 0 {
111109
if err := r.Rekey(); err != nil {
112110
return nil, fmt.Errorf("no key present, and %v", err)
113111
}
@@ -120,6 +118,7 @@ func StartManager(cfg *config.Configuration, configChanges chan<- *fspb.ClientIn
120118
if err != nil {
121119
return nil, fmt.Errorf("unable to create clientID: %v", err)
122120
}
121+
r.stats.AfterKeyLoaded(r.id, false, nil)
123122
log.Infof("Using client id: %v", r.id)
124123
}
125124

@@ -143,8 +142,9 @@ func StartManager(cfg *config.Configuration, configChanges chan<- *fspb.ClientIn
143142

144143
// Rekey creates a new private key and identity for the client.
145144
func (m *Manager) Rekey() (err error) {
145+
var id common.ClientID
146146
defer func() {
147-
m.stats.AfterRekey(err)
147+
m.stats.AfterKeyLoaded(id, true, err)
148148
}()
149149

150150
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
@@ -155,7 +155,7 @@ func (m *Manager) Rekey() (err error) {
155155
if err != nil {
156156
return fmt.Errorf("unable to marshal new key: %v", err)
157157
}
158-
id, err := common.MakeClientID(k.Public())
158+
id, err = common.MakeClientID(k.Public())
159159
if err != nil {
160160
return fmt.Errorf("unable to create client id: %v", err)
161161
}
@@ -220,7 +220,7 @@ func (m *Manager) AddRevokedSerials(revoked [][]byte) {
220220
}
221221
}
222222

223-
// Stop shuts down the Manager, in particular it will stop sychronizing to the
223+
// Stop shuts down the Manager, in particular it will stop synchronizing to the
224224
// writeback file.
225225
func (m *Manager) Stop() {
226226
if m.syncTicker != nil {
@@ -250,7 +250,7 @@ func (m *Manager) ClientID() common.ClientID {
250250
// RecordRunningService adds name to the list of services which this client is
251251
// currently running. This list will be included when sending a ClientInfo
252252
// record to the server. The optional parameter sig should be set when the
253-
// configuration was signed to emake it clear to the server which instance of
253+
// configuration was signed to make it clear to the server which instance of
254254
// the service is running.
255255
func (m *Manager) RecordRunningService(name string, sig []byte) {
256256
m.lock.Lock()

fleetspeak/src/client/internal/config/manager_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ type statsCollector struct {
3232
stats.ConfigManagerCollector
3333
mu sync.Mutex
3434
rekeys int
35+
ids []common.ClientID
3536
}
3637

37-
func (sc *statsCollector) AfterRekey(err error) {
38+
func (sc *statsCollector) AfterKeyLoaded(id common.ClientID, new bool, err error) {
3839
sc.mu.Lock()
3940
defer sc.mu.Unlock()
40-
sc.rekeys++
41+
sc.ids = append(sc.ids, id)
42+
if new {
43+
sc.rekeys++
44+
}
4145
}
4246

4347
func (sc *statsCollector) AfterConfigSync(err error) {
@@ -72,6 +76,9 @@ func TestRekey(t *testing.T) {
7276
if sc.rekeys != 2 {
7377
t.Errorf("Unexpected amount of rekeys reported, got: %d, want: 2", sc.rekeys)
7478
}
79+
if id1 != sc.ids[0] || id2 != sc.ids[1] {
80+
t.Errorf("Unexpected client IDs reported, got: %v, %v, want: %v, %v", sc.ids[0], sc.ids[1], id1, id2)
81+
}
7582
}
7683

7784
func TestWriteback(t *testing.T) {

fleetspeak/src/client/stats/collector.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package stats
1818

1919
import (
20+
"github.com/google/fleetspeak/fleetspeak/src/common"
21+
2022
fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak"
2123
)
2224

@@ -42,8 +44,11 @@ type ConfigManagerCollector interface {
4244
// AfterConfigSync is called after each config sync attempt by the config manager.
4345
// err is the result of the operation.
4446
AfterConfigSync(err error)
45-
// AfterRekey is called after each rekey attempt by the config manager.
46-
AfterRekey(err error)
47+
// AfterKeyLoaded is called after the config manager loads a client key. This
48+
// happens after reading the persisted state and on rekey operations.
49+
// id is the client ID that corresponds to the loaded key. new is true if the
50+
// key is newly generated, and err is the result of that operation.
51+
AfterKeyLoaded(id common.ClientID, new bool, err error)
4752
}
4853

4954
// ClientCollector gets notified about client operations.
@@ -132,8 +137,8 @@ func (c NoopCollector) MessageAcknowledged(msg *fspb.Message, size int) {}
132137
// AfterConfigSync implements Collector by doing nothing.
133138
func (c NoopCollector) AfterConfigSync(err error) {}
134139

135-
// AfterRekey implements Collector by doing nothing.
136-
func (c NoopCollector) AfterRekey(err error) {}
140+
// AfterKeyLoaded implements Collector by doing nothing.
141+
func (c NoopCollector) AfterKeyLoaded(id common.ClientID, new bool, err error) {}
137142

138143
// AfterMessageProcessed implements Collector by doing nothing.
139144
func (c NoopCollector) AfterMessageProcessed(msg *fspb.Message, isLocal bool, err error) {}

0 commit comments

Comments
 (0)