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
105 changes: 98 additions & 7 deletions desktop/internal/core/remote_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package core
import (
"context"
"encoding/json"
"strconv"
"strings"
)

// Connection window helpers: Core parses a quick-connect address, asks OpenSSH what it would use
Expand Down Expand Up @@ -253,9 +255,77 @@ type RemoteWorkspaceTrustOf struct {
// RemoteWorkspaceTrustState is the remote trust document as Core reads it, and the trust of the
// workspace the request named.
type RemoteWorkspaceTrustState struct {
Storage RemoteWorkspaceTrustStorage `json:"storage"`
ProjectCodeAllowed bool `json:"projectCodeAllowed"`
WorkspaceTrust *RemoteWorkspaceTrustOf `json:"workspaceTrust"`
Document RemoteWorkspaceTrustDocument `json:"document"`
Storage RemoteWorkspaceTrustStorage `json:"storage"`
ProjectCodeAllowed bool `json:"projectCodeAllowed"`
WorkspaceTrust *RemoteWorkspaceTrustOf `json:"workspaceTrust"`
}

// RemoteWorkspaceTrustDocument is the validated remote trust document: the decisions of every
// connection target, sorted by target.
type RemoteWorkspaceTrustDocument struct {
Version int `json:"version"`
Targets []RemoteWorkspaceTrustTarget `json:"targets"`
}

// The lists of one target's decisions, as RemoteWorkspaceTrustRevoke names them.
const (
RemoteTrustTrustedWorkspaces = "trustedWorkspaces"
RemoteTrustDistrustedWorkspaces = "distrustedWorkspaces"
RemoteTrustTrustedLocations = "trustedLocations"
)

// RemoteWorkspaceTrustTarget is the decisions on the workspaces of one connection target. Target
// is Core's key of the connection, passed back as it is; Label is the connection name shown with
// it, empty when no decision gave one. Each list holds sorted, distinct host real paths.
type RemoteWorkspaceTrustTarget struct {
Target json.RawMessage `json:"target"`
Label string `json:"label,omitempty"`
TrustedWorkspaces []string `json:"trustedWorkspaces,omitempty"`
DistrustedWorkspaces []string `json:"distrustedWorkspaces,omitempty"`
TrustedLocations []string `json:"trustedLocations,omitempty"`
}

// Place is the route of the target's hops, outermost first: "destination:port" for an SSH hop,
// with the User option in front when the destination names no user, and "engine:container" for
// a container, joined by " → ". It is empty for a target it cannot read.
func (t RemoteWorkspaceTrustTarget) Place() string {
var target struct {
Hops []struct {
SSH *struct {
Destination string `json:"destination"`
Port *uint16 `json:"port"`
PortOption string `json:"portOption"`
User string `json:"user"`
} `json:"ssh"`
Container *struct {
Engine string `json:"engine"`
Container string `json:"container"`
} `json:"container"`
} `json:"hops"`
}
if json.Unmarshal(t.Target, &target) != nil {
return ""
}
hops := make([]string, 0, len(target.Hops))
for _, hop := range target.Hops {
switch {
case hop.SSH != nil:
place := hop.SSH.Destination
if hop.SSH.User != "" && !strings.Contains(place, "@") {
place = hop.SSH.User + "@" + place
}
if hop.SSH.Port != nil {
place += ":" + strconv.Itoa(int(*hop.SSH.Port))
} else if hop.SSH.PortOption != "" {
place += ":" + hop.SSH.PortOption
}
hops = append(hops, place)
case hop.Container != nil:
hops = append(hops, hop.Container.Engine+":"+hop.Container.Container)
}
}
return strings.Join(hops, " → ")
}

// RemoteWorkspaceTrust reads document, the remote trust source or nil, and, when sessionID is
Expand All @@ -282,11 +352,21 @@ type RemoteWorkspaceTrustDecision struct {
Label string `json:"label,omitempty"`
}

// RemoteWorkspaceTrustPublication is the outcome of a decision. Document is the text to publish,
// nil when the decision changed nothing.
// RemoteWorkspaceTrustPublication is the outcome of a decision or a removal. Document is the text
// to publish, nil when the edit changed nothing; Decisions is the document after the edit.
type RemoteWorkspaceTrustPublication struct {
Changed bool `json:"changed"`
Document *string `json:"document"`
Changed bool `json:"changed"`
Document *string `json:"document"`
Decisions RemoteWorkspaceTrustDocument `json:"decisions"`
}

// RemoteWorkspaceTrustRevoke removes Path from the List (RemoteTrustTrustedWorkspaces,
// RemoteTrustDistrustedWorkspaces or RemoteTrustTrustedLocations) of Target's decisions, as
// RemoteWorkspaceTrustState listed them. It needs no open connection.
type RemoteWorkspaceTrustRevoke struct {
Target json.RawMessage `json:"target"`
List string `json:"list"`
Path string `json:"path"`
}

// EditRemoteWorkspaceTrust records decision in document, the remote trust source or nil. A
Expand All @@ -299,3 +379,14 @@ func (c *Client) EditRemoteWorkspaceTrust(ctx context.Context, document *string,
}{document, decision}, &publication)
return publication, err
}

