Skip to content

Introduce Initdata subcommand#41

Merged
bpradipt merged 10 commits into
confidential-devhub:mainfrom
bpradipt:initdata
Apr 30, 2026
Merged

Introduce Initdata subcommand#41
bpradipt merged 10 commits into
confidential-devhub:mainfrom
bpradipt:initdata

Conversation

@bpradipt
Copy link
Copy Markdown
Contributor

No description provided.

Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a new initdata command group to generate, dump, and validate initdata artifacts for Confidential Containers, while extending the pkg/initdata library with raw generation and decoding support.

Changes:

  • Add kubectl coco initdata {create,dump,validate} subcommands (plus shared helpers and fixtures).
  • Extend pkg/initdata with GenerateRaw (raw TOML) and Decode (base64+gzip → data map), and refactor Generate to reuse GenerateRaw.
  • Remove the legacy dump-initdata command and its tests; update root command wiring and local-only planning artifact guidance.

Reviewed changes

Copilot reviewed 20 out of 21 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
pkg/initdata/initdata.go Refactors generation to add GenerateRaw, adds Decode, adjusts policy loading logic.
pkg/initdata/initdata_test.go Adds unit tests for GenerateRaw, Generate, and Decode round-trips/error cases.
cmd/root.go Registers the new initdata command group.
cmd/initdata/initdata.go Adds initdata root command and wires create, dump, validate subcommands.
cmd/initdata/create.go Implements initdata create (config loading, optional CA bundle embed, writes raw TOML).
cmd/initdata/dump.go Implements initdata dump (reads saved TOML, outputs raw or base64+gzip).
cmd/initdata/validate.go Implements initdata validate (structure/version/algorithm/key presence + embedded cert checks).
cmd/initdata/common.go Shared helpers: config loading, blob decompress, cert parsing/validation, cert extraction.
cmd/initdata/common_test.go Test coverage for shared helpers (cert parsing/dir loading, blob decode, extraction).
cmd/initdata/create_test.go Test coverage for create behavior including cert validation and output mode.
cmd/initdata/dump_test.go Test coverage for dump raw/encoded output and missing file handling.
cmd/initdata/validate_test.go Test coverage for validate from file/stdin and invalid cases (version/algorithm/missing keys/certs).
cmd/initdata/testdata/*.toml Validation fixtures for valid/invalid initdata TOML inputs.
cmd/dump_initdata.go Removes the legacy dump-initdata command implementation.
cmd/dump_initdata_test.go Removes tests for the legacy dump-initdata command.
.gitignore Ignores local-only docs/ planning artifacts.
CLAUDE.md Documents that docs/ is for local-only AI planning artifacts and should not be committed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/initdata/common.go
Comment on lines +39 to +42
path := filepath.Join(dir, entry.Name())
// #nosec G304
data, err := os.ReadFile(path)
if err != nil {
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

Repo convention is to include a justification on gosec suppressions (e.g., // #nosec G304 -- <reason>). Please add a reason here so it’s clear why iterating and reading files from this directory is safe/expected.

Copilot uses AI. Check for mistakes.
Comment thread cmd/initdata/common.go
Comment on lines +128 to +131
if filePath != "" {
// #nosec G304
data, err := os.ReadFile(filePath)
if err != nil {
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

Repo convention is to include a justification on gosec suppressions (e.g., // #nosec G304 -- <reason>). Please add a reason here (the path comes from --file), so the suppression is auditable.

Copilot uses AI. Check for mistakes.
Comment thread cmd/initdata/dump.go Outdated
filePath = filepath.Join(home, ".kube", "coco-initdata.toml")
}

// #nosec G304
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

Repo convention is to include a justification on gosec suppressions (e.g., // #nosec G304 -- <reason>). Please add a reason here (path is user-provided via --file or defaulted), so the suppression is consistent and reviewable.

Suggested change
// #nosec G304
// #nosec G304 -- path is user-provided via --file or defaults to ~/.kube/coco-initdata.toml for this CLI command

Copilot uses AI. Check for mistakes.
Comment thread cmd/initdata/create.go
createCmd.Flags().StringVar(&createCACert, "cacert", "", "Path to CA cert PEM file")
createCmd.Flags().StringVar(&createCAPath, "capath", "", "Path to directory of CA cert PEM files")
createCmd.Flags().StringVar(&createOutput, "output", "", "Output file for raw TOML (default: ~/.kube/coco-initdata.toml)")
createCmd.MarkFlagsMutuallyExclusive("cacert", "capath")
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

createCmd.MarkFlagsMutuallyExclusive(...) returns an error in cobra v1.10.x; ignoring it can mask issues if flag names change or initialization order is altered. Please handle the returned error (e.g., if err := ...; err != nil { return/panic }) to make failures explicit during command setup.

Suggested change
createCmd.MarkFlagsMutuallyExclusive("cacert", "capath")
if err := createCmd.MarkFlagsMutuallyExclusive("cacert", "capath"); err != nil {
panic(err)
}

Copilot uses AI. Check for mistakes.
Comment thread pkg/initdata/initdata.go
Comment on lines 210 to 217
cleanPath := filepath.Clean(path)

// For absolute paths, validate they don't escape the filesystem root
// For relative paths, ensure they're relative to current directory
if filepath.IsAbs(cleanPath) {
// Absolute paths are allowed for policy files
// but ensure path doesn't contain traversal attempts
if strings.Contains(path, "..") {
return "", fmt.Errorf("invalid policy path: contains directory traversal")
}
} else {
// For relative paths, ensure they resolve within current directory
if !filepath.IsAbs(cleanPath) {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get current directory: %w", err)
}
absPath := filepath.Join(cwd, cleanPath)
if !strings.HasPrefix(absPath, cwd) {
return "", fmt.Errorf("invalid policy path: escapes current directory")
}
cleanPath = absPath
cleanPath = filepath.Join(cwd, cleanPath)
}
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

loadPolicyFile no longer prevents relative paths from escaping the current working directory. With the current Clean + Join(cwd, cleanPath) logic, an input like ../secrets/policy.rego will resolve outside cwd, which reintroduces directory traversal risk for user-controlled config values (e.g., kata_agent_policy). Consider restoring the previous guard (e.g., compute an absolute path and ensure filepath.Rel(cwd, absPath) does not start with ..), or otherwise restrict/validate allowed locations before os.ReadFile.

Copilot uses AI. Check for mistakes.
Comment thread pkg/initdata/initdata.go Outdated
}

// #nosec G304 - Path is validated above
// #nosec G304
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

Repo convention is to include a justification on gosec suppressions (e.g., // #nosec G304 -- <reason>). Please add a reason here so future readers understand why reading this path is considered safe in this context.

Suggested change
// #nosec G304
// #nosec G304 -- path is cleaned and resolved to an absolute path before reading a user-specified local policy file

Copilot uses AI. Check for mistakes.
Comment thread cmd/initdata/common.go Outdated
Comment on lines +21 to +23
// #nosec G304
data, err := os.ReadFile(path)
if err != nil {
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

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

Repo convention is to include a justification on gosec suppressions (e.g., // #nosec G304 -- <reason>). Please add a reason here so future readers understand why reading this user-provided path is acceptable.

Copilot uses AI. Check for mistakes.
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
@bpradipt bpradipt merged commit 7534d41 into confidential-devhub:main Apr 30, 2026
3 checks passed
@bpradipt bpradipt deleted the initdata branch April 30, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants