Skip to content

Commit f801731

Browse files
authored
Merge pull request #233 from gatewayd-io/use-secure-config-for-checksum-verification
Use secure config for checksum verification
2 parents e3ee6a8 + f05f67d commit f801731

File tree

5 files changed

+29
-78
lines changed

5 files changed

+29
-78
lines changed

.github/workflows/test.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ jobs:
9797
- name: Checkout test plugin 🛎️
9898
uses: actions/checkout@v3
9999
with:
100-
repository: gatewayd-io/gatewayd-plugin-template
101-
path: gatewayd-plugin-template
100+
repository: gatewayd-io/plugin-template-go
101+
path: plugin-template-go
102102
token: ${{ secrets.GH_PLUGIN_TOKEN }}
103103

104104
- name: Build template plugin 🏗️
105105
run: |
106106
# Build GatewayD
107107
make build-dev
108108
# Build template plugin
109-
cd gatewayd-plugin-template && make build && cp gatewayd-plugin-template ../gdp-template && cd ..
110-
export SHA256SUM=$(sha256sum gdp-template | awk '{print $1}')
109+
cd plugin-template-go && make build && cp plugin-template-go ../ptg && cd ..
110+
export SHA256SUM=$(sha256sum ptg | awk '{print $1}')
111111
cat <<EOF > gatewayd_plugins.yaml
112112
verificationPolicy: "passdown"
113113
compatibilityPolicy: "strict"
@@ -118,9 +118,9 @@ jobs:
118118
timeout: 30s
119119
120120
plugins:
121-
- name: gatewayd-plugin-template
121+
- name: plugin-template-go
122122
enabled: True
123-
localPath: ./gdp-template
123+
localPath: ./ptg
124124
args: ["--log-level", "debug"]
125125
env:
126126
- MAGIC_COOKIE_KEY=GATEWAYD_PLUGIN

gatewayd_plugins.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ plugins:
8080
- PERIODIC_INVALIDATOR_INTERVAL=1m
8181
- PERIODIC_INVALIDATOR_START_DELAY=1m
8282
- API_ADDRESS=localhost:18080
83-
checksum: 28456728dd3427b91d2e22f38b909526355d1b2becc9379581e1b70bb9495aa9
83+
checksum: 4dc219c21043f99e68d90162b847a782595dd69713e2caf0380686b6de8864ab

plugin/plugin_registry.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package plugin
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"encoding/hex"
57
"sort"
68

79
semver "github.com/Masterminds/semver/v3"
@@ -414,6 +416,7 @@ func (reg *Registry) LoadPlugins(ctx context.Context, plugins []config.Plugin) {
414416
continue
415417
}
416418

