Skip to content
Draft
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
32 changes: 32 additions & 0 deletions app/artifact-cas/cmd/main.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ import (

"github.com/chainloop-dev/chainloop/app/artifact-cas/internal/conf"
"github.com/chainloop-dev/chainloop/app/artifact-cas/internal/server"
"github.com/chainloop-dev/chainloop/app/artifact-cas/internal/service"
backend "github.com/chainloop-dev/chainloop/pkg/blobmanager"
"github.com/chainloop-dev/chainloop/pkg/credentials"
"github.com/chainloop-dev/chainloop/pkg/credentials/manager"
Expand DownExpand Up@@ -121,6 +122,12 @@ func main() {

_ = logger.Log(log.LevelInfo, "msg", "starting artifact-cas service", "version", Version)

// Ensure the upload staging directory exists and sweep any files a previous
// crash left behind, so verification always starts from a clean volume.
if err := prepareStagingDir(&bc, logger); err != nil {
panic(err)
}

flush, err := initSentry(&bc, logger)
defer flush()
if err != nil {
Expand DownExpand Up@@ -152,6 +159,31 @@ func newProtoValidator() (protovalidate.Validator, error) {
return protovalidate.New()
}

// prepareStagingDir resolves the upload staging directory (falling back to the
// OS temp dir when unconfigured — dev only; production mounts a dedicated
// volume), creates it, and removes any leftover staging files from a previous
// run. It must use the same directory the service is configured with (see
// serviceOpts / conf.staging_dir).
func prepareStagingDir(bc *conf.Bootstrap, logger log.Logger) error {
dir := bc.GetStagingDir()
if dir == "" {
dir = os.TempDir()
_ = logger.Log(log.LevelWarn, "msg", "staging_dir not configured, falling back to OS temp dir (dev only)", "dir", dir)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fail here do not fallback

}

if err := os.MkdirAll(dir, 0o700); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When the configured or fallback staging directory exists but is not writable, MkdirAll succeeds and startup continues, so every upload later fails in os.CreateTemp. Probe-create and remove a staging file during startup, then fail before serving traffic when that check fails.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At app/artifact-cas/cmd/main.go, line 174:
<comment>When the configured or fallback staging directory exists but is not writable, `MkdirAll` succeeds and startup continues, so every upload later fails in `os.CreateTemp`. Probe-create and remove a staging file during startup, then fail before serving traffic when that check fails.</comment>
<file context>
@@ -152,6 +159,31 @@ func newProtoValidator() (protovalidate.Validator, error) {
+ _ = logger.Log(log.LevelWarn, "msg", "staging_dir not configured, falling back to OS temp dir (dev only)", "dir", dir)
+	}
+
+	if err := os.MkdirAll(dir, 0o700); err != nil {
+ return err
+	}
</file context>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this work on scrach container?

return err
}

if _, err := service.SweepStagingDir(dir, servicelogger.ScopedHelper(logger, "staging")); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

staging is overloaded, what about staging directory

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you sweeping on boot that might cause problems booting, I'd keep startup process as limited as possible

// A sweep failure is not fatal: the deferred per-upload cleanup still
// applies, so log and continue rather than blocking startup.
_ = logger.Log(log.LevelWarn, "msg", "failed to sweep staging dir", "dir", dir, "error", err.Error())
}

return nil
}

func initSentry(c *conf.Bootstrap, logger log.Logger) (cleanupFunc func(), err error) {
cleanupFunc = func() {
sentry.Flush(2 * time.Second)
Expand Down
3 changes: 2 additions & 1 deletion app/artifact-cas/cmd/wire.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,10 +51,11 @@ func wireApp(*conf.Bootstrap, *conf.Server, *conf.Auth, credentials.Reader, log.
)
}

func serviceOpts(l log.Logger, audit *service.AuditDispatcher) []service.NewOpt {
func serviceOpts(l log.Logger, audit *service.AuditDispatcher, bc *conf.Bootstrap) []service.NewOpt {
return []service.NewOpt{
service.WithLogger(l),
service.WithAuditDispatcher(audit),
service.WithStagingDir(bc.GetStagingDir()),
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/artifact-cas/cmd/wire_gen.go

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

21 changes: 18 additions & 3 deletions app/artifact-cas/internal/conf/conf.pb.go

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

6 changes: 6 additions & 0 deletions app/artifact-cas/internal/conf/conf.proto
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,6 +28,12 @@ message Bootstrap {
// Optional NATS server configuration to publish audit events to the
// control-plane-owned stream. When unset, event publishing is disabled.
NatsServer nats_server = 5;
// Local directory where uploads (and, later, downloads) are staged on disk
// and verified against the declared digest before reaching the backend. It
// must be a writable volume; in production a dedicated emptyDir is mounted
// here (NOT tmpfs/RAM, and NOT the /tmp secret mount). When unset the service
// falls back to the OS temp dir, which is only appropriate for local dev.
string staging_dir = 6;

message NatsServer {
// NATS server URI, e.g. "nats://localhost:4222"
Expand Down
Loading
Loading