// RevokeRemoteWorkspaceTrust removes one entry from document, the remote trust source or nil.
// Removing an absent entry changes nothing; a target left without entries is dropped.
func (c *Client) RevokeRemoteWorkspaceTrust(ctx context.Context, document *string, revoke RemoteWorkspaceTrustRevoke) (RemoteWorkspaceTrustPublication, error) {
var publication RemoteWorkspaceTrustPublication
err := c.Execute(ctx, "remote.editWorkspaceTrust", struct {
Document *string `json:"document"`
Revoke RemoteWorkspaceTrustRevoke `json:"revoke"`
}{document, revoke}, &publication)
return publication, err
}
28 changes: 26 additions & 2 deletions desktop/internal/i18n/shared.generated.go

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

61 changes: 54 additions & 7 deletions desktop/internal/remoteconn/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type TrustKernel interface {
RemoteWorkspaceTrust(ctx context.Context, document *string, sessionID, workspace string) (core.RemoteWorkspaceTrustState, error)
EditRemoteWorkspaceTrust(ctx context.Context, document *string, decision core.RemoteWorkspaceTrustDecision) (core.RemoteWorkspaceTrustPublication, error)
RevokeRemoteWorkspaceTrust(ctx context.Context, document *string, revoke core.RemoteWorkspaceTrustRevoke) (core.RemoteWorkspaceTrustPublication, error)
}

// Trust is the remote trust document with the bytes it was read from, and the trust of the
Expand Down Expand Up @@ -78,19 +79,65 @@ func (t Trust) Decide(ctx context.Context, kernel TrustKernel, decision core.Rem
}
publication, err := kernel.EditRemoteWorkspaceTrust(ctx, text, decision)
if err != nil {
var coreErr *core.Error
if errors.As(err, &coreErr) && coreErr.Code == core.CodeParseFailed {
err = errors.Join(ErrDamaged, err)
}
return false, err
return false, trustEditError(err)
}
if !publication.Changed {
return false, nil
}
if publication.Document == nil {
return false, errors.New("remoteconn: Core changed the trust decisions without a document")
return false, errNoTrustDocument
}
return t.publish(ctx, *publication.Document)
}

// Revoke removes each entry of revokes, in order, from the bytes t was read from, and publishes
// the result once when any removal changed it; after is the document it leads to. Nothing is
// written when Core refuses a removal or when none changes anything. A file changed since t was
// read fails with ErrConflict and writes nothing; committed reports that the new document reached
// the disk even when a later step failed. Revoking needs no open connection.
func (t Trust) Revoke(ctx context.Context, kernel TrustKernel, revokes []core.RemoteWorkspaceTrustRevoke) (after core.RemoteWorkspaceTrustDocument, committed bool, err error) {
if t.root == "" {
return t.State.Document, false, ErrUnavailable
}
text, err := t.file.Text()
if err != nil {
return t.State.Document, false, errors.Join(ErrDamaged, err)
}
committed, err = t.file.Replace(ctx, []byte(*publication.Document), func() error { return t.file.Verify(ctx) })
after, changed := t.State.Document, false
for _, revoke := range revokes {
publication, err := kernel.RevokeRemoteWorkspaceTrust(ctx, text, revoke)
if err != nil {
return t.State.Document, false, trustEditError(err)
}
after = publication.Decisions
if publication.Changed {
if publication.Document == nil {
return t.State.Document, false, errNoTrustDocument
}
text, changed = publication.Document, true
}
}
if !changed {
return after, false, nil
}
committed, err = t.publish(ctx, *text)
return after, committed, err
}

var errNoTrustDocument = errors.New("remoteconn: Core changed the trust decisions without a document")

// trustEditError marks Core's refusal to decode the stored document as ErrDamaged.
func trustEditError(err error) error {
var coreErr *core.Error
if errors.As(err, &coreErr) && coreErr.Code == core.CodeParseFailed {
return errors.Join(ErrDamaged, err)
}
return err
}