419+
var secureConfig *goplugin.SecureConfig
417420
if !reg.devMode {
418421
// Checksum of the plugin.
419422
if plugin.ID.Checksum == "" {
@@ -424,22 +427,24 @@ func (reg *Registry) LoadPlugins(ctx context.Context, plugins []config.Plugin) {
424427

425428
// Verify the checksum.
426429
// TODO: Load the plugin from a remote location if the checksum didn't match?
427-
if sum, err := SHA256SUM(plugin.LocalPath); err != nil {
430+
checksum, err := hex.DecodeString(plugin.ID.Checksum)
431+
if err != nil {
428432
reg.Logger.Debug().Str("name", plugin.ID.Name).Err(err).Msg(
429-
"Failed to calculate checksum")
433+
"Failed to decode checksum")
430434
continue
431-
} else if sum != plugin.ID.Checksum {
432-
reg.Logger.Debug().Fields(
433-
map[string]interface{}{
434-
"calculated": sum,
435-
"expected": plugin.ID.Checksum,
436-
"name": plugin.ID.Name,
437-
},
438-
).Msg("Checksum mismatch")
435+
}
436+
437+
if len(checksum) != sha256.Size {
438+
reg.Logger.Debug().Str("name", plugin.ID.Name).Msg("Invalid checksum length")
439439
continue
440440
}
441441

442-
span.AddEvent("Verified plugin checksum")
442+
secureConfig = &goplugin.SecureConfig{
443+
Checksum: checksum,
444+
Hash: sha256.New(),
445+
}
446+
447+
span.AddEvent("Created secure config for validating plugin checksum")
443448
} else {
444449
span.AddEvent("Skipping plugin checksum verification (dev mode)")
445450
}
@@ -460,12 +465,12 @@ func (reg *Registry) LoadPlugins(ctx context.Context, plugins []config.Plugin) {
460465
AllowedProtocols: []goplugin.Protocol{
461466
goplugin.ProtocolGRPC,
462467
},
463-
// SecureConfig: nil,
464-
Logger: logAdapter,
465-
Managed: true,
466-
MinPort: config.DefaultMinPort,
467-
MaxPort: config.DefaultMaxPort,
468-
AutoMTLS: true,
468+
SecureConfig: secureConfig,
469+
Logger: logAdapter,
470+
Managed: true,
471+
MinPort: config.DefaultMinPort,
472+
MaxPort: config.DefaultMaxPort,
473+
AutoMTLS: true,
469474
},
470475
)
471476

plugin/utils.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,14 @@
11
package plugin
22

33
import (
4-
"bufio"
5-
"crypto/sha256"
6-
"errors"
7-
"fmt"
8-
"io"
9-
"os"
104
"os/exec"
115
"time"
126

13-
"github.com/gatewayd-io/gatewayd/config"
14-
gerr "github.com/gatewayd-io/gatewayd/errors"
157
"github.com/google/go-cmp/cmp"
168
"github.com/google/go-cmp/cmp/cmpopts"
179
"google.golang.org/protobuf/types/known/structpb"
1810
)
1911

20-
// SHA256SUM returns the sha256 checksum of a file.
21-
// Ref: https://github.com/codingsince1985/checksum
22-
// A little copying is better than a little dependency.
23-
func SHA256SUM(filename string) (string, *gerr.GatewayDError) {
24-
if info, err := os.Stat(filename); err != nil || info.IsDir() {
25-
return "", gerr.ErrFileNotFound.Wrap(err)
26-
}
27-
28-
file, err := os.Open(filename)
29-
if err != nil {
30-
return "", gerr.ErrFileOpenFailed.Wrap(err)
31-
}
32-
defer func() { _ = file.Close() }()
33-
34-
hashAlgorithm := sha256.New()
35-
36-
buf := make([]byte, config.ChecksumBufferSize)
37-
for {
38-
n, err := bufio.NewReader(file).Read(buf)
39-
//nolint:gocritic
40-
if err == nil {
41-
hashAlgorithm.Write(buf[:n])
42-
} else if errors.Is(err, io.EOF) {
43-
return fmt.Sprintf("%x", hashAlgorithm.Sum(nil)), nil
44-
} else {
45-
return "", gerr.ErrFileReadFailed.Wrap(err)
46-
}
47-
}
48-
}
49-
5012
// Verify compares two structs and returns true if they are equal.
5113
func Verify(params, returnVal *structpb.Struct) bool {
5214
return cmp.Equal(params.AsMap(), returnVal.AsMap(), cmp.Options{

plugin/utils_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@ import (
77
"google.golang.org/protobuf/types/known/structpb"
88
)
99

10-
// Test_sha256sum tests the sha256sum function.
11-
func Test_sha256sum(t *testing.T) {
12-
checksum, err := SHA256SUM("../LICENSE")
13-
assert.Nil(t, err)
14-
assert.Equal(t,
15-
"8486a10c4393cee1c25392769ddd3b2d6c242d6ec7928e1414efff7dfb2f07ef",
16-
checksum,
17-
)
18-
}
19-
20-
// Test_sha256sum_fail tests the sha256sum function with a file that does not exist.
21-
func Test_sha256sum_fail(t *testing.T) {
22-
_, err := SHA256SUM("not_a_file")
23-
assert.NotNil(t, err)
24-
}
25-
2610
// Test_Verify tests the Verify function.
2711
func Test_Verify(t *testing.T) {
2812
params, err := structpb.NewStruct(

0 commit comments

Comments
 (0)