Skip to content

Commit

Permalink
Update auth handling to match TES Compliance Suite
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeckman314 committed Jan 25, 2024
1 parent 579dd1b commit 7df0738
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func authorize(ctx context.Context, user, password string) error {
if requser == user && reqpass == password {
return nil
}
return status.Errorf(codes.PermissionDenied, "")
return status.Errorf(codes.PermissionDenied, "AUTH DENIED")
}
}
}

return status.Errorf(codes.Unauthenticated, "")
return status.Errorf(codes.Unauthenticated, "UNAUTHENTICATED")
}

// parseBasicAuth parses an HTTP Basic Authentication string.
Expand Down
26 changes: 21 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -58,7 +59,7 @@ func newDebugInterceptor(log *logger.Logger) grpc.UnaryServerInterceptor {

// customErrorHandler is a custom error handler for the gRPC gateway
// Returns '400' for invalid backend parameters and '500' for all other errors
// Required for Compliance Tests
// Required for TES Compliance Tests
func customErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
const fallback = `{"error": "failed to process the request"}`

Expand All @@ -70,10 +71,25 @@ func customErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler ru
}

// Map specific gRPC error codes to HTTP status codes
if (strings.Contains(st.Message(), "backend parameters not supported")) {
w.WriteHeader(http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusInternalServerError)
switch st.Code() {
case codes.NotFound:
// Special case for missing tasks (TES Compliance Suite)
if (strings.Contains(st.Message(), "task not found")) {
w.WriteHeader(http.StatusInternalServerError) // 500
} else {
w.WriteHeader(http.StatusNotFound) // 404
}
case codes.PermissionDenied:
w.WriteHeader(http.StatusForbidden) // 403
case codes.Unauthenticated:
w.WriteHeader(http.StatusUnauthorized) // 401
default:
// Special case for missing backend parameters (TODO: send error codes from backends?)
if (strings.Contains(st.Message(), "backend parameters not supported")) {
w.WriteHeader(http.StatusBadRequest) // 400
} else {
w.WriteHeader(http.StatusInternalServerError) // 500
}
}

// Write the error message
Expand Down
2 changes: 1 addition & 1 deletion tests/core/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestGetUnknownTask(t *testing.T) {
Id: "nonexistent-task-id",
View: tes.View_MINIMAL.String(),
})
if err == nil || !strings.Contains(err.Error(), "STATUS CODE - 404") {
if err == nil || !strings.Contains(err.Error(), "STATUS CODE - 500") {
t.Error("expected not found error", err)
}

Expand Down

0 comments on commit 7df0738

Please sign in to comment.