From 06e85162905bfea2254ace0218ababeeba2fe3db Mon Sep 17 00:00:00 2001 From: ICHINOSE Shogo Date: Fri, 12 Jan 2024 16:15:15 +0900 Subject: [PATCH] Add slog package for logging Remove ctxlog package Update dependencies in go.mod and go.sum --- provider/assume-role/assume-role.go | 20 +++++++++----------- provider/assume-role/go.mod | 1 - provider/assume-role/go.sum | 2 -- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/provider/assume-role/assume-role.go b/provider/assume-role/assume-role.go index 70c129dd..1986035c 100644 --- a/provider/assume-role/assume-role.go +++ b/provider/assume-role/assume-role.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io" + "log/slog" "net/http" "strconv" "strings" @@ -20,10 +21,8 @@ import ( "github.com/aws/aws-sdk-go-v2/service/sts/types" "github.com/aws/smithy-go" "github.com/fuller-inc/actions-aws-assume-role/provider/assume-role/github" - "github.com/shogo82148/aws-xray-yasdk-go/xray" "github.com/shogo82148/aws-xray-yasdk-go/xrayaws-v2" "github.com/shogo82148/aws-xray-yasdk-go/xrayhttp" - log "github.com/shogo82148/ctxlog" ) const ( @@ -64,13 +63,15 @@ func NewHandler() *Handler { cfg, err := config.LoadDefaultConfig(ctx, xrayaws.WithXRay()) if err != nil { - log.Fatalf("unable to load SDK config, %v", err) + slog.ErrorContext(ctx, "unable to load SDK config", slog.String("error", err.Error())) + panic(err) } client := xrayhttp.Client(nil) githubClient, err := github.NewClient(client) if err != nil { - log.Fatalf("unable to initialize: %v", err) + slog.ErrorContext(ctx, "unable to create github client", slog.String("error", err.Error())) + panic(err) } return &Handler{ @@ -111,9 +112,6 @@ type errorResponseBody struct { func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - ctx = log.With(ctx, log.Fields{ - "x-amzn-trace-id": xray.ContextTraceID(ctx), - }) data, err := io.ReadAll(r.Body) if err != nil { @@ -136,7 +134,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(resp); err != nil { - log.Printf("failed to write the response: %v", err) + slog.InfoContext(ctx, "failed to write the response", slog.String("error", err.Error())) } } @@ -156,7 +154,7 @@ func (h *Handler) handle(ctx context.Context, req *requestBody) (*responseBody, } } } else { - log.Warn(ctx, "oidc is not available", nil) + slog.InfoContext(ctx, "OIDC token is not available") warning = "Using GITHUB_TOKEN is deprecated. Use OIDC instead of it. " + "See https://github.com/fuller-inc/actions-aws-assume-role/issues/454 and https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect" if err := h.validateGitHubToken(ctx, req); err != nil { @@ -183,7 +181,7 @@ func (h *Handler) handle(ctx context.Context, req *requestBody) (*responseBody, resp1.Warning += "It looks that you use legacy node IDs. You need to migrate them. " + "See https://github.com/fuller-inc/actions-aws-assume-role#migrate-your-node-id-to-the-next-format for more detail.\n" + err0.Error() - log.Warn(ctx, "using legacy node id", nil) + slog.InfoContext(ctx, "using legacy node id") return resp1, nil } @@ -235,7 +233,7 @@ func (h *Handler) validate(ctx context.Context, req *requestBody) error { } func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) { - log.Printf("error: %v", err) + slog.ErrorContext(r.Context(), "failed to handle the request", slog.String("error", err.Error())) status := http.StatusInternalServerError var body *errorResponseBody diff --git a/provider/assume-role/go.mod b/provider/assume-role/go.mod index 51a92275..2be06117 100644 --- a/provider/assume-role/go.mod +++ b/provider/assume-role/go.mod @@ -9,7 +9,6 @@ require ( github.com/aws/smithy-go v1.19.0 github.com/shogo82148/aws-xray-yasdk-go v1.8.0 github.com/shogo82148/aws-xray-yasdk-go/xrayaws-v2 v1.1.9 - github.com/shogo82148/ctxlog v0.1.0 github.com/shogo82148/goat v0.1.0 github.com/shogo82148/ridgenative v1.4.0 ) diff --git a/provider/assume-role/go.sum b/provider/assume-role/go.sum index ac5a70c8..3ac7e53e 100644 --- a/provider/assume-role/go.sum +++ b/provider/assume-role/go.sum @@ -36,8 +36,6 @@ github.com/shogo82148/aws-xray-yasdk-go v1.8.0 h1:efBLrzo9t3JwwLGdOUgzhZxKSiqsz6 github.com/shogo82148/aws-xray-yasdk-go v1.8.0/go.mod h1:84RrgJYiUbPssr17V+AHErBnaNBEoBKQ7RhbVAv70P0= github.com/shogo82148/aws-xray-yasdk-go/xrayaws-v2 v1.1.9 h1:IruaY6p9m2+3JsDWCrG5ze44ynDT2r/vG1/rAsgZVu0= github.com/shogo82148/aws-xray-yasdk-go/xrayaws-v2 v1.1.9/go.mod h1:ku07sIiwZUSEWc95lCjl+BgZ+zZF9kkWofHQv4zwdpQ= -github.com/shogo82148/ctxlog v0.1.0 h1:WN1kcHxnFFPrqMNKK+ZM8GKrjqAKQymWekjeq6EZ7ao= -github.com/shogo82148/ctxlog v0.1.0/go.mod h1:1vzyF5O3lITc5QGi4oYH12DwupLpZytmfxdN2vH1ZZ0= github.com/shogo82148/forwarded-header v0.1.0 h1:OZX131i0B8GGZR+s5QEZdSEYDTUPkUX8ISIIQE+swKM= github.com/shogo82148/forwarded-header v0.1.0/go.mod h1:Ge73zEgn9jrdyf65vWTgDmRZQFQPHoFU0TaILJcvs54= github.com/shogo82148/goat v0.1.0 h1:CD8v23E0rGSspMyKnS2FqiX+BJnEYs/mhgYDmo4i30k=