-
Notifications
You must be signed in to change notification settings - Fork 129
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
T864 - Separate log file for unparsed queries #295
Changes from 6 commits
3530243
de2c4ff
eed25c6
b527a53
182083e
c0aa689
e4a67cf
7022de4
8d30a6f
53edf56
41717f1
5cd5f7d
21d7207
8645634
c74fd15
c5b571b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,25 +18,30 @@ package acracensor | |
|
||
import ( | ||
"github.com/cossacklabs/acra/acra-censor/common" | ||
"github.com/cossacklabs/acra/acra-censor/handlers" | ||
log "github.com/sirupsen/logrus" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// ServiceName to use in logs | ||
const ServiceName = "acra-censor" | ||
|
||
// AcraCensor describes censor data: query handler, logger and reaction on parsing errors. | ||
type AcraCensor struct { | ||
handlers []QueryHandlerInterface | ||
ignoreParseError bool | ||
logger *log.Entry | ||
handlers []QueryHandlerInterface | ||
ignoreParseError bool | ||
parsedQueriesWriter *common.QueryWriter | ||
unparsedQueriesWriter *common.QueryWriter | ||
logger *log.Entry | ||
} | ||
|
||
// NewAcraCensor creates new censor object. | ||
func NewAcraCensor() *AcraCensor { | ||
acraCensor := &AcraCensor{} | ||
acraCensor.logger = log.WithField("service", ServiceName) | ||
acraCensor.ignoreParseError = false | ||
acraCensor.parsedQueriesWriter = nil | ||
acraCensor.unparsedQueriesWriter = nil | ||
return acraCensor | ||
} | ||
|
||
|
@@ -65,37 +70,25 @@ func (acraCensor *AcraCensor) ReleaseAll() { | |
|
||
// HandleQuery processes every query through each handler. | ||
func (acraCensor *AcraCensor) HandleQuery(rawQuery string) error { | ||
if len(acraCensor.handlers) == 0 { | ||
if len(acraCensor.handlers) == 0 && acraCensor.parsedQueriesWriter == nil && acraCensor.unparsedQueriesWriter == nil { | ||
// no handlers, AcraCensor won't work | ||
return nil | ||
} | ||
normalizedQuery, queryWithHiddenValues, parsedQuery, err := common.HandleRawSQLQuery(rawQuery) | ||
// Unparsed query handling | ||
if err == common.ErrQuerySyntaxError { | ||
acraCensor.logger.WithError(err).Warning("Failed to parse input query") | ||
acraCensor.saveUnparsedQuery(rawQuery) | ||
if acraCensor.ignoreParseError { | ||
acraCensor.logger.Infof("Unparsed query has been allowed") | ||
acraCensor.logger.Infoln("Unparsed query has been allowed") | ||
return nil | ||
} | ||
acraCensor.logger.Errorf("Unparsed query has been forbidden") | ||
acraCensor.logger.Errorln("Unparsed query has been forbidden") | ||
return err | ||
} | ||
|
||
// Parsed query handling | ||
acraCensor.saveParsedQuery(queryWithHiddenValues) | ||
for _, handler := range acraCensor.handlers { | ||
// in QueryCapture Handler we use only redacted queries | ||
if queryCaptureHandler, ok := handler.(*handlers.QueryCaptureHandler); ok { | ||
queryCaptureHandler.CheckQuery(queryWithHiddenValues, parsedQuery) | ||
continue | ||
} | ||
// in QueryIgnore Handler we use only raw queries | ||
if queryIgnoreHandler, ok := handler.(*handlers.QueryIgnoreHandler); ok { | ||
continueHandling, _ := queryIgnoreHandler.CheckQuery(rawQuery, parsedQuery) | ||
if continueHandling { | ||
continue | ||
} else { | ||
break | ||
} | ||
} | ||
// remained handlers operate | ||
continueHandling, err := handler.CheckQuery(normalizedQuery, parsedQuery) | ||
if err != nil { | ||
acraCensor.logger.Errorf("Forbidden query: '%s'", queryWithHiddenValues) | ||
|
@@ -110,3 +103,40 @@ func (acraCensor *AcraCensor) HandleQuery(rawQuery string) error { | |
acraCensor.logger.Infof("Allowed query: '%s'", queryWithHiddenValues) | ||
return nil | ||
} | ||
|
||
// GetLoggingTimeout returns current timeout of censor's logging process | ||
func (acraCensor *AcraCensor) GetLoggingTimeout() time.Duration { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we doesn't need such methods because they will not called anywhere and logging timeout we set once at first censor configurations with some constant and it will not changed during acra-server run. so I think we don't need extend AcraCensorInterface. anyway it's specific settings for handler, not censor at all. |
||
return acraCensor.parsedQueriesWriter.GetSerializationTimeout() | ||
} | ||
|
||
// SetLoggingTimeout sets timeout of censor's logging process | ||
func (acraCensor *AcraCensor) SetLoggingTimeout(duration time.Duration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. save as above |
||
acraCensor.parsedQueriesWriter.SetSerializationTimeout(duration) | ||
acraCensor.unparsedQueriesWriter.SetSerializationTimeout(duration) | ||
} | ||
|
||
func (acraCensor *AcraCensor) saveUnparsedQuery(query string) { | ||
if acraCensor.unparsedQueriesWriter != nil { | ||
saveQuery(acraCensor.unparsedQueriesWriter, query) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imho better to use |
||
} | ||
} | ||
|
||
func (acraCensor *AcraCensor) saveParsedQuery(query string) { | ||
if acraCensor.parsedQueriesWriter != nil { | ||
saveQuery(acraCensor.parsedQueriesWriter, query) | ||
} | ||
} | ||
|
||
func saveQuery(writer *common.QueryWriter, query string) { | ||
//skip already captured queries | ||
for _, capturedQuery := range writer.Queries { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is logic of QueryWriter, not censor. better to move this logic to query writer and add some method like after moving we will not need writer.Queries as public field and change to private |
||
if strings.EqualFold(capturedQuery.RawQuery, query) { | ||
return | ||
} | ||
} | ||
queryInfo := &common.QueryInfo{} | ||
queryInfo.RawQuery = query | ||
queryInfo.IsForbidden = false | ||
writer.Queries = append(writer.Queries, queryInfo) | ||
writer.BufferedQueries = append(writer.BufferedQueries, queryInfo) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,10 @@ limitations under the License. | |
// https://github.com/cossacklabs/acra/wiki/AcraCensor | ||
package acracensor | ||
|
||
import "github.com/cossacklabs/acra/sqlparser" | ||
import ( | ||
"github.com/cossacklabs/acra/sqlparser" | ||
"time" | ||
) | ||
|
||
// QueryHandlerInterface describes what actions are available for queries. | ||
type QueryHandlerInterface interface { | ||
|
@@ -36,4 +39,6 @@ type AcraCensorInterface interface { | |
AddHandler(handler QueryHandlerInterface) | ||
RemoveHandler(handler QueryHandlerInterface) | ||
ReleaseAll() | ||
GetLoggingTimeout() time.Duration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imho we don't need it |
||
SetLoggingTimeout(duration time.Duration) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can skip it because its already nil