|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build !windows |
| 16 | +// +build !windows |
| 17 | + |
| 18 | +package proxy |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "syscall" |
| 23 | + |
| 24 | + "github.com/hanwen/go-fuse/v2/fs" |
| 25 | + "github.com/hanwen/go-fuse/v2/fuse" |
| 26 | + "github.com/hanwen/go-fuse/v2/fuse/nodefs" |
| 27 | +) |
| 28 | + |
| 29 | +// symlink implements a symbolic link, returning the underlying path when |
| 30 | +// Readlink is called. |
| 31 | +type symlink struct { |
| 32 | + fs.Inode |
| 33 | + path string |
| 34 | +} |
| 35 | + |
| 36 | +// Readlink implements fs.NodeReadlinker and returns the symlink's path. |
| 37 | +func (s *symlink) Readlink(ctx context.Context) ([]byte, syscall.Errno) { |
| 38 | + return []byte(s.path), fs.OK |
| 39 | +} |
| 40 | + |
| 41 | +// readme represents a static read-only text file. |
| 42 | +type readme struct { |
| 43 | + fs.Inode |
| 44 | +} |
| 45 | + |
| 46 | +const readmeText = ` |
| 47 | +When applications attempt to open files in this directory, a remote connection |
| 48 | +to the AlloyDB instance of the same name will be established. |
| 49 | +
|
| 50 | +For example, when you run one of the following commands, the proxy will initiate |
| 51 | +a connection to the corresponding Cloud SQL instance, given you have the correct |
| 52 | +IAM permissions. |
| 53 | +
|
| 54 | + psql "host=/somedir/project.region.cluster.instance dbname=mydb user=myuser" |
| 55 | +
|
| 56 | +The proxy will create a directory with the instance short name, and create a |
| 57 | +socket inside that directory with the special Postgres name: .s.PGSQL.5432. |
| 58 | +
|
| 59 | +Listing the contents of this directory will show all instances with active |
| 60 | +connections. |
| 61 | +` |
| 62 | + |
| 63 | +// Getattr implements fs.NodeGetattrer and indicates that this file is a regular |
| 64 | +// file. |
| 65 | +func (*readme) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { |
| 66 | + *out = fuse.AttrOut{Attr: fuse.Attr{ |
| 67 | + Mode: 0444 | syscall.S_IFREG, |
| 68 | + Size: uint64(len(readmeText)), |
| 69 | + }} |
| 70 | + return fs.OK |
| 71 | +} |
| 72 | + |
| 73 | +// Read implements fs.NodeReader and supports incremental reads. |
| 74 | +func (*readme) Read(ctx context.Context, f fs.FileHandle, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { |
| 75 | + end := int(off) + len(dest) |
| 76 | + if end > len(readmeText) { |
| 77 | + end = len(readmeText) |
| 78 | + } |
| 79 | + return fuse.ReadResultData([]byte(readmeText[off:end])), fs.OK |
| 80 | +} |
| 81 | + |
| 82 | +// Open implements fs.NodeOpener and supports opening the README as a read-only |
| 83 | +// file. |
| 84 | +func (*readme) Open(ctx context.Context, mode uint32) (fs.FileHandle, uint32, syscall.Errno) { |
| 85 | + df := nodefs.NewDataFile([]byte(readmeText)) |
| 86 | + rf := nodefs.NewReadOnlyFile(df) |
| 87 | + return rf, 0, fs.OK |
| 88 | +} |
0 commit comments