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

Move AuditLogger interface to ecs-agent module #3653

Merged
merged 5 commits into from
Apr 26, 2023
Merged
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
3 changes: 3 additions & 0 deletions agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/aws/amazon-ecs-agent/agent
go 1.19

require (
github.com/aws/amazon-ecs-agent/ecs-agent v0.0.0
github.com/aws/aws-sdk-go v1.36.0
github.com/awslabs/go-config-generator-for-fluentd-and-fluentbit v0.0.0-20210308162251-8959c62cb8f9
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575
Expand Down Expand Up @@ -71,3 +72,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/aws/amazon-ecs-agent/ecs-agent => ../ecs-agent
5 changes: 3 additions & 2 deletions agent/handlers/task_server_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/aws/amazon-ecs-agent/agent/logger/audit"
"github.com/aws/amazon-ecs-agent/agent/stats"
"github.com/aws/amazon-ecs-agent/agent/utils/retry"
auditinterface "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit"
"github.com/cihub/seelog"
"github.com/didip/tollbooth"
"github.com/gorilla/mux"
Expand All @@ -48,7 +49,7 @@ const (
)

func taskServerSetup(credentialsManager credentials.Manager,
auditLogger audit.AuditLogger,
auditLogger auditinterface.AuditLogger,
state dockerstate.TaskEngineState,
ecsClient api.ECSClient,
cluster string,
Expand Down Expand Up @@ -109,7 +110,7 @@ func v2HandlersSetup(muxRouter *mux.Router,
statsEngine stats.Engine,
cluster string,
credentialsManager credentials.Manager,
auditLogger audit.AuditLogger,
auditLogger auditinterface.AuditLogger,
availabilityZone string,
containerInstanceArn string) {
muxRouter.HandleFunc(v2.CredentialsPath, v2.CredentialsHandler(credentialsManager, auditLogger))
Expand Down
2 changes: 1 addition & 1 deletion agent/handlers/task_server_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ import (
v2 "github.com/aws/amazon-ecs-agent/agent/handlers/v2"
v3 "github.com/aws/amazon-ecs-agent/agent/handlers/v3"
v4 "github.com/aws/amazon-ecs-agent/agent/handlers/v4"
mock_audit "github.com/aws/amazon-ecs-agent/agent/logger/audit/mocks"
"github.com/aws/amazon-ecs-agent/agent/stats"
mock_stats "github.com/aws/amazon-ecs-agent/agent/stats/mock"
agentutils "github.com/aws/amazon-ecs-agent/agent/utils"
mock_audit "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/mocks"
"github.com/aws/aws-sdk-go/aws"
"github.com/docker/docker/api/types"
"github.com/golang/mock/gomock"
Expand Down
4 changes: 2 additions & 2 deletions agent/handlers/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"fmt"
"net/http"

"github.com/aws/amazon-ecs-agent/agent/logger/audit"
"github.com/aws/amazon-ecs-agent/agent/logger/audit/request"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/request"
"github.com/cihub/seelog"
"github.com/gorilla/mux"
)
Expand Down
9 changes: 5 additions & 4 deletions agent/handlers/v1/credentials_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"github.com/aws/amazon-ecs-agent/agent/credentials"
handlersutils "github.com/aws/amazon-ecs-agent/agent/handlers/utils"
"github.com/aws/amazon-ecs-agent/agent/logger/audit"
"github.com/aws/amazon-ecs-agent/agent/logger/audit/request"
"github.com/aws/amazon-ecs-agent/agent/utils"
auditinterface "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/request"
"github.com/cihub/seelog"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ const (

// CredentialsHandler creates response for the 'v1/credentials' API. It returns a JSON response
// containing credentials when found. The HTTP status code of 400 is returned otherwise.
func CredentialsHandler(credentialsManager credentials.Manager, auditLogger audit.AuditLogger) func(http.ResponseWriter, *http.Request) {
func CredentialsHandler(credentialsManager credentials.Manager, auditLogger auditinterface.AuditLogger) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
credentialsID := getCredentialsID(r)
errPrefix := fmt.Sprintf("CredentialsV%dRequest: ", apiVersion)
Expand All @@ -67,7 +68,7 @@ func CredentialsHandler(credentialsManager credentials.Manager, auditLogger audi

// CredentialsHandlerImpl is the major logic in CredentialsHandler, abstract this out
// because v2.CredentialsHandler also uses the same logic.
func CredentialsHandlerImpl(w http.ResponseWriter, r *http.Request, auditLogger audit.AuditLogger, credentialsManager credentials.Manager, credentialsID string, errPrefix string) {
func CredentialsHandlerImpl(w http.ResponseWriter, r *http.Request, auditLogger auditinterface.AuditLogger, credentialsManager credentials.Manager, credentialsID string, errPrefix string) {
responseJSON, arn, roleType, errorMessage, err := processCredentialsRequest(credentialsManager, r, credentialsID, errPrefix)
if err != nil {
errResponseJSON, err := json.Marshal(errorMessage)
Expand Down Expand Up @@ -139,7 +140,7 @@ func processCredentialsRequest(credentialsManager credentials.Manager, r *http.R
return credentialsJSON, credentials.ARN, credentials.IAMRoleCredentials.RoleType, nil, nil
}

func writeCredentialsRequestResponse(w http.ResponseWriter, r *http.Request, httpStatusCode int, eventType string, arn string, auditLogger audit.AuditLogger, message []byte) {
func writeCredentialsRequestResponse(w http.ResponseWriter, r *http.Request, httpStatusCode int, eventType string, arn string, auditLogger auditinterface.AuditLogger, message []byte) {
auditLogger.Log(request.LogRequest{Request: r, ARN: arn}, httpStatusCode, eventType)

handlersutils.WriteJSONToResponse(w, httpStatusCode, message, handlersutils.RequestTypeCreds)
Expand Down
4 changes: 2 additions & 2 deletions agent/handlers/v2/credentials_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/aws/amazon-ecs-agent/agent/credentials"
"github.com/aws/amazon-ecs-agent/agent/handlers/utils"
v1 "github.com/aws/amazon-ecs-agent/agent/handlers/v1"
"github.com/aws/amazon-ecs-agent/agent/logger/audit"
auditinterface "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit"
"github.com/gorilla/mux"
)

Expand All @@ -40,7 +40,7 @@ const (
var CredentialsPath = credentials.V2CredentialsPath + "/" + utils.ConstructMuxVar(credentialsIDMuxName, utils.AnythingRegEx)

// CredentialsHandler creates response for the 'v2/credentials' API.
func CredentialsHandler(credentialsManager credentials.Manager, auditLogger audit.AuditLogger) func(http.ResponseWriter, *http.Request) {
func CredentialsHandler(credentialsManager credentials.Manager, auditLogger auditinterface.AuditLogger) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
credentialsID := getCredentialsID(r)
errPrefix := fmt.Sprintf("CredentialsV%dRequest: ", apiVersion)
Expand Down
11 changes: 3 additions & 8 deletions agent/logger/audit/audit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ import (

"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/logger"
"github.com/aws/amazon-ecs-agent/agent/logger/audit/request"
auditinterface "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/request"
)

type AuditLogger interface {
Log(r request.LogRequest, httpResponseCode int, eventType string)
GetContainerInstanceArn() string
GetCluster() string
}

type InfoLogger interface {
Info(i ...interface{})
}
Expand All @@ -39,7 +34,7 @@ type auditLog struct {
cfg *config.Config
}

func NewAuditLog(containerInstanceArn string, cfg *config.Config, logger InfoLogger) AuditLogger {
func NewAuditLog(containerInstanceArn string, cfg *config.Config, logger InfoLogger) auditinterface.AuditLogger {
return &auditLog{
cluster: cfg.Cluster,
containerInstanceArn: containerInstanceArn,
Expand Down
2 changes: 1 addition & 1 deletion agent/logger/audit/audit_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/credentials"
mock_infologger "github.com/aws/amazon-ecs-agent/agent/logger/audit/mocks"
"github.com/aws/amazon-ecs-agent/agent/logger/audit/request"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/request"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion agent/logger/audit/entry_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"time"

"github.com/aws/amazon-ecs-agent/agent/credentials"
"github.com/aws/amazon-ecs-agent/agent/logger/audit/request"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/request"
log "github.com/cihub/seelog"
)

Expand Down
2 changes: 1 addition & 1 deletion agent/logger/audit/generate_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

package audit

//go:generate mockgen -destination=mocks/audit_log_mocks.go -copyright_file=../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/agent/logger/audit AuditLogger,InfoLogger
//go:generate mockgen -destination=mocks/audit_log_mocks.go -copyright_file=../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/agent/logger/audit InfoLogger
66 changes: 1 addition & 65 deletions agent/logger/audit/mocks/audit_log_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions agent/vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ github.com/Microsoft/go-winio/pkg/guid
# github.com/Microsoft/hcsshim v0.8.25
## explicit; go 1.13
github.com/Microsoft/hcsshim/osversion
# github.com/aws/amazon-ecs-agent/ecs-agent v0.0.0 => ../ecs-agent
## explicit; go 1.19
github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit
github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/mocks
github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/request
# github.com/aws/aws-sdk-go v1.36.0
## explicit; go 1.11
github.com/aws/aws-sdk-go/aws
Expand Down Expand Up @@ -413,3 +418,4 @@ google.golang.org/protobuf/types/known/timestamppb
# gopkg.in/yaml.v3 v3.0.1
## explicit
gopkg.in/yaml.v3
# github.com/aws/amazon-ecs-agent/ecs-agent => ../ecs-agent
3 changes: 2 additions & 1 deletion ecs-agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ go 1.19

require (
github.com/aws/aws-sdk-go v1.36.0
github.com/stretchr/testify v1.8.2
github.com/golang/mock v1.4.1
github.com/stretchr/testify v1.7.0
golang.org/x/tools v0.6.0
)

Expand Down
Loading