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

T864 - Separate log file for unparsed queries #295

Merged
merged 16 commits into from
Dec 27, 2018
32 changes: 20 additions & 12 deletions acra-censor/acra-censor_configuration_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package acracensor

import (
"github.com/cossacklabs/acra/acra-censor/common"
"github.com/cossacklabs/acra/acra-censor/handlers"
"gopkg.in/yaml.v2"
"strings"
Expand All @@ -37,9 +38,10 @@ type Config struct {
Queries []string
Tables []string
Patterns []string
Filepath string
FilePath string
}
IgnoreParseError bool `yaml:"ignore_parse_error"`
IgnoreParseError bool `yaml:"ignore_parse_error"`
ParseErrorsLog string `yaml:"parse_errors_log"`
}

// LoadConfiguration loads configuration of AcraCensor
Expand All @@ -50,6 +52,15 @@ func (acraCensor *AcraCensor) LoadConfiguration(configuration []byte) error {
return err
}
acraCensor.ignoreParseError = censorConfiguration.IgnoreParseError
if !strings.EqualFold(censorConfiguration.ParseErrorsLog, "") {
queryWriter, err := common.NewFileQueryWriter(censorConfiguration.ParseErrorsLog)
if err != nil {
return err
}
go queryWriter.Start()
acraCensor.unparsedQueriesWriter = queryWriter
}

for _, handlerConfiguration := range censorConfiguration.Handlers {
switch handlerConfiguration.Handler {
case WhitelistConfigStr:
Expand Down Expand Up @@ -78,21 +89,18 @@ func (acraCensor *AcraCensor) LoadConfiguration(configuration []byte) error {
}
acraCensor.AddHandler(blacklistHandler)
break
case QueryCaptureConfigStr:
if strings.EqualFold(handlerConfiguration.Filepath, "") {
break
}
queryCaptureHandler, err := handlers.NewQueryCaptureHandler(handlerConfiguration.Filepath)
if err != nil {
return err
}
acraCensor.AddHandler(queryCaptureHandler)
break
case QueryIgnoreConfigStr:
queryIgnoreHandler := handlers.NewQueryIgnoreHandler()
queryIgnoreHandler.AddQueries(handlerConfiguration.Queries)
acraCensor.AddHandler(queryIgnoreHandler)
break
case QueryCaptureConfigStr:
queryCaptureHandler, err := handlers.NewQueryCaptureHandler(handlerConfiguration.FilePath)
if err != nil {
return err
}
go queryCaptureHandler.Start()
acraCensor.AddHandler(queryCaptureHandler)
default:
break
}
Expand Down
37 changes: 17 additions & 20 deletions acra-censor/acra-censor_implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ 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
unparsedQueriesWriter *common.QueryWriter
logger *log.Entry
}

// NewAcraCensor creates new censor object.
Expand Down Expand Up @@ -65,37 +66,27 @@ 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.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
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
}
queryCaptureHandler.CheckQuery(queryWithHiddenValues, nil)
}
// remained handlers operate
continueHandling, err := handler.CheckQuery(normalizedQuery, parsedQuery)
if err != nil {
acraCensor.logger.Errorf("Forbidden query: '%s'", queryWithHiddenValues)
Expand All @@ -110,3 +101,9 @@ func (acraCensor *AcraCensor) HandleQuery(rawQuery string) error {
acraCensor.logger.Infof("Allowed query: '%s'", queryWithHiddenValues)
return nil
}

func (acraCensor *AcraCensor) saveUnparsedQuery(query string) {
if acraCensor.unparsedQueriesWriter != nil {
acraCensor.unparsedQueriesWriter.WriteQuery(query)
}
}
4 changes: 3 additions & 1 deletion acra-censor/acra-censor_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ 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"
)

// QueryHandlerInterface describes what actions are available for queries.
type QueryHandlerInterface interface {
Expand Down
Loading