Skip to content

Commit 7e4aecf

Browse files
committed
Add watcher for config change reload
1 parent d307999 commit 7e4aecf

7 files changed

Lines changed: 111 additions & 24 deletions

File tree

config/gateway.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@
142142
"pdp_event_index_name": "pdp-events"
143143
}
144144
}
145-
}
145+
}

services/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ require (
4242
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 // indirect
4343
github.com/davecgh/go-spew v1.1.1 // indirect
4444
github.com/dgraph-io/badger/v3 v3.2103.3 // indirect
45+
github.com/fsnotify/fsnotify v1.6.0 // indirect
4546
github.com/ghodss/yaml v1.0.0 // indirect
4647
github.com/go-logr/logr v1.2.3 // indirect
4748
github.com/go-logr/stdr v1.2.2 // indirect

services/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ github.com/frankban/quicktest v1.7.2/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03D
126126
github.com/frankban/quicktest v1.10.0/go.mod h1:ui7WezCLWMWxVWr1GETZY3smRy0G4KWq9vcPtJmFl7Y=
127127
github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og=
128128
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
129+
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
130+
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
129131
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
130132
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
131133
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@@ -636,6 +638,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
636638
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
637639
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
638640
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
641+
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
639642
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
640643
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
641644
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

services/pkg/common/config/repository_file.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"os"
66
"sync"
77

8+
"github.com/fsnotify/fsnotify"
89
config_api "github.com/safedep/gateway/services/gen"
10+
"github.com/safedep/gateway/services/pkg/common/logger"
911
"github.com/safedep/gateway/services/pkg/common/utils"
1012
)
1113

@@ -77,5 +79,49 @@ func (c *configFileRepository) load() error {
7779
}
7880

7981
func (c *configFileRepository) monitorForChange() error {
80-
return nil
82+
watcher, err := fsnotify.NewWatcher()
83+
if err != nil {
84+
return err
85+
}
86+
87+
// We are OK to leak the goroutine as the watcher will
88+
// never terminate
89+
wcb := func() {
90+
for {
91+
select {
92+
case event, ok := <-watcher.Events:
93+
if !ok {
94+
logger.Errorf("Failed to read from events channel")
95+
return
96+
}
97+
98+
if event.Has(fsnotify.Write) {
99+
logger.Debugf("Detected changes in configuration file")
100+
err := c.load()
101+
102+
if err != nil {
103+
logger.Errorf("Failed to reload config: %v", err)
104+
} else {
105+
logger.Debugf("Successfully reloaded gateway config")
106+
}
107+
}
108+
109+
case err, ok := <-watcher.Errors:
110+
if !ok {
111+
logger.Errorf("Failed to read from errors channel")
112+
return
113+
}
114+
115+
logger.Errorf("Watcher returned error: %v", err)
116+
}
117+
}
118+
}
119+
120+
err = watcher.Add(c.path)
121+
if err == nil {
122+
go wcb()
123+
}
124+
125+
logger.Debugf("Watcher initialized with error: %v", err)
126+
return err
81127
}
Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,65 @@
11
package openssf
22

33
import (
4-
"strings"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
87
)
98

