Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions cmd/browsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ type Int64Flag struct {
Value int64
}

// Regular expression to validate CUID2 identifiers (24 lowercase alphanumeric characters).
var cuidRegex = regexp.MustCompile(`^[a-z0-9]{24}$`)
// Regular expression to validate CUID2 identifiers (starts with a letter, 24 lowercase alphanumeric characters).
var cuidRegex = regexp.MustCompile(`^[a-z][a-z0-9]{23}$`)

// getAvailableViewports returns the list of supported viewport configurations.
func getAvailableViewports() []string {
Expand Down
188 changes: 188 additions & 0 deletions cmd/projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package cmd

import (
"encoding/json"
"fmt"

"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/packages/param"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

var projectsCmd = &cobra.Command{
Comment thread
masnwilliams marked this conversation as resolved.
Outdated
Use: "projects",
Short: "Manage projects",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
}

var projectsListCmd = &cobra.Command{
Use: "list",
Short: "List projects",
RunE: func(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
ctx := cmd.Context()

projects, err := client.Projects.List(ctx, kernel.ProjectListParams{})
if err != nil {
pterm.Error.Println("Failed to list projects:", err)
return nil
}

if len(projects.Items) == 0 {
pterm.Info.Println("No projects found")
return nil
}

table := pterm.TableData{{"ID", "Name", "Status", "Created At"}}
for _, p := range projects.Items {
table = append(table, []string{p.ID, p.Name, string(p.Status), p.CreatedAt.String()})
}
_ = pterm.DefaultTable.WithHasHeader(true).WithData(table).Render()
return nil
},
}

var projectsCreateCmd = &cobra.Command{
Use: "create <name>",
Short: "Create a project",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
ctx := cmd.Context()

project, err := client.Projects.New(ctx, kernel.ProjectNewParams{
CreateProjectRequest: kernel.CreateProjectRequestParam{
Name: args[0],
},
})
if err != nil {
pterm.Error.Println("Failed to create project:", err)
return nil
}

pterm.Success.Printf("Created project: %s (ID: %s)\n", project.Name, project.ID)
return nil
},
}

var projectsGetCmd = &cobra.Command{
Use: "get <id>",
Short: "Get a project by ID",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
ctx := cmd.Context()

project, err := client.Projects.Get(ctx, args[0])
if err != nil {
pterm.Error.Println("Failed to get project:", err)
return nil
}

table := pterm.TableData{
{"Field", "Value"},
{"ID", project.ID},
{"Name", project.Name},
{"Status", string(project.Status)},
{"Created At", project.CreatedAt.String()},
{"Updated At", project.UpdatedAt.String()},
}
_ = pterm.DefaultTable.WithHasHeader(true).WithData(table).Render()
return nil
},
}

var projectsDeleteCmd = &cobra.Command{
Use: "delete <id>",
Short: "Delete a project",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
ctx := cmd.Context()

err := client.Projects.Delete(ctx, args[0])
if err != nil {
pterm.Error.Println("Failed to delete project:", err)
return nil
}

pterm.Success.Printf("Deleted project: %s\n", args[0])
return nil
},
}

var projectsLimitsGetCmd = &cobra.Command{
Use: "get-limits <project-id>",
Short: "Get project limit overrides",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
ctx := cmd.Context()

limits, err := client.Projects.Limits.Get(ctx, args[0])
if err != nil {
pterm.Error.Println("Failed to get project limits:", err)
return nil
}

out, _ := json.MarshalIndent(limits, "", " ")
Comment thread
masnwilliams marked this conversation as resolved.
Outdated
fmt.Println(string(out))
return nil
},
}

var projectsLimitsSetCmd = &cobra.Command{
Use: "set-limits <project-id>",
Short: "Set project limit overrides",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := getKernelClient(cmd)
ctx := cmd.Context()

inner := kernel.UpdateProjectLimitsRequestParam{}
if v, _ := cmd.Flags().GetInt64("max-concurrent-sessions"); v >= 0 && cmd.Flags().Changed("max-concurrent-sessions") {
inner.MaxConcurrentSessions = param.NewOpt(v)
}
if v, _ := cmd.Flags().GetInt64("max-persistent-sessions"); v >= 0 && cmd.Flags().Changed("max-persistent-sessions") {
inner.MaxPersistentSessions = param.NewOpt(v)
}
if v, _ := cmd.Flags().GetInt64("max-concurrent-invocations"); v >= 0 && cmd.Flags().Changed("max-concurrent-invocations") {
inner.MaxConcurrentInvocations = param.NewOpt(v)
}
if v, _ := cmd.Flags().GetInt64("max-pooled-sessions"); v >= 0 && cmd.Flags().Changed("max-pooled-sessions") {
inner.MaxPooledSessions = param.NewOpt(v)
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
}
params := kernel.ProjectLimitUpdateParams{
UpdateProjectLimitsRequest: inner,
}

limits, err := client.Projects.Limits.Update(ctx, args[0], params)
if err != nil {
pterm.Error.Println("Failed to set project limits:", err)
return nil
}

out, _ := json.MarshalIndent(limits, "", " ")
pterm.Success.Println("Project limits updated:")
fmt.Println(string(out))
return nil
},
}

func init() {
projectsLimitsSetCmd.Flags().Int64("max-concurrent-sessions", 0, "Maximum concurrent browser sessions (0 to remove cap)")
projectsLimitsSetCmd.Flags().Int64("max-persistent-sessions", 0, "Maximum persistent browser sessions (0 to remove cap)")
projectsLimitsSetCmd.Flags().Int64("max-concurrent-invocations", 0, "Maximum concurrent app invocations (0 to remove cap)")
projectsLimitsSetCmd.Flags().Int64("max-pooled-sessions", 0, "Maximum pooled sessions capacity (0 to remove cap)")

projectsCmd.AddCommand(projectsListCmd)
projectsCmd.AddCommand(projectsCreateCmd)
projectsCmd.AddCommand(projectsGetCmd)
projectsCmd.AddCommand(projectsDeleteCmd)
projectsCmd.AddCommand(projectsLimitsGetCmd)
projectsCmd.AddCommand(projectsLimitsSetCmd)
}
2 changes: 1 addition & 1 deletion cmd/proxies/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (p ProxyCmd) Check(ctx context.Context, in ProxyCheckInput) error {
pterm.Info.Printf("Running health check on proxy %s...\n", in.ID)
}

proxy, err := p.proxies.Check(ctx, in.ID)
proxy, err := p.proxies.Check(ctx, in.ID, kernel.ProxyCheckParams{})
if err != nil {
return util.CleanedUpSdkError{Err: err}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/proxies/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestProxyCheck_ShowsBypassHosts(t *testing.T) {
buf := captureOutput(t)

fake := &FakeProxyService{
CheckFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ProxyCheckResponse, error) {
CheckFunc: func(ctx context.Context, id string, body kernel.ProxyCheckParams, opts ...option.RequestOption) (*kernel.ProxyCheckResponse, error) {
return &kernel.ProxyCheckResponse{
ID: id,
Name: "Proxy 1",
Expand Down
6 changes: 3 additions & 3 deletions cmd/proxies/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type FakeProxyService struct {
GetFunc func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ProxyGetResponse, error)
NewFunc func(ctx context.Context, body kernel.ProxyNewParams, opts ...option.RequestOption) (*kernel.ProxyNewResponse, error)
DeleteFunc func(ctx context.Context, id string, opts ...option.RequestOption) error
CheckFunc func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ProxyCheckResponse, error)
CheckFunc func(ctx context.Context, id string, body kernel.ProxyCheckParams, opts ...option.RequestOption) (*kernel.ProxyCheckResponse, error)
}

func (f *FakeProxyService) List(ctx context.Context, opts ...option.RequestOption) (*[]kernel.ProxyListResponse, error) {
Expand Down Expand Up @@ -73,9 +73,9 @@ func (f *FakeProxyService) Delete(ctx context.Context, id string, opts ...option
return nil
}

func (f *FakeProxyService) Check(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ProxyCheckResponse, error) {
func (f *FakeProxyService) Check(ctx context.Context, id string, body kernel.ProxyCheckParams, opts ...option.RequestOption) (*kernel.ProxyCheckResponse, error) {
Comment thread
cursor[bot] marked this conversation as resolved.
if f.CheckFunc != nil {
return f.CheckFunc(ctx, id, opts...)
return f.CheckFunc(ctx, id, body, opts...)
}
return &kernel.ProxyCheckResponse{ID: id, Type: kernel.ProxyCheckResponseTypeDatacenter}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/proxies/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ProxyService interface {
Get(ctx context.Context, id string, opts ...option.RequestOption) (res *kernel.ProxyGetResponse, err error)
New(ctx context.Context, body kernel.ProxyNewParams, opts ...option.RequestOption) (res *kernel.ProxyNewResponse, err error)
Delete(ctx context.Context, id string, opts ...option.RequestOption) (err error)
Check(ctx context.Context, id string, opts ...option.RequestOption) (res *kernel.ProxyCheckResponse, err error)
Check(ctx context.Context, id string, body kernel.ProxyCheckParams, opts ...option.RequestOption) (res *kernel.ProxyCheckResponse, err error)
}

// ProxyCmd handles proxy operations independent of cobra.
Expand Down
67 changes: 65 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func init() {
rootCmd.PersistentFlags().BoolP("version", "v", false, "Print the CLI version")
rootCmd.PersistentFlags().BoolP("no-color", "", false, "Disable color output")
rootCmd.PersistentFlags().String("log-level", "warn", "Set the log level (trace, debug, info, warn, error, fatal, print)")
rootCmd.PersistentFlags().String("project", "", "Project ID or name to scope all requests to (or set KERNEL_PROJECT_ID env var)")
rootCmd.SilenceUsage = true
rootCmd.SilenceErrors = true
cobra.OnInitialize(initConfig)
Expand All @@ -122,12 +123,41 @@ func init() {
return nil
}

// Get authenticated client with OAuth tokens or API key fallback
client, err := auth.GetAuthenticatedClient(option.WithHeader("X-Kernel-Cli-Version", metadata.Version))
clientOpts := []option.RequestOption{
option.WithHeader("X-Kernel-Cli-Version", metadata.Version),
}

projectVal, _ := cmd.Flags().GetString("project")
if projectVal == "" {
projectVal = os.Getenv("KERNEL_PROJECT_ID")
Comment thread
masnwilliams marked this conversation as resolved.
Outdated
}

// If the value looks like a name (not a cuid2 ID), we need to
// resolve it after authenticating. Build the client first without
// the project header, resolve, then re-create with the header.
needsResolve := projectVal != "" && !looksLikeCUID(projectVal)

if projectVal != "" && !needsResolve {
clientOpts = append(clientOpts, option.WithHeader("X-Kernel-Project-Id", projectVal))
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Global --project flag skips name-to-ID resolution

High Severity

The --project flag handler in PersistentPreRunE passes the raw projectVal directly into the X-Kernel-Project-Id header without calling resolveProjectArg or resolveProjectByName. When a user supplies a project name instead of an ID, the name string is sent verbatim as the header value rather than being resolved to an actual project ID. The resolveProjectByName function is defined in the same file but never wired into this code path, so the documented name-resolution behavior (CUID2 check, then list-and-match) doesn't occur for the global flag.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a20844d. Configure here.


client, err := auth.GetAuthenticatedClient(clientOpts...)
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}

if needsResolve {
resolved, resolveErr := resolveProjectByName(cmd.Context(), *client, projectVal)
if resolveErr != nil {
return resolveErr
}
clientOpts = append(clientOpts, option.WithHeader("X-Kernel-Project-Id", resolved))
client, err = auth.GetAuthenticatedClient(clientOpts...)
if err != nil {
return fmt.Errorf("authentication required: %w", err)
}
}

ctx := context.WithValue(cmd.Context(), util.KernelClientKey, *client)
cmd.SetContext(ctx)
return nil
Expand All @@ -144,6 +174,7 @@ func init() {
rootCmd.AddCommand(extensionsCmd)
rootCmd.AddCommand(credentialsCmd)
rootCmd.AddCommand(credentialProvidersCmd)
rootCmd.AddCommand(projectsCmd)
rootCmd.AddCommand(createCmd)
rootCmd.AddCommand(mcp.MCPCmd)
rootCmd.AddCommand(upgradeCmd)
Expand Down Expand Up @@ -223,6 +254,38 @@ func isUsageError(err error) bool {
return false
}

// looksLikeCUID returns true if s matches the cuid2 format used for resource IDs.
// Delegates to the shared cuidRegex defined in browsers.go.
func looksLikeCUID(s string) bool {
return cuidRegex.MatchString(s)
}
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated

// resolveProjectByName lists the caller's projects and returns the ID of the
// one whose name matches (case-insensitive). Returns an error if no match or
// multiple matches are found.
func resolveProjectByName(ctx context.Context, client kernel.Client, name string) (string, error) {
Comment thread
masnwilliams marked this conversation as resolved.
Outdated
projects, err := client.Projects.List(ctx, kernel.ProjectListParams{})
if err != nil {
return "", fmt.Errorf("failed to resolve project name %q: %w", name, err)
}
var matched []struct{ id, name string }
lower := strings.ToLower(name)
for _, p := range projects.Items {
if strings.ToLower(p.Name) == lower {
matched = append(matched, struct{ id, name string }{p.ID, p.Name})
}
}
switch len(matched) {
case 0:
return "", fmt.Errorf("no project found with name %q", name)
case 1:
pterm.Debug.Printf("Resolved project %q → %s\n", matched[0].name, matched[0].id)
return matched[0].id, nil
default:
return "", fmt.Errorf("multiple projects match name %q; use a project ID instead", name)
}
}
Comment thread
cursor[bot] marked this conversation as resolved.

// onCancel runs a function when the provided context is cancelled
func onCancel(ctx context.Context, fn func()) {
go func() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/charmbracelet/lipgloss/v2 v2.0.0-beta.1
github.com/golang-jwt/jwt/v5 v5.2.2
github.com/joho/godotenv v1.5.1
github.com/kernel/kernel-go-sdk v0.44.1-0.20260323174449-5e56fc5d99a6
github.com/kernel/kernel-go-sdk v0.48.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/pterm/pterm v0.12.80
github.com/samber/lo v1.51.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kernel/kernel-go-sdk v0.44.1-0.20260323174449-5e56fc5d99a6 h1:RBlGCN3IagI0b+XrWsb5FOUV/18tniuL6oHFAb7MMHE=
github.com/kernel/kernel-go-sdk v0.44.1-0.20260323174449-5e56fc5d99a6/go.mod h1:EeZzSuHZVeHKxKCPUzxou2bovNGhXaz0RXrSqKNf1AQ=
github.com/kernel/kernel-go-sdk v0.48.0 h1:XX1VVs8D5q+rBMkZovXmKAQa94w+6oEJzxBLikfPaxw=
github.com/kernel/kernel-go-sdk v0.48.0/go.mod h1:EeZzSuHZVeHKxKCPUzxou2bovNGhXaz0RXrSqKNf1AQ=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
Expand Down
Loading