-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to define custom outcome handler in middleware (#51)
This allows library users to define their own heuristics on how they want outcomes to be defined. This could be using just the `gin.Context` object that the function takes, or any other heuristic based on the program's logic. Signed-off-by: Juan Antonio Osorio <[email protected]>
- Loading branch information
Showing
4 changed files
with
142 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2022 Equinix, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package ginaudit | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"github.com/metal-toolbox/auditevent" | ||
) | ||
|
||
// OutcomeHandler is a function that returns the AuditEvent outcome | ||
// for a given request. This will be called after other middleware; e.g. | ||
// the given gin context should already contain a result status. | ||
// It is recommended to return one of the samples defined in | ||
// `samples.go`. | ||
type OutcomeHandler func(c *gin.Context) string | ||
|
||
// GetOutcomeDefault is the default outcome handler that's set in | ||
// the middleware constructor. It will return `failed` for HTTP response | ||
// statuses 500 and above, `denied` for requests 400 and above and | ||
// `succeeded` otherwise. | ||
func GetOutcomeDefault(c *gin.Context) string { | ||
status := c.Writer.Status() | ||
if status >= http.StatusBadRequest && status < http.StatusInternalServerError { | ||
return auditevent.OutcomeDenied | ||
} | ||
if status >= http.StatusInternalServerError { | ||
return auditevent.OutcomeFailed | ||
} | ||
return auditevent.OutcomeSucceeded | ||
} |