Skip to content

Commit 23b5486

Browse files
committed
Route all commands through SpiceDB gating
1 parent 4eb763b commit 23b5486

File tree

6 files changed

+62
-11
lines changed

6 files changed

+62
-11
lines changed

cmd/connect.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ Copyright © 2025 ALESSIO TONIOLO
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
89
"log"
910
"os"
1011
"os/exec"
1112
"strings"
1213

1314
"github.com/atoniolo76/gotoni/pkg/remote"
15+
"github.com/atoniolo76/gotoni/pkg/spicedb"
1416

1517
"github.com/spf13/cobra"
1618
)
@@ -64,17 +66,18 @@ var connectCmd = &cobra.Command{
6466
instanceName = target
6567
} else {
6668
// For SSH, try to resolve to IP first, fallback to name
67-
apiToken := remote.GetAPIToken()
68-
if apiToken != "" {
69-
httpClient := remote.NewHTTPClient()
70-
instance, err := remote.ResolveInstance(httpClient, apiToken, target)
71-
if err == nil {
72-
// Found instance, use IP for SSH
73-
instanceName = instance.IP
74-
} else {
75-
// Not found, assume it's an SSH config entry name
76-
instanceName = target
69+
apiToken := remote.GetAPIToken()
70+
if apiToken != "" {
71+
httpClient := remote.NewHTTPClient()
72+
instance, err := remote.ResolveInstance(httpClient, apiToken, target)
73+
if err == nil {
74+
if checkErr := spicedb.Check(context.Background(), "resource", instance.ID, "ssh"); checkErr != nil {
75+
log.Fatalf("Permission denied: %v", checkErr)
7776
}
77+
instanceName = instance.IP
78+
} else {
79+
instanceName = target
80+
}
7881
} else {
7982
// No API token, assume SSH config entry
8083
instanceName = target

cmd/kill.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Copyright © 2025 ALESSIO TONIOLO
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
89
"log"
910
"net/http"
1011
"strings"
1112

1213
"github.com/atoniolo76/gotoni/pkg/remote"
14+
"github.com/atoniolo76/gotoni/pkg/spicedb"
1315

1416
"github.com/spf13/cobra"
1517
)
@@ -130,13 +132,24 @@ Use --all to terminate every running resource for the selected provider.`,
130132
resolvedNames = append(resolvedNames, displayName)
131133
}
132134

135+
ctx := context.Background()
136+
for _, id := range instanceIDs {
137+
if err := spicedb.Check(ctx, "resource", id, "delete"); err != nil {
138+
log.Fatalf("Permission denied for %s: %v", id, err)
139+
}
140+
}
141+
133142
fmt.Printf("Terminating %s(s): %s\n", resType, strings.Join(resolvedNames, ", "))
134143

135144
resp, err := cloudProvider.TerminateInstance(httpClient, apiToken, instanceIDs)
136145
if err != nil {
137146
log.Fatalf("Error terminating %s(s): %v", resType, err)
138147
}
139148

149+
for _, id := range instanceIDs {
150+
spicedb.DeleteResource(ctx, id)
151+
}
152+
140153
// Lambda manages state via API, but we still clean up any leftover config refs
141154
if provider == "lambda" {
142155
for _, id := range instanceIDs {

cmd/launch.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Copyright © 2025 ALESSIO TONIOLO
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
89
"log"
910
"os"
@@ -12,6 +13,7 @@ import (
1213

1314
"github.com/atoniolo76/gotoni/pkg/db"
1415
"github.com/atoniolo76/gotoni/pkg/remote"
16+
"github.com/atoniolo76/gotoni/pkg/spicedb"
1517

1618
"github.com/spf13/cobra"
1719
)
@@ -264,6 +266,10 @@ Examples:
264266
fmt.Println("Note: Modal provider uses Volumes instead of filesystems. Use --volume flag for Modal.")
265267
}
266268

269+
if err := spicedb.CheckCreate(context.Background()); err != nil {
270+
log.Fatalf("Permission denied: %v", err)
271+
}
272+
267273
fmt.Printf("\nLaunching instance...\n")
268274
if provider == "orgo" {
269275
// Orgo provider - use project-based launch
@@ -313,6 +319,12 @@ Examples:
313319
}
314320
}
315321

322+
if launchErr == nil {
323+
for _, inst := range launchedInstances {
324+
spicedb.WriteResourceOwnership(context.Background(), inst.ID)
325+
}
326+
}
327+
316328
// Save SSH key to database (so we can look up file paths later)
317329
// Note: Instance state is managed by Lambda API, not local DB
318330
if launchErr == nil {

cmd/list.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Copyright © 2025 ALESSIO TONIOLO
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
89
"log"
910
"os"
1011
"strings"
1112

1213
"github.com/atoniolo76/gotoni/pkg/remote"
14+
"github.com/atoniolo76/gotoni/pkg/spicedb"
1315

1416
"github.com/spf13/cobra"
1517
)
@@ -70,6 +72,16 @@ var listCmd = &cobra.Command{
7072
log.Fatalf("Error listing running instances: %v", err)
7173
}
7274

75+
if allowed := spicedb.ViewableResourceIDs(context.Background()); allowed != nil {
76+
filtered := runningInstances[:0]
77+
for _, inst := range runningInstances {
78+
if allowed[inst.ID] {
79+
filtered = append(filtered, inst)
80+
}
81+
}
82+
runningInstances = filtered
83+
}
84+
7385
if len(runningInstances) == 0 {
7486
fmt.Println("No running instances found.")
7587
return

cmd/open.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ Copyright © 2025 ALESSIO TONIOLO
44
package cmd
55

66
import (
7+
"context"
78
"log"
89
"strings"
910

1011
"github.com/atoniolo76/gotoni/pkg/remote"
12+
"github.com/atoniolo76/gotoni/pkg/spicedb"
1113
"github.com/spf13/cobra"
1214
)
1315

@@ -54,8 +56,11 @@ Requirements:
5456
apiToken := remote.GetAPIToken()
5557
if apiToken != "" {
5658
httpClient := remote.NewHTTPClient()
57-
_, err := remote.ResolveInstance(httpClient, apiToken, target)
59+
inst, err := remote.ResolveInstance(httpClient, apiToken, target)
5860
if err == nil {
61+
if checkErr := spicedb.Check(context.Background(), "resource", inst.ID, "ssh"); checkErr != nil {
62+
log.Fatalf("Permission denied: %v", checkErr)
63+
}
5964
instanceName = target
6065
} else {
6166
instanceName = target

cmd/run.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Copyright © 2025 ALESSIO TONIOLO
44
package cmd
55

66
import (
7+
"context"
78
"fmt"
89
"log"
910
"os"
1011
"strings"
1112

1213
"github.com/atoniolo76/gotoni/pkg/remote"
14+
"github.com/atoniolo76/gotoni/pkg/spicedb"
1315

1416
"github.com/spf13/cobra"
1517
)
@@ -129,6 +131,10 @@ Examples:
129131
instanceID = instanceDetails.ID
130132
}
131133

134+
if err := spicedb.Check(context.Background(), "resource", instanceID, "ssh"); err != nil {
135+
log.Fatalf("Permission denied: %v", err)
136+
}
137+
132138
// Execute command based on provider
133139
if provider == "orgo" || provider == "modal" {
134140
// Use provider's ExecuteBashCommand

0 commit comments

Comments
 (0)