Skip to content

Commit

Permalink
i/prompting: remove ValidateConstraintsOutcomeLifespan* functions
Browse files Browse the repository at this point in the history
`ValidateConstraintsOutcomeLifespanExpiration` should be replaced by a
`Validate` method on the forthcoming `RequestRule` type, while
`ValidateConstraintsOutcomeLifespanDuration` should be unnecessary,
since validation of outcomes and lifespans will occur during
unmarshalling, and converting from duration to expiration should be done
explicitly when necessary.

Signed-off-by: Oliver Calder <oliver.calder@canonical.com>
  • Loading branch information
olivercalder committed Apr 29, 2024
1 parent f50cb62 commit 1cce32f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 93 deletions.
26 changes: 0 additions & 26 deletions interfaces/prompting/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package prompting
import (
"errors"
"fmt"
"time"

"github.com/snapcore/snapd/sandbox/apparmor/notify"
"github.com/snapcore/snapd/strutil"
Expand Down Expand Up @@ -230,28 +229,3 @@ func abstractPermissionsToAppArmorFilePermissions(iface string, permissions []st
}
return filePerms, nil
}

// ValidateConstraintsOutcomeLifespanExpiration returns an error if the given
// constraints, outcome, lifespan, or duration are invalid, else returns nil.
func ValidateConstraintsOutcomeLifespanExpiration(iface string, constraints *Constraints, outcome OutcomeType, lifespan LifespanType, expiration *time.Time, currTime time.Time) error {
if err := constraints.ValidateForInterface(iface); err != nil {
return err
}
if err := ValidateOutcome(outcome); err != nil {
return err
}
return ValidateLifespanExpiration(lifespan, expiration, currTime)
}

// ValidateConstraintsOutcomeLifespanDuration returns an error if the given
// constraints, outcome, lifespan, or duration are invalid. Otherwise, converts
// the given duration to an expiration timestamp and returns it and nil error.
func ValidateConstraintsOutcomeLifespanDuration(iface string, constraints *Constraints, outcome OutcomeType, lifespan LifespanType, duration string) (*time.Time, error) {
if err := constraints.ValidateForInterface(iface); err != nil {
return nil, err
}
if err := ValidateOutcome(outcome); err != nil {
return nil, err
}
return ValidateLifespanParseDuration(lifespan, duration)
}
67 changes: 0 additions & 67 deletions interfaces/prompting/constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package prompting_test

import (
"fmt"
"time"

// TODO: remove once PR #13849 is merged
"testing"
Expand Down Expand Up @@ -551,69 +550,3 @@ func (s *promptingSuite) TestAbstractPermissionsToAppArmorFilePermissionsUnhappy
c.Check(err, ErrorMatches, testCase.errStr)
}
}

func (s *promptingSuite) TestValidateConstraintsOutcomeLifespanExpiration(c *C) {
goodInterface := "home"
badInterface := "foo"
goodConstraints := &prompting.Constraints{
PathPattern: "/path/to/something",
Permissions: []string{"read", "write", "execute"},
}
badConstraints := &prompting.Constraints{
PathPattern: "/path{with*,groups?}/**",
Permissions: []string{"read", "write", "append"},
}
goodOutcome := prompting.OutcomeDeny
badOutcome := prompting.OutcomeUnset
goodLifespan := prompting.LifespanTimespan
badLifespan := prompting.LifespanType("foo")
currTime := time.Now()
goodExpiration := currTime.Add(10 * time.Second)
badExpiration := currTime.Add(-1 * time.Second)

err := prompting.ValidateConstraintsOutcomeLifespanExpiration(goodInterface, goodConstraints, goodOutcome, goodLifespan, &goodExpiration, currTime)
c.Check(err, IsNil)
err = prompting.ValidateConstraintsOutcomeLifespanExpiration(badInterface, goodConstraints, goodOutcome, goodLifespan, &goodExpiration, currTime)
c.Check(err, NotNil)
err = prompting.ValidateConstraintsOutcomeLifespanExpiration(goodInterface, badConstraints, goodOutcome, goodLifespan, &goodExpiration, currTime)
c.Check(err, ErrorMatches, "unsupported permission.*")
err = prompting.ValidateConstraintsOutcomeLifespanExpiration(goodInterface, goodConstraints, badOutcome, goodLifespan, &goodExpiration, currTime)
c.Check(err, ErrorMatches, "invalid outcome.*")
err = prompting.ValidateConstraintsOutcomeLifespanExpiration(goodInterface, goodConstraints, goodOutcome, badLifespan, &goodExpiration, currTime)
c.Check(err, ErrorMatches, "invalid lifespan.*")
err = prompting.ValidateConstraintsOutcomeLifespanExpiration(goodInterface, goodConstraints, goodOutcome, goodLifespan, &badExpiration, currTime)
c.Check(err, ErrorMatches, "invalid expiration.*")
}

func (s *promptingSuite) TestValidateConstraintsOutcomeLifespanDuration(c *C) {
goodInterface := "home"
badInterface := "foo"
goodConstraints := &prompting.Constraints{
PathPattern: "/path/to/something",
Permissions: []string{"read"},
}
// badConstraints := &prompting.Constraints{
// PathPattern: "bad\\path",
// Permissions: []string{"read"},
// }
goodOutcome := prompting.OutcomeAllow
badOutcome := prompting.OutcomeUnset
goodLifespan := prompting.LifespanTimespan
badLifespan := prompting.LifespanUnset
goodDuration := "10s"
badDuration := "foo"

_, err := prompting.ValidateConstraintsOutcomeLifespanDuration(goodInterface, goodConstraints, goodOutcome, goodLifespan, goodDuration)
c.Check(err, IsNil)
_, err = prompting.ValidateConstraintsOutcomeLifespanDuration(badInterface, goodConstraints, goodOutcome, goodLifespan, goodDuration)
c.Check(err, NotNil)
// TODO: add this once PR #13730 is merged:
// _, err = prompting.ValidateConstraintsOutcomeLifespanDuration(goodInterface, badConstraints, goodOutcome, goodLifespan, goodDuration)
// c.Check(err, ErrorMatches, "invalid path pattern.*")
_, err = prompting.ValidateConstraintsOutcomeLifespanDuration(goodInterface, goodConstraints, badOutcome, goodLifespan, goodDuration)
c.Check(err, ErrorMatches, "invalid outcome.*")
_, err = prompting.ValidateConstraintsOutcomeLifespanDuration(goodInterface, goodConstraints, goodOutcome, badLifespan, goodDuration)
c.Check(err, ErrorMatches, "invalid lifespan.*")
_, err = prompting.ValidateConstraintsOutcomeLifespanDuration(goodInterface, goodConstraints, goodOutcome, goodLifespan, badDuration)
c.Check(err, ErrorMatches, "invalid duration.*")
}

0 comments on commit 1cce32f

Please sign in to comment.