-
Notifications
You must be signed in to change notification settings - Fork 847
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2956 from redpanda-data/allow-deny-list
Add allow/deny list support
- Loading branch information
Showing
11 changed files
with
214 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2024 Redpanda Data, Inc. | ||
// | ||
// Licensed as a Redpanda Enterprise file under the Redpanda Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/redpanda-data/connect/blob/main/licenses/rcl.md | ||
|
||
package cli | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/redpanda-data/benthos/v4/public/service" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type connectorsList struct { | ||
Allow []string `yaml:"allow"` | ||
Deny []string `yaml:"deny"` | ||
} | ||
|
||
// ApplyConnectorsList attempts to read a path (if the file exists) and modifies | ||
// the provided schema according to its contents. | ||
func ApplyConnectorsList(path string, s *service.ConfigSchema) (bool, error) { | ||
cListBytes, err := os.ReadFile(path) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return false, fmt.Errorf("failed to read connector list file: %w", err) | ||
} | ||
|
||
var cList connectorsList | ||
if err := yaml.Unmarshal(cListBytes, &cList); err != nil { | ||
return false, fmt.Errorf("failed to parse connector list file: %w", err) | ||
} | ||
|
||
if len(cList.Allow) > 0 && len(cList.Deny) > 0 { | ||
return false, errors.New("connector list must only contain deny or allow items, not both") | ||
} | ||
|
||
if len(cList.Allow) == 0 && len(cList.Deny) == 0 { | ||
return false, nil | ||
} | ||
|
||
env := s.Environment() | ||
if len(cList.Allow) > 0 { | ||
env = env.With(cList.Allow...) | ||
} | ||
if len(cList.Deny) > 0 { | ||
env = env.Without(cList.Deny...) | ||
} | ||
|
||
s.SetEnvironment(env) | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2024 Redpanda Data, Inc. | ||
// | ||
// Licensed as a Redpanda Enterprise file under the Redpanda Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/redpanda-data/connect/blob/main/licenses/rcl.md | ||
|
||
package cli_test | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/redpanda-data/benthos/v4/public/service" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/redpanda-data/connect/v4/internal/cli" | ||
) | ||
|
||
func testSchema(t testing.TB) *service.ConfigSchema { | ||
t.Helper() | ||
s := service.NewEmptyEnvironment() | ||
for _, n := range []string{"a", "b", "c"} { | ||
require.NoError(t, s.RegisterInput(n, service.NewConfigSpec(), nil)) | ||
} | ||
return s.CoreConfigSchema("", "") | ||
} | ||
|
||
func TestConnectorsList(t *testing.T) { | ||
for _, testCase := range []struct { | ||
name string | ||
input string | ||
expectedMod bool | ||
expectedErrContains string | ||
expectedInputs []string | ||
}{ | ||
{ | ||
name: "no content", | ||
input: ``, | ||
expectedMod: false, | ||
expectedInputs: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
name: "two lists", | ||
input: ` | ||
deny: [ a ] | ||
allow: [ c ] | ||
`, | ||
expectedErrContains: `must only contain deny or allow items`, | ||
}, | ||
{ | ||
name: "not valid yaml", | ||
input: `&&!^@&@%$^@#$`, | ||
expectedErrContains: `failed to parse connector list file`, | ||
}, | ||
{ | ||
name: "no items listed", | ||
input: ` | ||
allow: [] | ||
deny: [] | ||
`, | ||
expectedMod: false, | ||
expectedInputs: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
name: "basic allow", | ||
input: `allow: [ a, c ]`, | ||
expectedMod: true, | ||
expectedInputs: []string{"a", "c"}, | ||
}, | ||
{ | ||
name: "basic deny", | ||
input: `deny: [ a ]`, | ||
expectedMod: true, | ||
expectedInputs: []string{"b", "c"}, | ||
}, | ||
} { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
inputPath := path.Join(tmpDir, "components_list.yaml") | ||
require.NoError(t, os.WriteFile(inputPath, []byte(testCase.input), 0666)) | ||
|
||
sch := testSchema(t) | ||
actMod, err := cli.ApplyConnectorsList(inputPath, sch) | ||
if testCase.expectedErrContains != "" { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), testCase.expectedErrContains) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, testCase.expectedMod, actMod) | ||
|
||
var actInputs []string | ||
sch.Environment().WalkInputs(func(n string, c *service.ConfigView) { | ||
actInputs = append(actInputs, n) | ||
}) | ||
assert.Equal(t, testCase.expectedInputs, actInputs) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters