Skip to content

Commit 4f1c136

Browse files
fix: resolve node platform CSA key at service start
1 parent df196e1 commit 4f1c136

2 files changed

Lines changed: 56 additions & 20 deletions

File tree

core/services/chainlink/node_platform.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/smartcontractkit/chainlink-common/pkg/timeutil"
1212
commonv1 "github.com/smartcontractkit/chainlink-protos/node-platform/common/v1"
1313
"github.com/smartcontractkit/chainlink/v2/core/logger"
14+
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
1415
"github.com/smartcontractkit/chainlink/v2/core/static"
1516
)
1617

@@ -32,6 +33,7 @@ type NodePlatformBuildInfoService struct {
3233
type NodePlatformBuildInfoConfig struct {
3334
Beat time.Duration
3435
Lggr logger.Logger
36+
CSAKeyStore keystore.CSA
3537
CSAPublicKey string
3638
CommitSHA string
3739
DockerTag string
@@ -40,18 +42,6 @@ type NodePlatformBuildInfoConfig struct {
4042
}
4143

4244
func NewNodePlatformBuildInfoConfig(opts ApplicationOpts) NodePlatformBuildInfoConfig {
43-
csaKey := ""
44-
csaKeys, err := opts.KeyStore.CSA().GetAll()
45-
if err != nil {
46-
opts.Logger.Errorw("failed to get CSA keys for node-platform build info", "err", err)
47-
}
48-
49-
if len(csaKeys) > 0 {
50-
csaKey = csaKeys[0].PublicKeyString()
51-
} else {
52-
opts.Logger.Warn("no CSA key found for node-platform build info")
53-
}
54-
5545
version := opts.Version
5646
if version == "" {
5747
version = static.Version
@@ -68,13 +58,13 @@ func NewNodePlatformBuildInfoConfig(opts ApplicationOpts) NodePlatformBuildInfoC
6858
}
6959

7060
return NodePlatformBuildInfoConfig{
71-
Beat: opts.Config.Telemetry().HeartbeatInterval(),
72-
Lggr: opts.Logger,
73-
CSAPublicKey: csaKey,
74-
CommitSHA: static.Sha,
75-
DockerTag: dockerTag,
76-
VersionTag: versionTag,
77-
Version: version,
61+
Beat: opts.Config.Telemetry().HeartbeatInterval(),
62+
Lggr: opts.Logger,
63+
CSAKeyStore: opts.KeyStore.CSA(),
64+
CommitSHA: static.Sha,
65+
DockerTag: dockerTag,
66+
VersionTag: versionTag,
67+
Version: version,
7868
}
7969
}
8070

@@ -93,11 +83,26 @@ func NewNodePlatformBuildInfoService(cfg NodePlatformBuildInfoConfig) NodePlatfo
9383
return s
9484
}
9585

96-
func (s *NodePlatformBuildInfoService) start(_ context.Context) error {
86+
func (s *NodePlatformBuildInfoService) start(ctx context.Context) error {
87+
s.resolveCSAPublicKey(ctx)
9788
s.eng.GoTick(timeutil.NewTicker(s.GetBeat), s.emit)
9889
return nil
9990
}
10091

92+
func (s *NodePlatformBuildInfoService) resolveCSAPublicKey(ctx context.Context) {
93+
if s.opts.CSAKeyStore == nil {
94+
return
95+
}
96+
97+
csaKey, err := keystore.GetDefault(ctx, s.opts.CSAKeyStore)
98+
if err != nil {
99+
s.eng.Errorw("failed to resolve CSA key for node-platform build info", "err", err)
100+
return
101+
}
102+
103+
s.opts.CSAPublicKey = csaKey.PublicKeyString()
104+
}
105+
101106
func (s *NodePlatformBuildInfoService) emit(ctx context.Context) {
102107
payloadBytes, err := proto.Marshal(&commonv1.NodeBuildInfo{
103108
CsaPublicKey: s.opts.CSAPublicKey,

core/services/chainlink/node_platform_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import (
44
"testing"
55
"time"
66

7+
"github.com/stretchr/testify/mock"
78
"github.com/stretchr/testify/require"
89
"google.golang.org/protobuf/proto"
910

11+
"github.com/smartcontractkit/chainlink-common/keystore/corekeys/csakey"
1012
"github.com/smartcontractkit/chainlink-common/pkg/beholder"
1113
"github.com/smartcontractkit/chainlink-common/pkg/beholder/beholdertest"
1214
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
1315
commonv1 "github.com/smartcontractkit/chainlink-protos/node-platform/common/v1"
16+
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest"
1417
"github.com/smartcontractkit/chainlink/v2/core/logger"
1518
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
19+
keystoremocks "github.com/smartcontractkit/chainlink/v2/core/services/keystore/mocks"
1620
)
1721

1822
func TestNodePlatformBuildInfo_EmitsNodeBuildInfo(t *testing.T) {
@@ -47,3 +51,30 @@ func TestNodePlatformBuildInfo_EmitsNodeBuildInfo(t *testing.T) {
4751
require.Equal(t, "version-tag", payload.VersionTag)
4852
require.Equal(t, "1.2.3", payload.Version)
4953
}
54+
55+
func TestNodePlatformBuildInfo_ResolvesCSAKeyOnStart(t *testing.T) {
56+
obs := beholdertest.NewObserver(t)
57+
csaStore := &keystoremocks.CSA{}
58+
59+
csaStore.EXPECT().EnsureKey(mock.Anything).Return(nil).Once()
60+
csaStore.EXPECT().GetAll().Return([]csakey.KeyV2{cltest.DefaultCSAKey}, nil).Once()
61+
62+
servicetest.Run(t, chainlink.NewNodePlatformBuildInfoService(chainlink.NodePlatformBuildInfoConfig{
63+
Beat: 10 * time.Millisecond,
64+
Lggr: logger.TestLogger(t),
65+
CSAKeyStore: csaStore,
66+
CommitSHA: "commit-sha",
67+
DockerTag: "docker-tag",
68+
VersionTag: "version-tag",
69+
Version: "1.2.3",
70+
}))
71+
72+
require.Eventually(t, func() bool {
73+
return obs.Len(t, beholder.AttrKeyEntity, "common.v1.NodeBuildInfo") > 0
74+
}, time.Second, 10*time.Millisecond)
75+
76+
msg := obs.Messages(t, beholder.AttrKeyEntity, "common.v1.NodeBuildInfo")[0]
77+
var payload commonv1.NodeBuildInfo
78+
require.NoError(t, proto.Unmarshal(msg.Body, &payload))
79+
require.Equal(t, cltest.DefaultCSAKey.PublicKeyString(), payload.CsaPublicKey)
80+
}

0 commit comments

Comments
 (0)