Skip to content

Commit

Permalink
remove API layer validation for now
Browse files Browse the repository at this point in the history
  • Loading branch information
flotter committed Oct 21, 2024
1 parent c33576c commit 927116f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 52 deletions.
8 changes: 8 additions & 0 deletions client/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func (client *Client) AddLayer(opts *AddLayerOptions) error {
Format: "yaml",
Layer: string(opts.LayerData),
}

// Add label validation here once layer persistence is supported over
// the API. We cannot do this in the plan library because JUJU already
// has labels in production systems that violates the layers file
// naming convention (which includes the label). Since JUJU uses its
// own client, we can enforce the label naming convention on all other
// systems using the Pebble supplied client by validating it here.

var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(&payload); err != nil {
return err
Expand Down
17 changes: 0 additions & 17 deletions internals/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1237,24 +1237,7 @@ func (p *Plan) checkCycles() error {
return err
}

// labelRegexp represents a match of a valid layer label, which may include a
// directory prefix (which excludes the '.d' ending, in the same way the
// file suffix is omitted).
//
// | Label | Description |
// | -------- | ---------------------------------- |
// | abc | Label of file in layers root |
// | foo/bar | Label of file inside sub-directory |
var labelRegexp = regexp.MustCompile(`^(([a-z](?:-?[a-z0-9]){2,})/)?([a-z](?:-?[a-z0-9]){2,})$`)

func ParseLayer(order int, label string, data []byte) (*Layer, error) {
// This function can be called directly over the daemon API. We
// must fail the API request if the label is not valid.
match := labelRegexp.FindStringSubmatch(label)
if match == nil {
return nil, fmt.Errorf("cannot parse layer: invalid label %q", label)
}

layer := &Layer{
Services: make(map[string]*Service),
Checks: make(map[string]*Check),
Expand Down
35 changes: 0 additions & 35 deletions internals/plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2308,38 +2308,3 @@ func (s *S) TestReadLayersDir(c *C) {
}
}
}

func (s *S) TestParseLayerLabelValidatorOK(c *C) {
for _, label := range []string{
"f12345",
"foo",
"f-1-2-3-4",
"foo-bar-baz-12345",
"f12345/f12345",
"foo/foo",
"f-1-2-3-4/f-1-2-3-4",
"foo-bar-baz-12345/foo-bar-baz-12345",
} {
_, err := plan.ParseLayer(0, label, []byte(""))
c.Assert(err, IsNil)
}
}

func (s *S) TestParseLayerLabelValidatorFail(c *C) {
for _, label := range []string{
"0foo",
"01234",
"f",
"foo--bar",
"fooBar",
"foo/0foo",
"foo/01234",
"foo/f",
"foo/foo--bar",
"foo/fooBar",
"foo/bar/baz",
} {
_, err := plan.ParseLayer(0, label, []byte(""))
c.Assert(err, ErrorMatches, ".*invalid label.*")
}
}

0 comments on commit 927116f

Please sign in to comment.