// publish replaces the file with document, against the bytes t was read from.
func (t Trust) publish(ctx context.Context, document string) (committed bool, err error) {
committed, err = t.file.Replace(ctx, []byte(document), func() error { return t.file.Verify(ctx) })
if errors.Is(err, configfile.ErrConflict) {
err = errors.Join(ErrConflict, err)
}
Expand Down
65 changes: 65 additions & 0 deletions desktop/internal/remoteconn/trust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,68 @@ func TestRemoteTrustReadsWithoutWritingAndRefusesAClosedSession(t *testing.T) {
t.Fatalf("a refused decision created %s: %v", root, err)
}
}

// Revoking needs no connection: it removes the named records, drops a target left empty and
// writes once; removals that name nothing write nothing, and a file changed since it was read
// is refused with ErrConflict.
func TestRemoteTrustRevokeRemovesRecordsWithoutAConnection(t *testing.T) {
ctx := testContext(t)
kernel := core.NewClient()
root := filepath.Join(t.TempDir(), "preferences")
file := filepath.Join(root, "toolchains", "remote-trust.json")
if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
t.Fatal(err)
}
devbox := `{"hops":[{"ssh":{"destination":"devbox","port":2222,"user":"dev"}}]}`
lab := `{"hops":[{"ssh":{"destination":"lab"}},{"container":{"engine":"docker","program":"docker","container":"web"}}]}`
seed := `{"version":1,"targets":[` +
`{"target":` + devbox + `,"label":"Dev box","trustedWorkspaces":["/srv/app"],"distrustedWorkspaces":["/srv/tmp"]},` +
`{"target":` + lab + `,"trustedLocations":["/work"]}]}`
if err := os.WriteFile(file, []byte(seed), 0o600); err != nil {
t.Fatal(err)
}
trust, err := ReadTrust(ctx, kernel, root, "", "")
if err != nil {
t.Fatal(err)
}
targets := trust.State.Document.Targets
if len(targets) != 2 {
t.Fatalf("targets = %d", len(targets))
}
if place := targets[0].Place(); targets[0].Label != "Dev box" || place != "dev@devbox:2222" {
t.Fatalf("first target: label set = %v, place matches = %v", targets[0].Label != "", place == "dev@devbox:2222")
}
if place := targets[1].Place(); place != "lab → docker:web" {
t.Fatalf("second target place matches = %v", place == "lab → docker:web")
}

absent := []core.RemoteWorkspaceTrustRevoke{{Target: targets[0].Target, List: core.RemoteTrustTrustedLocations, Path: "/srv"}}
after, committed, err := trust.Revoke(ctx, kernel, absent)
if err != nil || committed || len(after.Targets) != 2 {
t.Fatalf("absent removal: committed = %v, targets = %d, err = %v", committed, len(after.Targets), err)
}
if text, _ := os.ReadFile(file); string(text) != seed {
t.Fatal("a removal that names no record rewrote the file")
}

after, committed, err = trust.Revoke(ctx, kernel, []core.RemoteWorkspaceTrustRevoke{
{Target: targets[0].Target, List: core.RemoteTrustDistrustedWorkspaces, Path: "/srv/tmp"},
{Target: targets[1].Target, List: core.RemoteTrustTrustedLocations, Path: "/work"},
})
if err != nil || !committed {
t.Fatalf("removal: committed = %v, err = %v", committed, err)
}
if len(after.Targets) != 1 || len(after.Targets[0].TrustedWorkspaces) != 1 || len(after.Targets[0].DistrustedWorkspaces) != 0 {
t.Fatalf("after removal: targets = %d", len(after.Targets))
}
reread, err := ReadTrust(ctx, kernel, root, "", "")
if err != nil || len(reread.State.Document.Targets) != 1 || reread.State.Document.Targets[0].Label != "Dev box" {
t.Fatalf("re-read: targets = %d, err = %v", len(reread.State.Document.Targets), err)
}

// trust still holds the bytes read before the removal: writing against them is refused.
_, committed, err = trust.Revoke(ctx, kernel, []core.RemoteWorkspaceTrustRevoke{{Target: targets[0].Target, List: core.RemoteTrustTrustedWorkspaces, Path: "/srv/app"}})
if !errors.Is(err, ErrConflict) || committed {
t.Fatalf("stale removal: committed = %v, conflict = %v", committed, errors.Is(err, ErrConflict))
}
}
Loading
Loading