Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Adds more explicit 404 status codes #1294

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sdk/nanotdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ const (
cipherModeAes256gcm128Bit CipherMode = 5
)

const (
ErrNanoTDFHeaderRead = Error("nanoTDF read error")
var (
ErrNanoTDFHeaderRead = fmt.Errorf("nanoTDF read error [%w]", ErrParseFailed)
)

// Binding config byte format
Expand Down
6 changes: 3 additions & 3 deletions sdk/resource_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,18 @@ func (rl ResourceLocator) writeResourceLocator(writer io.Writer) error {
// readResourceLocator - read the encoded protocol and body string into a ResourceLocator
func (rl *ResourceLocator) readResourceLocator(reader io.Reader) error {
if err := binary.Read(reader, binary.BigEndian, &rl.protocol); err != nil {
return errors.Join(Error("Error reading ResourceLocator protocol value"), err)
return errors.Join(ErrParseFailed, Error("error reading ResourceLocator protocol value"), ErrParseFailed, err)
}
if (rl.protocol != urlProtocolHTTP) && (rl.protocol != urlProtocolHTTPS) { // TODO - support 'shared' protocol?
return errors.New("Unsupported protocol: " + strconv.Itoa(int(rl.protocol)))
}
var lengthBody byte
if err := binary.Read(reader, binary.BigEndian, &lengthBody); err != nil {
return errors.Join(Error("Error reading ResourceLocator body length value"), err)
return errors.Join(ErrParseFailed, Error("Error reading ResourceLocator body length value"), err)
}
body := make([]byte, lengthBody)
if err := binary.Read(reader, binary.BigEndian, &body); err != nil {
return errors.Join(Error("Error reading ResourceLocator body value"), err)
return errors.Join(ErrParseFailed, Error("Error reading ResourceLocator body value"), err)
}
rl.body = string(body) // TODO - normalize to lowercase?
return nil
Expand Down
4 changes: 3 additions & 1 deletion sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ const (
// Failure while connecting to a service.
// Check your configuration and/or retry.
ErrGrpcDialFailed = Error("failed to dial grpc endpoint")
ErrShutdownFailed = Error("failed to shutdown sdk")
ErrInvalidAttributes = Error("one or more attributes is invalid or undefined")
ErrParseFailed = Error("invalid file syntax or structure")
ErrPlatformConfigFailed = Error("failed to retrieve platform configuration")
ErrPlatformEndpointMalformed = Error("platform endpoint is malformed")
ErrShutdownFailed = Error("failed to shutdown sdk")
)

type Error string
Expand Down
6 changes: 6 additions & 0 deletions sdk/tdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"github.com/opentdf/platform/sdk/internal/archive"
"github.com/opentdf/platform/sdk/internal/autoconfigure"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var (
Expand Down Expand Up @@ -128,7 +130,7 @@
return nil, fmt.Errorf("NewTDFConfig failed: %w", err)
}

if tdfConfig.autoconfigure {

Check failure on line 133 in sdk/tdf.go

View workflow job for this annotation

GitHub Actions / go (sdk)

`if tdfConfig.autoconfigure` has complex nested blocks (complexity: 6) (nestif)
var g autoconfigure.Granter
if len(tdfConfig.attributeValues) > 0 {
g, err = autoconfigure.NewGranterFromAttributes(tdfConfig.attributeValues...)
Expand All @@ -136,6 +138,10 @@
g, err = autoconfigure.NewGranterFromService(ctx, s.Attributes, tdfConfig.attributes...)
}
if err != nil {
if status.Code(err) == codes.NotFound {
// One or more attribute was not found
return nil, errors.Join(err, ErrInvalidAttributes)
}
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion service/authorization/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (as *AuthorizationService) GetDecisions(ctx context.Context, req *authoriza
if err != nil {
// if attribute an FQN does not exist
// return deny for all entity chains aginst this RA set and continue to next
if errors.Is(err, db.StatusifyError(db.ErrNotFound, "")) {
if errors.Is(err, db.ErrNotFound) {
for _, ec := range dr.GetEntityChains() {
decisionResp := &authorization.DecisionResponse{
Decision: authorization.DecisionResponse_DECISION_DENY,
Expand Down
5 changes: 5 additions & 0 deletions service/integration/attribute_fqns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ func (s *AttributeFqnSuite) TestGetAttributesByValueFqns_Fails_WithDeactivatedNa
s.Require().NoError(err)

// get the attribute by the value fqn for v1
var nfe db.NotFoundError
v, err := s.db.PolicyClient.GetAttributesByValueFqns(s.ctx, &attributes.GetAttributeValuesByFqnsRequest{
Fqns: []string{fqnBuilder(ns.GetName(), attr.GetName(), v1.GetValue())},
WithValue: &policy.AttributeValueSelector{
Expand All @@ -731,6 +732,8 @@ func (s *AttributeFqnSuite) TestGetAttributesByValueFqns_Fails_WithDeactivatedNa
s.Require().Error(err)
s.Nil(v)
s.Require().ErrorIs(err, db.ErrNotFound)
s.Require().ErrorAs(err, &nfe)
s.Contains(nfe.What, "https://test_fqn_namespace.co/attr/test_attr/value/value1")

// get the attribute by the value fqn for v2
v, err = s.db.PolicyClient.GetAttributesByValueFqns(s.ctx, &attributes.GetAttributeValuesByFqnsRequest{
Expand All @@ -742,6 +745,8 @@ func (s *AttributeFqnSuite) TestGetAttributesByValueFqns_Fails_WithDeactivatedNa
s.Require().Error(err)
s.Nil(v)
s.Require().ErrorIs(err, db.ErrNotFound)
s.Require().ErrorAs(err, &nfe)
s.Contains(nfe.What, "https://test_fqn_namespace.co/attr/test_attr/value/value2")
}

func (s *AttributeFqnSuite) TestGetAttributesByValueFqns_Fails_WithDeactivatedAttributeDefinition() {
Expand Down
12 changes: 12 additions & 0 deletions service/pkg/db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ var (
ErrMissingValue = errors.New("ErrMissingValue: value must be included")
)

type NotFoundError struct {
What []string
}

func (e NotFoundError) Error() string {
return fmt.Sprintf("not found: %v", e.What)
}

func (e NotFoundError) Unwrap() error {
return ErrNotFound
}

// Get helpful error message for PostgreSQL violation
func WrapIfKnownInvalidQueryErr(err error) error {
if e := isPgError(err); e != nil {
Expand Down
3 changes: 3 additions & 0 deletions service/policy/db/attribute_fqn.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ func (c *PolicyDBClient) GetAttributesByValueFqns(ctx context.Context, r *attrib
attr, err := c.GetAttributeByFqn(ctx, fqn)
if err != nil {
c.logger.Error("could not get attribute by FQN", slog.String("fqn", fqn), slog.String("error", err.Error()))
if errors.Is(err, db.ErrNotFound) {
return nil, errors.Join(db.NotFoundError{What: []string{fqn}}, err)
}
return nil, err
}
filtered, selected := prepareValues(attr.GetValues(), fqn)
Expand Down
Loading