Skip to content

Commit

Permalink
Merge branch 'thallgren/read-only'
Browse files Browse the repository at this point in the history
  • Loading branch information
thallgren committed Nov 15, 2024
2 parents 459a057 + 11d486a commit d26d8f5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM --platform=$BUILDPLATFORM golang:alpine as builder
FROM --platform=$BUILDPLATFORM golang:alpine AS builder

RUN apk add --no-cache --virtual .build-deps gcc libc-dev
WORKDIR docker-volume-telemount
WORKDIR /docker-volume-telemount
COPY . .
ARG TARGETOS
ARG TARGETARCH
Expand Down
21 changes: 17 additions & 4 deletions pkg/sftp/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (d *driver) Create(r *volume.CreateRequest) (err error) {

var container, dir, host string
var port uint16
var readOnly bool
for key, val := range r.Options {
switch key {
case "container":
Expand All @@ -68,6 +69,11 @@ func (d *driver) Create(r *volume.CreateRequest) (err error) {
} else {
port = uint16(pv)
}
case "ro":
readOnly, err = strconv.ParseBool(val)
if err != nil {
return fmt.Errorf("ro must be a boolean")
}
default:
return fmt.Errorf("illegal option %q", key)
}
Expand All @@ -88,7 +94,7 @@ func (d *driver) Create(r *volume.CreateRequest) (err error) {
}
d.lock.Lock()
defer d.lock.Unlock()
m, err := d.getRemoteMount(host, port)
m, err := d.getRemoteMount(host, port, readOnly)
if err != nil {
return err
}
Expand Down Expand Up @@ -232,13 +238,20 @@ func (d *driver) Capabilities() *volume.CapabilitiesResponse {
return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "local"}}
}

func (d *driver) getRemoteMount(host string, port uint16) (*mount, error) {
func (d *driver) getRemoteMount(host string, port uint16, readOnly bool) (*mount, error) {
ps := strconv.Itoa(int(port))
key := net.JoinHostPort(host, ps)
if m, ok := d.remoteMounts[key]; ok {
return m, nil
if m.readOnly == readOnly {
return m, nil
}
if m.readOnly {
return nil, fmt.Errorf("writable access requested for read-only %s", key)
}
// Can't let a writable volume pose as read-only
return nil, fmt.Errorf("read-only access requested writeable %s", key)
}
m := newMount(filepath.Join(d.volumePath, host, ps), host, port, func() {
m := newMount(filepath.Join(d.volumePath, host, ps), host, port, readOnly, func() {
d.lock.Lock()
delete(d.remoteMounts, key)
d.lock.Unlock()
Expand Down
9 changes: 7 additions & 2 deletions pkg/sftp/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ type mount struct {
mountPoint string
host string
port uint16
readOnly bool
mounted atomic.Bool
done chan error
volumes map[string]*volumeDir
proc *os.Process
}

func newMount(mountPoint, host string, port uint16, cancel context.CancelFunc) *mount {
func newMount(mountPoint, host string, port uint16, readOnly bool, cancel context.CancelFunc) *mount {
return &mount{
mountPoint: mountPoint,
host: host,
port: port,
readOnly: readOnly,
volumes: make(map[string]*volumeDir),
cancel: cancel,
}
}

func (m *mount) String() string {
return fmt.Sprintf("port=%d, mountPoint=%s", m.port, m.mountPoint)
return fmt.Sprintf("port=%d, mountPoint=%s, readOnly=%t", m.port, m.mountPoint, m.readOnly)
}

func (m *mount) addVolume(name, dir string) {
Expand Down Expand Up @@ -102,6 +104,9 @@ func (m *mount) mountVolume() error {
"-o", "auto_unmount",
"-o", "allow_root", // needed to make --docker-run work as docker runs as root
}
if m.readOnly {
sshfsArgs = append(sshfsArgs, "-o", "ro")
}

var sl io.Writer
if log.IsDebug() {
Expand Down

0 comments on commit d26d8f5

Please sign in to comment.