Skip to content

Commit

Permalink
Merge pull request #401 from sapcc/context-background
Browse files Browse the repository at this point in the history
Replace context.Background() in all places, use context for audit trail
  • Loading branch information
majewsky committed Jul 9, 2024
2 parents 5fb2498 + 426e373 commit 11f46b9
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/anycastmonitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func run(cmd *cobra.Command, args []string) {
// expose metrics endpoint
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
ctx := httpext.ContextWithSIGINT(context.Background(), 1*time.Second)
ctx := httpext.ContextWithSIGINT(cmd.Context(), 1*time.Second)
go func() {
must.Succeed(httpext.ListenAndServeContext(ctx, listenAddress, mux))
}()
Expand Down
5 changes: 2 additions & 3 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package apicmd

import (
"context"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -64,8 +63,8 @@ func run(cmd *cobra.Command, args []string) {
keppel.SetTaskName("api")

cfg := keppel.ParseConfiguration()
ctx := httpext.ContextWithSIGINT(context.Background(), 10*time.Second)
auditor := keppel.InitAuditTrail()
ctx := httpext.ContextWithSIGINT(cmd.Context(), 10*time.Second)
auditor := keppel.InitAuditTrail(ctx)

db := must.Return(keppel.InitDB(cfg.DatabaseURL))
must.Succeed(setupDBIfRequested(db))
Expand Down
2 changes: 1 addition & 1 deletion cmd/healthmonitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type healthMonitorJob struct {
}

func run(cmd *cobra.Command, args []string) {
ctx := httpext.ContextWithSIGINT(context.Background(), 1*time.Second)
ctx := httpext.ContextWithSIGINT(cmd.Context(), 1*time.Second)
keppel.SetTaskName("health-monitor")
prometheus.MustRegister(healthmonitorResultGauge)

Expand Down
5 changes: 2 additions & 3 deletions cmd/janitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package janitorcmd

import (
"context"
"net/http"
"time"

Expand Down Expand Up @@ -54,8 +53,8 @@ func run(cmd *cobra.Command, args []string) {
keppel.SetTaskName("janitor")

cfg := keppel.ParseConfiguration()
ctx := httpext.ContextWithSIGINT(context.Background(), 10*time.Second)
auditor := keppel.InitAuditTrail()
ctx := httpext.ContextWithSIGINT(cmd.Context(), 10*time.Second)
auditor := keppel.InitAuditTrail(ctx)

db := must.Return(keppel.InitDB(cfg.DatabaseURL))
ad := must.Return(keppel.NewAuthDriver(ctx, osext.MustGetenv("KEPPEL_DRIVER_AUTH"), nil))
Expand Down
2 changes: 1 addition & 1 deletion cmd/trivyproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ The token is used to both authenticate API requests to the proxy, as well to aut
func run(cmd *cobra.Command, args []string) {
keppel.SetTaskName("trivy")

ctx := httpext.ContextWithSIGINT(context.Background(), 10*time.Second)
ctx := httpext.ContextWithSIGINT(cmd.Context(), 10*time.Second)

token := osext.MustGetenv("KEPPEL_TRIVY_TOKEN")
dbMirrorPrefix := osext.MustGetenv("KEPPEL_TRIVY_DB_MIRROR_PREFIX")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/redis/go-redis/v9 v9.5.3
github.com/rs/cors v1.11.0
github.com/sapcc/go-api-declarations v1.11.3
github.com/sapcc/go-bits v0.0.0-20240708133634-c3fa8372671d
github.com/sapcc/go-bits v0.0.0-20240709125621-b1e90ef040ad
github.com/spf13/cobra v1.8.1
github.com/timewasted/go-accept-headers v0.0.0-20130320203746-c78f304b1b09
go.uber.org/automaxprocs v1.5.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/sapcc/go-api-declarations v1.11.3 h1:A8JgeSmOdziYXuiOes9Lp3LKZ0FsU2lc9FOxoM3kRR0=
github.com/sapcc/go-api-declarations v1.11.3/go.mod h1:83R3hTANhuRXt/pXDby37IJetw8l7DG41s33Tp9NXxI=
github.com/sapcc/go-bits v0.0.0-20240708133634-c3fa8372671d h1:T2+b3BdnLVgagXxNgTyk/1MTSKDk1wAEmt6tFwrQo2U=
github.com/sapcc/go-bits v0.0.0-20240708133634-c3fa8372671d/go.mod h1:d9JN0Gm8lF5jUMQeH/3MS5iNhs6/AlR/wVQ8vxRAGmo=
github.com/sapcc/go-bits v0.0.0-20240709125621-b1e90ef040ad h1:e0kDKCEhohs+oKwYRRAibCJdqd2DvLwWrK17yELxUpY=
github.com/sapcc/go-bits v0.0.0-20240709125621-b1e90ef040ad/go.mod h1:d9JN0Gm8lF5jUMQeH/3MS5iNhs6/AlR/wVQ8vxRAGmo=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
8 changes: 4 additions & 4 deletions internal/drivers/openstack/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func hashCacheKey(cacheKey string) string {
return "keystone-" + hex.EncodeToString(sha256Hash[:])
}

func (c redisCacher) StoreTokenPayload(cacheKey string, payload []byte) {
err := c.Set(context.Background(), hashCacheKey(cacheKey), payload, 5*time.Minute).Err()
func (c redisCacher) StoreTokenPayload(ctx context.Context, cacheKey string, payload []byte) {
err := c.Set(ctx, hashCacheKey(cacheKey), payload, 5*time.Minute).Err()
if err != nil {
logg.Error("cannot cache token payload in Redis: %s", err.Error())
}
}

func (c redisCacher) LoadTokenPayload(cacheKey string) []byte {
payload, err := c.Get(context.Background(), hashCacheKey(cacheKey)).Bytes()
func (c redisCacher) LoadTokenPayload(ctx context.Context, cacheKey string) []byte {
payload, err := c.Get(ctx, hashCacheKey(cacheKey)).Bytes()
if errors.Is(err, redis.Nil) {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/drivers/openstack/keystone.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (d *keystoneDriver) AuthenticateUser(ctx context.Context, userName, passwor
throwAwayClient.SetTokenAndAuthResult(nil) //nolint:errcheck

t := d.TokenValidator.CheckCredentials(
fmt.Sprintf("username=%s,password=%s", userName, password),
ctx, fmt.Sprintf("username=%s,password=%s", userName, password),
func() gopherpolicy.TokenResult { return tokens.Create(ctx, &throwAwayClient, &authOpts) },
)

Expand Down
5 changes: 3 additions & 2 deletions internal/keppel/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package keppel

import (
"context"
"encoding/json"
"net"
"net/http"
Expand Down Expand Up @@ -76,7 +77,7 @@ type auditorImpl struct {

// InitAuditTrail initializes a Auditor from the configuration variables
// found in the environment.
func InitAuditTrail() Auditor {
func InitAuditTrail(ctx context.Context) Auditor {
logg.Debug("initializing audit trail...")

prometheus.MustRegister(auditEventPublishSuccessCounter)
Expand Down Expand Up @@ -110,7 +111,7 @@ func InitAuditTrail() Auditor {
EventSink: eventSink,
OnSuccessfulPublish: func() { auditEventPublishSuccessCounter.Inc() },
OnFailedPublish: func() { auditEventPublishFailedCounter.Inc() },
}.Commit(rabbitURI, rabbitQueueName)
}.Commit(ctx, rabbitURI, rabbitQueueName)
}

return auditorImpl{
Expand Down
2 changes: 1 addition & 1 deletion internal/test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func NewSetup(t *testing.T, opts ...SetupOption) Setup {
quotasSetFor := make(map[string]bool)
for _, account := range params.Accounts {
mustDo(t, s.DB.Insert(account))
fd.RecordExistingAccount(context.Background(), *account, s.Clock.Now()) //nolint:errcheck
fd.RecordExistingAccount(s.Ctx, *account, s.Clock.Now()) //nolint:errcheck
if params.WithQuotas && !quotasSetFor[account.AuthTenantID] {
mustDo(t, s.DB.Insert(&models.Quotas{
AuthTenantID: account.AuthTenantID,
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/sapcc/go-bits/audittools/rabbitmq.go

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

5 changes: 3 additions & 2 deletions vendor/github.com/sapcc/go-bits/audittools/trail.go

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

5 changes: 3 additions & 2 deletions vendor/github.com/sapcc/go-bits/gopherpolicy/cache.go

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

13 changes: 7 additions & 6 deletions vendor/github.com/sapcc/go-bits/gopherpolicy/pkg.go

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ github.com/samber/lo
## explicit; go 1.21
github.com/sapcc/go-api-declarations/bininfo
github.com/sapcc/go-api-declarations/cadf
# github.com/sapcc/go-bits v0.0.0-20240708133634-c3fa8372671d
# github.com/sapcc/go-bits v0.0.0-20240709125621-b1e90ef040ad
## explicit; go 1.22
github.com/sapcc/go-bits/assert
github.com/sapcc/go-bits/audittools
Expand Down

0 comments on commit 11f46b9

Please sign in to comment.