Skip to content

Commit 3246306

Browse files
AlgebraicWolfpsergee
authored andcommitted
pack: scaffolding for integrity check file generation
This patch provides a noop `integrity` module intended to be used as a scaffolding for generating integrity check files. The logic of `tt pack` command is modified to perform calls to appropriate `integrity` methods wherever it is required. It is designed as a separate module to allow easy replacement via directive in `go.mod` file.
1 parent 87e0f3f commit 3246306

File tree

11 files changed

+216
-661
lines changed

11 files changed

+216
-661
lines changed

cli/cmd/pack.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/apex/log"
88
"github.com/spf13/cobra"
99
"github.com/tarantool/tt/cli/cmdcontext"
10+
"github.com/tarantool/tt/cli/integrity"
1011
"github.com/tarantool/tt/cli/modules"
1112
"github.com/tarantool/tt/cli/pack"
1213
)
@@ -88,6 +89,9 @@ The supported types are: tgz, deb, rpm`,
8889
packCtx.TarantoolVersion,
8990
"Version of the tarantool for pack in docker (only with --use-docker flag).")
9091

92+
// Integrity flags.
93+
integrity.RegisterWithIntegrityFlag(packCmd.Flags(), &packCtx.IntegrityPrivateKey)
94+
9195
return packCmd
9296
}
9397

cli/integrity/go.mod

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module github.com/tarantool/tt/cli/integrity
2+
3+
go 1.20
4+
5+
require (
6+
github.com/spf13/cobra v1.8.0
7+
github.com/stretchr/testify v1.8.4
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
github.com/spf13/pflag v1.0.5 // indirect
15+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
16+
gopkg.in/yaml.v3 v3.0.1 // indirect
17+
)

cli/integrity/go.sum

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
5+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
6+
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
7+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
8+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
9+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
10+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
14+
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
15+
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
16+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
17+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
18+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
19+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
22+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
23+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
24+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

cli/integrity/integrity.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package integrity
2+
3+
import (
4+
"errors"
5+
6+
"github.com/spf13/pflag"
7+
)
8+
9+
// Signer implements high-level API for package signing.
10+
type Signer interface {
11+
// Sign generates data to sign a package.
12+
Sign(basePath string, appNames []string) error
13+
}
14+
15+
// NewSigner constructs a noop Signer.
16+
func NewSigner(path string) (Signer, error) {
17+
return nil, errors.New("integrity signer should never be created in ce")
18+
}
19+
20+
// RegisterWithIntegrityFlag is a noop function that is intended to add
21+
// flags to `tt pack` command.
22+
func RegisterWithIntegrityFlag(flagset *pflag.FlagSet, dst *string) {}

cli/integrity/integrity_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package integrity_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/pflag"
7+
"github.com/stretchr/testify/require"
8+
"github.com/tarantool/tt/cli/integrity"
9+
)
10+
11+
func TestNewSigner(t *testing.T) {
12+
testCases := []struct {
13+
name string
14+
privateKeyPath string
15+
}{
16+
{
17+
name: "Empty path",
18+
privateKeyPath: "",
19+
},
20+
{
21+
name: "Arbitrary path",
22+
privateKeyPath: "private.pem",
23+
},
24+
}
25+
26+
for _, testCase := range testCases {
27+
t.Run(testCase.name, func(t *testing.T) {
28+
signer, err := integrity.NewSigner(testCase.privateKeyPath)
29+
require.Nil(t, signer, "signer must not be created")
30+
require.EqualError(t, err, "integrity signer should never be created in ce", "an error should be produced")
31+
})
32+
}
33+
}
34+
35+
func TestRegisterWithIntegritySigner(t *testing.T) {
36+
someStr := ""
37+
38+
testCases := []struct {
39+
name string
40+
flagSet *pflag.FlagSet
41+
dst *string
42+
}{
43+
{
44+
name: "Empty flagSet and dst",
45+
flagSet: nil,
46+
dst: nil,
47+
},
48+
{
49+
name: "Empty dst",
50+
flagSet: &pflag.FlagSet{},
51+
dst: nil,
52+
},
53+
{
54+
name: "Empty flagSet",
55+
flagSet: nil,
56+
dst: &someStr,
57+
},
58+
{
59+
name: "Nothing empty",
60+
flagSet: &pflag.FlagSet{},
61+
dst: nil,
62+
},
63+
}
64+
65+
for _, testCase := range testCases {
66+
t.Run(testCase.name, func(t *testing.T) {
67+
integrity.RegisterWithIntegrityFlag(testCase.flagSet, testCase.dst)
68+
69+
if testCase.flagSet != nil {
70+
require.False(t, testCase.flagSet.HasFlags(),
71+
"command must not be modified")
72+
}
73+
})
74+
}
75+
}

cli/pack/common.go

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/tarantool/tt/cli/cmdcontext"
1515
"github.com/tarantool/tt/cli/config"
1616
"github.com/tarantool/tt/cli/configure"
17+
"github.com/tarantool/tt/cli/integrity"
1718
"github.com/tarantool/tt/cli/running"
1819
"github.com/tarantool/tt/cli/util"
1920
lua "github.com/yuin/gopher-lua"
@@ -113,6 +114,16 @@ func skipArtifacts(cliOpts *config.CliOpts) func(src string) (bool, error) {
113114
func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
114115
cliOpts *config.CliOpts, buildRocks bool) (string, error) {
115116
var err error
117+
var signer integrity.Signer = nil
118+
119+
// If integrity checks are enabled, create an IntegritySigner.
120+
if packCtx.IntegrityPrivateKey != "" {
121+
signer, err = integrity.NewSigner(packCtx.IntegrityPrivateKey)
122+
123+
if err != nil {
124+
return "", err
125+
}
126+
}
116127

117128
// Create temporary directory step.
118129
basePath, err := os.MkdirTemp("", "tt_pack")
@@ -257,6 +268,19 @@ func prepareBundle(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
257268
if err != nil {
258269
return "", err
259270
}
271+
272+
var appNames []string
273+
for _, app := range appList {
274+
appNames = append(appNames, app.Name)
275+
}
276+
277+
if packCtx.IntegrityPrivateKey != "" {
278+
err = signer.Sign(basePath, appNames)
279+
if err != nil {
280+
return "", err
281+
}
282+
}
283+
260284
return basePath, nil
261285
}
262286

cli/pack/opts.go

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package pack
22

33
import (
4+
"errors"
5+
46
"github.com/tarantool/tt/cli/cmdcontext"
57
)
68

@@ -17,6 +19,10 @@ const (
1719
func FillCtx(cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx,
1820
args []string) error {
1921

22+
if (packCtx.IntegrityPrivateKey != "") && packCtx.CartridgeCompat {
23+
return errors.New("cannot pack with integrity checks in cartridge-compat mode")
24+
}
25+
2026
packCtx.TarantoolIsSystem = cmdCtx.Cli.IsSystem
2127
packCtx.TarantoolExecutable = cmdCtx.Cli.TarantoolCli.Executable
2228
packCtx.Type = args[0]

cli/pack/pack.go

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type PackCtx struct {
3232
CartridgeCompat bool
3333
// TarantoolVersion specifies the version of the tarantool for pack in docker.
3434
TarantoolVersion string
35+
// IntegrityPrivateKey contains the path to private key for signing hash files.
36+
IntegrityPrivateKey string
3537
}
3638

3739
// ArchiveCtx contains flags specific for tgz type.

go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ require (
1818
github.com/mitchellh/mapstructure v1.4.3
1919
github.com/moby/term v0.0.0-20221105221325-4eb28fa6025c
2020
github.com/otiai10/copy v1.7.1
21-
github.com/spf13/cobra v1.3.0
21+
github.com/spf13/cobra v1.8.0
2222
github.com/stretchr/testify v1.8.4
2323
github.com/tarantool/cartridge-cli v0.0.0-20220605082730-53e6a5be9a61
2424
github.com/tarantool/go-prompt v1.0.0
2525
github.com/tarantool/go-tarantool v1.10.1-0.20230309143354-e257ff30dd4d
26+
github.com/tarantool/tt/cli/integrity v0.0.0-00010101000000-000000000000
2627
github.com/vmihailenco/msgpack/v5 v5.3.5
2728
github.com/yuin/gopher-lua v1.1.1-0.20230219103905-71163b697a8f
2829
go.etcd.io/etcd/api/v3 v3.5.9
@@ -55,7 +56,7 @@ require (
5556
github.com/gogo/protobuf v1.3.2 // indirect
5657
github.com/golang/protobuf v1.5.3 // indirect
5758
github.com/hpcloud/tail v1.0.0 // indirect
58-
github.com/inconshreveable/mousetrap v1.0.0 // indirect
59+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5960
github.com/klauspost/compress v1.11.13 // indirect
6061
github.com/mattn/go-colorable v0.1.12 // indirect
6162
github.com/mattn/go-pointer v0.0.1 // indirect
@@ -100,4 +101,5 @@ require (
100101
replace (
101102
github.com/c-bata/go-prompt => github.com/tarantool/go-prompt v0.2.6-tarantool
102103
github.com/tarantool/cartridge-cli => ./cli/cartridge/third_party/cartridge-cli
104+
github.com/tarantool/tt/cli/integrity => ./cli/integrity
103105
)

0 commit comments

Comments
 (0)