Skip to content

Commit 8c8e2be

Browse files
authored
Merge pull request #264 from klihub/fixes/accumulate-oci-hook-ownership
api: fix OCI hook ownership tracking.
2 parents ed596cb + 91a7760 commit 8c8e2be

5 files changed

Lines changed: 72 additions & 11 deletions

File tree

pkg/adaptation/adaptation_suite_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ var _ = Describe("Plugin connection", func() {
201201
s.Cleanup()
202202
})
203203

204+
It("should reject plugins with an invalid name", func() {
205+
var (
206+
validPlugin = &mockPlugin{
207+
name: "abcd-0123+EFGH_4567.ijkl",
208+
idx: "05",
209+
}
210+
invalidPlugin = &mockPlugin{
211+
name: "foo,bar",
212+
idx: "10",
213+
}
214+
)
215+
216+
s.Startup()
217+
218+
Expect(validPlugin.Start(s.dir)).To(Succeed())
219+
Expect(invalidPlugin.Start(s.dir)).ToNot(Succeed())
220+
})
221+
204222
It("should configure the plugin", func() {
205223
var (
206224
plugin = s.plugins[0]

pkg/adaptation/plugin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ func (p *plugin) qualifiedName() string {
435435
// RegisterPlugin handles the plugin's registration request.
436436
func (p *plugin) RegisterPlugin(ctx context.Context, req *RegisterPluginRequest) (*RegisterPluginResponse, error) {
437437
if p.isExternal() {
438-
if req.PluginName == "" {
439-
p.regC <- fmt.Errorf("plugin %q registered with an empty name", p.qualifiedName())
440-
return &RegisterPluginResponse{}, errors.New("invalid (empty) plugin name")
438+
if err := api.CheckPluginName(req.PluginName); err != nil {
439+
p.regC <- fmt.Errorf("plugin registered with an invalid name: %w", err)
440+
return &RegisterPluginResponse{}, fmt.Errorf("invalid plugin name: %w", err)
441441
}
442442
if err := api.CheckPluginIndex(req.PluginIdx); err != nil {
443443
p.regC <- fmt.Errorf("plugin %q registered with an invalid index: %w", req.PluginName, err)

pkg/api/owners.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,7 @@ func (f *FieldOwners) ClaimMount(destination, plugin string) error {
475475
}
476476

477477
func (f *FieldOwners) ClaimHooks(plugin string) error {
478-
plugins := plugin
479-
480-
if current, ok := f.simpleOwner(Field_OciHooks.Key()); ok {
481-
f.clearSimple(Field_OciHooks.Key(), plugin)
482-
plugins = current + "," + plugin
483-
}
484-
485-
f.claimSimple(Field_OciHooks.Key(), plugins)
478+
f.accumulateSimple(Field_OciHooks.Key(), plugin)
486479
return nil
487480
}
488481

@@ -678,6 +671,14 @@ func (f *FieldOwners) ClearRdt(plugin string) {
678671
f.clearSimple(Field_RdtEnableMonitoring.Key(), plugin)
679672
}
680673

674+
func (f *FieldOwners) accumulateSimple(field int32, plugin string) {
675+
old, ok := f.simpleOwner(field)
676+
if ok {
677+
plugin = old + "," + plugin
678+
}
679+
f.Simple[field] = plugin
680+
}
681+
681682
func (f *FieldOwners) Conflict(field int32, plugin, other string, qualifiers ...string) error {
682683
return fmt.Errorf("plugins %q and %q both tried to set %s",
683684
plugin, other, qualify(field, qualifiers...))

pkg/api/owners_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,24 @@ func TestCompoundClaims(t *testing.T) {
113113

114114
require.Equal(t, api.Field_Annotations.String(), "Annotations", "annotation field name")
115115
}
116+
117+
func TestAccumulatingOwnership(t *testing.T) {
118+
o := api.NewOwningPlugins()
119+
120+
// claim OCI hooks of ctr0
121+
err := o.ClaimHooks("ctr0", "plugin0")
122+
require.NoError(t, err, "ctr0 OCI hooks by plugin0")
123+
124+
// claim OCI hooks of ctr0
125+
err = o.ClaimHooks("ctr0", "plugin1")
126+
require.NoError(t, err, "ctr0 OCI hooks by plugin1")
127+
128+
// claim OCI hooks of ctr0
129+
err = o.ClaimHooks("ctr0", "plugin2")
130+
require.NoError(t, err, "ctr0 OCI hooks by plugin2")
131+
132+
owners, ok := o.HooksOwner("ctr0")
133+
require.True(t, ok, "ctr0 has hooks owners")
134+
require.Equal(t, "plugin0,plugin1,plugin2", owners, "ctr0 hooks owners")
135+
136+
}

pkg/api/plugin.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package api
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"strings"
2223
)
@@ -57,3 +58,23 @@ func CheckPluginIndex(idx string) error {
5758
}
5859
return nil
5960
}
61+
62+
// CheckPluginName verifies that a plugin name is not empty and only contains
63+
// characters from the set [a-zA-Z0-9_.+-].
64+
func CheckPluginName(name string) error {
65+
if name == "" {
66+
return errors.New("invalid plugin name: name is empty")
67+
}
68+
69+
for _, r := range name {
70+
switch {
71+
case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z':
72+
case '0' <= r && r <= '9':
73+
case r == '-', r == '_', r == '.', r == '+':
74+
default:
75+
return fmt.Errorf("invalid plugin name %q: contains invalid character %q", name, r)
76+
}
77+
}
78+
79+
return nil
80+
}

0 commit comments

Comments
 (0)