Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable linter contextcheck and fix potential problems #4714

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangcilint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ linters:
- lll
- misspell
- goheader
- contextcheck
linters-settings:
# ...
forbidigo:
Expand Down
5 changes: 3 additions & 2 deletions control/beaconing/originator_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2019 Anapaya Systems
// Copyright 2025 SCION Association
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,10 +95,10 @@ func TestOriginatorRun(t *testing.T) {

sender := mock_beaconing.NewMockSender(mctrl)
sender.EXPECT().Send(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(_ context.Context, b *seg.PathSegment) error {
func(ctx context.Context, b *seg.PathSegment) error {
// Check the beacon is valid and verifiable.
assert.NoError(t, b.Validate(seg.ValidateBeacon))
assert.NoError(t, b.VerifyASEntry(context.Background(),
assert.NoError(t, b.VerifyASEntry(ctx,
segVerifier{pubKey: pub}, b.MaxIdx()))
// Extract the hop field from the current AS entry to compare.
hopF := b.ASEntries[b.MaxIdx()].HopEntry.HopField
Expand Down
27 changes: 14 additions & 13 deletions control/beaconing/propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ func TestPropagatorRunNonCore(t *testing.T) {
gomock.Any()).Times(1).DoAndReturn(

func(_ context.Context, _ addr.IA, egIfID uint16,
nextHop *net.UDPAddr) (beaconing.Sender, error) {

nextHop *net.UDPAddr,
) (beaconing.Sender, error) {
sender := mock_beaconing.NewMockSender(mctrl)
sender.EXPECT().Send(gomock.Any(), gomock.Any()).Times(3).DoAndReturn(
func(_ context.Context, b *seg.PathSegment) error {
validateSend(t, b, egIfID, nextHop, pub, topo)
func(ctx context.Context, b *seg.PathSegment) error {
validateSend(ctx, t, b, egIfID, nextHop, pub, topo)
return nil
},
)
Expand Down Expand Up @@ -167,12 +167,12 @@ func TestPropagatorRunCore(t *testing.T) {
senderFactory.EXPECT().NewSender(gomock.Any(), gomock.Any(), uint16(1121),
gomock.Any()).DoAndReturn(
func(_ context.Context, _ addr.IA, egIfID uint16,
nextHop *net.UDPAddr) (beaconing.Sender, error) {

nextHop *net.UDPAddr,
) (beaconing.Sender, error) {
sender := mock_beaconing.NewMockSender(mctrl)
sender.EXPECT().Send(gomock.Any(), gomock.Any()).Times(2).DoAndReturn(
func(_ context.Context, b *seg.PathSegment) error {
validateSend(t, b, egIfID, nextHop, pub, topo)
func(ctx context.Context, b *seg.PathSegment) error {
validateSend(ctx, t, b, egIfID, nextHop, pub, topo)
return nil
},
)
Expand All @@ -183,12 +183,12 @@ func TestPropagatorRunCore(t *testing.T) {
senderFactory.EXPECT().NewSender(gomock.Any(), gomock.Any(), uint16(1113),
gomock.Any()).DoAndReturn(
func(_ context.Context, _ addr.IA, egIfID uint16,
nextHop *net.UDPAddr) (beaconing.Sender, error) {

nextHop *net.UDPAddr,
) (beaconing.Sender, error) {
sender := mock_beaconing.NewMockSender(mctrl)
sender.EXPECT().Send(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(_ context.Context, b *seg.PathSegment) error {
validateSend(t, b, egIfID, nextHop, pub, topo)
func(ctx context.Context, b *seg.PathSegment) error {
validateSend(ctx, t, b, egIfID, nextHop, pub, topo)
return nil
},
)
Expand Down Expand Up @@ -275,6 +275,7 @@ func TestPropagatorFastRecovery(t *testing.T) {
}

func validateSend(
ctx context.Context,
t *testing.T,
b *seg.PathSegment,
egIfID uint16,
Expand All @@ -284,7 +285,7 @@ func validateSend(
) {
// Check the beacon is valid and verifiable.
assert.NoError(t, b.Validate(seg.ValidateBeacon))
assert.NoError(t, b.VerifyASEntry(context.Background(),
assert.NoError(t, b.VerifyASEntry(ctx,
segVerifier{pubKey: pub}, b.MaxIdx()))
// Extract the hop field from the current AS entry to compare.
hopF := b.ASEntries[b.MaxIdx()].HopEntry.HopField
Expand Down
9 changes: 4 additions & 5 deletions control/cmd/control/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func realMain(ctx context.Context) error {
MTU: topo.MTU(),
Topology: adaptTopology(topo),
}
quicStack, err := nc.QUICStack()
quicStack, err := nc.QUICStack(ctx)
if err != nil {
return serrors.Wrap("initializing QUIC stack", err)
}
Expand Down Expand Up @@ -393,10 +393,11 @@ func realMain(ctx context.Context) error {
},
Registrations: libmetrics.NewPromCounter(metrics.SegmentRegistrationsTotal),
})

}

signer := cs.NewSigner(topo.IA(), trustDB, globalCfg.General.ConfigDir)
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
signer := cs.NewSigner(ctx, topo.IA(), trustDB, globalCfg.General.ConfigDir)

var chainBuilder renewal.ChainBuilder
var caClient *caapi.Client
Expand Down Expand Up @@ -837,7 +838,6 @@ func createBeaconStore(
policyConfig config.Policies,
provider beacon.ChainProvider,
) (cs.Store, bool, error) {

if core {
policies, err := cs.LoadCorePolicies(policyConfig)
if err != nil {
Expand Down Expand Up @@ -967,7 +967,6 @@ func getCAHealth(
ctx context.Context,
caClient *caapi.Client,
) (api.CAHealthStatus, error) {

logger := log.FromCtx(ctx)
rep, err := caClient.GetHealthcheck(ctx)
if err != nil {
Expand Down
14 changes: 5 additions & 9 deletions control/drkey/grpc/drkey_service.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2022 ETH Zurich
// Copyright 2025 SCION Association
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,7 +42,7 @@ type Engine interface {
GetSecretValue(ctx context.Context, meta drkey.SecretValueMeta) (drkey.SecretValue, error)
GetLevel1Key(ctx context.Context, meta drkey.Level1Meta) (drkey.Level1Key, error)

DeriveLevel1(meta drkey.Level1Meta) (drkey.Level1Key, error)
DeriveLevel1(ctx context.Context, meta drkey.Level1Meta) (drkey.Level1Key, error)
DeriveASHost(ctx context.Context, meta drkey.ASHostMeta) (drkey.ASHostKey, error)
DeriveHostAS(ctx context.Context, meta drkey.HostASMeta) (drkey.HostASKey, error)
DeriveHostHost(ctx context.Context, meta drkey.HostHostMeta) (drkey.HostHostKey, error)
Expand All @@ -66,7 +67,6 @@ func (d *Server) DRKeyLevel1(
ctx context.Context,
req *cppb.DRKeyLevel1Request,
) (*cppb.DRKeyLevel1Response, error) {

peer, ok := peer.FromContext(ctx)
if !ok {
return nil, serrors.New("cannot retrieve peer information from ctx")
Expand All @@ -87,7 +87,7 @@ func (d *Server) DRKeyLevel1(
"proto_id", lvl1Meta.ProtoId)
}

lvl1Key, err := d.Engine.DeriveLevel1(lvl1Meta)
lvl1Key, err := d.Engine.DeriveLevel1(ctx, lvl1Meta)
if err != nil {
return nil, serrors.Wrap("deriving level 1 key", err)
}
Expand All @@ -100,7 +100,6 @@ func (d *Server) DRKeyIntraLevel1(
ctx context.Context,
req *cppb.DRKeyIntraLevel1Request,
) (*cppb.DRKeyIntraLevel1Response, error) {

peer, ok := peer.FromContext(ctx)
if !ok {
return nil, serrors.New("cannot retrieve peer information from ctx")
Expand Down Expand Up @@ -132,7 +131,6 @@ func (d *Server) DRKeyASHost(
ctx context.Context,
req *cppb.DRKeyASHostRequest,
) (*cppb.DRKeyASHostResponse, error) {

peer, ok := peer.FromContext(ctx)
if !ok {
return nil, serrors.New("cannot retrieve peer information from ctx")
Expand Down Expand Up @@ -160,7 +158,6 @@ func (d *Server) DRKeyHostAS(
ctx context.Context,
req *cppb.DRKeyHostASRequest,
) (*cppb.DRKeyHostASResponse, error) {

peer, ok := peer.FromContext(ctx)
if !ok {
return nil, serrors.New("cannot retrieve peer information from ctx")
Expand All @@ -187,7 +184,6 @@ func (d *Server) DRKeyHostHost(
ctx context.Context,
req *cppb.DRKeyHostHostRequest,
) (*cppb.DRKeyHostHostResponse, error) {

peer, ok := peer.FromContext(ctx)
if !ok {
return nil, serrors.New("cannot retrieve peer information from ctx")
Expand Down Expand Up @@ -215,7 +211,6 @@ func (d *Server) DRKeySecretValue(
ctx context.Context,
req *cppb.DRKeySecretValueRequest,
) (*cppb.DRKeySecretValueResponse, error) {

peer, ok := peer.FromContext(ctx)
if !ok {
return nil, serrors.New("cannot retrieve peer information from ctx")
Expand Down Expand Up @@ -363,7 +358,8 @@ func hostAddrFromPeer(peerAddr net.Addr) (net.IP, error) {
}

func getMeta(protoId drkeypb.Protocol, ts *timestamppb.Timestamp, srcIA,
dstIA addr.IA) (drkey.Level1Meta, error) {
dstIA addr.IA,
) (drkey.Level1Meta, error) {
err := ts.CheckValid()
if err != nil {
return drkey.Level1Meta{}, serrors.Wrap("invalid valTime from pb req", err)
Expand Down
6 changes: 4 additions & 2 deletions control/drkey/grpc/fetcher_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2022 ETH Zurich
// Copyright 2025 SCION Association
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,7 +63,9 @@ func TestLevel1KeyFetching(t *testing.T) {
ctrl := gomock.NewController(t)

lvl1db := mock_grpc.NewMockEngine(ctrl)
lvl1db.EXPECT().DeriveLevel1(gomock.Any()).AnyTimes().Return(drkey.Level1Key{}, nil)
lvl1db.EXPECT().DeriveLevel1(gomock.Any(), gomock.Any()).
AnyTimes().
Return(drkey.Level1Key{}, nil)

db := mock_trust.NewMockDB(ctrl)
db.EXPECT().SignedTRC(gomock.Any(), gomock.Any()).AnyTimes().Return(trc, nil)
Expand Down Expand Up @@ -94,7 +97,6 @@ func TestLevel1KeyFetching(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {

// TODO(matzf): change xtest library to allow specifying the client
// credentials for individual calls so that server does not need to be
// recreated here.
Expand Down
8 changes: 4 additions & 4 deletions control/drkey/grpc/mock_grpc/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 7 additions & 12 deletions control/drkey/service_engine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2022 ETH Zurich
// Copyright 2025 SCION Association
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +35,7 @@ type Fetcher interface {
// Level1PrefetchListKeeper maintains a list for those level1 keys
// that are recently/frequently used.
type Level1PrefetchListKeeper interface {
//Update updates the keys in Level1Cache based on the Level1Key metadata.
// Update updates the keys in Level1Cache based on the Level1Key metadata.
Update(key Level1PrefetchInfo)
// GetLevel1InfoArray retrieves an array whose members contains information regarding
// level1 keys to prefetch.
Expand Down Expand Up @@ -63,7 +64,6 @@ func (s *ServiceEngine) GetSecretValue(
ctx context.Context,
meta drkey.SecretValueMeta,
) (drkey.SecretValue, error) {

return s.SecretBackend.getSecretValue(ctx, meta)
}

Expand All @@ -73,7 +73,6 @@ func (s *ServiceEngine) GetLevel1Key(
ctx context.Context,
meta drkey.Level1Meta,
) (drkey.Level1Key, error) {

key, err := s.getLevel1Key(ctx, meta)
if err == nil && ctx.Value(fromPrefetcher{}) == nil && meta.SrcIA != s.LocalIA {
keyInfo := Level1PrefetchInfo{
Expand All @@ -91,8 +90,10 @@ func (s *ServiceEngine) GetLevel1PrefetchInfo() []Level1PrefetchInfo {
}

// DeriveLevel1 returns a Level1 key based on the presented information.
func (s *ServiceEngine) DeriveLevel1(meta drkey.Level1Meta) (drkey.Level1Key, error) {
sv, err := s.GetSecretValue(context.Background(), drkey.SecretValueMeta{
func (s *ServiceEngine) DeriveLevel1(
ctx context.Context, meta drkey.Level1Meta,
) (drkey.Level1Key, error) {
sv, err := s.GetSecretValue(ctx, drkey.SecretValueMeta{
ProtoId: meta.ProtoId,
Validity: meta.Validity,
})
Expand All @@ -111,7 +112,6 @@ func (s *ServiceEngine) DeriveASHost(
ctx context.Context,
meta drkey.ASHostMeta,
) (drkey.ASHostKey, error) {

var key drkey.Key
var err error

Expand Down Expand Up @@ -146,7 +146,6 @@ func (s *ServiceEngine) DeriveHostAS(
ctx context.Context,
meta drkey.HostASMeta,
) (drkey.HostASKey, error) {

var key drkey.Key
var err error

Expand Down Expand Up @@ -182,7 +181,6 @@ func (s *ServiceEngine) DeriveHostHost(
ctx context.Context,
meta drkey.HostHostMeta,
) (drkey.HostHostKey, error) {

hostASMeta := drkey.HostASMeta{
ProtoId: meta.ProtoId,
Validity: meta.Validity,
Expand Down Expand Up @@ -238,9 +236,8 @@ func (s *ServiceEngine) getLevel1Key(
ctx context.Context,
meta drkey.Level1Meta,
) (drkey.Level1Key, error) {

if meta.SrcIA == s.LocalIA {
return s.DeriveLevel1(meta)
return s.DeriveLevel1(ctx, meta)
}

if meta.DstIA != s.LocalIA {
Expand Down Expand Up @@ -278,7 +275,6 @@ func (s *ServiceEngine) obtainLevel1Key(
srcIA addr.IA,
dstIA addr.IA,
) (drkey.Level1Key, error) {

if !proto.IsPredefined() {
proto = drkey.Generic
}
Expand All @@ -289,7 +285,6 @@ func (s *ServiceEngine) obtainLevel1Key(
ProtoId: proto,
}
return s.GetLevel1Key(ctx, level1Meta)

}

type fromPrefetcher struct{}
Expand Down
8 changes: 4 additions & 4 deletions control/drkey/service_engine_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2022 ETH Zurich
// Copyright 2025 SCION Association
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestDeriveLevel1Key(t *testing.T) {
Validity: time.Now(),
}

key, err := store.DeriveLevel1(meta)
key, err := store.DeriveLevel1(context.Background(), meta)
assert.NoError(t, err)
assert.Equal(t, meta.DstIA, key.DstIA)
assert.Equal(t, meta.ProtoId, key.ProtoId)
Expand Down Expand Up @@ -135,7 +136,7 @@ func TestDeriveHostAS(t *testing.T) {
PrefetchKeeper: cache,
}

var tests = []drkey.Protocol{
tests := []drkey.Protocol{
drkey.SCMP,
drkey.Protocol(7),
}
Expand Down Expand Up @@ -231,7 +232,7 @@ func TestGetLevel1Key(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, secondLevel1Key, rcvKey3)
//Simulate a call coming from the prefetcher, it must not update cache
// Simulate a call coming from the prefetcher, it must not update cache
pref_ctx := context.WithValue(context.Background(), cs_drkey.FromPrefetcher(), true)
rcvKey4, err := store.GetLevel1Key(pref_ctx, drkey.Level1Meta{
ProtoId: firstLevel1Key.ProtoId,
Expand All @@ -258,7 +259,6 @@ func TestGetLevel1Key(t *testing.T) {
}
_, err = store.GetLevel1Key(context.Background(), locallvl1Meta)
assert.NoError(t, err)

}

func newLevel1Database(t *testing.T) *level1_sql.Backend {
Expand Down
Loading
Loading