Skip to content
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
29 changes: 11 additions & 18 deletions app/controlplane/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ package main

import (
"context"
"fmt"
"math/rand"
_ "net/http/pprof"
"os"
"time"

"buf.build/go/protovalidate"
"github.com/getsentry/sentry-go"
"github.com/nats-io/nats.go"
flag "github.com/spf13/pflag"

conf "github.com/chainloop-dev/chainloop/app/controlplane/internal/conf/controlplane/config/v1"
Expand All @@ -35,6 +33,7 @@ import (
"github.com/chainloop-dev/chainloop/app/controlplane/plugins/sdk/v1"
"github.com/chainloop-dev/chainloop/pkg/credentials"
"github.com/chainloop-dev/chainloop/pkg/credentials/manager"
"github.com/chainloop-dev/chainloop/pkg/natsconn"
"github.com/chainloop-dev/chainloop/pkg/servicelogger"

"github.com/go-kratos/kratos/v2"
Expand Down Expand Up @@ -145,7 +144,7 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

app, cleanup, err := wireApp(&bc, credsWriter, logger, availablePlugins)
app, cleanup, err := wireApp(ctx, &bc, credsWriter, logger, availablePlugins)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -215,29 +214,23 @@ type app struct {
apiTokenStaleRevoker *biz.APITokenStaleRevoker
}

// Connection to nats is optional, if not configured, pubsub will be disabled
func newNatsConnection(c *conf.Bootstrap_NatsServer) (*nats.Conn, error) {
// newNatsConfig converts the proto config to a plain natsconn.Config.
func newNatsConfig(c *conf.Bootstrap_NatsServer) *natsconn.Config {
uri := c.GetUri()
if uri == "" {
return nil, nil
return nil
}

var opts []nats.Option
if c.GetAuthentication() != nil {
switch c.GetAuthentication().(type) {
case *conf.Bootstrap_NatsServer_Token:
opts = append(opts, nats.Token(c.GetToken()))
default:
return nil, fmt.Errorf("unsupported nats authentication type: %T", c.GetAuthentication())
}
cfg := &natsconn.Config{
URI: uri,
Name: "chainloop-controlplane",
}

nc, err := nats.Connect(uri, opts...)
if err != nil {
return nil, fmt.Errorf("failed to connect to nats: %w", err)
if c.GetToken() != "" {
cfg.Token = c.GetToken()
}

return nc, nil
return cfg
}

func filterSensitiveArgs(_ log.Level, keyvals ...interface{}) bool {
Expand Down
31 changes: 18 additions & 13 deletions app/controlplane/cmd/wire.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2024-2025 The Chainloop Authors.
// Copyright 2024-2026 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
package main

import (
"context"
"time"

conf "github.com/chainloop-dev/chainloop/app/controlplane/internal/conf/controlplane/config/v1"
Expand All @@ -38,13 +39,13 @@ import (
"github.com/chainloop-dev/chainloop/pkg/blobmanager/loader"
"github.com/chainloop-dev/chainloop/pkg/cache"
"github.com/chainloop-dev/chainloop/pkg/credentials"
"github.com/chainloop-dev/chainloop/pkg/natsconn"
"github.com/go-kratos/kratos/v2/log"
"github.com/golang-jwt/jwt/v4"
"github.com/google/wire"
"github.com/nats-io/nats.go"
)

func wireApp(*conf.Bootstrap, credentials.ReaderWriter, log.Logger, sdk.AvailablePlugins) (*app, func(), error) {
func wireApp(context.Context, *conf.Bootstrap, credentials.ReaderWriter, log.Logger, sdk.AvailablePlugins) (*app, func(), error) {
panic(
wire.Build(
wire.Bind(new(credentials.Reader), new(credentials.ReaderWriter)),
Expand All @@ -65,7 +66,8 @@ func wireApp(*conf.Bootstrap, credentials.ReaderWriter, log.Logger, sdk.Availabl
newProtoValidator,
newDataConf,
newPolicyProviderConfig,
newNatsConnection,
newNatsConfig,
natsconn.New,
cacheProviderSet,
auditor.NewAuditLogPublisher,
newCASServerOptions,
Expand Down Expand Up @@ -141,37 +143,40 @@ var cacheProviderSet = wire.NewSet(
newPolicyEvalBundleCache,
)

func newClaimsCache(conn *nats.Conn, logger log.Logger) (cache.Cache[*jwt.MapClaims], error) {
func newClaimsCache(ctx context.Context, rc *natsconn.ReloadableConnection, logger log.Logger) (cache.Cache[*jwt.MapClaims], error) {
l := log.NewHelper(logger)
backend := "memory"
opts := []cache.Option{cache.WithTTL(10 * time.Second), cache.WithLogger(&kratosLogAdapter{h: l}), cache.WithDescription("Cache for JWT claims")}
if conn != nil {
if rc != nil {
backend = "nats"
opts = append(opts, cache.WithNATS(conn, "chainloop-jwt-claims"))
opts = append(opts, cache.WithNATS(rc.Conn, "chainloop-jwt-claims"))
opts = append(opts, cache.WithReconnect(rc.Subscribe(ctx)))
}
l.Infow("msg", "cache initialized", "bucket", "chainloop-jwt-claims", "backend", backend, "ttl", "10s")
return cache.New[*jwt.MapClaims](opts...)
}

func newMembershipsCache(conn *nats.Conn, logger log.Logger) (cache.Cache[*entities.Membership], error) {
func newMembershipsCache(ctx context.Context, rc *natsconn.ReloadableConnection, logger log.Logger) (cache.Cache[*entities.Membership], error) {
l := log.NewHelper(logger)
backend := "memory"
opts := []cache.Option{cache.WithTTL(time.Second), cache.WithLogger(&kratosLogAdapter{h: l}), cache.WithDescription("Cache for org memberships")}
if conn != nil {
if rc != nil {
backend = "nats"
opts = append(opts, cache.WithNATS(conn, "chainloop-memberships"))
opts = append(opts, cache.WithNATS(rc.Conn, "chainloop-memberships"))
opts = append(opts, cache.WithReconnect(rc.Subscribe(ctx)))
}
l.Infow("msg", "cache initialized", "bucket", "chainloop-memberships", "backend", backend, "ttl", "1s")
return cache.New[*entities.Membership](opts...)
}

func newPolicyEvalBundleCache(conn *nats.Conn, logger log.Logger) (cache.Cache[[]byte], error) {
func newPolicyEvalBundleCache(ctx context.Context, rc *natsconn.ReloadableConnection, logger log.Logger) (cache.Cache[[]byte], error) {
l := log.NewHelper(logger)
backend := "memory"
opts := []cache.Option{cache.WithTTL(24 * time.Hour), cache.WithLogger(&kratosLogAdapter{h: l}), cache.WithDescription("Cache for policy evaluation bundles from CAS")}
if conn != nil {
if rc != nil {
backend = "nats"
opts = append(opts, cache.WithNATS(conn, "chainloop-policy-eval-bundles"))
opts = append(opts, cache.WithNATS(rc.Conn, "chainloop-policy-eval-bundles"))
opts = append(opts, cache.WithReconnect(rc.Subscribe(ctx)))
}
l.Infow("msg", "cache initialized", "bucket", "chainloop-policy-eval-bundles", "backend", backend, "ttl", "24h")
return cache.New[[]byte](opts...)
Expand Down
Loading
Loading