Skip to content

Commit

Permalink
Add slog package for logging
Browse files Browse the repository at this point in the history
Remove ctxlog package
Update dependencies in go.mod and go.sum
  • Loading branch information
shogo82148 committed Jan 12, 2024
1 parent 2224a80 commit 06e8516
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
20 changes: 9 additions & 11 deletions provider/assume-role/assume-role.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"strconv"
"strings"
Expand All @@ -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 (
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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()))
}
}

Expand All @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion provider/assume-role/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 0 additions & 2 deletions provider/assume-role/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 06e8516

Please sign in to comment.