109
// Potential flaky test
1110
func TestOsvExternalServiceApi(t *testing.T) {
12-
svc := NewOsvServiceAdapter(DefaultServiceAdapterConfig())
13-
vulns, err := svc.QueryPackage("Maven", "org.apache.logging.log4j:log4j-core", "2.16.0")
11+
cases := []struct {
12+
name string
13+
14+
// Inputs
15+
ecosystem, pkgName, pkgVersion string
1416

15-
assert.Nil(t, err)
16-
assert.True(t, len(*vulns.Vulns) > 0)
17+
// Assertions
18+
isEmptyVulns bool
19+
knownVulnId string
20+
}{
21+
{
22+
"Log4j in Maven Ecosystem",
23+
"Maven",
24+
"org.apache.logging.log4j:log4j-core",
25+
"2.16.0",
1726

18-
var knownVuln *OsvVulnerability = nil
19-
for _, v := range *vulns.Vulns {
20-
if *v.Id == "GHSA-8489-44mv-ggj8" {
21-
knownVuln = &v
22-
}
27+
false,
28+
"GHSA-8489-44mv-ggj8",
29+
},
30+
{
31+
"No such package in Ecosystem",
32+
"Maven",
33+
"no.such.pkg:no.pkg",
34+
"0.0.0",
35+
36+
true,
37+
"",
38+
},
2339
}
2440

25-
assert.NotNil(t, knownVuln)
41+
svc := NewOsvServiceAdapter(DefaultServiceAdapterConfig())
42+
43+
for _, test := range cases {
44+
t.Run(test.name, func(t *testing.T) {
45+
vulns, err := svc.QueryPackage(test.ecosystem,
46+
test.pkgName, test.pkgVersion)
47+
48+
if test.isEmptyVulns {
49+
assert.NotNil(t, err)
50+
} else {
51+
assert.Nil(t, err)
52+
assert.True(t, len(*vulns.Vulns) > 0)
2653

27-
assert.True(t, strings.Contains(*knownVuln.Details, "Log4j2"))
28-
assert.False(t, strings.Contains(*knownVuln.Details, "Log4j31337"))
54+
var knownVuln *OsvVulnerability = nil
55+
for _, v := range *vulns.Vulns {
56+
if *v.Id == test.knownVulnId {
57+
knownVuln = &v
58+
}
59+
}
60+
61+
assert.NotNil(t, knownVuln)
62+
}
63+
})
64+
}
2965
}

services/pkg/dcs/opensearch.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (s *opensearchIndexer) buildOpenSearchClient() (*opensearch.Client, error)
108108

109109
tlsConfig := tls.Config{}
110110
if cfg.GetAuthType() == gen.OpensearchIntegrationConfig_NONE {
111-
tlsConfig = tls.Config{InsecureSkipVerify: true}
111+
logger.Infof("Using Opensearch without authentication")
112112
} else {
113113
return nil, fmt.Errorf("unsupported auth type: %s", cfg.GetAuthType().String())
114114
}
@@ -147,7 +147,8 @@ func (s *opensearchIndexer) initOpenSearchIndex(name string) error {
147147
}
148148

149149
s.shardedNames.Store(name, shardableName)
150-
logger.Debugf("Created OpenSearch index with name: %s status:%s", shardableName,
150+
logger.Debugf("Created OpenSearch index with name: %s status:%s",
151+
shardableName,
151152
createIndexRes.Status())
152153
return nil
153154
}

services/pkg/pdp/authorizer.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ func (s *authorizationService) checkInternal(ctx context.Context,
7474

7575
upstreamArtefact, upstream, err := s.resolveRequestedArtefact(httpReq)
7676
if err != nil {
77-
logger.Infof("No artefact resolved: %s", err.Error())
77+
logger.Errorf("No artefact resolved: %s", err.Error())
7878
return &envoy_service_auth_v3.CheckResponse{}, err
7979
}
8080

8181
identity, err := s.authenticateForUpstream(exCtx, upstream, httpReq)
8282
if err != nil {
83-
logger.Infof("Error resolving userId: %v", err)
83+
logger.Warnf("Error resolving userId: %v", err)
8484
return s.authenticationChallenge(exCtx, upstream, httpReq)
8585
}
8686

@@ -100,9 +100,9 @@ func (s *authorizationService) checkInternal(ctx context.Context,
100100
})
101101

102102
if enrichmentErr != nil {
103-
logger.Infof("Failed to enrich artefact with vulnerability information: %v", enrichmentErr)
103+
logger.Errorf("Failed to enrich artefact with vulnerability information: %v", enrichmentErr)
104104
} else {
105-
logger.Infof("Enriched artefact (%s/%s/%s) with data: %s",
105+
logger.Debugf("Enriched artefact (%s/%s/%s) with data: %s",
106106
upstreamArtefact.Group, upstreamArtefact.Name, upstreamArtefact.Version,
107107
utils.Introspect(pdsResponse))
108108
}
@@ -122,7 +122,7 @@ func (s *authorizationService) checkInternal(ctx context.Context,
122122
})
123123

124124
if err != nil {
125-
logger.Infof("Failed to evaluate policy: %s", err.Error())
125+
logger.Errorf("Failed to evaluate policy: %s", err.Error())
126126
return &envoy_service_auth_v3.CheckResponse{}, err
127127
}
128128

@@ -256,11 +256,11 @@ func (s *authorizationService) publishDecisionEvent(ctx *extendedContext,
256256
event.Data.Result.PackageQueryStatus.Code = grpcStatus.Code().String()
257257
event.Data.Result.PackageQueryStatus.Message = grpcStatus.Message()
258258

259-
logger.With("event", event).Info("Event published")
259+
logger.With("event", event).Debug("Event published")
260260

261261
topic := config.PdpServiceConfig().PublisherConfig.TopicNames.PolicyAudit
262262
err := s.messagingService.Publish(topic, event)
263263
if err != nil {
264-
logger.Infof("[ERROR] Failed to publish audit event to topic: %s err: %v", topic, err)
264+
logger.Errorf("Failed to publish audit event to topic: %s err: %v", topic, err)
265265
}
266266
}

0 commit comments

Comments
 (0)